Skip to content

Commit

Permalink
Merge pull request #525 from ekristen/route53-hosted-zone
Browse files Browse the repository at this point in the history
feat: route53 new resources
  • Loading branch information
ekristen authored Jan 27, 2025
2 parents fe7fc3b + 57ff815 commit 3ce2ae4
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 28 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.6

require (
github.com/aws/aws-sdk-go v1.55.5
github.com/aws/aws-sdk-go-v2 v1.32.7
github.com/aws/aws-sdk-go-v2 v1.34.0
github.com/aws/aws-sdk-go-v2/config v1.28.7
github.com/aws/aws-sdk-go-v2/credentials v1.17.48
github.com/aws/aws-sdk-go-v2/service/iam v1.38.3
Expand All @@ -13,7 +13,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssmquicksetup v1.3.2
github.com/aws/aws-sdk-go-v2/service/sts v1.33.3
github.com/aws/aws-sdk-go-v2/service/transfer v1.55.1
github.com/aws/smithy-go v1.22.1
github.com/aws/smithy-go v1.22.2
github.com/ekristen/libnuke v0.24.1
github.com/fatih/color v1.18.0
github.com/golang/mock v1.6.0
Expand All @@ -32,14 +32,16 @@ require (
require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.22 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.26 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.4.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.7 // indirect
github.com/aws/aws-sdk-go-v2/service/route53 v1.48.3 // indirect
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.4.12 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.8 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.7 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.32.7 h1:ky5o35oENWi0JYWUZkB7WYvVPP+bcRF5/Iq7JWSb5Rw=
github.com/aws/aws-sdk-go-v2 v1.32.7/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U=
github.com/aws/aws-sdk-go-v2 v1.34.0 h1:9iyL+cjifckRGEVpRKZP3eIxVlL06Qk1Tk13vreaVQU=
github.com/aws/aws-sdk-go-v2 v1.34.0/go.mod h1:JgstGg0JjWU1KpVJjD5H0y0yyAIpSdKEq556EI6yOOM=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 h1:lL7IfaFzngfx0ZwUGOZdsFFnQ5uLvR0hWqqhyE7Q9M8=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7/go.mod h1:QraP0UcVlQJsmHfioCrveWOC1nbiWUl3ej08h4mXWoc=
github.com/aws/aws-sdk-go-v2/config v1.28.7 h1:GduUnoTXlhkgnxTD93g1nv4tVPILbdNQOzav+Wpg7AE=
Expand All @@ -14,8 +16,12 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.22 h1:kqOrpojG71DxJm/KDPO+Z/
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.22/go.mod h1:NtSFajXVVL8TA2QNngagVZmUtXciyrHOt7xgz4faS/M=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26 h1:I/5wmGMffY4happ8NOCuIUEWGUvvFp5NSeQcXl9RHcI=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26/go.mod h1:FR8f4turZtNy6baO0KJ5FJUmXH/cSkI9fOngs0yl6mA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.29 h1:Ej0Rf3GMv50Qh4G4852j2djtoDb7AzQ7MuQeFHa3D70=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.29/go.mod h1:oeNTC7PwJNoM5AznVr23wxhLnuJv0ZDe5v7w0wqIs9M=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26 h1:zXFLuEuMMUOvEARXFUVJdfqZ4bvvSgdGRq/ATcrQxzM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26/go.mod h1:3o2Wpy0bogG1kyOPrgkXA8pgIfEEv0+m19O9D5+W8y8=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.29 h1:6e8a71X+9GfghragVevC5bZqvATtc3mAMgxpSNbgzF0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.29/go.mod h1:c4jkZiQ+BWpNqq7VtrxjwISrLrt/VvPq3XiopkUIolI=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.26 h1:GeNJsIFHB+WW5ap2Tec4K6dzcVTsRbsT1Lra46Hv9ME=
Expand All @@ -30,6 +36,10 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.7 h1:8eUsivBQz
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.7/go.mod h1:kLPQvGUmxn/fqiCrDeohwG33bq2pQpGeY62yRO6Nrh0=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.7 h1:Hi0KGbrnr57bEHWM0bJ1QcBzxLrL/k2DHvGYhb8+W1w=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.7/go.mod h1:wKNgWgExdjjrm4qvfbTorkvocEstaoDl4WCvGfeCy9c=
github.com/aws/aws-sdk-go-v2/service/route53 v1.48.3 h1:9m6dc70AMaAIwephy90ApV/smdya4XA48zCWQTITcJE=
github.com/aws/aws-sdk-go-v2/service/route53 v1.48.3/go.mod h1:CpxUf0l25aMre5K8cD0L2UeivINz0wiWM+CiWrHRxho=
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.4.12 h1:nFo2bMCOCr1S3/iurBI+DPLTTq6DMKg8vVhgMd91AOE=
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.4.12/go.mod h1:V5DW+JWvGcbimFHReG61uJtl0tnBpqCCYJpvHWkKnVY=
github.com/aws/aws-sdk-go-v2/service/s3 v1.72.0 h1:SAfh4pNx5LuTafKKWR02Y+hL3A+3TX8cTKG1OIAJaBk=
github.com/aws/aws-sdk-go-v2/service/s3 v1.72.0/go.mod h1:r+xl5yzMk9083rMR+sJ5TYj9Tihvf/l1oxzZXDgGj2Q=
github.com/aws/aws-sdk-go-v2/service/s3control v1.52.1 h1:xxGbXbGtO/VMz2JqB1UwEDlSchryUss0KmQJSZ0oTUE=
Expand All @@ -46,6 +56,8 @@ github.com/aws/aws-sdk-go-v2/service/transfer v1.55.1 h1:bENkaFtA6rxHAwNPjYbgwYx
github.com/aws/aws-sdk-go-v2/service/transfer v1.55.1/go.mod h1:C7x9hpm90ZocJ9GbauHMkVMU0m7knEiKhOaa4um9tBU=
github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/aws/smithy-go v1.22.2 h1:6D9hW43xKFrRx/tXXfAlIZc4JI+yQe6snnWcQyxSyLQ=
github.com/aws/smithy-go v1.22.2/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package resources

import (
"context"

"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/gotidy/ptr"

"github.com/aws/aws-sdk-go/service/route53"

"github.com/ekristen/libnuke/pkg/registry"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (l *Route53HostedZoneLister) List(_ context.Context, o interface{}) ([]reso
hostedZones = append(hostedZones, resp.HostedZones...)

params.Marker = resp.NextMarker
if aws.StringValue(params.Marker) == "" {
if ptr.ToString(params.Marker) == "" {
break
}
}
Expand All @@ -54,7 +54,7 @@ func (l *Route53HostedZoneLister) List(_ context.Context, o interface{}) ([]reso
for _, hz := range hostedZones {
tags, err := svc.ListTagsForResource(&route53.ListTagsForResourceInput{
ResourceId: hz.Id,
ResourceType: aws.String("hostedzone"),
ResourceType: ptr.String("hostedzone"),
})

if err != nil {
Expand All @@ -63,44 +63,39 @@ func (l *Route53HostedZoneLister) List(_ context.Context, o interface{}) ([]reso

resources = append(resources, &Route53HostedZone{
svc: svc,
id: hz.Id,
name: hz.Name,
tags: tags.ResourceTagSet.Tags,
ID: hz.Id,
Name: hz.Name,
Tags: tags.ResourceTagSet.Tags,
})
}
return resources, nil
}

type Route53HostedZone struct {
svc *route53.Route53
id *string
name *string
tags []*route53.Tag
ID *string
Name *string
Tags []*route53.Tag
}

func (hz *Route53HostedZone) Remove(_ context.Context) error {
func (r *Route53HostedZone) Remove(_ context.Context) error {
params := &route53.DeleteHostedZoneInput{
Id: hz.id,
Id: r.ID,
}

_, err := hz.svc.DeleteHostedZone(params)
_, err := r.svc.DeleteHostedZone(params)
if err != nil {
return err
}

return nil
}

func (hz *Route53HostedZone) Properties() types.Properties {
properties := types.NewProperties()
for _, tag := range hz.tags {
properties.SetTag(tag.Key, tag.Value)
}
properties.Set("Name", hz.name)
properties.Set("ID", hz.id)
return properties
func (r *Route53HostedZone) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (hz *Route53HostedZone) String() string {
return fmt.Sprintf("%s (%s)", *hz.id, *hz.name)
func (r *Route53HostedZone) String() string {
// TODO(v4): change the stringer format
return fmt.Sprintf("%s (%s)", *r.ID, *r.Name)
}
139 changes: 139 additions & 0 deletions resources/route53-profile-association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package resources

import (
"context"
"errors"
"time"

"github.com/gotidy/ptr"

"github.com/aws/aws-sdk-go-v2/service/route53profiles"
rtypes "github.com/aws/aws-sdk-go-v2/service/route53profiles/types"

liberror "github.com/ekristen/libnuke/pkg/errors"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)

const Route53ProfileAssociationResource = "Route53ProfileAssociation"

func init() {
registry.Register(&registry.Registration{
Name: Route53ProfileAssociationResource,
Scope: nuke.Account,
Resource: &Route53ProfileAssociation{},
Lister: &Route53ProfileAssociationLister{},
})
}

type Route53ProfileAssociationLister struct{}

func (l *Route53ProfileAssociationLister) List(ctx context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := route53profiles.NewFromConfig(*opts.Config)
var resources []resource.Resource

params := &route53profiles.ListProfileAssociationsInput{
MaxResults: ptr.Int32(100),
}

for {
res, err := svc.ListProfileAssociations(ctx, params)
if err != nil {
return nil, err
}

for _, p := range res.ProfileAssociations {
resources = append(resources, &Route53ProfileAssociation{
svc: svc,
ID: p.Id,
Name: p.Name,
OwnerID: p.OwnerId,
ProfileID: p.ProfileId,
ResourceID: p.ResourceId,
Status: ptr.String(string(p.Status)),
CreationTime: p.CreationTime,
ModificationTime: p.ModificationTime,
})
}

if res.NextToken == nil {
break
}

params.NextToken = res.NextToken
}

return resources, nil
}

type Route53ProfileAssociation struct {
svc *route53profiles.Client
ID *string
Name *string
OwnerID *string
ProfileID *string
ResourceID *string
Status *string
CreationTime *time.Time
ModificationTime *time.Time
}

func (r *Route53ProfileAssociation) Filter() error {
if ptr.ToString(r.Status) == string(rtypes.ProfileStatusCreating) {
return errors.New("cannot delete profile association in CREATING state")
}

return nil
}

func (r *Route53ProfileAssociation) Remove(ctx context.Context) error {
// Note: if somehow the status is already deleting we do not want to try to delete again. However, this is the
// first resource to take advantage of the HandleWait method since deletion is not immediate and the disassociation
// is not immediate.
if ptr.ToString(r.Status) == string(rtypes.ProfileStatusDeleting) {
return nil
}

// Note: disassociation is not immediate.
_, err := r.svc.DisassociateProfile(ctx, &route53profiles.DisassociateProfileInput{
ProfileId: r.ProfileID,
ResourceId: r.ResourceID,
})
return err
}

func (r *Route53ProfileAssociation) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (r *Route53ProfileAssociation) String() string {
return *r.Name
}

func (r *Route53ProfileAssociation) HandleWait(ctx context.Context) error {
var notFound *rtypes.ResourceNotFoundException
p, err := r.svc.GetProfileAssociation(ctx, &route53profiles.GetProfileAssociationInput{
ProfileAssociationId: r.ID,
})
if err != nil {
if errors.As(err, &notFound) {
return nil
}

return err
}

currentStatus := string(p.ProfileAssociation.Status)

r.Status = ptr.String(currentStatus)

if currentStatus == string(rtypes.ProfileStatusDeleting) {
return liberror.ErrWaitResource("waiting for operation to complete")
}

return nil
}
Loading

0 comments on commit 3ce2ae4

Please sign in to comment.