Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Add opensearch packages vpc endpoint support #1078

62 changes: 62 additions & 0 deletions resources/opensearchservice-packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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)

listResp, err := svc.DescribePackages(&opensearchservice.DescribePackagesInput{})
if err != nil {
return nil, err
}

resources := make([]Resource, 0)

for _, pkg := range listResp.PackageDetailsList {
resources = append(resources, &OSPackage{
svc: svc,
packageID: pkg.PackageID,
packageName: pkg.PackageName,
createdTime: pkg.CreatedAt,
})
}

return resources, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, another thing I just noticed - both resources need paging as well, just like you did in #1044. Could you do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@der-eismann No worries, updated with pagination and noted for my next PR's!

}

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
}
76 changes: 76 additions & 0 deletions resources/opensearchservice-vpcendpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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)

vpcEndpointIds, err := getOpenSearchVpcEndpointIds(svc)
if err != nil {
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 {
resources = append(resources, &OSVPCEndpoint{
svc: svc,
vpcEndpointId: vpcEndpoint.VpcEndpointId,
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!
If we have the IDs already, why are we describing them again? To make sure they exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@der-eismann apologies. You are correct. That should have been removed and is now updated.


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,
})

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
}