Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Sites: handle OAC exist error
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Oct 14, 2024
1 parent c3aa599 commit a2da71c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
45 changes: 44 additions & 1 deletion pkg/server/resource/aws-origin-access-control.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 {
Expand All @@ -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",
Expand Down Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions pkg/server/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit a2da71c

Please sign in to comment.