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 1 commit
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
55 changes: 55 additions & 0 deletions modules/aws/route53.go
Original file line number Diff line number Diff line change
@@ -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"),
james03160927 marked this conversation as resolved.
Show resolved Hide resolved
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.
james03160927 marked this conversation as resolved.
Show resolved Hide resolved
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, 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)
})

}