Repository navigation
Expand file tree
/
Copy pathiot_wrapper.py
More file actions
455 lines (397 loc) · 16.5 KB
/
Copy pathiot_wrapper.py
File metadata and controls
455 lines (397 loc) · 16.5 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to use the AWS SDK for Python (Boto3) with AWS IoT to manage things,
certificates, and topic rules.
"""
import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.iot.IoTWrapper]
# snippet-start:[python.example_code.iot.IoTWrapper.decl]
class IoTWrapper:
"""Encapsulates AWS IoT actions."""
def __init__(self, iot_client, iot_data_client=None):
"""
:param iot_client: A Boto3 AWS IoT client.
:param iot_data_client: A Boto3 AWS IoT Data Plane client.
"""
self.iot_client = iot_client
self.iot_data_client = iot_data_client
@classmethod
def from_client(cls):
iot_client = boto3.client("iot")
iot_data_client = boto3.client("iot-data")
return cls(iot_client, iot_data_client)
# snippet-end:[python.example_code.iot.IoTWrapper.decl]
# snippet-start:[python.example_code.iot.CreateThing]
def create_thing(self, thing_name):
"""
Creates an AWS IoT thing.
:param thing_name: The name of the thing to create.
:return: The name and ARN of the created thing.
"""
try:
response = self.iot_client.create_thing(thingName=thing_name)
logger.info("Created thing %s.", thing_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceAlreadyExistsException":
logger.info("Thing %s already exists. Skipping creation.", thing_name)
return None
logger.error(
"Couldn't create thing %s. Here's why: %s: %s",
thing_name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response
# snippet-end:[python.example_code.iot.CreateThing]
# snippet-start:[python.example_code.iot.ListThings]
def list_things(self):
"""
Lists AWS IoT things.
:return: The list of things.
"""
try:
things = []
paginator = self.iot_client.get_paginator("list_things")
for page in paginator.paginate():
things.extend(page["things"])
logger.info("Retrieved %s things.", len(things))
return things
except ClientError as err:
if err.response["Error"]["Code"] == "ThrottlingException":
logger.error("Request throttled. Please try again later.")
else:
logger.error(
"Couldn't list things. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.ListThings]
# snippet-start:[python.example_code.iot.CreateKeysAndCertificate]
def create_keys_and_certificate(self):
"""
Creates keys and a certificate for an AWS IoT thing.
:return: The certificate ID, ARN, and PEM.
"""
try:
response = self.iot_client.create_keys_and_certificate(setAsActive=True)
logger.info("Created certificate %s.", response["certificateId"])
except ClientError as err:
if err.response["Error"]["Code"] == "ThrottlingException":
logger.error("Request throttled. Please try again later.")
else:
logger.error(
"Couldn't create keys and certificate. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response
# snippet-end:[python.example_code.iot.CreateKeysAndCertificate]
# snippet-start:[python.example_code.iot.AttachThingPrincipal]
def attach_thing_principal(self, thing_name, principal):
"""
Attaches a certificate to an AWS IoT thing.
:param thing_name: The name of the thing.
:param principal: The ARN of the certificate.
"""
try:
self.iot_client.attach_thing_principal(
thingName=thing_name, principal=principal
)
logger.info("Attached principal %s to thing %s.", principal, thing_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("Cannot attach principal. Resource not found.")
return
logger.error(
"Couldn't attach principal to thing. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.AttachThingPrincipal]
# snippet-start:[python.example_code.iot.DescribeEndpoint]
def describe_endpoint(self, endpoint_type="iot:Data-ATS"):
"""
Gets the AWS IoT endpoint.
:param endpoint_type: The endpoint type.
:return: The endpoint.
"""
try:
response = self.iot_client.describe_endpoint(endpointType=endpoint_type)
logger.info("Retrieved endpoint %s.", response["endpointAddress"])
except ClientError as err:
if err.response["Error"]["Code"] == "ThrottlingException":
logger.error("Request throttled. Please try again later.")
else:
logger.error(
"Couldn't describe endpoint. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["endpointAddress"]
# snippet-end:[python.example_code.iot.DescribeEndpoint]
# snippet-start:[python.example_code.iot.ListCertificates]
def list_certificates(self):
"""
Lists AWS IoT certificates.
:return: The list of certificates.
"""
try:
certificates = []
paginator = self.iot_client.get_paginator("list_certificates")
for page in paginator.paginate():
certificates.extend(page["certificates"])
logger.info("Retrieved %s certificates.", len(certificates))
return certificates
except ClientError as err:
if err.response["Error"]["Code"] == "ThrottlingException":
logger.error("Request throttled. Please try again later.")
else:
logger.error(
"Couldn't list certificates. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.ListCertificates]
# snippet-start:[python.example_code.iot.DetachThingPrincipal]
def detach_thing_principal(self, thing_name, principal):
"""
Detaches a certificate from an AWS IoT thing.
:param thing_name: The name of the thing.
:param principal: The ARN of the certificate.
"""
try:
self.iot_client.detach_thing_principal(
thingName=thing_name, principal=principal
)
logger.info("Detached principal %s from thing %s.", principal, thing_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("Cannot detach principal. Resource not found.")
return
logger.error(
"Couldn't detach principal from thing. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.DetachThingPrincipal]
# snippet-start:[python.example_code.iot.DeleteCertificate]
def delete_certificate(self, certificate_id):
"""
Deletes an AWS IoT certificate.
:param certificate_id: The ID of the certificate to delete.
"""
try:
self.iot_client.update_certificate(
certificateId=certificate_id, newStatus="INACTIVE"
)
self.iot_client.delete_certificate(certificateId=certificate_id)
logger.info("Deleted certificate %s.", certificate_id)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("Cannot delete certificate. Resource not found.")
return
logger.error(
"Couldn't delete certificate. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.DeleteCertificate]
# snippet-start:[python.example_code.iot.CreateTopicRule]
def create_topic_rule(self, rule_name, topic, sns_action_arn, role_arn):
"""
Creates an AWS IoT topic rule.
:param rule_name: The name of the rule.
:param topic: The MQTT topic to subscribe to.
:param sns_action_arn: The ARN of the SNS topic to publish to.
:param role_arn: The ARN of the IAM role.
"""
try:
self.iot_client.create_topic_rule(
ruleName=rule_name,
topicRulePayload={
"sql": f"SELECT * FROM '{topic}'",
"actions": [
{"sns": {"targetArn": sns_action_arn, "roleArn": role_arn}}
],
},
)
logger.info("Created topic rule %s.", rule_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceAlreadyExistsException":
logger.info("Topic rule %s already exists. Skipping creation.", rule_name)
return
logger.error(
"Couldn't create topic rule. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.CreateTopicRule]
# snippet-start:[python.example_code.iot.ListTopicRules]
def list_topic_rules(self):
"""
Lists AWS IoT topic rules.
:return: The list of topic rules.
"""
try:
rules = []
paginator = self.iot_client.get_paginator("list_topic_rules")
for page in paginator.paginate():
rules.extend(page["rules"])
logger.info("Retrieved %s topic rules.", len(rules))
return rules
except ClientError as err:
if err.response["Error"]["Code"] == "ThrottlingException":
logger.error("Request throttled. Please try again later.")
else:
logger.error(
"Couldn't list topic rules. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.ListTopicRules]
# snippet-start:[python.example_code.iot.SearchIndex]
def search_index(self, query):
"""
Searches the AWS IoT index.
:param query: The search query.
:return: The list of things found.
"""
try:
response = self.iot_client.search_index(queryString=query)
logger.info("Found %s things.", len(response.get("things", [])))
except ClientError as err:
if err.response["Error"]["Code"] == "ThrottlingException":
logger.error("Request throttled. Please try again later.")
else:
logger.error(
"Couldn't search index. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response.get("things", [])
# snippet-end:[python.example_code.iot.SearchIndex]
# snippet-start:[python.example_code.iot.UpdateIndexingConfiguration]
def update_indexing_configuration(self):
"""
Updates the AWS IoT indexing configuration to enable thing indexing.
"""
try:
self.iot_client.update_indexing_configuration(
thingIndexingConfiguration={"thingIndexingMode": "REGISTRY"}
)
logger.info("Updated indexing configuration.")
except ClientError as err:
logger.error(
"Couldn't update indexing configuration. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.UpdateIndexingConfiguration]
# snippet-start:[python.example_code.iot.DeleteThing]
def delete_thing(self, thing_name):
"""
Deletes an AWS IoT thing.
:param thing_name: The name of the thing to delete.
"""
try:
self.iot_client.delete_thing(thingName=thing_name)
logger.info("Deleted thing %s.", thing_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("Cannot delete thing. Resource not found.")
return
logger.error(
"Couldn't delete thing. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.DeleteThing]
# snippet-start:[python.example_code.iot.DeleteTopicRule]
def delete_topic_rule(self, rule_name):
"""
Deletes an AWS IoT topic rule.
:param rule_name: The name of the rule to delete.
"""
try:
self.iot_client.delete_topic_rule(ruleName=rule_name)
logger.info("Deleted topic rule %s.", rule_name)
except ClientError as err:
logger.error(
"Couldn't delete topic rule. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.DeleteTopicRule]
# snippet-start:[python.example_code.iot.UpdateThingShadow]
def update_thing_shadow(self, thing_name, shadow_state):
"""
Updates the shadow for an AWS IoT thing.
:param thing_name: The name of the thing.
:param shadow_state: The shadow state as a dictionary.
"""
import json
try:
self.iot_data_client.update_thing_shadow(
thingName=thing_name, payload=json.dumps(shadow_state)
)
logger.info("Updated shadow for thing %s.", thing_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("Cannot update thing shadow. Resource not found.")
return
logger.error(
"Couldn't update thing shadow. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.iot.UpdateThingShadow]
# snippet-start:[python.example_code.iot.GetThingShadow]
def get_thing_shadow(self, thing_name):
"""
Gets the shadow for an AWS IoT thing.
:param thing_name: The name of the thing.
:return: The shadow state as a dictionary.
"""
import json
try:
response = self.iot_data_client.get_thing_shadow(thingName=thing_name)
shadow = json.loads(response["payload"].read())
logger.info("Retrieved shadow for thing %s.", thing_name)
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("Cannot get thing shadow. Resource not found.")
return None
logger.error(
"Couldn't get thing shadow. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return shadow
# snippet-end:[python.example_code.iot.GetThingShadow]
# snippet-end:[python.example_code.iot.IoTWrapper]