Skip to content

Commit

Permalink
Merge pull request #46 from ekristen/resources-opensearch
Browse files Browse the repository at this point in the history
 add new open search service resources from upstream converted to libnuke format
  • Loading branch information
ekristen authored Jan 26, 2024
2 parents c35bdb8 + dedde5e commit 4e0ee69
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
98 changes: 98 additions & 0 deletions resources/opensearchservice-packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package resources

import (
"context"
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go/service/opensearchservice"

"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/pkg/nuke"
)

const OSPackageResource = "OSPackage"

func init() {
resource.Register(&resource.Registration{
Name: OSPackageResource,
Scope: nuke.Account,
Lister: &OSPackageLister{},
})
}

type OSPackageLister struct{}

func (l *OSPackageLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := opensearchservice.New(opts.Session)
resources := make([]resource.Resource, 0)
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
}

type OSPackage struct {
svc *opensearchservice.OpenSearchService
packageID *string
packageName *string
createdTime *time.Time
}

func (o *OSPackage) Filter() error {
if strings.HasPrefix(*o.packageID, "G") {
return fmt.Errorf("cannot delete default opensearch packages")
}
return nil
}

func (o *OSPackage) Remove(_ context.Context) 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
}
82 changes: 82 additions & 0 deletions resources/opensearchservice-vpcendpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/service/opensearchservice"

"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/pkg/nuke"
)

const OSVPCEndpointResource = "OSVPCEndpoint"

func init() {
resource.Register(&resource.Registration{
Name: OSVPCEndpointResource,
Scope: nuke.Account,
Lister: &OSVPCEndpointLister{},
})
}

type OSVPCEndpointLister struct{}

func (l *OSVPCEndpointLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := opensearchservice.New(opts.Session)
resources := make([]resource.Resource, 0)
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
}

type OSVPCEndpoint struct {
svc *opensearchservice.OpenSearchService
vpcEndpointId *string
}

func (o *OSVPCEndpoint) Remove(_ context.Context) 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
}

0 comments on commit 4e0ee69

Please sign in to comment.