Skip to content

Commit

Permalink
Merge pull request #1421 from gruntwork-io/route53
Browse files Browse the repository at this point in the history
Add Route53
  • Loading branch information
james03160927 authored Jul 23, 2024
2 parents fc3c632 + e9e4b65 commit 5a79983
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
65 changes: 65 additions & 0 deletions modules/aws/route53.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aws

import (
"fmt"
"strings"
"testing"

"github.com/aws/aws-sdk-go/service/route53"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
)

// 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
}

// 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
}

o, err := route53Client.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{
HostedZoneId: &hostedZoneID,
StartRecordName: &recordName,
StartRecordType: &recordType,
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
}

// 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 {
return nil, err
}

return route53.New(sess), nil
}
86 changes: 86 additions & 0 deletions modules/aws/route53_test.go
Original file line number Diff line number Diff line change
@@ -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, *resourceRecordSet.Type, region)
require.NotNil(t, route53Record)
assert.Equal(t, recordName+".", *route53Record.Name)
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, "A", region)
assert.Error(t, err)
assert.Nil(t, route53Record)
})

}

0 comments on commit 5a79983

Please sign in to comment.