Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Route53 #1421

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
james03160927 marked this conversation as resolved.
Show resolved Hide resolved
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)
})

}