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 7c06f0a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cmd/del/route53/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright © 2022 James Harrington <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package route53

import (
"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 (
dryRun bool
)

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
}

input := &route53.DeleteHostedZoneInput{}

_, err = awsClient.DeleteHostedZonesByName(input)
if err != nil {
reporter.Errorf("Unable to delete route53: %s", err)
}
return
}

func init() {
// Add global flags
flags := Cmd.Flags()
arguments.AddFlags(flags)
Cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Validate if delete would be successful")
}
19 changes: 19 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,24 @@ 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
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
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 7c06f0a

Please sign in to comment.