-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from ekristen/resources-opensearch
add new open search service resources from upstream converted to libnuke format
- Loading branch information
Showing
2 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |