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

added global accelerator resources #524

Merged
merged 5 commits into from
Aug 11, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions resources/ga-accelerators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/globalaccelerator"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

// GAAccelerator model
type GAAccelerator struct {
svc *globalaccelerator.GlobalAccelerator
ARN *string
}

func init() {
register("GAAccelerator", ListGAAccelerators)
Copy link
Member

Choose a reason for hiding this comment

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

Why did you name it GAAccelerator and not GlobalAccelerator? We usually take the prefix from the AWS SDK package name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it is the type/resource Accelerator within the service GlobalAccelerator. The naming is in line with EC2Image, EC2Address and so on. And how should it look in your opinion for GlobalAccelerator with the 3 parts Accelerator, Listeners and EndpointGroups?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I find GlobalAcceleratorAccelerator is super ugly. For GlobalAcceleratorListener and GlobalAcceleratorEndpointGroup it is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@svenwltr @der-eismann I'm still waiting for a recommendation on how to rename it correctly

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the late response.

I prefer prefixing the resource name with the package name from the AWS SDK. Also I think it is okay to skip the later part for the actual accelerator. Therefore I suggest:

  • GlobalAccelerator
  • GlobalAcceleratorListener
  • GlobalAcceleratorEndpointGroup

}

// ListGAAccelerators enumerates all available accelerators
func ListGAAccelerators(sess *session.Session) ([]Resource, error) {
svc := globalaccelerator.New(sess)
resources := []Resource{}

params := &globalaccelerator.ListAcceleratorsInput{
MaxResults: aws.Int64(100),
}

for {
output, err := svc.ListAccelerators(params)
if err != nil {
return nil, err
}

for _, accelerator := range output.Accelerators {
resources = append(resources, &GAAccelerator{
svc: svc,
ARN: accelerator.AcceleratorArn,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}

return resources, nil
}

// Remove resource
func (gaa *GAAccelerator) Remove() error {
_, err := gaa.svc.DeleteAccelerator(&globalaccelerator.DeleteAcceleratorInput{
AcceleratorArn: gaa.ARN,
})

return err
}

// Properties definition
func (gaa *GAAccelerator) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Arn", gaa.ARN)
return properties
}

// String representation
func (gaa *GAAccelerator) String() string {
return *gaa.ARN
}
124 changes: 124 additions & 0 deletions resources/ga-endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/globalaccelerator"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

// GAEndpointGroup model
type GAEndpointGroup struct {
svc *globalaccelerator.GlobalAccelerator
ARN *string
}

func init() {
register("GAEndpointGroup", ListGAEndpointGroups)
}

// ListGAEndpointGroups enumerates all available accelerators
func ListGAEndpointGroups(sess *session.Session) ([]Resource, error) {
svc := globalaccelerator.New(sess)
acceleratorARNs := []*string{}
listenerARNs := []*string{}
resources := []Resource{}

// get all accelerator arns
acceleratorParams := &globalaccelerator.ListAcceleratorsInput{
MaxResults: aws.Int64(100),
}

for {
output, err := svc.ListAccelerators(acceleratorParams)
if err != nil {
return nil, err
}

for _, accelerator := range output.Accelerators {
acceleratorARNs = append(acceleratorARNs, accelerator.AcceleratorArn)
}

if output.NextToken == nil {
break
}

acceleratorParams.NextToken = output.NextToken
}

// get all listerners arns of all accelerators
for _, acceleratorARN := range acceleratorARNs {
listenerParams := &globalaccelerator.ListListenersInput{
MaxResults: aws.Int64(100),
AcceleratorArn: acceleratorARN,
}

for {
output, err := svc.ListListeners(listenerParams)
if err != nil {
return nil, err
}

for _, listener := range output.Listeners {
listenerARNs = append(listenerARNs, listener.ListenerArn)
}

if output.NextToken == nil {
break
}

listenerParams.NextToken = output.NextToken
}
}

// get all endpoints based on all listeners based on all accelerator
for _, listenerArn := range listenerARNs {
params := &globalaccelerator.ListEndpointGroupsInput{
MaxResults: aws.Int64(100),
ListenerArn: listenerArn,
}

for {
output, err := svc.ListEndpointGroups(params)
if err != nil {
return nil, err
}

for _, endpointGroup := range output.EndpointGroups {
resources = append(resources, &GAEndpointGroup{
svc: svc,
ARN: endpointGroup.EndpointGroupArn,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}
}

return resources, nil
}

// Remove resource
func (gaeg *GAEndpointGroup) Remove() error {
_, err := gaeg.svc.DeleteEndpointGroup(&globalaccelerator.DeleteEndpointGroupInput{
EndpointGroupArn: gaeg.ARN,
})

return err
}

// Properties definition
func (gaeg *GAEndpointGroup) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Arn", gaeg.ARN)
return properties
}

// String representation
func (gaeg *GAEndpointGroup) String() string {
return *gaeg.ARN
}
98 changes: 98 additions & 0 deletions resources/ga-listeners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/globalaccelerator"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

// GAListener model
type GAListener struct {
svc *globalaccelerator.GlobalAccelerator
ARN *string
}

func init() {
register("GAListener", ListGAListeners)
}

// ListGAListeners enumerates all available listeners of all available accelerators
func ListGAListeners(sess *session.Session) ([]Resource, error) {
svc := globalaccelerator.New(sess)
acceleratorARNs := []*string{}
resources := []Resource{}

// get all accelerator arns
acceleratorParams := &globalaccelerator.ListAcceleratorsInput{
MaxResults: aws.Int64(100),
}

for {
output, err := svc.ListAccelerators(acceleratorParams)
if err != nil {
return nil, err
}

for _, accelerator := range output.Accelerators {
acceleratorARNs = append(acceleratorARNs, accelerator.AcceleratorArn)
}

if output.NextToken == nil {
break
}

acceleratorParams.NextToken = output.NextToken
}

// get all listerners
for _, acceleratorARN := range acceleratorARNs {
params := &globalaccelerator.ListListenersInput{
MaxResults: aws.Int64(100),
AcceleratorArn: acceleratorARN,
}

for {
output, err := svc.ListListeners(params)
if err != nil {
return nil, err
}

for _, listener := range output.Listeners {
resources = append(resources, &GAListener{
svc: svc,
ARN: listener.ListenerArn,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}
}

return resources, nil
}

// Remove resource
func (gal *GAListener) Remove() error {
_, err := gal.svc.DeleteListener(&globalaccelerator.DeleteListenerInput{
ListenerArn: gal.ARN,
})

return err
}

// Properties definition
func (gal *GAListener) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Arn", gal.ARN)
return properties
}

// String representation
func (gal *GAListener) String() string {
return *gal.ARN
}