Skip to content

Commit

Permalink
fix: allow more http methods in datasource
Browse files Browse the repository at this point in the history
Signed-off-by: Henrik Gerdes <hegerdes@outlook.de>
  • Loading branch information
hegerdes committed Oct 21, 2024
1 parent 8360fc9 commit c2a75ff
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
7 changes: 3 additions & 4 deletions datasource/http/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"`
// The URL to request data from. This URL must respond with a `2xx` range response code and a `text/*` or `application/json` Content-Type.
Url string `mapstructure:"url" required:"true"`
// HTTP method used for the request. Supported methods are `HEAD`, `GET`, `POST`, and `PUT`. Default is `GET`.
// HTTP method used for the request. Supported methods are `HEAD`, `GET`, `POST`, `PUT`, `DELETE`, `OPTIONS`, `PATCH`. Default is `GET`.
Method string `mapstructure:"method" required:"false"`
// A map of strings representing additional HTTP headers to include in the request.
RequestHeaders map[string]string `mapstructure:"request_headers" required:"false"`
Expand Down Expand Up @@ -73,10 +73,11 @@ func (d *Datasource) Configure(raws ...interface{}) error {

// Check if the input is in the list of allowed methods
validMethod := false
allowedMethods := []string{"HEAD", "GET", "POST", "PUT"}
allowedMethods := []string{"HEAD", "GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"}
for _, method := range allowedMethods {
if method == d.config.Method {
validMethod = true
break
}
}
if !validMethod {
Expand Down Expand Up @@ -132,8 +133,6 @@ func (d *Datasource) Execute() (cty.Value, error) {
var requestBody io.Reader
if d.config.RequestBody != "" {
requestBody = strings.NewReader(d.config.RequestBody)
} else {
requestBody = nil
}

req, err := http.NewRequestWithContext(ctx, method, url, requestBody)
Expand Down
2 changes: 1 addition & 1 deletion datasource/http/data.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion datasource/http/test-fixtures/invalid_method.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source "null" "example" {

data "http" "basic" {
url = "https://www.packer.io/"
method = "DELETE"
method = "NONEEXISTING"
}

locals {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- Code generated from the comments of the Config struct in datasource/http/data.go; DO NOT EDIT MANUALLY -->

- `method` (string) - HTTP method used for the request. Supported methods are `HEAD`, `GET`, `POST`, `PUT`, `DELETE`, `OPTIONS`, `PATCH`. Default is `GET`.

- `request_headers` (map[string]string) - A map of strings representing additional HTTP headers to include in the request.

- `request_body` (string) - HTTP request payload send with the request. Default is empty.

<!-- End of code generated from the comments of the Config struct in datasource/http/data.go; -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Code generated from the comments of the Config struct in datasource/http/data.go; DO NOT EDIT MANUALLY -->

- `url` (string) - The URL to request data from. This URL must respond with a `200 OK` response and a `text/*` or `application/json` Content-Type.
- `url` (string) - The URL to request data from. This URL must respond with a `2xx` range response code and a `text/*` or `application/json` Content-Type.

<!-- End of code generated from the comments of the Config struct in datasource/http/data.go; -->

0 comments on commit c2a75ff

Please # to comment.