From e9138a506cdb4783375e7238b250a3cc516f6c7b Mon Sep 17 00:00:00 2001 From: oisakov Date: Mon, 19 Sep 2022 13:37:01 +0300 Subject: [PATCH] added delete route53 --- .gitignore | 3 +- cmd/del/cmd.go | 3 +- cmd/del/route53/cmd.go | 118 ++++++++++++++++++++++++++++++++++++++++ pkg/arguments/global.go | 8 ++- pkg/aws/client.go | 22 ++++++++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 cmd/del/route53/cmd.go diff --git a/.gitignore b/.gitignore index f1b909c..c305351 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -./aws-resource +aws-resource +.idea/ diff --git a/cmd/del/cmd.go b/cmd/del/cmd.go index 547aec7..fd6583e 100644 --- a/cmd/del/cmd.go +++ b/cmd/del/cmd.go @@ -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" ) @@ -40,5 +41,5 @@ func init() { DelCmd.AddCommand(ec2.Cmd) DelCmd.AddCommand(images.Cmd) DelCmd.AddCommand(snapshots.Cmd) - + DelCmd.AddCommand(route53.Cmd) } diff --git a/cmd/del/route53/cmd.go b/cmd/del/route53/cmd.go new file mode 100644 index 0000000..e0c031b --- /dev/null +++ b/cmd/del/route53/cmd.go @@ -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 +} diff --git a/pkg/arguments/global.go b/pkg/arguments/global.go index 61cef98..d74a78a 100644 --- a/pkg/arguments/global.go +++ b/pkg/arguments/global.go @@ -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") } diff --git a/pkg/aws/client.go b/pkg/aws/client.go index 205c32f..da38a1d 100644 --- a/pkg/aws/client.go +++ b/pkg/aws/client.go @@ -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) } @@ -52,6 +53,7 @@ type ClientBuilder struct { profile *string roleArn *string credentials *credentials.Value + hostedZone *string } func NewClient() *ClientBuilder { @@ -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{ @@ -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)