2016-05-09 23:48:16 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsRoute53RecordMigrateState(
|
|
|
|
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
switch v {
|
|
|
|
case 0:
|
2016-08-02 01:54:17 +02:00
|
|
|
log.Println("[INFO] Found AWS Route53 Record State v0; migrating to v1 then v2")
|
|
|
|
v1InstanceState, err := migrateRoute53RecordStateV0toV1(is)
|
|
|
|
if err != nil {
|
|
|
|
return v1InstanceState, err
|
|
|
|
}
|
|
|
|
return migrateRoute53RecordStateV1toV2(v1InstanceState)
|
2015-08-12 18:03:47 +02:00
|
|
|
case 1:
|
2016-05-31 22:24:46 +02:00
|
|
|
log.Println("[INFO] Found AWS Route53 Record State v1; migrating to v2")
|
2015-08-12 18:03:47 +02:00
|
|
|
return migrateRoute53RecordStateV1toV2(is)
|
2016-05-09 23:48:16 +02:00
|
|
|
default:
|
|
|
|
return is, fmt.Errorf("Unexpected schema version: %d", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateRoute53RecordStateV0toV1(is *terraform.InstanceState) (*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)
|
|
|
|
newName := strings.TrimSuffix(is.Attributes["name"], ".")
|
|
|
|
is.Attributes["name"] = newName
|
|
|
|
log.Printf("[DEBUG] Attributes after migration: %#v, new name: %s", is.Attributes, newName)
|
|
|
|
return is, nil
|
|
|
|
}
|
2015-08-12 18:03:47 +02:00
|
|
|
|
|
|
|
func migrateRoute53RecordStateV1toV2(is *terraform.InstanceState) (*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)
|
|
|
|
if is.Attributes["weight"] != "" && is.Attributes["weight"] != "-1" {
|
|
|
|
is.Attributes["weighted_routing_policy.#"] = "1"
|
|
|
|
key := fmt.Sprintf("weighted_routing_policy.0.weight")
|
|
|
|
is.Attributes[key] = is.Attributes["weight"]
|
|
|
|
}
|
|
|
|
if is.Attributes["failover"] != "" {
|
|
|
|
is.Attributes["failover_routing_policy.#"] = "1"
|
|
|
|
key := fmt.Sprintf("failover_routing_policy.0.type")
|
|
|
|
is.Attributes[key] = is.Attributes["failover"]
|
|
|
|
}
|
|
|
|
delete(is.Attributes, "weight")
|
|
|
|
delete(is.Attributes, "failover")
|
|
|
|
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
|
|
|
|
return is, nil
|
|
|
|
}
|