From b5c792ede3e078647dd9dae61ba3ad4bc7babe51 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Wed, 12 Jul 2023 16:59:37 -0500 Subject: [PATCH 1/9] Create opensearchservice-packages.go Adding support for opensearch packages. --- resources/opensearchservice-packages.go | 64 +++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 resources/opensearchservice-packages.go diff --git a/resources/opensearchservice-packages.go b/resources/opensearchservice-packages.go new file mode 100644 index 000000000..a9e7b27ef --- /dev/null +++ b/resources/opensearchservice-packages.go @@ -0,0 +1,64 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/opensearchservice" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type OSPackage struct { + svc *opensearchservice.OpenSearchService + domainName *string + packageID *string +} + +func init() { + register("OSPackage", ListOSPackages) +} + +func ListOSPackages(sess *session.Session) ([]Resource, error) { + svc := opensearchservice.New(sess) + + listResp, err := svc.DescribePackages(&opensearchservice.DescribePackagesInput{}) + if err != nil { + return nil, err + } + + resources := make([]Resource, 0) + + for _, pkg := range listResp.PackageDetailsList { + domainResp, err := svc.ListDomainsForPackage(&opensearchservice.ListDomainsForPackageInput{ + PackageID: pkg.PackageID, + }) + if err != nil { + return nil, err + } + + for _, domain := range domainResp.DomainPackageDetailsList { + resources = append(resources, &OSPackage{ + svc: svc, + domainName: domain.DomainName, + packageID: pkg.PackageID, + }) + } + } + + return resources, nil +} + +func (o *OSPackage) Remove() error { + _, err := o.svc.DeletePackage(&opensearchservice.DeletePackageInput{ + PackageID: o.packageID, + }) + + return err +} + +func (o *OSPackage) Properties() types.Properties { + properties := types.NewProperties() + return properties +} + +func (o *OSPackage) String() string { + return *o.packageID +} From 55571a7dbc4ffb0341762f9e5680c34eba424003 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Wed, 12 Jul 2023 22:03:58 -0500 Subject: [PATCH 2/9] Update opensearchservice-packages.go Confirmed working cleanup of os packages. --- resources/opensearchservice-packages.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/resources/opensearchservice-packages.go b/resources/opensearchservice-packages.go index a9e7b27ef..ca03e9061 100644 --- a/resources/opensearchservice-packages.go +++ b/resources/opensearchservice-packages.go @@ -27,20 +27,10 @@ func ListOSPackages(sess *session.Session) ([]Resource, error) { resources := make([]Resource, 0) for _, pkg := range listResp.PackageDetailsList { - domainResp, err := svc.ListDomainsForPackage(&opensearchservice.ListDomainsForPackageInput{ - PackageID: pkg.PackageID, + resources = append(resources, &OSPackage{ + svc: svc, + packageID: pkg.PackageID, }) - if err != nil { - return nil, err - } - - for _, domain := range domainResp.DomainPackageDetailsList { - resources = append(resources, &OSPackage{ - svc: svc, - domainName: domain.DomainName, - packageID: pkg.PackageID, - }) - } } return resources, nil From 84fbd14186c982159071b8907aa799f81d183680 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Thu, 13 Jul 2023 11:59:27 -0500 Subject: [PATCH 3/9] Adding opensearch vpcendpoints functionality. Removing unused var from packages. --- resources/opensearchservice-packages.go | 5 +- resources/opensearchservice-vpcendpoints.go | 55 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 resources/opensearchservice-vpcendpoints.go diff --git a/resources/opensearchservice-packages.go b/resources/opensearchservice-packages.go index ca03e9061..a24ff03d3 100644 --- a/resources/opensearchservice-packages.go +++ b/resources/opensearchservice-packages.go @@ -7,9 +7,8 @@ import ( ) type OSPackage struct { - svc *opensearchservice.OpenSearchService - domainName *string - packageID *string + svc *opensearchservice.OpenSearchService + packageID *string } func init() { diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go new file mode 100644 index 000000000..8d89eed7c --- /dev/null +++ b/resources/opensearchservice-vpcendpoints.go @@ -0,0 +1,55 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/opensearchservice" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type OSVPCEndpoint struct { + svc *opensearchservice.OpenSearchService + vpcEndpointId *string +} + +func init() { + register("OSVPCEndpoint", ListOSVPCEndpoints) +} + +func ListOSVPCEndpoints(sess *session.Session) ([]Resource, error) { + svc := opensearchservice.New(sess) + + listResp, err := svc.DescribeVpcEndpoints(&opensearchservice.DescribeVpcEndpointsInput{ + VpcEndpointIds: []*string{}, + }) + if err != nil { + return nil, err + } + + resources := make([]Resource, 0) + + for _, vpcEndpoint := range listResp.VpcEndpoints { + resources = append(resources, &OSVPCEndpoint{ + svc: svc, + vpcEndpointId: vpcEndpoint.VpcEndpointId, + }) + } + + return resources, nil +} + +func (o *OSVPCEndpoint) Remove() error { + _, err := o.svc.DeleteVpcEndpoint(&opensearchservice.DeleteVpcEndpointInput{ + VpcEndpointId: o.vpcEndpointId, + }) + + return err +} + +func (o *OSVPCEndpoint) Properties() types.Properties { + properties := types.NewProperties() + return properties +} + +func (o *OSVPCEndpoint) String() string { + return *o.vpcEndpointId +} From 378ccab9cddd85c584498082090f01159e129b60 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Thu, 13 Jul 2023 13:35:03 -0500 Subject: [PATCH 4/9] Update opensearchservice-vpcendpoints.go Correctly retrieving VPC endpoint ids. --- resources/opensearchservice-vpcendpoints.go | 22 ++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go index 8d89eed7c..0a687990a 100644 --- a/resources/opensearchservice-vpcendpoints.go +++ b/resources/opensearchservice-vpcendpoints.go @@ -18,8 +18,13 @@ func init() { func ListOSVPCEndpoints(sess *session.Session) ([]Resource, error) { svc := opensearchservice.New(sess) + vpcEndpointIds, err := getOpenSearchVpcEndpointIds(svc) + if err != nil { + return nil, err + } + listResp, err := svc.DescribeVpcEndpoints(&opensearchservice.DescribeVpcEndpointsInput{ - VpcEndpointIds: []*string{}, + VpcEndpointIds: vpcEndpointIds, }) if err != nil { return nil, err @@ -37,6 +42,21 @@ func ListOSVPCEndpoints(sess *session.Session) ([]Resource, error) { return resources, nil } +func getOpenSearchVpcEndpointIds(svc *opensearchservice.OpenSearchService) ([]*string, error) { + vpcEndpointIds := make([]*string, 0) + + listResp, err := svc.ListVpcEndpoints(&opensearchservice.ListVpcEndpointsInput{}) + if err != nil { + return nil, err + } + + for _, vpcEndpoint := range listResp.VpcEndpointSummaryList { + vpcEndpointIds = append(vpcEndpointIds, vpcEndpoint.VpcEndpointId) + } + + return vpcEndpointIds, nil +} + func (o *OSVPCEndpoint) Remove() error { _, err := o.svc.DeleteVpcEndpoint(&opensearchservice.DeleteVpcEndpointInput{ VpcEndpointId: o.vpcEndpointId, From ade341a6f8e0e44d939d68d3791ecc0d425f7e87 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Fri, 14 Jul 2023 11:48:05 -0500 Subject: [PATCH 5/9] Update opensearchservice-packages.go Setting property values. --- resources/opensearchservice-packages.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/resources/opensearchservice-packages.go b/resources/opensearchservice-packages.go index a24ff03d3..cdac640cc 100644 --- a/resources/opensearchservice-packages.go +++ b/resources/opensearchservice-packages.go @@ -1,14 +1,18 @@ package resources import ( + "time" + "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/opensearchservice" "github.com/rebuy-de/aws-nuke/v2/pkg/types" ) type OSPackage struct { - svc *opensearchservice.OpenSearchService - packageID *string + svc *opensearchservice.OpenSearchService + packageID *string + packageName *string + createdTime *time.Time } func init() { @@ -27,8 +31,10 @@ func ListOSPackages(sess *session.Session) ([]Resource, error) { for _, pkg := range listResp.PackageDetailsList { resources = append(resources, &OSPackage{ - svc: svc, - packageID: pkg.PackageID, + svc: svc, + packageID: pkg.PackageID, + packageName: pkg.PackageName, + createdTime: pkg.CreatedAt, }) } @@ -45,6 +51,9 @@ func (o *OSPackage) Remove() error { func (o *OSPackage) Properties() types.Properties { properties := types.NewProperties() + properties.Set("PackageID", o.packageID) + properties.Set("PackageName", o.packageName) + properties.Set("CreatedTime", o.createdTime.Format(time.RFC3339)) return properties } From 41e22d20213ec4c58e82f1ce9411f136856475f5 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Fri, 14 Jul 2023 12:04:45 -0500 Subject: [PATCH 6/9] Update opensearchservice-vpcendpoints.go Adding id to properties. --- resources/opensearchservice-vpcendpoints.go | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go index 0a687990a..7f757300c 100644 --- a/resources/opensearchservice-vpcendpoints.go +++ b/resources/opensearchservice-vpcendpoints.go @@ -67,6 +67,7 @@ func (o *OSVPCEndpoint) Remove() error { func (o *OSVPCEndpoint) Properties() types.Properties { properties := types.NewProperties() + properties.Set("VpcEndpointId", o.vpcEndpointId) return properties } From 7ddbe328da7056d6da701493a7c2259cd1a91b01 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Wed, 23 Aug 2023 17:59:29 -0500 Subject: [PATCH 7/9] Update opensearchservice-vpcendpoints.go Removing unneeded describe call. --- resources/opensearchservice-vpcendpoints.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go index 7f757300c..a17c4704c 100644 --- a/resources/opensearchservice-vpcendpoints.go +++ b/resources/opensearchservice-vpcendpoints.go @@ -23,19 +23,12 @@ func ListOSVPCEndpoints(sess *session.Session) ([]Resource, error) { return nil, err } - listResp, err := svc.DescribeVpcEndpoints(&opensearchservice.DescribeVpcEndpointsInput{ - VpcEndpointIds: vpcEndpointIds, - }) - if err != nil { - return nil, err - } - resources := make([]Resource, 0) - for _, vpcEndpoint := range listResp.VpcEndpoints { + for _, vpcEndpointId := range vpcEndpointIds { resources = append(resources, &OSVPCEndpoint{ svc: svc, - vpcEndpointId: vpcEndpoint.VpcEndpointId, + vpcEndpointId: vpcEndpointId, }) } From d6deab9e06409496a34ebca6d8041372b8351334 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Thu, 24 Aug 2023 09:10:44 -0500 Subject: [PATCH 8/9] Update opensearchservice-vpcendpoints.go Removed extra function for getting id's, incorporated into list function. --- resources/opensearchservice-vpcendpoints.go | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go index a17c4704c..80d652c8d 100644 --- a/resources/opensearchservice-vpcendpoints.go +++ b/resources/opensearchservice-vpcendpoints.go @@ -18,38 +18,23 @@ func init() { func ListOSVPCEndpoints(sess *session.Session) ([]Resource, error) { svc := opensearchservice.New(sess) - vpcEndpointIds, err := getOpenSearchVpcEndpointIds(svc) + listResp, err := svc.ListVpcEndpoints(&opensearchservice.ListVpcEndpointsInput{}) if err != nil { return nil, err } resources := make([]Resource, 0) - for _, vpcEndpointId := range vpcEndpointIds { + for _, vpcEndpoint := range listResp.VpcEndpointSummaryList { resources = append(resources, &OSVPCEndpoint{ svc: svc, - vpcEndpointId: vpcEndpointId, + vpcEndpointId: vpcEndpoint.VpcEndpointId, }) } return resources, nil } -func getOpenSearchVpcEndpointIds(svc *opensearchservice.OpenSearchService) ([]*string, error) { - vpcEndpointIds := make([]*string, 0) - - listResp, err := svc.ListVpcEndpoints(&opensearchservice.ListVpcEndpointsInput{}) - if err != nil { - return nil, err - } - - for _, vpcEndpoint := range listResp.VpcEndpointSummaryList { - vpcEndpointIds = append(vpcEndpointIds, vpcEndpoint.VpcEndpointId) - } - - return vpcEndpointIds, nil -} - func (o *OSVPCEndpoint) Remove() error { _, err := o.svc.DeleteVpcEndpoint(&opensearchservice.DeleteVpcEndpointInput{ VpcEndpointId: o.vpcEndpointId, From ea85816f11bfecf704b1ba8d280caa3c6ca4c0c7 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Thu, 24 Aug 2023 09:16:36 -0500 Subject: [PATCH 9/9] Adding pagination to list functions. --- resources/opensearchservice-packages.go | 37 ++++++++++++++------- resources/opensearchservice-vpcendpoints.go | 33 ++++++++++++------ 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/resources/opensearchservice-packages.go b/resources/opensearchservice-packages.go index cdac640cc..8dde96cfd 100644 --- a/resources/opensearchservice-packages.go +++ b/resources/opensearchservice-packages.go @@ -21,21 +21,34 @@ func init() { func ListOSPackages(sess *session.Session) ([]Resource, error) { svc := opensearchservice.New(sess) + resources := []Resource{} + var nextToken *string - listResp, err := svc.DescribePackages(&opensearchservice.DescribePackagesInput{}) - if err != nil { - return nil, err - } + for { + params := &opensearchservice.DescribePackagesInput{ + NextToken: nextToken, + } + listResp, err := svc.DescribePackages(params) + if err != nil { + return nil, err + } + + for _, pkg := range listResp.PackageDetailsList { + resources = append(resources, &OSPackage{ + svc: svc, + packageID: pkg.PackageID, + packageName: pkg.PackageName, + createdTime: pkg.CreatedAt, + }) + } - resources := make([]Resource, 0) + // Check if there are more results + if listResp.NextToken == nil { + break // No more results, exit the loop + } - for _, pkg := range listResp.PackageDetailsList { - resources = append(resources, &OSPackage{ - svc: svc, - packageID: pkg.PackageID, - packageName: pkg.PackageName, - createdTime: pkg.CreatedAt, - }) + // Set the nextToken for the next iteration + nextToken = listResp.NextToken } return resources, nil diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go index 80d652c8d..ad2253ba6 100644 --- a/resources/opensearchservice-vpcendpoints.go +++ b/resources/opensearchservice-vpcendpoints.go @@ -17,19 +17,32 @@ func init() { func ListOSVPCEndpoints(sess *session.Session) ([]Resource, error) { svc := opensearchservice.New(sess) + resources := []Resource{} + var nextToken *string - listResp, err := svc.ListVpcEndpoints(&opensearchservice.ListVpcEndpointsInput{}) - if err != nil { - return nil, err - } + for { + params := &opensearchservice.ListVpcEndpointsInput{ + NextToken: nextToken, + } + listResp, err := svc.ListVpcEndpoints(params) + if err != nil { + return nil, err + } + + for _, vpcEndpoint := range listResp.VpcEndpointSummaryList { + resources = append(resources, &OSVPCEndpoint{ + svc: svc, + vpcEndpointId: vpcEndpoint.VpcEndpointId, + }) + } - resources := make([]Resource, 0) + // Check if there are more results + if listResp.NextToken == nil { + break // No more results, exit the loop + } - for _, vpcEndpoint := range listResp.VpcEndpointSummaryList { - resources = append(resources, &OSVPCEndpoint{ - svc: svc, - vpcEndpointId: vpcEndpoint.VpcEndpointId, - }) + // Set the nextToken for the next iteration + nextToken = listResp.NextToken } return resources, nil