From 5b047cb00554f52491095de9dc31d333f26b19a2 Mon Sep 17 00:00:00 2001 From: Mike Schouw Date: Sat, 18 Nov 2023 14:09:02 +0100 Subject: [PATCH 1/3] feat: add cloudfront cache policy --- resources/cloudfront-cache-policy.go | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 resources/cloudfront-cache-policy.go diff --git a/resources/cloudfront-cache-policy.go b/resources/cloudfront-cache-policy.go new file mode 100644 index 00000000..2732beee --- /dev/null +++ b/resources/cloudfront-cache-policy.go @@ -0,0 +1,68 @@ +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 CloudFrontCachePolicy struct { + svc *cloudfront.CloudFront + ID *string +} + +func init() { + register("CloudFrontCachePolicy", ListCloudFrontCachePolicy) +} + +func ListCloudFrontCachePolicy(sess *session.Session) ([]Resource, error) { + svc := cloudfront.New(sess) + resources := []Resource{} + params := &cloudfront.ListCachePoliciesInput{} + + for { + resp, err := svc.ListCachePolicies(params) + if err != nil { + return nil, err + } + + for _, item := range resp.CachePolicyList.Items { + if *item.Type == "custom" { + resources = append(resources, &CloudFrontCachePolicy{ + svc: svc, + ID: item.CachePolicy.Id, + }) + } + } + + if resp.CachePolicyList.NextMarker == nil { + break + } + + params.Marker = resp.CachePolicyList.NextMarker + } + + return resources, nil +} + +func (f *CloudFrontCachePolicy) Remove() error { + resp, err := f.svc.GetCachePolicy(&cloudfront.GetCachePolicyInput{ + Id: f.ID, + }) + if err != nil { + return err + } + + _, err = f.svc.DeleteCachePolicy(&cloudfront.DeleteCachePolicyInput{ + Id: f.ID, + IfMatch: resp.ETag, + }) + + return err +} + +func (f *CloudFrontCachePolicy) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("ID", f.ID) + return properties +} From abab6e87b08ddb3f4a155dbcf0a9261df8179d8b Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Fri, 12 Apr 2024 18:45:42 -0600 Subject: [PATCH 2/3] refactor: convert cloudfront cache policy to libnuke format --- resources/cloudfront-cache-policy.go | 30 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/resources/cloudfront-cache-policy.go b/resources/cloudfront-cache-policy.go index 2732beee..8284b17f 100644 --- a/resources/cloudfront-cache-policy.go +++ b/resources/cloudfront-cache-policy.go @@ -1,9 +1,15 @@ package resources import ( - "github.com/aws/aws-sdk-go/aws/session" + "context" + "github.com/aws/aws-sdk-go/service/cloudfront" - "github.com/rebuy-de/aws-nuke/v2/pkg/types" + + "github.com/ekristen/libnuke/pkg/registry" + "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/libnuke/pkg/types" + + "github.com/ekristen/aws-nuke/pkg/nuke" ) type CloudFrontCachePolicy struct { @@ -11,13 +17,23 @@ type CloudFrontCachePolicy struct { ID *string } +const CloudFrontCachePolicyResource = "CloudFrontCachePolicy" + func init() { - register("CloudFrontCachePolicy", ListCloudFrontCachePolicy) + registry.Register(®istry.Registration{ + Name: CloudFrontCachePolicyResource, + Scope: nuke.Account, + Lister: &CloudFrontCachePolicyLister{}, + }) } -func ListCloudFrontCachePolicy(sess *session.Session) ([]Resource, error) { - svc := cloudfront.New(sess) - resources := []Resource{} +type CloudFrontCachePolicyLister struct{} + +func (l *CloudFrontCachePolicyLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { + opts := o.(*nuke.ListerOpts) + + svc := cloudfront.New(opts.Session) + resources := make([]resource.Resource, 0) params := &cloudfront.ListCachePoliciesInput{} for { @@ -45,7 +61,7 @@ func ListCloudFrontCachePolicy(sess *session.Session) ([]Resource, error) { return resources, nil } -func (f *CloudFrontCachePolicy) Remove() error { +func (f *CloudFrontCachePolicy) Remove(_ context.Context) error { resp, err := f.svc.GetCachePolicy(&cloudfront.GetCachePolicyInput{ Id: f.ID, }) From e266b70d54f3a14f80492fad3b015a621cc7e3a2 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Fri, 12 Apr 2024 18:51:19 -0600 Subject: [PATCH 3/3] chore: add nolint for goconst --- resources/cloudfront-cache-policy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/cloudfront-cache-policy.go b/resources/cloudfront-cache-policy.go index 8284b17f..c6b2ad6c 100644 --- a/resources/cloudfront-cache-policy.go +++ b/resources/cloudfront-cache-policy.go @@ -43,7 +43,7 @@ func (l *CloudFrontCachePolicyLister) List(_ context.Context, o interface{}) ([] } for _, item := range resp.CachePolicyList.Items { - if *item.Type == "custom" { + if *item.Type == "custom" { //nolint:goconst resources = append(resources, &CloudFrontCachePolicy{ svc: svc, ID: item.CachePolicy.Id,