diff --git a/modules/aws/route53.go b/modules/aws/route53.go new file mode 100644 index 000000000..abb7a56ba --- /dev/null +++ b/modules/aws/route53.go @@ -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 +} diff --git a/modules/aws/route53_test.go b/modules/aws/route53_test.go new file mode 100644 index 000000000..3f98d3183 --- /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, *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) + }) + +}