def create_thing()

in gg_group_setup/cmd.py [0:0]


    def create_thing(self, thing_name, region=None, cert_dir=None, force=False):
        if region is None:
            region = self._region
        iot_client = _get_iot_session(region=region)
        ###
        # Here begins the essence of the `create_thing` command
        # Create a Key and Certificate in the AWS IoT service per Thing
        keys_cert = iot_client.create_keys_and_certificate(setAsActive=True)
        # Create a named Thing in the AWS IoT Service
        thing = iot_client.create_thing(thingName=thing_name)
        thing_arn=thing['thingArn']
        
        # get and store the account's IoT endpoint for future use
        ep = iot_client.describe_endpoint(endpointType='iot:Data-ATS')
        iot_host=ep['endpointAddress']
        gg_host=self.get_gg_endpoint(region=region)
        iot_client.update_thing(
            thingName=thing_name,
            attributePayload={
                'attributes': {
                    'thingArn': thing['thingArn'],
                    'certificateId': keys_cert['certificateId']
                },
                'merge': True
            }
        )
        # Attach the previously created Certificate to the created Thing
        iot_client.attach_thing_principal(
            thingName=thing_name, principal=keys_cert['certificateArn'])
        # This ends the essence of the `create_core` command
        ###
        if cert_dir is None:
            cfg_dir = os.getcwd()
        else:
            cfg_dir = cert_dir

        # Save all Key and Certificate files locally for future use
        try:
            cert_name = cfg_dir + '/' + thing_name + ".pem"
            public_key_file = cfg_dir + '/' + thing_name + ".pub"
            private_key_file = cfg_dir + '/' + thing_name + ".prv"
            gg_config_file = cfg_dir + '/' + thing_name + ".config.json"
            
            with open(gg_config_file, "w") as gg_config_file_link:
                gg_config_file_content = self.create_gg_config_file(thing_name,thing_arn,iot_host,gg_host)
                json.dump(gg_config_file_content, gg_config_file_link, indent=2)
                # gg_config_file_link.write(gg_config_file_content)
                logging.info("Thing Name: {0} and GG Config file: {1}".format(
                    thing_name, gg_config_file))

            with open(cert_name, "w") as pem_file:
                pem = keys_cert['certificatePem']
                pem_file.write(pem)
                logging.info("Thing Name: {0} and PEM file: {1}".format(
                    thing_name, cert_name))

            with open(public_key_file, "w") as pub_file:
                pub = keys_cert['keyPair']['PublicKey']
                pub_file.write(pub)
                logging.info("Thing Name: {0} Public Key File: {1}".format(
                    thing_name, public_key_file))

            with open(private_key_file, "w") as prv_file:
                prv = keys_cert['keyPair']['PrivateKey']
                prv_file.write(prv)
                logging.info("Thing Name: {0} Private Key File: {1}".format(
                    thing_name, private_key_file))
        except OSError as ose:
            logging.error(
                'OSError while writing a key or cert file. {0}'.format(ose)
            )
        return keys_cert, thing