/
blueprint.go
211 lines (177 loc) · 6.35 KB
/
blueprint.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package blueprints
import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/preview/blueprint/mgmt/2018-11-01-preview/blueprint"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func ManagedIdentitySchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
// ManagedServiceIdentityTypeNone is not valid; a valid and privileged Identity is required for the service to apply the changes.
// SystemAssigned type not currently supported - The Portal performs significant activity in temporary escalation of permissions to Owner on the target scope
// Such activity in the Provider would be brittle
// string(blueprint.ManagedServiceIdentityTypeSystemAssigned),
string(blueprint.ManagedServiceIdentityTypeUserAssigned),
}, true),
// The first character of value returned by the service is always in lower case - bug?
DiffSuppressFunc: suppress.CaseDifference,
},
"identity_ids": {
// The API only seems to care about the "key" portion of this struct, which is the ResourceID of the Identity
Type: schema.TypeList,
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.UserAssignedIdentityId,
},
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
}
func blueprintAssignmentCreateStateRefreshFunc(ctx context.Context, client *blueprint.AssignmentsClient, scope, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := client.Get(ctx, scope, name)
if err != nil {
return nil, "", fmt.Errorf("unable to retrieve Blueprint Assignment %q (Scope %q): %+v", name, scope, err)
}
if resp.ProvisioningState == blueprint.Failed {
return resp, string(resp.ProvisioningState), fmt.Errorf("Blueprint Assignment provisioning entered a Failed state.")
}
return resp, string(resp.ProvisioningState), nil
}
}
func blueprintAssignmentDeleteStateRefreshFunc(ctx context.Context, client *blueprint.AssignmentsClient, scope, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := client.Get(ctx, scope, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return resp, "NotFound", nil
} else {
return nil, "", fmt.Errorf("unable to retrieve Blueprint Assignment %q (Scope %q): %+v", name, scope, err)
}
}
return resp, string(resp.ProvisioningState), nil
}
}
func normalizeAssignmentParameterValuesJSON(jsonString interface{}) string {
if jsonString == nil || jsonString == "" {
return ""
}
var values map[string]*blueprint.ParameterValue
if err := json.Unmarshal([]byte(jsonString.(string)), &values); err != nil {
return fmt.Sprintf("unable to parse JSON: %+v", err)
}
b, _ := json.Marshal(values)
return string(b)
}
func normalizeAssignmentResourceGroupValuesJSON(jsonString interface{}) string {
if jsonString == nil || jsonString == "" {
return ""
}
var values map[string]*blueprint.ResourceGroupValue
if err := json.Unmarshal([]byte(jsonString.(string)), &values); err != nil {
return fmt.Sprintf("unable to parse JSON: %+v", err)
}
b, _ := json.Marshal(values)
return string(b)
}
func expandArmBlueprintAssignmentParameters(input string) map[string]*blueprint.ParameterValue {
var result map[string]*blueprint.ParameterValue
// the string has been validated by the schema, therefore the error is ignored here, since it will never happen.
_ = json.Unmarshal([]byte(input), &result)
return result
}
func expandArmBlueprintAssignmentResourceGroups(input string) map[string]*blueprint.ResourceGroupValue {
var result map[string]*blueprint.ResourceGroupValue
// the string has been validated by the schema, therefore the error is ignored here, since it will never happen.
_ = json.Unmarshal([]byte(input), &result)
return result
}
func expandArmBlueprintAssignmentIdentity(input []interface{}) (*blueprint.ManagedServiceIdentity, error) {
if len(input) == 0 || input[0] == nil {
return nil, fmt.Errorf("Managed Service Identity was empty")
}
raw := input[0].(map[string]interface{})
identity := blueprint.ManagedServiceIdentity{
Type: blueprint.ManagedServiceIdentityType(raw["type"].(string)),
}
identityIdsRaw := raw["identity_ids"].([]interface{})
identityIds := make(map[string]*blueprint.UserAssignedIdentity)
for _, v := range identityIdsRaw {
identityIds[v.(string)] = &blueprint.UserAssignedIdentity{}
}
identity.UserAssignedIdentities = identityIds
return &identity, nil
}
func flattenArmBlueprintAssignmentIdentity(input *blueprint.ManagedServiceIdentity) []interface{} {
if input == nil {
return []interface{}{}
}
identityIds := make([]string, 0)
if input.UserAssignedIdentities != nil {
for k := range input.UserAssignedIdentities {
identityIds = append(identityIds, k)
}
}
principalId := ""
if input.PrincipalID != nil {
principalId = *input.PrincipalID
}
tenantId := ""
if input.TenantID != nil {
tenantId = *input.TenantID
}
return []interface{}{
map[string]interface{}{
"type": string(input.Type),
"identity_ids": identityIds,
"principal_id": principalId,
"tenant_id": tenantId,
},
}
}
func flattenArmBlueprintAssignmentParameters(input map[string]*blueprint.ParameterValue) (string, error) {
if len(input) == 0 {
return "", nil
}
b, err := json.Marshal(input)
if err != nil {
return "", err
}
return string(b), nil
}
func flattenArmBlueprintAssignmentResourceGroups(input map[string]*blueprint.ResourceGroupValue) (string, error) {
if len(input) == 0 {
return "", nil
}
b, err := json.Marshal(input)
if err != nil {
return "", err
}
return string(b), nil
}