From a2da71cdd0795a0008b0d5b88dff260bf42633c3 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 14 Oct 2024 14:27:48 -0400 Subject: [PATCH] Sites: handle OAC exist error --- .../resource/aws-origin-access-control.go | 45 ++++++++++++++++++- pkg/server/resource/resource.go | 9 ++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/pkg/server/resource/aws-origin-access-control.go b/pkg/server/resource/aws-origin-access-control.go index 077fd6bff..c441f17c6 100644 --- a/pkg/server/resource/aws-origin-access-control.go +++ b/pkg/server/resource/aws-origin-access-control.go @@ -1,7 +1,9 @@ package resource import ( + "errors" "log/slog" + "math/rand" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/cloudfront" @@ -19,6 +21,31 @@ type OriginAccessControlInputs struct { type OriginAccessControlOutputs struct { } +func (r *OriginAccessControl) Read(input *DeleteInput[OriginAccessControlOutputs], output *ReadResult[OriginAccessControlOutputs]) error { + cfg, err := r.config() + if err != nil { + return err + } + cf := cloudfront.NewFromConfig(cfg) + + resp, err := cf.GetOriginAccessControl(r.context, &cloudfront.GetOriginAccessControlInput{ + Id: aws.String(input.ID), + }) + if err != nil { + var alreadyExistsErr *types.NoSuchOriginAccessControl + if errors.As(err, &alreadyExistsErr) { + *output = ReadResult[OriginAccessControlOutputs]{} + return nil + } + return err + } + + *output = ReadResult[OriginAccessControlOutputs]{ + ID: *resp.OriginAccessControl.Id, + Outs: OriginAccessControlOutputs{}, + } + return nil +} func (r *OriginAccessControl) Create(input *OriginAccessControlInputs, output *CreateResult[OriginAccessControlOutputs]) error { cfg, err := r.config() if err != nil { @@ -28,7 +55,7 @@ func (r *OriginAccessControl) Create(input *OriginAccessControlInputs, output *C slog.Info("creating origin access control") resp, err := cf.CreateOriginAccessControl(r.context, &cloudfront.CreateOriginAccessControlInput{ OriginAccessControlConfig: &types.OriginAccessControlConfig{ - Name: aws.String(input.Name), + Name: aws.String(generateName(input.Name)), Description: aws.String("Created by SST"), OriginAccessControlOriginType: "s3", SigningBehavior: "always", @@ -66,3 +93,19 @@ func (r *OriginAccessControl) Delete(input *DeleteInput[OriginAccessControlOutpu } return nil } + +func generateName(name string) string { + // Truncate the name to 55 characters + if len(name) > 55 { + name = name[:55] + } + + // Append a random 8 character + const charset = "abcdefghijklmnopqrstuvwxyz0123456789" + result := make([]byte, 8) + for i := range result { + result[i] = charset[rand.Intn(len(charset))] + } + + return name + "-" + string(result) +} diff --git a/pkg/server/resource/resource.go b/pkg/server/resource/resource.go index b6270c204..1fa58e3d5 100644 --- a/pkg/server/resource/resource.go +++ b/pkg/server/resource/resource.go @@ -10,6 +10,15 @@ import ( "github.com/sst/ion/pkg/project/provider" ) +type ReadInput[T any] struct { + ID string `json:"id"` +} + +type ReadResult[T any] struct { + ID string `json:"id"` + Outs T `json:"outs"` +} + type CreateResult[T any] struct { ID string `json:"id"` Outs T `json:"outs"`