forked from hashicorp/terraform-provider-aws
/
resource_aws_iot_policy_attachment.go
137 lines (110 loc) · 3.42 KB
/
resource_aws_iot_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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsIotPolicyAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIotPolicyAttachmentCreate,
Read: resourceAwsIotPolicyAttachmentRead,
Delete: resourceAwsIotPolicyAttachmentDelete,
Schema: map[string]*schema.Schema{
"policy": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"target": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsIotPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
policyName := d.Get("policy").(string)
target := d.Get("target").(string)
_, err := conn.AttachPolicy(&iot.AttachPolicyInput{
PolicyName: aws.String(policyName),
Target: aws.String(target),
})
if err != nil {
return fmt.Errorf("error attaching policy %s to target %s: %s", policyName, target, err)
}
d.SetId(fmt.Sprintf("%s|%s", policyName, target))
return resourceAwsIotPolicyAttachmentRead(d, meta)
}
func listIotPolicyAttachmentPages(conn *iot.IoT, input *iot.ListAttachedPoliciesInput,
fn func(out *iot.ListAttachedPoliciesOutput, lastPage bool) bool) error {
for {
page, err := conn.ListAttachedPolicies(input)
if err != nil {
return err
}
lastPage := page.NextMarker == nil
shouldContinue := fn(page, lastPage)
if !shouldContinue || lastPage {
break
}
input.Marker = page.NextMarker
}
return nil
}
func getIotPolicyAttachment(conn *iot.IoT, target, policyName string) (*iot.Policy, error) {
var policy *iot.Policy
input := &iot.ListAttachedPoliciesInput{
PageSize: aws.Int64(250),
Recursive: aws.Bool(false),
Target: aws.String(target),
}
err := listIotPolicyAttachmentPages(conn, input, func(out *iot.ListAttachedPoliciesOutput, lastPage bool) bool {
for _, att := range out.Policies {
if policyName == aws.StringValue(att.PolicyName) {
policy = att
return false
}
}
return true
})
return policy, err
}
func resourceAwsIotPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
policyName := d.Get("policy").(string)
target := d.Get("target").(string)
var policy *iot.Policy
policy, err := getIotPolicyAttachment(conn, target, policyName)
if err != nil {
return fmt.Errorf("error listing policy attachments for target %s: %s", target, err)
}
if policy == nil {
log.Printf("[WARN] IOT Policy Attachment (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return nil
}
func resourceAwsIotPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
policyName := d.Get("policy").(string)
target := d.Get("target").(string)
_, err := conn.DetachPolicy(&iot.DetachPolicyInput{
PolicyName: aws.String(policyName),
Target: aws.String(target),
})
// DetachPolicy doesn't return an error if the policy doesn't exist,
// but it returns an error if the Target is not found.
if isAWSErr(err, iot.ErrCodeInvalidRequestException, "Invalid Target") {
log.Printf("[WARN] IOT target (%s) not found, removing attachment to policy (%s) from state", target, policyName)
return nil
}
if err != nil {
return fmt.Errorf("error detaching policy %s from target %s: %s", policyName, target, err)
}
return nil
}