-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/cloudflare: Add migration for v1 to v4 API libraries
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
builtin/providers/cloudflare/resource_cloudflare_record_migrate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
// NOTE: Temporary until they merge my PR: | ||
"github.com/mitchellh/cloudflare-go" | ||
) | ||
|
||
func resourceAwsCloudFlareRecordMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found AWS CloudFlare Record State v0; migrating to v1") | ||
return migrateCloudFlareRecordStateV0toV1(is, meta) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateCloudFlareRecordStateV0toV1(is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
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) | ||
} | ||
|
||
// all other information is ignored in the DNSRecords call | ||
searchRecord := cloudflare.DNSRecord{ | ||
Type: is.Attributes["type"], | ||
Name: is.Attributes["hostname"], | ||
Content: is.Attributes["value"], | ||
} | ||
|
||
records, err := client.DNSRecords(zoneId, searchRecord) | ||
if err != nil { | ||
return is, err | ||
} | ||
|
||
for _, r := range records { | ||
if is.Attributes["ttl"] != "" { | ||
v, err := strconv.Atoi(is.Attributes["ttl"]) | ||
if err != nil { | ||
return is, fmt.Errorf("Error converting ttl to int in CloudFlare Record Migration") | ||
} | ||
|
||
if v != r.TTL { | ||
continue | ||
} | ||
} | ||
|
||
if is.Attributes["proxied"] != "" { | ||
b, err := strconv.ParseBool(is.Attributes["proxied"]) | ||
if err != nil { | ||
return is, fmt.Errorf("Error converting proxied to bool in CloudFlare Record Migration") | ||
} | ||
|
||
if b != r.Proxied { | ||
continue | ||
} | ||
} | ||
|
||
if is.Attributes["priority"] != "" { | ||
v, err := strconv.Atoi(is.Attributes["priority"]) | ||
if err != nil { | ||
return is, fmt.Errorf("Error converting priority to int in CloudFlare Record Migration") | ||
} | ||
|
||
if v != r.Priority { | ||
continue | ||
} | ||
} | ||
|
||
// assume record found | ||
is.Attributes["id"] = r.ID | ||
is.ID = r.ID | ||
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) | ||
return is, nil | ||
} | ||
|
||
// assume no record found | ||
log.Printf("[DEBUG] Attributes after no migration: %#v", is.Attributes) | ||
return is, fmt.Errorf("No matching Record found") | ||
} |