/
iot_thing_groups.go
251 lines (234 loc) · 7.58 KB
/
iot_thing_groups.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package iot
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iot"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
func IotThingGroups() *schema.Table {
return &schema.Table{
Name: "aws_iot_thing_groups",
Description: "Groups allow you to manage several things at once by categorizing them into groups",
Resolver: fetchIotThingGroups,
Multiplex: client.ServiceAccountRegionMultiplexer("iot"),
IgnoreError: client.IgnoreCommonErrors,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "things_in_group",
Description: "Lists the things in the specified group",
Type: schema.TypeStringArray,
Resolver: ResolveIotThingGroupThingsInGroup,
},
{
Name: "policies",
Description: "Policies associated with the thing group",
Type: schema.TypeStringArray,
Resolver: ResolveIotThingGroupPolicies,
IgnoreInTests: true,
},
{
Name: "tags",
Description: "Tags of the resource",
Type: schema.TypeJSON,
Resolver: ResolveIotThingGroupTags,
},
{
Name: "index_name",
Description: "The dynamic thing group index name.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "query_string",
Description: "The dynamic thing group search query string.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "query_version",
Description: "The dynamic thing group query version.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "status",
Description: "The dynamic thing group status.",
Type: schema.TypeString,
},
{
Name: "arn",
Description: "The thing group ARN.",
Type: schema.TypeString,
Resolver: schema.PathResolver("ThingGroupArn"),
},
{
Name: "id",
Description: "The thing group ID.",
Type: schema.TypeString,
Resolver: schema.PathResolver("ThingGroupId"),
},
{
Name: "creation_date",
Description: "The UNIX timestamp of when the thing group was created.",
Type: schema.TypeTimestamp,
Resolver: schema.PathResolver("ThingGroupMetadata.CreationDate"),
},
{
Name: "parent_group_name",
Description: "The parent thing group name.",
Type: schema.TypeString,
Resolver: schema.PathResolver("ThingGroupMetadata.ParentGroupName"),
},
{
Name: "root_to_parent_thing_groups",
Description: "The root parent thing group.",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("ThingGroupMetadata.RootToParentThingGroups"),
},
{
Name: "name",
Description: "The name of the thing group.",
Type: schema.TypeString,
Resolver: schema.PathResolver("ThingGroupName"),
},
{
Name: "attribute_payload_attributes",
Description: "A JSON string containing up to three key-value pair in JSON format",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("ThingGroupProperties.AttributePayload.Attributes"),
},
{
Name: "attribute_payload_merge",
Description: "Specifies whether the list of attributes provided in the AttributePayload is merged with the attributes stored in the registry, instead of overwriting them. To remove an attribute, call UpdateThing with an empty attribute value",
Type: schema.TypeBool,
Resolver: schema.PathResolver("ThingGroupProperties.AttributePayload.Merge"),
},
{
Name: "thing_group_description",
Description: "The thing group description.",
Type: schema.TypeString,
Resolver: schema.PathResolver("ThingGroupProperties.ThingGroupDescription"),
},
{
Name: "version",
Description: "The version of the thing group.",
Type: schema.TypeBigInt,
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchIotThingGroups(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
input := iot.ListThingGroupsInput{
MaxResults: aws.Int32(250),
}
c := meta.(*client.Client)
svc := c.Services().IOT
for {
response, err := svc.ListThingGroups(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
for _, g := range response.ThingGroups {
group, err := svc.DescribeThingGroup(ctx, &iot.DescribeThingGroupInput{
ThingGroupName: g.GroupName,
}, func(options *iot.Options) {
options.Region = c.Region
})
if err != nil {
return diag.WrapError(err)
}
res <- group
}
if aws.ToString(response.NextToken) == "" {
break
}
input.NextToken = response.NextToken
}
return nil
}
func ResolveIotThingGroupThingsInGroup(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
i := resource.Item.(*iot.DescribeThingGroupOutput)
cl := meta.(*client.Client)
svc := cl.Services().IOT
input := iot.ListThingsInThingGroupInput{
ThingGroupName: i.ThingGroupName,
MaxResults: aws.Int32(250),
}
var things []string
for {
response, err := svc.ListThingsInThingGroup(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
things = append(things, response.Things...)
if aws.ToString(response.NextToken) == "" {
break
}
input.NextToken = response.NextToken
}
return diag.WrapError(resource.Set(c.Name, things))
}
func ResolveIotThingGroupPolicies(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
i := resource.Item.(*iot.DescribeThingGroupOutput)
cl := meta.(*client.Client)
svc := cl.Services().IOT
input := iot.ListAttachedPoliciesInput{
Target: i.ThingGroupArn,
PageSize: aws.Int32(250),
}
var policies []string
for {
response, err := svc.ListAttachedPolicies(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
for _, p := range response.Policies {
policies = append(policies, *p.PolicyArn)
}
if aws.ToString(response.NextMarker) == "" {
break
}
input.Marker = response.NextMarker
}
return diag.WrapError(resource.Set(c.Name, policies))
}
func ResolveIotThingGroupTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
i := resource.Item.(*iot.DescribeThingGroupOutput)
cl := meta.(*client.Client)
svc := cl.Services().IOT
input := iot.ListTagsForResourceInput{
ResourceArn: i.ThingGroupArn,
}
tags := make(map[string]string)
for {
response, err := svc.ListTagsForResource(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
client.TagsIntoMap(response.Tags, tags)
if aws.ToString(response.NextToken) == "" {
break
}
input.NextToken = response.NextToken
}
return diag.WrapError(resource.Set(c.Name, tags))
}