diff --git a/internal/cmd/provider/create_version.go b/internal/cmd/provider/create_version.go index e8b8059..ee9929f 100644 --- a/internal/cmd/provider/create_version.go +++ b/internal/cmd/provider/create_version.go @@ -3,6 +3,7 @@ package provider import ( "context" "fmt" + "net/http" "github.com/pkg/errors" "github.com/shurcooL/graphql" @@ -18,6 +19,7 @@ func createVersion() cli.ActionFunc { dir := cliCtx.String(flagGoReleaserDir.Name) providerType := cliCtx.String(flagProviderType.Name) + useRegisterPlatformV2 := cliCtx.Bool(flagUseRegisterPlatformV2.Name) fmt.Println("Retrieving release data from ", dir) versionData, err := internal.BuildGoReleaserVersionData(dir) @@ -73,12 +75,12 @@ func createVersion() cli.ActionFunc { } fmt.Println("Uploading the checksums file") - if err := checksumsFile.Upload(cliCtx.Context, dir, createMutation.CreateTerraformProviderVersion.SHA256SumsUploadURL); err != nil { + if err := checksumsFile.Upload(cliCtx.Context, dir, createMutation.CreateTerraformProviderVersion.SHA256SumsUploadURL, checksumsFile.MetadataHeaders()); err != nil { return errors.Wrap(err, "could not upload checksums file") } fmt.Println("Uploading the signatures file") - if err := signatureFile.Upload(cliCtx.Context, dir, createMutation.CreateTerraformProviderVersion.SHA256SumsSigUploadURL); err != nil { + if err := signatureFile.Upload(cliCtx.Context, dir, createMutation.CreateTerraformProviderVersion.SHA256SumsSigUploadURL, signatureFile.MetadataHeaders()); err != nil { return errors.Wrap(err, "could not upload signature file") } @@ -90,8 +92,14 @@ func createVersion() cli.ActionFunc { return errors.Wrapf(err, "invalid artifact filename: %s", archives[i].Name) } - if err := registerPlatform(cliCtx.Context, dir, versionID, &archives[i]); err != nil { - return err + if useRegisterPlatformV2 { + if err := registerPlatformV2(cliCtx.Context, dir, versionID, &archives[i]); err != nil { + return err + } + } else { + if err := registerPlatform(cliCtx.Context, dir, versionID, &archives[i]); err != nil { + return err + } } } fmt.Printf("Draft version %s created\n", versionID) @@ -121,6 +129,7 @@ func createVersion() cli.ActionFunc { } } +// deprecated, use registerPlatformV2 instead. func registerPlatform(ctx context.Context, dir string, versionID string, artifact *internal.GoReleaserArtifact) error { var mutation struct { RegisterTerraformProviderVersionPlatform string `graphql:"terraformProviderVersionRegisterPlatform(version: $version, input: $input)"` @@ -147,7 +156,53 @@ func registerPlatform(ctx context.Context, dir string, versionID string, artifac return err } - if err := artifact.Upload(ctx, dir, mutation.RegisterTerraformProviderVersionPlatform); err != nil { + if err := artifact.Upload(ctx, dir, mutation.RegisterTerraformProviderVersionPlatform, artifact.MetadataHeaders()); err != nil { + return errors.Wrapf(err, "could not upload artifact: %s", artifact.Name) + } + + return nil +} + +func registerPlatformV2(ctx context.Context, dir string, versionID string, artifact *internal.GoReleaserArtifact) error { + var mutation struct { + RegisterTerraformProviderVersionPlatform struct { + UploadUrl string `json:"uploadUrl"` + UploadHeaders struct { + Entries []struct { + Key string `json:"key"` + Value string `json:"value"` + } `json:"entries"` + } `json:"uploadHeaders"` + } `graphql:"terraformProviderVersionRegisterPlatformV2(version: $version, input: $input)"` + } + + archiveChecksum, err := artifact.Checksum(dir) + if err != nil { + return errors.Wrap(err, "could not calculate checksum of artifact") + } + + fmt.Printf("Uploading the artifact for %s/%s\n", *artifact.OS, *artifact.Arch) + + variables := map[string]any{ + "version": graphql.ID(versionID), + "input": TerraformProviderVersionPlatformInput{ + Architecture: *artifact.Arch, + OS: *artifact.OS, + ArchiveChecksum: archiveChecksum, + BinaryChecksum: artifact.Extra.Checksum.BinarySHA256(), + }, + } + + if err := authenticated.Client.Mutate(ctx, &mutation, variables); err != nil { + return err + } + + header := http.Header{} + for _, entry := range mutation.RegisterTerraformProviderVersionPlatform.UploadHeaders.Entries { + header.Set(entry.Key, entry.Value) + } + + if err := artifact.Upload(ctx, dir, mutation.RegisterTerraformProviderVersionPlatform.UploadUrl, header); err != nil { return errors.Wrapf(err, "could not upload artifact: %s", artifact.Name) } diff --git a/internal/cmd/provider/flags.go b/internal/cmd/provider/flags.go index e50f4a1..45eb885 100644 --- a/internal/cmd/provider/flags.go +++ b/internal/cmd/provider/flags.go @@ -42,6 +42,11 @@ var flagProviderType = &cli.StringFlag{ Required: true, } +var flagUseRegisterPlatformV2 = &cli.BoolFlag{ + Name: "use-register-platform-v2", + Usage: "Use register platform v2 mutation", +} + var flagProviderVersionProtocols = &cli.StringSliceFlag{ Name: "protocols", Usage: "Terraform plugin protocols supported by the provider", diff --git a/internal/cmd/provider/internal/goreleaser.go b/internal/cmd/provider/internal/goreleaser.go index cf4aec6..3734b71 100644 --- a/internal/cmd/provider/internal/goreleaser.go +++ b/internal/cmd/provider/internal/goreleaser.go @@ -140,7 +140,7 @@ func (a *GoReleaserArtifact) Checksum(dir string) (string, error) { } // Upload uploads the artifact's content to the given URL using HTTP PUT method. -func (a *GoReleaserArtifact) Upload(ctx context.Context, dir string, url string) error { +func (a *GoReleaserArtifact) Upload(ctx context.Context, dir string, url string, header http.Header) error { content, err := a.content(dir) if err != nil { return errors.Wrapf(err, "could not get artifact content for %s", a.Name) @@ -157,16 +157,8 @@ func (a *GoReleaserArtifact) Upload(ctx context.Context, dir string, url string) return errors.Wrapf(err, "could not create request for %s", a.Name) } - if a.OS != nil { - request.Header.Set("x-amz-meta-binary-os", *a.OS) - } - - if a.Arch != nil { - request.Header.Set("x-amz-meta-binary-architecture", *a.Arch) - } - - if checksum := a.Extra.Checksum.BinarySHA256(); checksum != "" { - request.Header.Set("x-amz-meta-binary-checksum", checksum) + for k := range header { + request.Header.Set(k, header.Get(k)) } response, err := http.DefaultClient.Do(request) @@ -187,6 +179,22 @@ func (a *GoReleaserArtifact) Upload(ctx context.Context, dir string, url string) return nil } +func (a *GoReleaserArtifact) MetadataHeaders() http.Header { + headers := http.Header{} + if a.OS != nil { + headers.Set("x-amz-meta-binary-os", *a.OS) + } + + if a.Arch != nil { + headers.Set("x-amz-meta-binary-architecture", *a.Arch) + } + + if checksum := a.Extra.Checksum.BinarySHA256(); checksum != "" { + headers.Set("x-amz-meta-binary-checksum", checksum) + } + return headers +} + // ValidateFilename validates that the artifact's name matches the expected // format. func (a *GoReleaserArtifact) ValidateFilename(providerType, versionNumber string) error {