Skip to content

Commit

Permalink
Merge pull request #39944 from hashicorp/b-aws_route53_record.geoloca…
Browse files Browse the repository at this point in the history
…tion_routing_policy-crash

r/aws_route53_record: Fix `panic: interface conversion` when `geolocation_routing_policy` is empty
  • Loading branch information
ewbankkit authored Oct 30, 2024
2 parents db786a4 + d84e793 commit fd8e814
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .changelog/39944.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_route53_record: Fix `interface conversion: interface {} is nil, not map[string]interface {}` panic when `geolocation_routing_policy` is empty
```
71 changes: 29 additions & 42 deletions internal/service/route53/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,65 +918,54 @@ func expandResourceRecordSet(d *schema.ResourceData, zoneName string) *awstypes.
}

// Alias record
if v, ok := d.GetOk(names.AttrAlias); ok {
aliases := v.([]interface{})
alias := aliases[0].(map[string]interface{})
if v, ok := d.GetOk(names.AttrAlias); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.AliasTarget = &awstypes.AliasTarget{
DNSName: aws.String(alias[names.AttrName].(string)),
EvaluateTargetHealth: alias["evaluate_target_health"].(bool),
HostedZoneId: aws.String(alias["zone_id"].(string)),
DNSName: aws.String(tfMap[names.AttrName].(string)),
EvaluateTargetHealth: tfMap["evaluate_target_health"].(bool),
HostedZoneId: aws.String(tfMap["zone_id"].(string)),
}
}

if v, ok := d.GetOk("cidr_routing_policy"); ok {
records := v.([]interface{})
cidr := records[0].(map[string]interface{})

if v, ok := d.GetOk("cidr_routing_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.CidrRoutingConfig = &awstypes.CidrRoutingConfig{
CollectionId: aws.String(cidr["collection_id"].(string)),
LocationName: aws.String(cidr["location_name"].(string)),
CollectionId: aws.String(tfMap["collection_id"].(string)),
LocationName: aws.String(tfMap["location_name"].(string)),
}
}

if v, ok := d.GetOk("failover_routing_policy"); ok {
records := v.([]interface{})
failover := records[0].(map[string]interface{})

apiObject.Failover = awstypes.ResourceRecordSetFailover(failover[names.AttrType].(string))
if v, ok := d.GetOk("failover_routing_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.Failover = awstypes.ResourceRecordSetFailover(tfMap[names.AttrType].(string))
}

if v, ok := d.GetOk("geolocation_routing_policy"); ok {
geolocations := v.([]interface{})
geolocation := geolocations[0].(map[string]interface{})

if v, ok := d.GetOk("geolocation_routing_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.GeoLocation = &awstypes.GeoLocation{
ContinentCode: nilString(geolocation["continent"].(string)),
CountryCode: nilString(geolocation["country"].(string)),
SubdivisionCode: nilString(geolocation["subdivision"].(string)),
ContinentCode: nilString(tfMap["continent"].(string)),
CountryCode: nilString(tfMap["country"].(string)),
SubdivisionCode: nilString(tfMap["subdivision"].(string)),
}
}

if v, ok := d.GetOk("geoproximity_routing_policy"); ok {
geoproximityvalues := v.([]interface{})
geoproximity := geoproximityvalues[0].(map[string]interface{})

if v, ok := d.GetOk("geoproximity_routing_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.GeoProximityLocation = &awstypes.GeoProximityLocation{
AWSRegion: nilString(geoproximity["aws_region"].(string)),
Bias: aws.Int32(int32(geoproximity["bias"].(int))),
Coordinates: expandCoordinates(geoproximity["coordinates"].(*schema.Set).List()),
LocalZoneGroup: nilString(geoproximity["local_zone_group"].(string)),
AWSRegion: nilString(tfMap["aws_region"].(string)),
Bias: aws.Int32(int32(tfMap["bias"].(int))),
Coordinates: expandCoordinates(tfMap["coordinates"].(*schema.Set).List()),
LocalZoneGroup: nilString(tfMap["local_zone_group"].(string)),
}
}

if v, ok := d.GetOk("health_check_id"); ok {
apiObject.HealthCheckId = aws.String(v.(string))
}

if v, ok := d.GetOk("latency_routing_policy"); ok {
records := v.([]interface{})
latency := records[0].(map[string]interface{})

apiObject.Region = awstypes.ResourceRecordSetRegion(latency[names.AttrRegion].(string))
if v, ok := d.GetOk("latency_routing_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.Region = awstypes.ResourceRecordSetRegion(tfMap[names.AttrRegion].(string))
}

if v, ok := d.GetOk("multivalue_answer_routing_policy"); ok {
Expand All @@ -987,11 +976,9 @@ func expandResourceRecordSet(d *schema.ResourceData, zoneName string) *awstypes.
apiObject.SetIdentifier = aws.String(v.(string))
}

if v, ok := d.GetOk("weighted_routing_policy"); ok {
records := v.([]interface{})
weight := records[0].(map[string]interface{})

apiObject.Weight = aws.Int64(int64(weight[names.AttrWeight].(int)))
if v, ok := d.GetOk("weighted_routing_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject.Weight = aws.Int64(int64(tfMap[names.AttrWeight].(int)))
}

return apiObject
Expand Down

0 comments on commit fd8e814

Please sign in to comment.