forked from openshift/terraform-provider-aws
/
resource_aws_iot_thing_principal_attachment.go
109 lines (88 loc) · 2.82 KB
/
resource_aws_iot_thing_principal_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
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 resourceAwsIotThingPrincipalAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIotThingPrincipalAttachmentCreate,
Read: resourceAwsIotThingPrincipalAttachmentRead,
Delete: resourceAwsIotThingPrincipalAttachmentDelete,
Schema: map[string]*schema.Schema{
"principal": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"thing": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsIotThingPrincipalAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
principal := d.Get("principal").(string)
thing := d.Get("thing").(string)
_, err := conn.AttachThingPrincipal(&iot.AttachThingPrincipalInput{
Principal: aws.String(principal),
ThingName: aws.String(thing),
})
if err != nil {
return fmt.Errorf("error attaching principal %s to thing %s: %s", principal, thing, err)
}
d.SetId(fmt.Sprintf("%s|%s", thing, principal))
return resourceAwsIotThingPrincipalAttachmentRead(d, meta)
}
func getIoTThingPricipalAttachment(conn *iot.IoT, thing, principal string) (bool, error) {
out, err := conn.ListThingPrincipals(&iot.ListThingPrincipalsInput{
ThingName: aws.String(thing),
})
if isAWSErr(err, iot.ErrCodeResourceNotFoundException, "") {
return false, nil
} else if err != nil {
return false, err
}
found := false
for _, name := range out.Principals {
if principal == aws.StringValue(name) {
found = true
break
}
}
return found, nil
}
func resourceAwsIotThingPrincipalAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
principal := d.Get("principal").(string)
thing := d.Get("thing").(string)
found, err := getIoTThingPricipalAttachment(conn, thing, principal)
if err != nil {
return fmt.Errorf("error listing principals for thing %s: %s", thing, err)
}
if !found {
log.Printf("[WARN] IoT Thing Principal Attachment (%s) not found, removing from state", d.Id())
d.SetId("")
}
return nil
}
func resourceAwsIotThingPrincipalAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
principal := d.Get("principal").(string)
thing := d.Get("thing").(string)
_, err := conn.DetachThingPrincipal(&iot.DetachThingPrincipalInput{
Principal: aws.String(principal),
ThingName: aws.String(thing),
})
if isAWSErr(err, iot.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] IoT Principal %s or Thing %s not found, removing from state", principal, thing)
} else if err != nil {
return fmt.Errorf("error detaching principal %s from thing %s: %s", principal, thing, err)
}
return nil
}