forked from hashicorp/terraform-provider-aws
/
resource_aws_iot_thing_type.go
202 lines (166 loc) · 5.03 KB
/
resource_aws_iot_thing_type.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package aws
import (
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
// https://docs.aws.amazon.com/iot/latest/apireference/API_CreateThingType.html
func resourceAwsIotThingType() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIotThingTypeCreate,
Read: resourceAwsIotThingTypeRead,
Update: resourceAwsIotThingTypeUpdate,
Delete: resourceAwsIotThingTypeDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("name", d.Id())
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateIotThingTypeName,
},
"properties": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateIotThingTypeDescription,
},
"searchable_attributes": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 3,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateIotThingTypeSearchableAttribute,
},
},
},
},
},
"deprecated": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsIotThingTypeCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
params := &iot.CreateThingTypeInput{
ThingTypeName: aws.String(d.Get("name").(string)),
}
if v, ok := d.GetOk("properties"); ok {
configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})
if ok && config != nil {
params.ThingTypeProperties = expandIotThingTypeProperties(config)
}
}
log.Printf("[DEBUG] Creating IoT Thing Type: %s", params)
out, err := conn.CreateThingType(params)
if err != nil {
return err
}
d.SetId(*out.ThingTypeName)
if v := d.Get("deprecated").(bool); v {
params := &iot.DeprecateThingTypeInput{
ThingTypeName: aws.String(d.Id()),
UndoDeprecate: aws.Bool(false),
}
log.Printf("[DEBUG] Deprecating IoT Thing Type: %s", params)
_, err := conn.DeprecateThingType(params)
if err != nil {
return err
}
}
return resourceAwsIotThingTypeRead(d, meta)
}
func resourceAwsIotThingTypeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
params := &iot.DescribeThingTypeInput{
ThingTypeName: aws.String(d.Id()),
}
log.Printf("[DEBUG] Reading IoT Thing Type: %s", params)
out, err := conn.DescribeThingType(params)
if err != nil {
if isAWSErr(err, iot.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] IoT Thing Type %q not found, removing from state", d.Id())
d.SetId("")
}
return err
}
if out.ThingTypeMetadata != nil {
d.Set("deprecated", out.ThingTypeMetadata.Deprecated)
}
d.Set("arn", out.ThingTypeArn)
d.Set("properties", flattenIotThingTypeProperties(out.ThingTypeProperties))
return nil
}
func resourceAwsIotThingTypeUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
if d.HasChange("deprecated") {
params := &iot.DeprecateThingTypeInput{
ThingTypeName: aws.String(d.Id()),
UndoDeprecate: aws.Bool(!d.Get("deprecated").(bool)),
}
log.Printf("[DEBUG] Updating IoT Thing Type: %s", params)
_, err := conn.DeprecateThingType(params)
if err != nil {
return err
}
}
return resourceAwsIotThingTypeRead(d, meta)
}
func resourceAwsIotThingTypeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iotconn
// In order to delete an IoT Thing Type, you must deprecate it first and wait
// at least 5 minutes.
deprecateParams := &iot.DeprecateThingTypeInput{
ThingTypeName: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deprecating IoT Thing Type: %s", deprecateParams)
_, err := conn.DeprecateThingType(deprecateParams)
if err != nil {
return err
}
deleteParams := &iot.DeleteThingTypeInput{
ThingTypeName: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting IoT Thing Type: %s", deleteParams)
return resource.Retry(6*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteThingType(deleteParams)
if err != nil {
if isAWSErr(err, iot.ErrCodeInvalidRequestException, "Please wait for 5 minutes after deprecation and then retry") {
return resource.RetryableError(err)
}
// As the delay post-deprecation is about 5 minutes, it may have been
// deleted in between, thus getting a Not Found Exception.
if isAWSErr(err, iot.ErrCodeResourceNotFoundException, "") {
return nil
}
return resource.NonRetryableError(err)
}
return nil
})
}