]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_quick_start/thing.py
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_IoT_Libraries / tools / aws_config_quick_start / thing.py
1 #!/usr/bin/env python\r
2 \r
3 import boto3\r
4 import json\r
5 \r
6 \r
7 class Thing():\r
8     def __init__(self, name):\r
9         self.client = boto3.client('iot')\r
10         self.name = name\r
11         self.arn = ''\r
12 \r
13     def create(self):\r
14         assert not self.exists(), "Thing already exists"\r
15         result = self.client.create_thing(thingName=self.name)\r
16         self.arn = result['thingArn']\r
17 \r
18     def delete(self):\r
19         assert self.exists(), "Thing does not exist"\r
20         principals = self.list_principals()\r
21         for principal in principals:\r
22             self.detach_principal(principal)\r
23         self.client.delete_thing(thingName=self.name)\r
24 \r
25     def exists(self):\r
26         list_of_things = self.client.list_things()['things']\r
27         for thing in list_of_things:\r
28             if thing['thingName'] == self.name:\r
29                 return True\r
30         return False\r
31 \r
32     def attach_principal(self, arn):\r
33         assert self.exists(), "Thing does not exist"\r
34         self.client.attach_thing_principal(thingName=self.name, principal=arn)\r
35 \r
36     def detach_principal(self, arn):\r
37         assert self.exists(), "Thing does not exist"\r
38         self.client.detach_thing_principal(thingName=self.name, principal=arn)\r
39 \r
40     def list_principals(self):\r
41         assert self.exists(), "Thing does not exist"\r
42         principals = self.client.list_thing_principals(thingName=self.name)\r
43         principals = principals['principals']\r
44         return principals\r