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

Use zone_id if available and fallback on domain for record state migration #566

Merged
merged 2 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions cloudflare/resource_cloudflare_record_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ func migrateCloudflareRecordStateV0toV1(is *terraform.InstanceState, meta interf
client := meta.(*cloudflare.API)

// look up new id based on attributes
domain := is.Attributes["domain"]
zoneId, err := client.ZoneIDByName(domain)
if err != nil {
return is, fmt.Errorf("Error finding zone %q: %s", domain, err)
zoneId := is.Attributes["zone_id"]
if zoneId == "" {
domain := is.Attributes["domain"]
var err error
zoneId, err = client.ZoneIDByName(domain)
if err != nil {
return is, fmt.Errorf("Error finding zone %q: %s", domain, err)
}
}

// all other information is ignored in the DNSRecords call
Expand Down
53 changes: 49 additions & 4 deletions cloudflare/resource_cloudflare_record_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func TestCloudflareRecordMigrateState(t *testing.T) {
t.Fatalf("Error building Cloudflare API: %s", err)
}

// When several records are returned for a single DNS query, they are
// matched on ttl, proxied and priority: as the same response is returned
// for all testcases, select specific values for each testcase
cases := map[string]struct {
StateVersion int
ID string
Expand Down Expand Up @@ -128,6 +131,30 @@ func TestCloudflareRecordMigrateState(t *testing.T) {
},
Expected: "222ffe3f93a31231ad6b0c6d09185jjj",
},
"ttl_122_state_v0_with_v1_fields": {
StateVersion: 0,
ID: "12345678901234567890123456789012",
Attributes: map[string]string{
"created_on": "2018-03-07T11:52:02.454564Z",
"data.%": "0",
"hostname": "hashicorptest.com",
"id": "12345678901234567890123456789012",
"metadata.%": "3",
"metadata.auto_added": "false",
"metadata.managed_by_apps": "false",
"metadata.managed_by_argo_tunnel": "false",
"modified_on": "2018-03-07T11:52:02.454564Z",
"name": "hashicorptest.com",
"priority": "0",
"proxiable": "true",
"proxied": "true",
"ttl": "122",
"type": "A",
"value": "1.2.3.4",
"zone_id": "1234567890",
},
Expected: "12345678901234567890123456789012",
},
}

for tn, tc := range cases {
Expand All @@ -147,7 +174,7 @@ func TestCloudflareRecordMigrateState(t *testing.T) {
}

if is.ID != tc.Expected {
t.Fatalf("bad sg rule id: %s\n\n expected: %s", is.ID, tc.Expected)
t.Fatalf("bad record id: %s\n\n expected: %s", is.ID, tc.Expected)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great pickup! thanks!

}
}
}
Expand Down Expand Up @@ -320,14 +347,32 @@ const dnsResponse = `
"locked": false,
"zone_id": "1234567890",
"zone_name": "hashicorptest.com"
}
},
{
"id": "12345678901234567890123456789012",
"name": "hashicorptest.com",
"content": "1.2.3.4",
"proxiable": true,
"proxied": true,
"ttl": 122,
"locked": false,
"zone_id": "1234567890",
"zone_name": "hashicorptest.com",
"modified_on": "2018-03-07T11:52:02.454564Z",
"created_on": "2018-03-07T11:52:02.454564Z",
"meta": {
"auto_added": false,
"managed_by_apps": false,
"managed_by_argo_tunnel": false
}
}
],
"result_info": {
"page": 1,
"per_page": 20,
"total_pages": 1,
"count": 7,
"total_count": 7
"count": 8,
"total_count": 8
},
"success": true,
"errors": [],
Expand Down