in gg_group_setup/cmd.py [0:0]
def create_devices(self, thing_names, config_file, region=None,
cert_dir=None, append=False, account_id=None,
policy_name='ggd-discovery-policy', profile_name=None):
"""
Using the `thing_names` values, creates Things in AWS IoT, attaches and
downloads new keys & certs to the certificate directory, then records
the created information in the local config file for inclusion in the
Greengrass Group as Greengrass Devices.
:param thing_names: the thing name or list of thing names to create and
use as Greengrass Devices
:param config_file: config file used to track the Greengrass Devices in
the group
:param region: the region in which to create the new devices.
[default: us-west-2]
:param cert_dir: the directory in which to store the thing's keys and
certs. If `None` then use the current directory.
:param append: append the created devices to the list of devices in the
config file. [default: False]
:param account_id: the account ID in which to create devices. If 'None'
the config_file will be checked for an `account_id` value in the
`misc` section.
:param policy_name: the name of the policy to associate with the device.
[default: 'ggd-discovery-policy']
:param profile_name: the name of the `awscli` profile to use.
[default: None]
"""
logging.info("create_devices thing_names:{0}".format(thing_names))
config = GroupConfigFile(config_file=config_file)
if append is False and config.is_device_fresh() is False:
raise ValueError(
"Config file tracking previously created devices. Append "
"devices instead"
)
if region is None:
region = self._region
if account_id is None:
account_id = self._account_id
devices = dict()
if append:
devices = config['devices']
if type(thing_names) is str:
thing_names = [thing_names]
iot_client = _get_iot_session(region=region, profile_name=profile_name)
for thing_name in thing_names:
keys_cert, thing = self.create_thing(thing_name, region, cert_dir)
cert_arn = keys_cert['certificateArn']
devices[thing_name] = {
'thing_arn': thing['thingArn'],
'cert_arn': cert_arn,
'cert_id': keys_cert['certificateId'],
'thing_name': thing_name
}
logging.info("Thing:'{0}' associated with cert:'{1}'".format(
thing_name, cert_arn))
device_policy = self.get_device_policy(
device_name=thing_name, account_id=account_id, region=region
)
self._create_attach_thing_policy(cert_arn, device_policy,
iot_client, policy_name)
config['devices'] = devices
logging.info("create_devices cfg:{0}".format(config))