From 987a2bf557d0e93b04e4c74c997905444b4957c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Tue, 21 Nov 2023 09:21:58 +0100 Subject: [PATCH] Add ECRPublicRepository fixes #1155 --- resources/cloudcontrol.go | 1 - resources/ecr-public-repository.go | 89 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 resources/ecr-public-repository.go diff --git a/resources/cloudcontrol.go b/resources/cloudcontrol.go index 735d18a4b..0026dd15a 100644 --- a/resources/cloudcontrol.go +++ b/resources/cloudcontrol.go @@ -26,7 +26,6 @@ func init() { registerCloudControl("AWS::AppRunner::Service") registerCloudControl("AWS::ApplicationInsights::Application") registerCloudControl("AWS::Backup::Framework") - registerCloudControl("AWS::ECR::PublicRepository") registerCloudControl("AWS::ECR::PullThroughCacheRule") registerCloudControl("AWS::ECR::RegistryPolicy") registerCloudControl("AWS::ECR::ReplicationConfiguration") diff --git a/resources/ecr-public-repository.go b/resources/ecr-public-repository.go new file mode 100644 index 000000000..3ff5a6c22 --- /dev/null +++ b/resources/ecr-public-repository.go @@ -0,0 +1,89 @@ +package resources + +import ( + "fmt" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ecrpublic" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type ECRPublicRepository struct { + svc *ecrpublic.ECRPublic + name *string + createdTime *time.Time + tags []*ecrpublic.Tag +} + +func init() { + register("ECRPublicRepository", ListECRPublicRepositories) +} + +func ListECRPublicRepositories(sess *session.Session) ([]Resource, error) { + svc := ecrpublic.New(sess) + resources := []Resource{} + + input := &ecrpublic.DescribeRepositoriesInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.DescribeRepositories(input) + if err != nil { + return nil, err + } + + for _, repository := range output.Repositories { + fmt.Println(repository) + tagResp, err := svc.ListTagsForResource(&ecrpublic.ListTagsForResourceInput{ + ResourceArn: repository.RepositoryArn, + }) + if err != nil { + return nil, err + } + resources = append(resources, &ECRPublicRepository{ + svc: svc, + name: repository.RepositoryName, + createdTime: repository.CreatedAt, + tags: tagResp.Tags, + }) + } + + if output.NextToken == nil { + break + } + + input.NextToken = output.NextToken + } + + return resources, nil +} + +func (r *ECRPublicRepository) Filter() error { + return nil +} + +func (r *ECRPublicRepository) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreatedTime", r.createdTime.Format(time.RFC3339)) + + for _, t := range r.tags { + properties.SetTag(t.Key, t.Value) + } + return properties +} + +func (r *ECRPublicRepository) Remove() error { + params := &ecrpublic.DeleteRepositoryInput{ + RepositoryName: r.name, + Force: aws.Bool(true), + } + _, err := r.svc.DeleteRepository(params) + return err +} + +func (r *ECRPublicRepository) String() string { + return fmt.Sprintf("Repository: %s", *r.name) +}