-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_management_group.go
379 lines (310 loc) · 11.2 KB
/
resource_arm_management_group.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package azurerm
import (
"fmt"
"log"
"strings"
"github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2018-03-01-preview/managementgroups"
"github.com/google/uuid"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
var managementGroupCacheControl = "no-cache"
func resourceArmManagementGroup() *schema.Resource {
return &schema.Resource{
Create: resourceArmManagementGroupCreateUpdate,
Update: resourceArmManagementGroupCreateUpdate,
Read: resourceArmManagementGroupRead,
Delete: resourceArmManagementGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"parent_management_group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"subscription_ids": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func resourceArmManagementGroupCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).managementGroups.GroupsClient
subscriptionsClient := meta.(*ArmClient).managementGroups.SubscriptionClient
ctx := meta.(*ArmClient).StopContext
armTenantID := meta.(*ArmClient).tenantId
groupId := d.Get("group_id").(string)
if groupId == "" {
groupId = uuid.New().String()
}
parentManagementGroupId := d.Get("parent_management_group_id").(string)
if parentManagementGroupId == "" {
parentManagementGroupId = fmt.Sprintf("/providers/Microsoft.Management/managementGroups/%s", armTenantID)
}
recurse := false
if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, groupId, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Management Group %q: %s", groupId, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_management_group", *existing.ID)
}
}
log.Printf("[INFO] Creating Management Group %q", groupId)
properties := managementgroups.CreateManagementGroupRequest{
Name: utils.String(groupId),
CreateManagementGroupProperties: &managementgroups.CreateManagementGroupProperties{
TenantID: utils.String(armTenantID),
Details: &managementgroups.CreateManagementGroupDetails{
Parent: &managementgroups.CreateParentGroupInfo{
ID: utils.String(parentManagementGroupId),
},
},
},
}
if v, ok := d.GetOk("display_name"); ok {
properties.CreateManagementGroupProperties.DisplayName = utils.String(v.(string))
}
future, err := client.CreateOrUpdate(ctx, groupId, properties, managementGroupCacheControl)
if err != nil {
return fmt.Errorf("Error creating Management Group %q: %+v", groupId, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for creation of Management Group %q: %+v", groupId, err)
}
resp, err := client.Get(ctx, groupId, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
return fmt.Errorf("Error retrieving Management Group %q: %+v", groupId, err)
}
d.SetId(*resp.ID)
subscriptionIds := expandManagementGroupSubscriptionIds(d.Get("subscription_ids").(*schema.Set))
// first remove any which need to be removed
if !d.IsNewResource() {
log.Printf("[DEBUG] Determine which Subscriptions should be removed from Management Group %q", groupId)
if props := resp.Properties; props != nil {
subscriptionIdsToRemove, err2 := determineManagementGroupSubscriptionsIdsToRemove(props.Children, subscriptionIds)
if err2 != nil {
return fmt.Errorf("Error determining which subscriptions should be removed from Management Group %q: %+v", groupId, err2)
}
for _, subscriptionId := range *subscriptionIdsToRemove {
log.Printf("[DEBUG] De-associating Subscription ID %q from Management Group %q", subscriptionId, groupId)
deleteResp, err2 := subscriptionsClient.Delete(ctx, groupId, subscriptionId, managementGroupCacheControl)
if err2 != nil {
if !response.WasNotFound(deleteResp.Response) {
return fmt.Errorf("Error de-associating Subscription %q from Management Group %q: %+v", subscriptionId, groupId, err2)
}
}
}
}
}
// then add the new ones
log.Printf("[DEBUG] Preparing to assign Subscriptions to Management Group %q", groupId)
for _, subscriptionId := range subscriptionIds {
log.Printf("[DEBUG] Assigning Subscription ID %q to management group %q", subscriptionId, groupId)
_, err = subscriptionsClient.Create(ctx, groupId, subscriptionId, managementGroupCacheControl)
if err != nil {
return fmt.Errorf("[DEBUG] Error assigning Subscription ID %q to Management Group %q", subscriptionId, groupId)
}
}
return resourceArmManagementGroupRead(d, meta)
}
func resourceArmManagementGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).managementGroups.GroupsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseManagementGroupId(d.Id())
if err != nil {
return err
}
recurse := true
resp, err := client.Get(ctx, id.groupId, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Management Group %q doesn't exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Management Group %q: %+v", d.Id(), err)
}
d.Set("group_id", id.groupId)
if props := resp.Properties; props != nil {
d.Set("display_name", props.DisplayName)
subscriptionIds, err := flattenArmManagementGroupSubscriptionIds(props.Children)
if err != nil {
return fmt.Errorf("Error flattening `subscription_ids`: %+v", err)
}
d.Set("subscription_ids", subscriptionIds)
parentId := ""
if details := props.Details; details != nil {
if parent := details.Parent; parent != nil {
if pid := parent.ID; pid != nil {
parentId = *pid
}
}
}
d.Set("parent_management_group_id", parentId)
}
return nil
}
func resourceArmManagementGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).managementGroups.GroupsClient
subscriptionsClient := meta.(*ArmClient).managementGroups.SubscriptionClient
ctx := meta.(*ArmClient).StopContext
id, err := parseManagementGroupId(d.Id())
if err != nil {
return err
}
recurse := true
group, err := client.Get(ctx, id.groupId, "children", &recurse, "", managementGroupCacheControl)
if err != nil {
if utils.ResponseWasNotFound(group.Response) {
log.Printf("[DEBUG] Management Group %q doesn't exist in Azure - nothing to do!", id.groupId)
return nil
}
return fmt.Errorf("Error retrieving Management Group %q: %+v", id.groupId, err)
}
// before deleting a management group, return any subscriptions to the root management group
if props := group.Properties; props != nil {
if children := props.Children; children != nil {
for _, v := range *children {
if v.ID == nil {
continue
}
subscriptionId := *v.ID
log.Printf("[DEBUG] De-associating Subscription %q from Management Group %q..", subscriptionId, id.groupId)
// NOTE: whilst this says `Delete` it's actually `Deassociate` - which is /really/ helpful
deleteResp, err2 := subscriptionsClient.Delete(ctx, id.groupId, subscriptionId, managementGroupCacheControl)
if err2 != nil {
if !response.WasNotFound(deleteResp.Response) {
return fmt.Errorf("Error de-associating Subscription %q from Management Group %q: %+v", subscriptionId, id.groupId, err2)
}
}
}
}
}
resp, err := client.Delete(ctx, id.groupId, managementGroupCacheControl)
if err != nil {
return fmt.Errorf("Error deleting Management Group %q: %+v", id.groupId, err)
}
err = resp.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for the deletion of Management Group %q: %+v", id.groupId, err)
}
return nil
}
func expandManagementGroupSubscriptionIds(input *schema.Set) []string {
output := make([]string, 0)
if input != nil {
for _, v := range input.List() {
output = append(output, v.(string))
}
}
return output
}
func flattenArmManagementGroupSubscriptionIds(input *[]managementgroups.ChildInfo) (*schema.Set, error) {
subscriptionIds := &schema.Set{F: schema.HashString}
if input == nil {
return subscriptionIds, nil
}
for _, child := range *input {
if child.ID == nil {
continue
}
id, err := parseManagementGroupSubscriptionID(*child.ID)
if err != nil {
return nil, fmt.Errorf("Unable to parse child Subscription ID %+v", err)
}
if id != nil {
subscriptionIds.Add(id.subscriptionId)
}
}
return subscriptionIds, nil
}
type managementGroupId struct {
groupId string
}
type subscriptionId struct {
subscriptionId string
}
func parseManagementGroupId(input string) (*managementGroupId, error) {
// /providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000
segments := strings.Split(input, "/")
if len(segments) != 5 {
return nil, fmt.Errorf("Expected there to be 5 segments but got %d", len(segments))
}
id := managementGroupId{
groupId: segments[4],
}
return &id, nil
}
func parseManagementGroupSubscriptionID(input string) (*subscriptionId, error) {
// this is either:
// /subscriptions/00000000-0000-0000-0000-000000000000
// /providers/Microsoft.Management/managementGroups/e4115b99-6be7-4153-a73f-5ff5e778ce28
// we skip out the managementGroup ID's
if strings.HasPrefix(input, "/providers/Microsoft.Management/managementGroups/") {
return nil, nil
}
components := strings.Split(input, "/")
if len(components) == 0 {
return nil, fmt.Errorf("Subscription Id is empty or not formatted correctly: %s", input)
}
if len(components) != 3 {
return nil, fmt.Errorf("Subscription Id should have 2 segments, got %d: %q", len(components)-1, input)
}
id := subscriptionId{
subscriptionId: components[2],
}
return &id, nil
}
func determineManagementGroupSubscriptionsIdsToRemove(existing *[]managementgroups.ChildInfo, updated []string) (*[]string, error) {
subscriptionIdsToRemove := make([]string, 0)
if existing == nil {
return &subscriptionIdsToRemove, nil
}
for _, v := range *existing {
if v.ID == nil {
continue
}
id, err := parseManagementGroupSubscriptionID(*v.ID)
if err != nil {
return nil, fmt.Errorf("Error parsing Subscription ID %q: %+v", *v.ID, err)
}
// not a Subscription - so let's skip it
if id == nil {
continue
}
found := false
for _, subId := range updated {
if id.subscriptionId == subId {
found = true
break
}
}
if !found {
subscriptionIdsToRemove = append(subscriptionIdsToRemove, id.subscriptionId)
}
}
return &subscriptionIdsToRemove, nil
}