Skip to content

Commit

Permalink
added delete route53
Browse files Browse the repository at this point in the history
  • Loading branch information
olisakov committed Sep 19, 2022
1 parent e022cd8 commit e9138a5
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
./aws-resource
aws-resource
.idea/
3 changes: 2 additions & 1 deletion cmd/del/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/jharrington22/aws-resource/cmd/del/ec2"
"github.com/jharrington22/aws-resource/cmd/del/images"
"github.com/jharrington22/aws-resource/cmd/del/route53"
"github.com/jharrington22/aws-resource/cmd/del/snapshots"
"github.com/spf13/cobra"
)
Expand All @@ -40,5 +41,5 @@ func init() {
DelCmd.AddCommand(ec2.Cmd)
DelCmd.AddCommand(images.Cmd)
DelCmd.AddCommand(snapshots.Cmd)

DelCmd.AddCommand(route53.Cmd)
}
118 changes: 118 additions & 0 deletions cmd/del/route53/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package route53

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/jharrington22/aws-resource/pkg/arguments"
"github.com/jharrington22/aws-resource/pkg/aws"
logging "github.com/jharrington22/aws-resource/pkg/logging"
rprtr "github.com/jharrington22/aws-resource/pkg/reporter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

var (
zoneId string
)

var Cmd = &cobra.Command{
Use: "route53",
Short: "Delete route53 resources",
Long: `Delete route53 resources"
aws-resource delete route53.`,
RunE: run,
}

func run(cmd *cobra.Command, args []string) (err error) {

reporter := rprtr.CreateReporterOrExit()
logging := logging.CreateLoggerOrExit(reporter)

awsClient, err := aws.NewClient().
Logger(logging).
Profile(arguments.Profile).
RoleArn(arguments.RoleArn).
Region(arguments.Region).
Build()

if err != nil {
reporter.Errorf("Unable to build AWS client")
return err
}

regions, err := awsClient.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
reporter.Errorf("Failed to describe regions")
return err
}

if zoneId != "" {
err := deleteHostedZoneId(awsClient, zoneId)
if err != nil {
reporter.Errorf("Unable to delete hosted zone: %s", err)
}
reporter.Infof("Hosted zone %s deleted", zoneId)
}

if zoneId == "" {
reporter.Infof("No image id specified")
err := deleteAllHostedZones(reporter, logging, regions)
if err != nil {
reporter.Errorf("Unable to delete hosted zones: %s", err)
}
}

return
}

func init() {
flags := Cmd.Flags()
arguments.AddFlags(flags)
Cmd.Flags().StringVarP(&zoneId, "zone-id", "i", "", "Delete specific zone id")
}

func deleteHostedZoneId(client aws.Client, zoneId string) error {

input := &route53.DeleteHostedZoneInput{
Id: &zoneId,
}

_, err := client.DeleteHostedZonesByName(input)
if err != nil {
return fmt.Errorf("unable to delete hosted zone: %s", err)
}

return err
}

func deleteAllHostedZones(
reporter *rprtr.Object, logging *logrus.Logger, regions *ec2.DescribeRegionsOutput) error {
awsClient, err := aws.NewClient().
Logger(logging).
Profile(arguments.Profile).
RoleArn(arguments.RoleArn).
Build()

if err != nil {
reporter.Errorf("Unable to build AWS client")
os.Exit(1)
}

input := &route53.ListHostedZonesByNameInput{}

output, err := awsClient.ListHostedZonesByName(input)
if err != nil {
reporter.Errorf("Unable to describe hosted zones %s", err)
return err
}

for _, zone := range output.HostedZones {
err = deleteHostedZoneId(awsClient, *zone.Id)
if err != nil {
reporter.Errorf("Unable to delete hosted zone %s: %s", *zone.Id, err)
}
}
return nil
}
8 changes: 5 additions & 3 deletions pkg/arguments/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
)

var (
Region string
Profile string
RoleArn string
Region string
Profile string
RoleArn string
HostedZone string
)

func AddFlags(fs *pflag.FlagSet) {
fs.StringVarP(&Region, "region", "r", "us-east-1", "AWS Region")
fs.StringVarP(&Profile, "profile", "p", "", "AWS Profile")
fs.StringVarP(&RoleArn, "role-arn", "a", "", "AWS IAM Role ARN")
fs.StringVarP(&HostedZone, "hosted-zone", "z", "", "AWS Hosted Zone")
}
22 changes: 22 additions & 0 deletions pkg/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Client interface {
DescribeVolumes(input *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error)
GetCallerIdentity(input *sts.GetCallerIdentityInput) (*sts.GetCallerIdentityOutput, error)
ListHostedZonesByName(input *route53.ListHostedZonesByNameInput) (*route53.ListHostedZonesByNameOutput, error)
DeleteHostedZonesByName(input *route53.DeleteHostedZoneInput) (*route53.DeleteHostedZoneOutput, error)
TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error)
}

Expand All @@ -52,6 +53,7 @@ type ClientBuilder struct {
profile *string
roleArn *string
credentials *credentials.Value
hostedZone *string
}

func NewClient() *ClientBuilder {
Expand All @@ -78,6 +80,11 @@ func (b *ClientBuilder) RoleArn(value string) *ClientBuilder {
return b
}

func (b *ClientBuilder) HostedZone(value string) *ClientBuilder {
b.hostedZone = aws.String(value)
return b
}

// Create AWS session with a specific set of credentials
func (b *ClientBuilder) BuildSessionWithOptionsCredentials(value *credentials.Value) (*session.Session, error) {
return session.NewSessionWithOptions(session.Options{
Expand Down Expand Up @@ -413,6 +420,21 @@ func (c *awsClient) ListHostedZonesByName(input *route53.ListHostedZonesByNameIn

}

func (c *awsClient) DeleteHostedZonesByName(input *route53.DeleteHostedZoneInput) (
*route53.DeleteHostedZoneOutput, error) {
result, err := c.route53Client.DeleteHostedZone(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
return nil, aerr
}
}
return nil, err
}
return result, nil
}

func (c *awsClient) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {

result, err := c.ec2Client.TerminateInstances(input)
Expand Down

0 comments on commit e9138a5

Please sign in to comment.