diff --git a/cmd/del/route53/cmd.go b/cmd/del/route53/cmd.go new file mode 100644 index 0000000..4f6ce56 --- /dev/null +++ b/cmd/del/route53/cmd.go @@ -0,0 +1,71 @@ +/* +Copyright © 2022 James Harrington + +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") +} diff --git a/pkg/aws/client.go b/pkg/aws/client.go index 205c32f..9c339e4 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) } @@ -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)