forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_container_registry_migrate.go
108 lines (84 loc) · 2.96 KB
/
resource_arm_container_registry_migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package azurerm
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
func resourceAzureRMContainerRegistryMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found AzureRM Container Registry State v0; migrating to v1")
return migrateAzureRMContainerRegistryStateV0toV1(is)
case 1:
log.Println("[INFO] Found AzureRM Container Registry State v1; migrating to v2")
return migrateAzureRMContainerRegistryStateV1toV2(is, meta)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}
func migrateAzureRMContainerRegistryStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}
log.Printf("[DEBUG] ARM Container Registry Attributes before Migration: %#v", is.Attributes)
is.Attributes["sku"] = "Basic"
log.Printf("[DEBUG] ARM Container Registry Attributes after State Migration: %#v", is.Attributes)
return is, nil
}
func migrateAzureRMContainerRegistryStateV1toV2(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] ARM Container Registry Attributes before Migration: %#v", is.Attributes)
// Basic's been renamed Classic to allow for "ManagedBasic" ¯\_(ツ)_/¯
is.Attributes["sku"] = "Classic"
updateV1ToV2StorageAccountName(is, meta)
// we have to look this up, since we don't have the resource group name
log.Printf("[DEBUG] ARM Container Registry Attributes after State Migration: %#v", is.Attributes)
return is, nil
}
func updateV1ToV2StorageAccountName(is *terraform.InstanceState, meta interface{}) error {
reader := &schema.MapFieldReader{
Schema: resourceArmContainerRegistry().Schema,
Map: schema.BasicMapReader(is.Attributes),
}
result, err := reader.ReadField([]string{"storage_account"})
if err != nil {
return err
}
if result.Value == nil {
return nil
}
inputAccounts := result.Value.([]interface{})
inputAccount := inputAccounts[0]
if inputAccount == nil {
return nil
}
account := inputAccount.(map[string]interface{})
name := account["name"].(string)
storageAccountId, err := findAzureStorageAccountIdFromName(name, meta)
if err != nil {
return err
}
is.Attributes["storage_account_id"] = storageAccountId
return nil
}
func findAzureStorageAccountIdFromName(name string, meta interface{}) (string, error) {
ctx := meta.(*ArmClient).StopContext
client := meta.(*ArmClient).storageServiceClient
accounts, err := client.List(ctx)
if err != nil {
return "", err
}
for _, account := range *accounts.Value {
if strings.ToLower(*account.Name) == strings.ToLower(name) {
return *account.ID, nil
}
}
return "", fmt.Errorf("Unable to find ID for Storage Account %q", name)
}