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 20, 2022
1 parent e022cd8 commit a05b34e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 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)
}
98 changes: 98 additions & 0 deletions cmd/del/route53/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package route53

import (
"fmt"
"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/spf13/cobra"
)

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).
Build()

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

if zoneId != "" {
err := deleteHostedZoneId(awsClient, zoneId, reporter)
if err != nil {
reporter.Errorf("Unable to delete hosted zone: %s", err)
}
} else {
reporter.Infof("No zone id specified")
err := deleteAllHostedZones(reporter, awsClient)
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, reporter *rprtr.Object) error {

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

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

func deleteAllHostedZones(
reporter *rprtr.Object, awsClient aws.Client) error {

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, reporter)
if err != nil {
reporter.Errorf("Unable to delete hosted zone %s: %s", *zone.Id, err)
}
}
return nil
}
16 changes: 16 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 Down Expand Up @@ -413,6 +414,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 a05b34e

Please sign in to comment.