1#!/usr/bin/env python
2
3import boto3
4import json
5
6
7class Policy():
8    def __init__(self, name, policy=''):
9        self.name = name
10        self.policy = policy
11        self.client = boto3.client('iot')
12
13    def create(self):
14        assert not self.exists(), "Policy already exists"
15        self.client.create_policy(policyName=self.name,
16                                  policyDocument=self.policy)
17
18    def delete(self):
19        assert self.exists(), "Policy does not exist, cannot be deleted"
20        self.client.delete_policy(policyName=self.name)
21
22    def exists(self):
23        policies = self.client.list_policies()['policies']
24        for policy in policies:
25            if self.name == policy['policyName']:
26                return True
27        return False
28