in gg_group_setup/cmd.py [0:0]
def create(self, group_type, config_file, group_name=None,
region=None, profile_name=None):
"""
Create a Greengrass group in the given region.
:param group_type: the type of group to create. Must match a `key` in
the `group_types` dict
:param config_file: config file of the group to create
:param group_name: the name of the group. If no name is given, then
group_type will be used.
:param region: the region in which to create the new group.
[default: us-west-2]
:param profile_name: the name of the `awscli` profile to use.
[default: None]
"""
logging.info("[begin] create command using group_types:{0}".format(
self.group_types))
config = GroupConfigFile(config_file=config_file)
if config.is_fresh() is False:
raise ValueError(
"Config file already tracking previously created group"
)
if group_type not in self.group_types.keys():
raise ValueError("Can only create {0} groups.".format(
self.group_types)
)
if region is None:
region = self._region
# create an instance of the requested group type that uses the given
# config file and region
gt = self.group_types[group_type](config=config, region=region)
# get and store the account's IoT endpoint for future use
ep = _get_iot_session(region=region).describe_endpoint()
misc = config['misc']
misc['iot_endpoint'] = ep['endpointAddress']
config['misc'] = misc
# Create a Group
logging.info("[begin] Creating a Greengrass Group")
if group_name is None:
group_name = group_type
gg_client = _get_gg_session(region=region, profile_name=profile_name)
group_info = gg_client.create_group(Name="{0}".format(group_name))
config['group'] = {"id": group_info['Id']}
# setup the policies and roles
gt.create_and_attach_thing_policy()
gt.create_and_attach_iam_role()
cl_arn = self._create_core_definition(
gg_client=gg_client, group_type=gt,
config=config, group_name=group_name
)
dl_arn = self._create_device_definition(
gg_client=gg_client, group_type=gt,
config=config, group_name=group_name
)
lv_arn = self._create_function_definition(
gg_client=gg_client, group_type=gt,
config=config
)
log_arn = self._create_logger_definition(
gg_client=gg_client, group_type=gt,
config=config
)
sub_arn = self._create_subscription_definition(
gg_client=gg_client, group_type=gt,
config=config
)
logging.info(
'Group details, core_def:{0} device_def:{1} func_def:{2} '
'logger_def:{3} subs_def:{4}'.format(
cl_arn, dl_arn, lv_arn, log_arn, sub_arn)
)
# Add all the constituent parts to the Greengrass Group
group_args = {'GroupId': group_info['Id']}
if cl_arn:
group_args['CoreDefinitionVersionArn'] = cl_arn
if dl_arn:
group_args['DeviceDefinitionVersionArn'] = dl_arn
if lv_arn:
group_args['FunctionDefinitionVersionArn'] = lv_arn
if log_arn:
group_args['LoggerDefinitionVersionArn'] = log_arn
if sub_arn:
group_args['SubscriptionDefinitionVersionArn'] = sub_arn
grp = gg_client.create_group_version(
**group_args
)
# store info about the provisioned artifacts into the local config file
config['group'] = {
"id": group_info['Id'],
"version_arn": grp['Arn'],
"version": grp['Version'],
"name": group_name
}
logging.info(
"[end] Created Greengrass Group {0}".format(group_info['Id']))