/
resource_aws_account.go
418 lines (359 loc) · 12 KB
/
resource_aws_account.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package mcaf
import (
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAWSAccount() *schema.Resource {
return &schema.Resource{
Create: checkProvider("aws", resourceAWSAccountCreate),
Read: checkProvider("aws", resourceAWSAccountRead),
Update: checkProvider("aws", resourceAWSAccountUpdate),
Delete: checkProvider("aws", resourceAWSAccountDelete),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"email": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sso": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"firstname": {
Type: schema.TypeString,
Required: true,
},
"lastname": {
Type: schema.TypeString,
Required: true,
},
"email": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"organizational_unit": {
Type: schema.TypeString,
Optional: true,
Deprecated: "This field is deprecated and will be removed in a future version. Please use organizational_unit_path instead.",
ConflictsWith: []string{"organizational_unit_path"},
},
"organizational_unit_path": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"organizational_unit"},
},
"provisioned_product_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"account_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
var accountMutex sync.Mutex
func resourceAWSAccountCreate(d *schema.ResourceData, meta interface{}) error {
orgsconn := meta.(*Client).AWSClient.orgsconn
scconn := meta.(*Client).AWSClient.scconn
log.Printf("[DEBUG] Search the Account Factory product")
products, err := scconn.SearchProducts(&servicecatalog.SearchProductsInput{
Filters: map[string][]*string{"FullTextSearch": {aws.String("AWS Control Tower Account Factory")}},
})
if err != nil {
return fmt.Errorf("Error searching service catalog: %v", err)
}
if len(products.ProductViewSummaries) != 1 {
return fmt.Errorf("No Control Tower Account Factory found in your account. Please check your User and/or Role permissions and your Region settings.")
}
log.Printf("[DEBUG List all product artifacts to find the active artifact")
artifacts, err := scconn.ListProvisioningArtifacts(&servicecatalog.ListProvisioningArtifactsInput{
ProductId: products.ProductViewSummaries[0].ProductId,
})
if err != nil {
return fmt.Errorf("Error listing provisioning artifacts: %v", err)
}
// Try to find the active (which should be the latest) artifact.
artifactID := ""
for _, artifact := range artifacts.ProvisioningArtifactDetails {
if *artifact.Active {
artifactID = *artifact.Id
break
}
}
if artifactID == "" {
return fmt.Errorf("Could not find the provisioning artifact ID")
}
// Get organisation Root OU name and ID
roots, err := listRoots(orgsconn)
if err != nil {
return err
}
// Support both organizational_unit and organizational_unit_path until deprecated organizational_unit field is removed
ou, ouOk := d.GetOk("organizational_unit")
ouPath, ouPathOk := d.GetOk("organizational_unit_path")
if !ouOk && !ouPathOk {
return errors.New("one of organizational_unit or organizational_unit_path must be configured")
}
if ouOk {
ouPath = ou
}
// Get child OU name and ID from provided path
managedOu, err := returnChildOu(orgsconn, ouPath.(string), aws.StringValue(roots[0].Id), aws.StringValue(roots[0].Name))
if err != nil {
return err
}
// Get the name, ou and SSO details from the config.
name := d.Get("name").(string)
ppn := d.Get("provisioned_product_name").(string)
sso := d.Get("sso").([]interface{})[0].(map[string]interface{})
// If no provisioned product name was configured, use the name.
if ppn == "" {
ppn = name
}
// Create a new parameters struct.
params := &servicecatalog.ProvisionProductInput{
ProductId: products.ProductViewSummaries[0].ProductId,
ProvisionedProductName: aws.String(ppn),
ProvisioningArtifactId: aws.String(artifactID),
ProvisioningParameters: []*servicecatalog.ProvisioningParameter{
{
Key: aws.String("AccountName"),
Value: aws.String(name),
},
{
Key: aws.String("AccountEmail"),
Value: aws.String(d.Get("email").(string)),
},
{
Key: aws.String("SSOUserFirstName"),
Value: aws.String(sso["firstname"].(string)),
},
{
Key: aws.String("SSOUserLastName"),
Value: aws.String(sso["lastname"].(string)),
},
{
Key: aws.String("SSOUserEmail"),
Value: aws.String(sso["email"].(string)),
},
{
Key: aws.String("ManagedOrganizationalUnit"),
Value: aws.String(fmt.Sprintf("%s (%s)", aws.StringValue(managedOu.Name), aws.StringValue(managedOu.Id))),
},
},
}
log.Printf("[DEBUG] Provision product parameters: %+v\n", params)
accountMutex.Lock()
defer accountMutex.Unlock()
log.Printf("[DEBUG] Provision account %s in organizational unit %s (%s)", name, aws.StringValue(managedOu.Name), aws.StringValue(managedOu.Id))
account, err := scconn.ProvisionProduct(params)
if err != nil {
return fmt.Errorf("Error provisioning account %s: %v", name, err)
}
// Set the ID so we can cleanup the provisioned account in case of a failure.
d.SetId(*account.RecordDetail.ProvisionedProductId)
// Wait for the provisioning to finish.
err = waitForProvisioning(name, account.RecordDetail.RecordId, meta)
if err != nil {
return err
}
return resourceAWSAccountRead(d, meta)
}
func resourceAWSAccountRead(d *schema.ResourceData, meta interface{}) error {
scconn := meta.(*Client).AWSClient.scconn
// Get the name from the config.
name := d.Get("name").(string)
log.Printf("[DEBUG] Read configuration of provisioned account %s: %s", name, d.Id())
account, err := scconn.DescribeProvisionedProduct(&servicecatalog.DescribeProvisionedProductInput{
Id: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("Error reading configuration of provisioned account %s: %v", name, err)
}
record := &servicecatalog.DescribeRecordInput{
Id: account.ProvisionedProductDetail.LastRecordId,
}
status, err := scconn.DescribeRecord(record)
if err != nil {
return fmt.Errorf("Error reading configuration of provisioned account %s: %v", name, err)
}
// Update the config.
d.Set("provisioned_product_name", *account.ProvisionedProductDetail.Name)
for _, output := range status.RecordOutputs {
switch *output.OutputKey {
case "AccountName":
d.Set("name", *output.OutputValue)
case "AccountEmail":
d.Set("email", *output.OutputValue)
case "AccountId":
d.Set("account_id", *output.OutputValue)
}
}
return nil
}
func resourceAWSAccountUpdate(d *schema.ResourceData, meta interface{}) error {
orgsconn := meta.(*Client).AWSClient.orgsconn
scconn := meta.(*Client).AWSClient.scconn
// Get organisation Root OU name and ID
roots, err := listRoots(orgsconn)
if err != nil {
return err
}
// Support both organizational_unit and organizational_unit_path until deprecated organizational_unit field is removed
ou, ouOk := d.GetOk("organizational_unit")
ouPath, ouPathOk := d.GetOk("organizational_unit_path")
if !ouOk && !ouPathOk {
return errors.New("one of organizational_unit or organizational_unit_path must be configured")
}
if ouOk {
ouPath = ou
}
// Get child OU name and ID from provided path
managedOu, err := returnChildOu(orgsconn, ouPath.(string), aws.StringValue(roots[0].Id), aws.StringValue(roots[0].Name))
if err != nil {
return err
}
// Get the name, ou and SSO details from the config.
name := d.Get("name").(string)
sso := d.Get("sso").([]interface{})[0].(map[string]interface{})
// Create a new parameters struct.
params := &servicecatalog.UpdateProvisionedProductInput{
ProvisionedProductId: aws.String(d.Id()),
ProvisioningParameters: []*servicecatalog.UpdateProvisioningParameter{
{
Key: aws.String("AccountName"),
Value: aws.String(name),
},
{
Key: aws.String("AccountEmail"),
Value: aws.String(d.Get("email").(string)),
},
{
Key: aws.String("SSOUserFirstName"),
Value: aws.String(sso["firstname"].(string)),
},
{
Key: aws.String("SSOUserLastName"),
Value: aws.String(sso["lastname"].(string)),
},
{
Key: aws.String("SSOUserEmail"),
Value: aws.String(sso["email"].(string)),
},
{
Key: aws.String("ManagedOrganizationalUnit"),
Value: aws.String(fmt.Sprintf("%s (%s)", aws.StringValue(managedOu.Name), aws.StringValue(managedOu.Id))),
},
},
}
accountMutex.Lock()
defer accountMutex.Unlock()
log.Printf("[DEBUG] Update provisioned account %s: %s", name, d.Id())
account, err := scconn.UpdateProvisionedProduct(params)
if err != nil {
return fmt.Errorf("Error updating provisioned account %s: %v", name, err)
}
// Wait for the provisioning to finish.
err = waitForProvisioning(name, account.RecordDetail.RecordId, meta)
if err != nil {
return err
}
return resourceAWSAccountRead(d, meta)
}
func resourceAWSAccountDelete(d *schema.ResourceData, meta interface{}) error {
scconn := meta.(*Client).AWSClient.scconn
// Get the name from the config.
name := d.Get("name").(string)
accountMutex.Lock()
defer accountMutex.Unlock()
log.Printf("[DEBUG] Delete provisioned account %s: %s", name, d.Id())
account, err := scconn.TerminateProvisionedProduct(&servicecatalog.TerminateProvisionedProductInput{
ProvisionedProductId: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("Error deleting provisioned account %s: %v", name, err)
}
// Wait for the provisioning to finish.
return waitForProvisioning(name, account.RecordDetail.RecordId, meta)
}
// returnChildOu returns the ID of the child OU with the given path.
func returnChildOu(conn *organizations.Organizations, path, ouID, ouName string) (*organizations.OrganizationalUnit, error) {
ou := &organizations.OrganizationalUnit{}
for _, v := range strings.Split(path, "/") {
if strings.EqualFold(v, "Root") {
continue
}
input := &organizations.ListOrganizationalUnitsForParentInput{
ParentId: aws.String(ouID),
}
var childOuID, childOuName string
log.Printf("[DEBUG] Listing OUs under parent: %s (%s)", ouName, ouID)
err := conn.ListOrganizationalUnitsForParentPages(input, func(page *organizations.ListOrganizationalUnitsForParentOutput, lastPage bool) bool {
for _, childOu := range page.OrganizationalUnits {
if *childOu.Name == v {
childOuID = *childOu.Id
childOuName = *childOu.Name
ou = childOu
return false
}
}
return true
})
if err != nil {
return nil, fmt.Errorf("error listing organizational units for parent %s (%s): %v", ouName, ouID, err)
}
if childOuID == "" {
return nil, fmt.Errorf("organizational unit %s not found in parent %s (%s)", ou, ouName, ouID)
}
ouID = childOuID
ouName = childOuName
}
return ou, nil
}
// waitForProvisioning waits until the provisioning finished.
func waitForProvisioning(name string, recordID *string, meta interface{}) error {
scconn := meta.(*Client).AWSClient.scconn
record := &servicecatalog.DescribeRecordInput{
Id: recordID,
}
for {
// Get the provisioning status.
status, err := scconn.DescribeRecord(record)
if err != nil {
return fmt.Errorf("Error reading provisioning status of account %s: %v", name, err)
}
// If the provisioning succeeded we are done.
if *status.RecordDetail.Status == servicecatalog.RecordStatusSucceeded {
break
}
// If the provisioning failed we try to cleanup the tainted account.
if *status.RecordDetail.Status == servicecatalog.RecordStatusFailed {
return fmt.Errorf("Provisioning account %s failed: %s", name, *status.RecordDetail.RecordErrors[0].Description)
}
// Wait 5 seconds before checking the status again.
time.Sleep(5 * time.Second)
}
return nil
}