/
delegated_administrator.go
160 lines (142 loc) · 5.22 KB
/
delegated_administrator.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
package organizations
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/cloudposse/terraform-provider-awsutils/internal/conns"
"github.com/cloudposse/terraform-provider-awsutils/internal/verify"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceDelegatedAdministrator() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceDelegatedAdministratorCreate,
ReadWithoutTimeout: resourceDelegatedAdministratorRead,
DeleteWithoutTimeout: resourceDelegatedAdministratorDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: verify.ValidAccountID,
},
"service_principal": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"delegation_enabled_date": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"joined_method": {
Type: schema.TypeString,
Computed: true,
},
"joined_timestamp": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceDelegatedAdministratorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).OrganizationsConn
accountID := d.Get("account_id").(string)
servicePrincipal := d.Get("service_principal").(string)
input := &organizations.RegisterDelegatedAdministratorInput{
AccountId: aws.String(accountID),
ServicePrincipal: aws.String(servicePrincipal),
}
_, err := conn.RegisterDelegatedAdministratorWithContext(ctx, input)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating Organizations DelegatedAdministrator (%s): %w", accountID, err))
}
d.SetId(fmt.Sprintf("%s/%s", accountID, servicePrincipal))
return resourceDelegatedAdministratorRead(ctx, d, meta)
}
func resourceDelegatedAdministratorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).OrganizationsConn
accountID, servicePrincipal, err := DecodeOrganizationDelegatedAdministratorID(d.Id())
if err != nil {
return diag.FromErr(fmt.Errorf("error decoding ID AWS Organization (%s) DelegatedAdministrators: %w", d.Id(), err))
}
input := &organizations.ListDelegatedAdministratorsInput{
ServicePrincipal: aws.String(servicePrincipal),
}
var delegatedAccount *organizations.DelegatedAdministrator
err = conn.ListDelegatedAdministratorsPagesWithContext(ctx, input, func(page *organizations.ListDelegatedAdministratorsOutput, lastPage bool) bool {
for _, delegated := range page.DelegatedAdministrators {
if aws.StringValue(delegated.Id) == accountID {
delegatedAccount = delegated
}
}
return !lastPage
})
if err != nil {
return diag.FromErr(fmt.Errorf("error listing AWS Organization (%s) DelegatedAdministrators: %w", d.Id(), err))
}
if delegatedAccount == nil {
log.Printf("[WARN] AWS Organization DelegatedAdministrators not found (%s), removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("arn", delegatedAccount.Arn)
d.Set("delegation_enabled_date", aws.TimeValue(delegatedAccount.DelegationEnabledDate).Format(time.RFC3339))
d.Set("email", delegatedAccount.Email)
d.Set("joined_method", delegatedAccount.JoinedMethod)
d.Set("joined_timestamp", aws.TimeValue(delegatedAccount.JoinedTimestamp).Format(time.RFC3339))
d.Set("name", delegatedAccount.Name)
d.Set("status", delegatedAccount.Status)
d.Set("account_id", accountID)
d.Set("service_principal", servicePrincipal)
return nil
}
func resourceDelegatedAdministratorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).OrganizationsConn
accountID, servicePrincipal, err := DecodeOrganizationDelegatedAdministratorID(d.Id())
if err != nil {
return diag.FromErr(fmt.Errorf("error decoding ID AWS Organization (%s) DelegatedAdministrators: %w", d.Id(), err))
}
input := &organizations.DeregisterDelegatedAdministratorInput{
AccountId: aws.String(accountID),
ServicePrincipal: aws.String(servicePrincipal),
}
_, err = conn.DeregisterDelegatedAdministratorWithContext(ctx, input)
if err != nil {
return diag.FromErr(fmt.Errorf("error deleting Organizations DelegatedAdministrator (%s): %w", d.Id(), err))
}
return nil
}
func DecodeOrganizationDelegatedAdministratorID(id string) (string, string, error) {
idParts := strings.Split(id, "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return "", "", fmt.Errorf("expected ID in the form of account_id/service_principal, given: %q", id)
}
return idParts[0], idParts[1], nil
}