From 609f0c76a9bac0a1d45c058c5376a7849626e8a4 Mon Sep 17 00:00:00 2001 From: James Kwon <96548424+hongil0316@users.noreply.github.com> Date: Sat, 20 Jul 2024 19:37:05 -0400 Subject: [PATCH 1/2] Add Route53 --- modules/aws/route53.go | 55 ++++++++++++++++++++++++ modules/aws/route53_test.go | 86 +++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 modules/aws/route53.go create mode 100644 modules/aws/route53_test.go diff --git a/modules/aws/route53.go b/modules/aws/route53.go new file mode 100644 index 000000000..d4cfde88d --- /dev/null +++ b/modules/aws/route53.go @@ -0,0 +1,55 @@ +package aws + +import ( + "fmt" + "strings" + "testing" + + "github.com/aws/aws-sdk-go/service/route53" + "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/require" +) + +func GetRoute53Record(t *testing.T, hostedZoneID, recordName, awsRegion string) *route53.ResourceRecordSet { + r, err := GetRoute53RecordE(t, hostedZoneID, recordName, awsRegion) + require.NoError(t, err) + + return r +} + +func GetRoute53RecordE(t *testing.T, hostedZoneID, recordName, awsRegion string) (record *route53.ResourceRecordSet, err error) { + route53Client, err := NewRoute53ClientE(t, awsRegion) + if err != nil { + return nil, err + } + + o, err := route53Client.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{ + HostedZoneId: &hostedZoneID, + StartRecordName: &recordName, + StartRecordType: proto.String("A"), + MaxItems: proto.String("1"), + }) + if err != nil { + return + } + for _, record = range o.ResourceRecordSets { + if strings.EqualFold(recordName+".", *record.Name) { + break + } + record = nil + } + if record == nil { + err = fmt.Errorf("record not found") + } + return +} + +// NewS3ClientE creates an S3 client. +func NewRoute53ClientE(t *testing.T, region string) (*route53.Route53, error) { + sess, err := NewAuthenticatedSession(region) + if err != nil { + return nil, err + } + + return route53.New(sess), nil +} diff --git a/modules/aws/route53_test.go b/modules/aws/route53_test.go new file mode 100644 index 000000000..d61907aed --- /dev/null +++ b/modules/aws/route53_test.go @@ -0,0 +1,86 @@ +package aws + +import ( + "fmt" + "testing" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/route53" + "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRoute53Record(t *testing.T) { + t.Parallel() + region := GetRandomStableRegion(t, nil, nil) + c, err := NewRoute53ClientE(t, region) + require.NoError(t, err) + + domain := fmt.Sprintf("terratest%dexample.com", time.Now().UnixNano()) + hostedZone, err := c.CreateHostedZone(&route53.CreateHostedZoneInput{ + Name: aws.String(domain), + CallerReference: aws.String(fmt.Sprint(time.Now().UnixNano())), + }) + require.NoError(t, err) + t.Cleanup(func() { + _, err := c.DeleteHostedZone(&route53.DeleteHostedZoneInput{ + Id: hostedZone.HostedZone.Id, + }) + require.NoError(t, err) + }) + + recordName := fmt.Sprintf("record.%s", domain) + resourceRecordSet := &route53.ResourceRecordSet{ + Name: &recordName, + Type: aws.String("A"), + TTL: aws.Int64(60), + ResourceRecords: []*route53.ResourceRecord{ + { + Value: aws.String("127.0.0.1"), + }, + }, + } + _, err = c.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{ + HostedZoneId: hostedZone.HostedZone.Id, + ChangeBatch: &route53.ChangeBatch{ + Changes: []*route53.Change{ + { + Action: proto.String("CREATE"), + ResourceRecordSet: resourceRecordSet, + }, + }, + }, + }) + require.NoError(t, err) + t.Cleanup(func() { + _, err := c.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{ + HostedZoneId: hostedZone.HostedZone.Id, + ChangeBatch: &route53.ChangeBatch{ + Changes: []*route53.Change{ + { + Action: proto.String("DELETE"), + ResourceRecordSet: resourceRecordSet, + }, + }, + }, + }) + require.NoError(t, err) + }) + + t.Run("ExistingRecord", func(t *testing.T) { + route53Record := GetRoute53Record(t, *hostedZone.HostedZone.Id, recordName, region) + require.NotNil(t, route53Record) + assert.Equal(t, recordName+".", *route53Record.Name) + assert.Equal(t, "A", *route53Record.Type) + assert.Equal(t, "127.0.0.1", *route53Record.ResourceRecords[0].Value) + }) + + t.Run("NotExistRecord", func(t *testing.T) { + route53Record, err := GetRoute53RecordE(t, *hostedZone.HostedZone.Id, "ne"+recordName, region) + assert.Error(t, err) + assert.Nil(t, route53Record) + }) + +} From e9e4b655a6f4afdbb23de87eeed0c6347dcba59b Mon Sep 17 00:00:00 2001 From: James Kwon <96548424+hongil0316@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:13:40 -0400 Subject: [PATCH 2/2] Add Route53 --- modules/aws/route53.go | 20 +++++++++++++++----- modules/aws/route53_test.go | 6 +++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/modules/aws/route53.go b/modules/aws/route53.go index d4cfde88d..abb7a56ba 100644 --- a/modules/aws/route53.go +++ b/modules/aws/route53.go @@ -10,14 +10,16 @@ import ( "github.com/stretchr/testify/require" ) -func GetRoute53Record(t *testing.T, hostedZoneID, recordName, awsRegion string) *route53.ResourceRecordSet { - r, err := GetRoute53RecordE(t, hostedZoneID, recordName, awsRegion) +// GetRoute53Record returns a Route 53 Record +func GetRoute53Record(t *testing.T, hostedZoneID, recordName, recordType, awsRegion string) *route53.ResourceRecordSet { + r, err := GetRoute53RecordE(t, hostedZoneID, recordName, recordType, awsRegion) require.NoError(t, err) return r } -func GetRoute53RecordE(t *testing.T, hostedZoneID, recordName, awsRegion string) (record *route53.ResourceRecordSet, err error) { +// GetRoute53RecordE returns a Route 53 Record +func GetRoute53RecordE(t *testing.T, hostedZoneID, recordName, recordType, awsRegion string) (record *route53.ResourceRecordSet, err error) { route53Client, err := NewRoute53ClientE(t, awsRegion) if err != nil { return nil, err @@ -26,7 +28,7 @@ func GetRoute53RecordE(t *testing.T, hostedZoneID, recordName, awsRegion string) o, err := route53Client.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{ HostedZoneId: &hostedZoneID, StartRecordName: &recordName, - StartRecordType: proto.String("A"), + StartRecordType: &recordType, MaxItems: proto.String("1"), }) if err != nil { @@ -44,7 +46,15 @@ func GetRoute53RecordE(t *testing.T, hostedZoneID, recordName, awsRegion string) return } -// NewS3ClientE creates an S3 client. +// NewRoute53ClientE creates a route 53 client. +func NewRoute53Client(t *testing.T, region string) *route53.Route53 { + c, err := NewRoute53ClientE(t, region) + require.NoError(t, err) + + return c +} + +// NewRoute53ClientE creates a route 53 client. func NewRoute53ClientE(t *testing.T, region string) (*route53.Route53, error) { sess, err := NewAuthenticatedSession(region) if err != nil { diff --git a/modules/aws/route53_test.go b/modules/aws/route53_test.go index d61907aed..3f98d3183 100644 --- a/modules/aws/route53_test.go +++ b/modules/aws/route53_test.go @@ -70,15 +70,15 @@ func TestRoute53Record(t *testing.T) { }) t.Run("ExistingRecord", func(t *testing.T) { - route53Record := GetRoute53Record(t, *hostedZone.HostedZone.Id, recordName, region) + route53Record := GetRoute53Record(t, *hostedZone.HostedZone.Id, recordName, *resourceRecordSet.Type, region) require.NotNil(t, route53Record) assert.Equal(t, recordName+".", *route53Record.Name) - assert.Equal(t, "A", *route53Record.Type) + assert.Equal(t, *resourceRecordSet.Type, *route53Record.Type) assert.Equal(t, "127.0.0.1", *route53Record.ResourceRecords[0].Value) }) t.Run("NotExistRecord", func(t *testing.T) { - route53Record, err := GetRoute53RecordE(t, *hostedZone.HostedZone.Id, "ne"+recordName, region) + route53Record, err := GetRoute53RecordE(t, *hostedZone.HostedZone.Id, "ne"+recordName, "A", region) assert.Error(t, err) assert.Nil(t, route53Record) })