forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_definition_migration.go
110 lines (95 loc) · 2.33 KB
/
role_definition_migration.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
109
110
package migration
import (
"context"
"fmt"
"log"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
)
var _ pluginsdk.StateUpgrade = RoleDefinitionV0ToV1{}
type RoleDefinitionV0ToV1 struct{}
func (RoleDefinitionV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"role_definition_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: pluginsdk.TypeString,
Required: true,
},
"scope": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: pluginsdk.TypeString,
Optional: true,
},
//lintignore:XS003
"permissions": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"actions": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"not_actions": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"data_actions": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
Set: pluginsdk.HashString,
},
"not_data_actions": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
Set: pluginsdk.HashString,
},
},
},
},
"assignable_scopes": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}
func (RoleDefinitionV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
log.Println("[DEBUG] Migrating ID from v0 to v1 format")
oldID := rawState["id"].(string)
if oldID == "" {
return nil, fmt.Errorf("failed to migrate state: old ID empty")
}
scope := rawState["scope"].(string)
if scope == "" {
return nil, fmt.Errorf("failed to migrate state: scope missing")
}
newID := fmt.Sprintf("%s|%s", oldID, scope)
rawState["id"] = newID
return rawState, nil
}
}