Skip to content

Commit

Permalink
Fix(domain): issue on data attribute (scaleway#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
Monitob authored Feb 14, 2022
1 parent 00da88c commit 28515d5
Show file tree
Hide file tree
Showing 4 changed files with 1,183 additions and 601 deletions.
20 changes: 17 additions & 3 deletions scaleway/resource_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func resourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceDat
var dnsZone string
var projectID string

currentData := d.Get("data")
// check if this is an inline import. Like: "terraform import scaleway_domain_record.www subdomain.domain.tld/11111111-1111-1111-1111-111111111111"
if strings.Contains(d.Id(), "/") {
tab := strings.SplitN(d.Id(), "/", -1)
Expand Down Expand Up @@ -294,10 +295,18 @@ func resourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceDat
} else {
dnsZone = d.Get("dns_zone").(string)

recordTypeRaw, recordtTypeExist := d.GetOk("type")
if !recordtTypeExist {
return diag.FromErr(fmt.Errorf("record type not found"))
}
recordType := domain.RecordType(recordTypeRaw.(string))
if recordType == domain.RecordTypeUnknown {
return diag.FromErr(fmt.Errorf("record type unknow"))
}
res, err := domainAPI.ListDNSZoneRecords(&domain.ListDNSZoneRecordsRequest{
DNSZone: dnsZone,
Name: d.Get("name").(string),
Type: domain.RecordType(d.Get("type").(string)),
Type: recordType,
}, scw.WithAllPages())

if err != nil {
Expand All @@ -309,7 +318,8 @@ func resourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceDat
}

for _, r := range res.Records {
if flattenDomainData(r.Data, r.Type).(string) == d.Get("data").(string) {
flattedData := flattenDomainData(r.Data, r.Type).(string)
if strings.ToLower(currentData.(string)) == flattedData {
record = r
break
}
Expand Down Expand Up @@ -337,11 +347,15 @@ func resourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceDat
projectID = z.ProjectID
}

// retrieve data from record
if len(currentData.(string)) == 0 {
currentData = flattenDomainData(record.Data, record.Type).(string)
}
d.SetId(record.ID)
_ = d.Set("dns_zone", dnsZone)
_ = d.Set("name", record.Name)
_ = d.Set("type", record.Type.String())
_ = d.Set("data", flattenDomainData(record.Data, record.Type))
_ = d.Set("data", currentData.(string))
_ = d.Set("ttl", int(record.TTL))
_ = d.Set("priority", int(record.Priority))
_ = d.Set("geo_ip", flattenDomainGeoIP(record.GeoIPConfig))
Expand Down
31 changes: 31 additions & 0 deletions scaleway/resource_domain_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,37 @@ func TestAccScalewayDomainRecord_Basic(t *testing.T) {
testCheckResourceAttrUUID("scaleway_domain_record.tf_A", "id"),
),
},
{
Config: fmt.Sprintf(`
resource "scaleway_domain_record" "tf_A" {
dns_zone = %[1]q
name = "%s"
type = "%s"
data = "%s"
ttl = %d
priority = %d
}
resource "scaleway_domain_record" "tf_MX" {
dns_zone = %[1]q
name = "record_mx"
type = "MX"
data = "ASPMX.L.GOOGLE.COM."
ttl = 600
priority = 1
}
`, testDNSZone, name, recordType, dataUpdated, ttlUpdated, priorityUpdated),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayDomainRecordExists(tt, "scaleway_domain_record.tf_MX"),
resource.TestCheckResourceAttr("scaleway_domain_record.tf_MX", "dns_zone", testDNSZone),
resource.TestCheckResourceAttr("scaleway_domain_record.tf_MX", "name", "record_mx"),
resource.TestCheckResourceAttr("scaleway_domain_record.tf_MX", "type", "MX"),
resource.TestCheckResourceAttr("scaleway_domain_record.tf_MX", "data", "ASPMX.L.GOOGLE.COM."),
resource.TestCheckResourceAttr("scaleway_domain_record.tf_MX", "ttl", "600"),
resource.TestCheckResourceAttr("scaleway_domain_record.tf_MX", "priority", "1"),
testCheckResourceAttrUUID("scaleway_domain_record.tf_MX", "id"),
),
},
},
})
}
Expand Down
Loading

0 comments on commit 28515d5

Please sign in to comment.