forked from hashicorp/terraform-provider-aws
/
resource_aws_organizations_policy_attachment.go
154 lines (127 loc) · 4.03 KB
/
resource_aws_organizations_policy_attachment.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
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsOrganizationsPolicyAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsOrganizationsPolicyAttachmentCreate,
Read: resourceAwsOrganizationsPolicyAttachmentRead,
Delete: resourceAwsOrganizationsPolicyAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"target_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsOrganizationsPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).organizationsconn
policyID := d.Get("policy_id").(string)
targetID := d.Get("target_id").(string)
input := &organizations.AttachPolicyInput{
PolicyId: aws.String(policyID),
TargetId: aws.String(targetID),
}
log.Printf("[DEBUG] Creating Organizations Policy Attachment: %s", input)
err := resource.Retry(4*time.Minute, func() *resource.RetryError {
_, err := conn.AttachPolicy(input)
if err != nil {
if isAWSErr(err, organizations.ErrCodeFinalizingOrganizationException, "") {
log.Printf("[DEBUG] Trying to create policy attachment again: %q", err.Error())
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("error creating Organizations Policy Attachment: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s", targetID, policyID))
return resourceAwsOrganizationsPolicyAttachmentRead(d, meta)
}
func resourceAwsOrganizationsPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).organizationsconn
targetID, policyID, err := decodeAwsOrganizationsPolicyAttachmentID(d.Id())
if err != nil {
return err
}
input := &organizations.ListPoliciesForTargetInput{
Filter: aws.String(organizations.PolicyTypeServiceControlPolicy),
TargetId: aws.String(targetID),
}
log.Printf("[DEBUG] Listing Organizations Policies for Target: %s", input)
var output *organizations.PolicySummary
err = conn.ListPoliciesForTargetPages(input, func(page *organizations.ListPoliciesForTargetOutput, lastPage bool) bool {
for _, policySummary := range page.Policies {
if aws.StringValue(policySummary.Id) == policyID {
output = policySummary
return true
}
}
return !lastPage
})
if err != nil {
if isAWSErr(err, organizations.ErrCodeTargetNotFoundException, "") {
log.Printf("[WARN] Target does not exist, removing from state: %s", d.Id())
d.SetId("")
return nil
}
return err
}
if output == nil {
log.Printf("[WARN] Attachment does not exist, removing from state: %s", d.Id())
d.SetId("")
return nil
}
d.Set("policy_id", policyID)
d.Set("target_id", targetID)
return nil
}
func resourceAwsOrganizationsPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).organizationsconn
targetID, policyID, err := decodeAwsOrganizationsPolicyAttachmentID(d.Id())
if err != nil {
return err
}
input := &organizations.DetachPolicyInput{
PolicyId: aws.String(policyID),
TargetId: aws.String(targetID),
}
log.Printf("[DEBUG] Detaching Organizations Policy %q from %q", policyID, targetID)
_, err = conn.DetachPolicy(input)
if err != nil {
if isAWSErr(err, organizations.ErrCodePolicyNotFoundException, "") {
return nil
}
if isAWSErr(err, organizations.ErrCodeTargetNotFoundException, "") {
return nil
}
return err
}
return nil
}
func decodeAwsOrganizationsPolicyAttachmentID(id string) (string, string, error) {
idParts := strings.Split(id, ":")
if len(idParts) != 2 {
return "", "", fmt.Errorf("expected ID in format of TARGETID:POLICYID, received: %s", id)
}
return idParts[0], idParts[1], nil
}