summaryrefslogtreecommitdiff
path: root/FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_quick_start/policy.py
blob: 6cf13c23bedc4e100f331a18d8a859a87cf83f61 (plain)
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
#!/usr/bin/env python

import boto3
import json


class Policy():
    def __init__(self, name, policy=''):
        self.name = name
        self.policy = policy
        self.client = boto3.client('iot')

    def create(self):
        assert not self.exists(), "Policy already exists"
        self.client.create_policy(policyName=self.name,
                                  policyDocument=self.policy)

    def delete(self):
        assert self.exists(), "Policy does not exist, cannot be deleted"
        self.client.delete_policy(policyName=self.name)

    def exists(self):
        policies = self.client.list_policies()['policies']
        for policy in policies:
            if self.name == policy['policyName']:
                return True
        return False