From 534298272621d96ad8e103da643110efdbf7eb11 Mon Sep 17 00:00:00 2001 From: Sherd White <106187526+swhite-oreilly@users.noreply.github.com> Date: Fri, 25 Aug 2023 03:36:33 -0500 Subject: [PATCH] Add opensearch packages vpc endpoint support (#1078) * Create opensearchservice-packages.go Adding support for opensearch packages. * Update opensearchservice-packages.go Confirmed working cleanup of os packages. * Adding opensearch vpcendpoints functionality. Removing unused var from packages. * Update opensearchservice-vpcendpoints.go Correctly retrieving VPC endpoint ids. * Update opensearchservice-packages.go Setting property values. * Update opensearchservice-vpcendpoints.go Adding id to properties. * Update opensearchservice-vpcendpoints.go Removing unneeded describe call. * Update opensearchservice-vpcendpoints.go Removed extra function for getting id's, incorporated into list function. * Adding pagination to list functions. --- resources/opensearchservice-packages.go | 75 +++++++++++++++++++++ resources/opensearchservice-vpcendpoints.go | 67 ++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 resources/opensearchservice-packages.go create mode 100644 resources/opensearchservice-vpcendpoints.go diff --git a/resources/opensearchservice-packages.go b/resources/opensearchservice-packages.go new file mode 100644 index 000000000..8dde96cfd --- /dev/null +++ b/resources/opensearchservice-packages.go @@ -0,0 +1,75 @@ +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 + packageName *string + createdTime *time.Time +} + +func init() { + register("OSPackage", ListOSPackages) +} + +func ListOSPackages(sess *session.Session) ([]Resource, error) { + svc := opensearchservice.New(sess) + resources := []Resource{} + var nextToken *string + + 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, + }) + } + + // Check if there are more results + if listResp.NextToken == nil { + break // No more results, exit the loop + } + + // Set the nextToken for the next iteration + nextToken = listResp.NextToken + } + + 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() + properties.Set("PackageID", o.packageID) + properties.Set("PackageName", o.packageName) + properties.Set("CreatedTime", o.createdTime.Format(time.RFC3339)) + return properties +} + +func (o *OSPackage) String() string { + return *o.packageID +} diff --git a/resources/opensearchservice-vpcendpoints.go b/resources/opensearchservice-vpcendpoints.go new file mode 100644 index 000000000..ad2253ba6 --- /dev/null +++ b/resources/opensearchservice-vpcendpoints.go @@ -0,0 +1,67 @@ +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) + resources := []Resource{} + var nextToken *string + + 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, + }) + } + + // Check if there are more results + if listResp.NextToken == nil { + break // No more results, exit the loop + } + + // Set the nextToken for the next iteration + nextToken = listResp.NextToken + } + + 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() + properties.Set("VpcEndpointId", o.vpcEndpointId) + return properties +} + +func (o *OSVPCEndpoint) String() string { + return *o.vpcEndpointId +}