From e5ab704dbef4528bd1dcce21315edd254a1fb27d Mon Sep 17 00:00:00 2001 From: Douglas Harcourt Parsons Date: Wed, 1 May 2024 14:25:44 +0100 Subject: [PATCH] Set User Agent & a bunch of small fixes Set a User-Agent for the Terraform Provider so we can know in our infrastructure where these requests are originating from. The user agent is set to `terraform-provider-vercel`, followed by a `/`, and then the most recently tagged version. To do this, we use `ldflags` in the `goreleaser` config. This allows a hardcoded version (in client/request.go) to be replaced. I have updated the local build of the provider to also use goreleaser. I've also fixed the tfproviderlint linter. --- .gitignore | 1 + .goreleaser.yml | 6 +--- Taskfile.yml | 30 ++++++++++++------- ...protection_bypass_for_automation_update.go | 9 ++---- client/request.go | 8 +++++ docs/index.md | 2 +- vercel/provider.go | 2 +- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index badc2056..0f02a7d0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ terraform-provider-vercel .task/ Dist/ +dist/ .DS_Store diff --git a/.goreleaser.yml b/.goreleaser.yml index 03ff504a..1dc6b416 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,9 +1,5 @@ # Visit https://goreleaser.com for documentation on how to customize this # behavior. -before: - hooks: - # this is just an example and not a requirement for provider building/publishing - - go mod tidy builds: - env: # goreleaser does not work with CGO, it could also complicate @@ -14,7 +10,7 @@ builds: flags: - -trimpath ldflags: - - "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}" + - "-s -w -X github.com/vercel/terraform-provider-vercel/client.version={{.Version}}" goos: - freebsd - windows diff --git a/Taskfile.yml b/Taskfile.yml index a46cb24c..3416a047 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,8 +4,10 @@ version: "3" tasks: build: desc: "Build the provider binary" + deps: + - install-goreleaser cmds: - - go build -o terraform-provider-vercel + - goreleaser build --single-target --skip=validate --clean --snapshot sources: - "**/*.go" - go.mod @@ -27,6 +29,13 @@ tasks: status: - which tfplugindocs + install-goreleaser: + desc: "Install goreleaser" + cmds: + - go install github.com/goreleaser/goreleaser@latest + status: + - which goreleaser + docs: desc: "Update the docs generated from description fields" deps: @@ -50,22 +59,21 @@ tasks: status: - which staticcheck - # Temporarily disable tfproviderlint as it doesn't support go 1.18+ syntax - # install-providerlint: - # desc: "Install tfproviderlint tool" - # cmds: - # - go install github.com/bflad/tfproviderlint/cmd/tfproviderlint@latest - # status: - # - which tfproviderlint + install-providerlint: + desc: "Install tfproviderlint tool" + cmds: + - go install github.com/bflad/tfproviderlint/cmd/tfproviderlint@latest + status: + - which tfproviderlint lint: desc: "Run linters against the provider" deps: - install-staticcheck - # - install-providerlint + - install-providerlint cmds: - staticcheck -tags it ./... - # - tfproviderlint -R018=false -R009=false ./... + - tfproviderlint -R018=false -R009=false ./... - cmd: | export BADFMT=$(gofmt -s -l .) test -z "$BADFMT" || (echo -e "invalid gofmt: $BADFMT"; exit 1) @@ -79,7 +87,7 @@ tasks: cat << EOF > ~/.terraformrc provider_installation { dev_overrides { - "vercel/vercel" = "$(pwd)" + "vercel/vercel" = "$(pwd)/dist/terraform-provider-vercel_{{ OS }}_{{ ARCH }}" } direct {} } diff --git a/client/project_protection_bypass_for_automation_update.go b/client/project_protection_bypass_for_automation_update.go index 1dd504b2..25759723 100644 --- a/client/project_protection_bypass_for_automation_update.go +++ b/client/project_protection_bypass_for_automation_update.go @@ -2,7 +2,6 @@ package client import ( "context" - "encoding/json" "fmt" "time" @@ -26,18 +25,14 @@ func getUpdateBypassProtectionRequestBody(newValue bool, secret string) string { return "{}" } - bytes, err := json.Marshal(struct { + return string(mustMarshal(struct { Revoke revokeBypassProtectionRequest `json:"revoke"` }{ Revoke: revokeBypassProtectionRequest{ Regenerate: false, Secret: secret, }, - }) - if err != nil { - panic(err) - } - return string(bytes) + })) } func (c *Client) UpdateProtectionBypassForAutomation(ctx context.Context, request UpdateProtectionBypassForAutomationRequest) (s string, err error) { diff --git a/client/request.go b/client/request.go index 675f036f..50a60241 100644 --- a/client/request.go +++ b/client/request.go @@ -14,6 +14,12 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" ) +/* + * version is the tagged version of this repository. It is overriden at build time by ldflags. + * please see the .goreleaser.yml file for more information. + */ +var version = "dev" + // APIError is an error type that exposes additional information about why an API request failed. type APIError struct { Code string `json:"code"` @@ -46,6 +52,7 @@ func (cr *clientRequest) toHTTPRequest() (*http.Request, error) { if err != nil { return nil, err } + r.Header.Set("User-Agent", fmt.Sprintf("terraform-provider-vercel/%s", version)) if cr.body != "" { r.Header.Set("Content-Type", "application/json") } @@ -55,6 +62,7 @@ func (cr *clientRequest) toHTTPRequest() (*http.Request, error) { // doRequest is a helper function for consistently requesting data from vercel. // This manages: // - Setting the default Content-Type for requests with a body +// - Setting the User-Agent // - Authorization via the Bearer token // - Converting error responses into an inspectable type // - Unmarshaling responses diff --git a/docs/index.md b/docs/index.md index f872eb00..4197416a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -46,4 +46,4 @@ provider "vercel" { ### Optional - `api_token` (String, Sensitive) The Vercel API Token to use. This can also be specified with the `VERCEL_API_TOKEN` shell environment variable. Tokens can be created from your [Vercel settings](https://vercel.com/account/tokens). -- `team` (String) The default Vercel Team to use when creating resources. This can be provided as either a team slug, or team ID. The slug and ID are both available from the Team Settings page in the Vercel dashboard. +- `team` (String) The default Vercel Team to use when creating resources or reading data sources. This can be provided as either a team slug, or team ID. The slug and ID are both available from the Team Settings page in the Vercel dashboard. diff --git a/vercel/provider.go b/vercel/provider.go index 9c88bd8e..beee5a15 100644 --- a/vercel/provider.go +++ b/vercel/provider.go @@ -42,7 +42,7 @@ Use the navigation to the left to read about the available resources. }, "team": schema.StringAttribute{ Optional: true, - Description: "The default Vercel Team to use when creating resources. This can be provided as either a team slug, or team ID. The slug and ID are both available from the Team Settings page in the Vercel dashboard.", + Description: "The default Vercel Team to use when creating resources or reading data sources. This can be provided as either a team slug, or team ID. The slug and ID are both available from the Team Settings page in the Vercel dashboard.", }, }, }