/
policy_attachment.go
160 lines (132 loc) · 4.14 KB
/
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
155
156
157
158
159
160
package organizations
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ATenderholt/rainbow-test/terraform/conns"
"github.com/ATenderholt/rainbow-test/terraform/tfresource"
)
func ResourcePolicyAttachment() *schema.Resource {
return &schema.Resource{
Create: resourcePolicyAttachmentCreate,
Read: resourcePolicyAttachmentRead,
Delete: resourcePolicyAttachmentDelete,
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 resourcePolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.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 tfawserr.ErrCodeEquals(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 tfresource.TimedOut(err) {
_, err = conn.AttachPolicy(input)
}
if err != nil {
return fmt.Errorf("error creating Organizations Policy Attachment: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s", targetID, policyID))
return resourcePolicyAttachmentRead(d, meta)
}
func resourcePolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).OrganizationsConn
targetID, policyID, err := DecodePolicyAttachmentID(d.Id())
if err != nil {
return err
}
input := &organizations.ListTargetsForPolicyInput{
PolicyId: aws.String(policyID),
}
log.Printf("[DEBUG] Listing Organizations Policies for Target: %s", input)
var output *organizations.PolicyTargetSummary
err = conn.ListTargetsForPolicyPages(input, func(page *organizations.ListTargetsForPolicyOutput, lastPage bool) bool {
for _, policySummary := range page.Targets {
if aws.StringValue(policySummary.TargetId) == targetID {
output = policySummary
return true
}
}
return !lastPage
})
if err != nil {
if tfawserr.ErrCodeEquals(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 resourcePolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).OrganizationsConn
targetID, policyID, err := DecodePolicyAttachmentID(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 tfawserr.ErrCodeEquals(err, organizations.ErrCodePolicyNotFoundException) {
return nil
}
if tfawserr.ErrCodeEquals(err, organizations.ErrCodeTargetNotFoundException) {
return nil
}
return err
}
return nil
}
func DecodePolicyAttachmentID(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
}