Skip to content

Commit

Permalink
feat: use register platform v2 mutation (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-hontarau authored Nov 29, 2024
1 parent 9c7dcf1 commit cb4c9b1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
65 changes: 60 additions & 5 deletions internal/cmd/provider/create_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"net/http"

"github.com/pkg/errors"
"github.com/shurcooL/graphql"
Expand All @@ -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)
Expand Down Expand Up @@ -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.AWSMetadataHeaders()); 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.AWSMetadataHeaders()); err != nil {
return errors.Wrap(err, "could not upload signature file")
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)"`
Expand All @@ -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.AWSMetadataHeaders()); 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)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/provider/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 21 additions & 11 deletions internal/cmd/provider/internal/goreleaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -187,6 +179,24 @@ func (a *GoReleaserArtifact) Upload(ctx context.Context, dir string, url string)
return nil
}

// AWSMetadataHeaders returns the headers required for uploading with an AWS presigned URL.
// Deprecated: Use UploadHeaders from the gql TerraformProviderVersionRegisterPlatformV2 response instead.
func (a *GoReleaserArtifact) AWSMetadataHeaders() 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 {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Command() *cli.Command {
flagProviderVersionProtocols,
flagGoReleaserDir,
flagGPGKeyID,
flagUseRegisterPlatformV2,
},
Action: createVersion(),
Before: authenticated.Ensure,
Expand Down

0 comments on commit cb4c9b1

Please sign in to comment.