From c2a92c6e8d82238d4f32972490db8c4f5bd0ac98 Mon Sep 17 00:00:00 2001 From: Nicolas Pellegrin Date: Tue, 20 Jun 2023 10:43:58 +0200 Subject: [PATCH 1/2] Add CloudFront Function (#1027) --- resources/cloudfront-function.go | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 resources/cloudfront-function.go diff --git a/resources/cloudfront-function.go b/resources/cloudfront-function.go new file mode 100644 index 000000000..be1824fe4 --- /dev/null +++ b/resources/cloudfront-function.go @@ -0,0 +1,62 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CloudFrontFunction struct { + svc *cloudfront.CloudFront + name *string + stage *string +} + +func init() { + register("CloudFrontFunction", ListCloudFrontFunctions) +} + +func ListCloudFrontFunctions(sess *session.Session) ([]Resource, error) { + svc := cloudfront.New(sess) + resources := []Resource{} + + for { + resp, err := svc.ListFunctions(nil) + if err != nil { + return nil, err + } + + for _, item := range resp.FunctionList.Items { + resources = append(resources, &CloudFrontFunction{ + svc: svc, + name: item.Name, + stage: item.FunctionMetadata.Stage, + }) + } + return resources, nil + } +} + +func (f *CloudFrontFunction) Remove() error { + resp, err := f.svc.GetFunction(&cloudfront.GetFunctionInput{ + Name: f.name, + Stage: f.stage, + }) + if err != nil { + return err + } + + _, err = f.svc.DeleteFunction(&cloudfront.DeleteFunctionInput{ + Name: f.name, + IfMatch: resp.ETag, + }) + + return err +} + +func (f *CloudFrontFunction) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("name", f.name) + properties.Set("stage", f.stage) + return properties +} From 9d88a7b57a878f1488488adf11ef6ea880eee5fb Mon Sep 17 00:00:00 2001 From: Nicolas Pellegrin Date: Mon, 26 Jun 2023 22:44:37 +0200 Subject: [PATCH 2/2] Add missing pagination --- resources/cloudfront-function.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/resources/cloudfront-function.go b/resources/cloudfront-function.go index be1824fe4..3be06d6fc 100644 --- a/resources/cloudfront-function.go +++ b/resources/cloudfront-function.go @@ -19,9 +19,10 @@ func init() { func ListCloudFrontFunctions(sess *session.Session) ([]Resource, error) { svc := cloudfront.New(sess) resources := []Resource{} + params := &cloudfront.ListFunctionsInput{} for { - resp, err := svc.ListFunctions(nil) + resp, err := svc.ListFunctions(params) if err != nil { return nil, err } @@ -33,8 +34,15 @@ func ListCloudFrontFunctions(sess *session.Session) ([]Resource, error) { stage: item.FunctionMetadata.Stage, }) } - return resources, nil + + if resp.FunctionList.NextMarker == nil { + break + } + + params.Marker = resp.FunctionList.NextMarker + } + return resources, nil } func (f *CloudFrontFunction) Remove() error {