forked from hashicorp/terraform-provider-aws
/
resource_aws_iot_policy.go
131 lines (105 loc) · 2.74 KB
/
resource_aws_iot_policy.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
package aws
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsIotPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIotPolicyCreate,
Read: resourceAwsIotPolicyRead,
Update: resourceAwsIotPolicyUpdate,
Delete: resourceAwsIotPolicyDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"policy": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"default_version_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsIotPolicyCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
out, err := conn.CreatePolicy(&iot.CreatePolicyInput{
PolicyName: aws.String(d.Get("name").(string)),
PolicyDocument: aws.String(d.Get("policy").(string)),
})
if err != nil {
log.Printf("[ERROR] %s", err)
return err
}
d.SetId(*out.PolicyName)
return resourceAwsIotPolicyRead(d, meta)
}
func resourceAwsIotPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
out, err := conn.GetPolicy(&iot.GetPolicyInput{
PolicyName: aws.String(d.Id()),
})
if err != nil {
log.Printf("[ERROR] %s", err)
return err
}
d.Set("arn", out.PolicyArn)
d.Set("default_version_id", out.DefaultVersionId)
return nil
}
func resourceAwsIotPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
if d.HasChange("policy") {
_, err := conn.CreatePolicyVersion(&iot.CreatePolicyVersionInput{
PolicyName: aws.String(d.Id()),
PolicyDocument: aws.String(d.Get("policy").(string)),
SetAsDefault: aws.Bool(true),
})
if err != nil {
log.Printf("[ERROR] %s", err)
return err
}
}
return resourceAwsIotPolicyRead(d, meta)
}
func resourceAwsIotPolicyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
out, err := conn.ListPolicyVersions(&iot.ListPolicyVersionsInput{
PolicyName: aws.String(d.Id()),
})
if err != nil {
return err
}
// Delete all non-default versions of the policy
for _, ver := range out.PolicyVersions {
if !*ver.IsDefaultVersion {
_, err = conn.DeletePolicyVersion(&iot.DeletePolicyVersionInput{
PolicyName: aws.String(d.Id()),
PolicyVersionId: ver.VersionId,
})
if err != nil {
log.Printf("[ERROR] %s", err)
return err
}
}
}
//Delete default policy version
_, err = conn.DeletePolicy(&iot.DeletePolicyInput{
PolicyName: aws.String(d.Id()),
})
if err != nil {
log.Printf("[ERROR] %s", err)
return err
}
return nil
}