Skip to content

Commit fc02f92

Browse files
dylanratcliffeactions-user
authored andcommitted
Fixed logic with resource record mapping (#2231)
Fixes ENG-1026 GitOrigin-RevId: 117e3e7268af888b32afc1a942afe391325cae21
1 parent d2fb858 commit fc02f92

File tree

2 files changed

+108
-11
lines changed

2 files changed

+108
-11
lines changed

‎aws-source/adapters/route53-resource-record-set.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ func resourceRecordSetGetFunc(ctx context.Context, client *route53.Client, scope
1717
return nil, errors.New("get is not supported for route53-resource-record-set. Use search")
1818
}
1919

20+
// constructRecordFQDN constructs the full FQDN for a Route53 record based on
21+
// the record name and the hosted zone name. This handles the various edge cases
22+
// where the record name might already contain the full domain.
23+
func constructRecordFQDN(recordName, hostedZoneName string) string {
24+
// If the name is the same as the FQDN of the hosted zone, we don't have
25+
// to append it otherwise it'll be in there twice. It seems that NS and
26+
// MX records sometimes have the full FQDN in the name
27+
zoneFQDN := strings.TrimSuffix(hostedZoneName, ".")
28+
29+
if recordName == zoneFQDN {
30+
return recordName
31+
} else if strings.HasSuffix(recordName, "."+zoneFQDN) || strings.HasSuffix(recordName, hostedZoneName) {
32+
// Record name already contains the full domain
33+
return recordName
34+
} else {
35+
// Calculate the full FQDN based on the hosted zone name and the record name
36+
return recordName + "." + hostedZoneName
37+
}
38+
}
39+
2040
// ResourceRecordSetSearchFunc Search func that accepts a hosted zone or a
2141
// terraform ID in the format {hostedZone}_{recordName}_{type}. Unfortunately
2242
// the "name" means the record name within the scope of the hosted zone, not the
@@ -51,17 +71,7 @@ func resourceRecordSetSearchFunc(ctx context.Context, client *route53.Client, sc
5171
return nil, fmt.Errorf("hosted zone %s not found", hostedZoneID)
5272
}
5373

54-
// If the name is the same as the FQDN of the hosted zone, we don't have
55-
// to append it otherwise it'll be in there twice. It seems that NS and
56-
// MX records sometimes have the full FQDN in the name
57-
zoneFQDN := strings.TrimSuffix(*zoneResp.HostedZone.Name, ".")
58-
var fullName string
59-
if recordName == zoneFQDN {
60-
fullName = recordName
61-
} else {
62-
// Calculate the full FQDN based on the hosted zone name and the record name
63-
fullName = recordName + "." + *zoneResp.HostedZone.Name
64-
}
74+
fullName := constructRecordFQDN(recordName, *zoneResp.HostedZone.Name)
6575

6676
var maxItems int32 = 1
6777
req := route53.ListResourceRecordSetsInput{

‎aws-source/adapters/route53-resource-record-set_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,93 @@ func TestResourceRecordSetItemMapper(t *testing.T) {
113113
tests.Execute(t, item)
114114
}
115115

116+
// TestConstructRecordFQDN tests the FQDN construction logic
117+
// for various record name formats
118+
func TestConstructRecordFQDN(t *testing.T) {
119+
type testCase struct {
120+
name string
121+
hostedZoneName string
122+
recordName string
123+
expectedFQDN string
124+
description string
125+
}
126+
127+
testCases := []testCase{
128+
{
129+
name: "simple_subdomain",
130+
hostedZoneName: "example.com.",
131+
recordName: "www",
132+
expectedFQDN: "www.example.com.",
133+
description: "Simple subdomain record",
134+
},
135+
{
136+
name: "already_full_fqdn_with_trailing_dot",
137+
hostedZoneName: "example.com.",
138+
recordName: "subdomain.example.com.",
139+
expectedFQDN: "subdomain.example.com.",
140+
description: "Record name already contains full FQDN with trailing dot",
141+
},
142+
{
143+
name: "already_full_fqdn_without_trailing_dot",
144+
hostedZoneName: "example.com.",
145+
recordName: "subdomain.example.com",
146+
expectedFQDN: "subdomain.example.com",
147+
description: "Record name already contains full FQDN without trailing dot",
148+
},
149+
{
150+
name: "apex_record_matches_zone",
151+
hostedZoneName: "example.com.",
152+
recordName: "example.com",
153+
expectedFQDN: "example.com",
154+
description: "Apex record where name matches zone FQDN (without trailing dot)",
155+
},
156+
{
157+
name: "complex_subdomain_case",
158+
hostedZoneName: "a2d-dev.tv.",
159+
recordName: "davidtest-other.a2d-dev.tv",
160+
expectedFQDN: "davidtest-other.a2d-dev.tv",
161+
description: "Complex case from the bug report - prevents double domain concatenation",
162+
},
163+
{
164+
name: "nested_subdomain",
165+
hostedZoneName: "example.com.",
166+
recordName: "deep.nested.subdomain",
167+
expectedFQDN: "deep.nested.subdomain.example.com.",
168+
description: "Nested subdomain that needs zone appended",
169+
},
170+
{
171+
name: "ns_record_with_full_domain",
172+
hostedZoneName: "example.com.",
173+
recordName: "ns.example.com.",
174+
expectedFQDN: "ns.example.com.",
175+
description: "NS record with full domain (common pattern)",
176+
},
177+
{
178+
name: "zone_without_trailing_dot",
179+
hostedZoneName: "example.com",
180+
recordName: "www",
181+
expectedFQDN: "www.example.com",
182+
description: "Hosted zone name without trailing dot",
183+
},
184+
{
185+
name: "record_already_ends_with_zone_no_dot",
186+
hostedZoneName: "example.com",
187+
recordName: "subdomain.example.com",
188+
expectedFQDN: "subdomain.example.com",
189+
description: "Record already ends with zone name (no trailing dots)",
190+
},
191+
}
192+
193+
for _, tc := range testCases {
194+
t.Run(tc.name, func(t *testing.T) {
195+
result := constructRecordFQDN(tc.recordName, tc.hostedZoneName)
196+
if result != tc.expectedFQDN {
197+
t.Errorf("Expected FQDN %q but got %q. %s", tc.expectedFQDN, result, tc.description)
198+
}
199+
})
200+
}
201+
}
202+
116203
func TestNewRoute53ResourceRecordSetAdapter(t *testing.T) {
117204
client, account, region := route53GetAutoConfig(t)
118205

0 commit comments

Comments
 (0)