3131
3232#config = None
3333
34- def get_certificate (certificateId ):
34+ def get_certificate (certificate_id ):
3535 """Verify that the certificate is in IoT Core"""
3636 iot_client = boto3client ('iot' )
3737 try :
38- response = iot_client .describe_certificate (certificateId = certificateId )
38+ response = iot_client .describe_certificate (certificateId = certificate_id )
3939 return response ["certificateDescription" ].get ("certificateId" )
4040 except ClientError as error :
4141 assert error .response ['Error' ]['Code' ] == 'ResourceNotFoundException'
@@ -144,7 +144,7 @@ def process_thing(thing_name, certificate_id, thing_type_name):
144144 print (error )
145145 return None
146146
147- def requeue ():
147+ def requeue (config ):
148148 """
149149 Requeues the message for processing in case of unrecoverable error such
150150 as throttling. The structure is:
@@ -199,35 +199,40 @@ def process_certificate(config, requeue_cb):
199199 return None
200200
201201def process_thing_group (thing_group_name , thing_name ):
202+ """Attaches the configured thing group to the iot thing"""
202203 iot_client = boto3client ('iot' )
203204 try :
204205 thing_group_arn = get_thing_group (thing_group_name )
205206 thing_arn = get_thing (thing_name )
206-
207207 iot_client .add_thing_to_thing_group (thingGroupName = thing_group_name ,
208208 thingGroupArn = thing_group_arn ,
209209 thingName = thing_name ,
210210 thingArn = thing_arn ,
211211 overrideDynamicGroups = False )
212- except Exception as e :
213- print (e )
214- return None
212+ except ClientError as error :
213+ print (error )
214+
215+ return None
215216
216217def get_name_from_certificate (certificate_id ):
218+ """Assume the certificate cn is the thing name.
219+ TODO: Evaluate for deprecation, thing name identifier better
220+ evaluated in the vendor specific provider.
221+ """
217222 iot_client = boto3client ('iot' )
218223 response = iot_client .describe_certificate (certificateId = certificate_id )
219224 certificate_text = response ["certificateDescription" ].get ("certificatePem" )
220225 certificate_obj = x509 .load_pem_x509_certificate (data = certificate_text .encode ('ascii' ),
221226 backend = default_backend ())
222227 cn = certificate_obj .subject .get_attributes_for_oid (NameOID .COMMON_NAME )[0 ].value
223228 cn .replace (" " , "" )
224- # TODO: This must be really old because thing name is prescribed to the
229+ # TODO: This must be really old because thing name is prescribed to the
225230 # sqs message from the beginning, this might be eliminated
226231 logger .info ("Certificate common name [%s] to be IoT Thing name" , cn )
227232 return cn
228233
229234def process_sqs (config ):
230- certificate = config . get ( 'certificate' )
235+ """Main processing function to procedurally run through processing steps."""
231236 policy_name = config .get ('policy_name' )
232237 thing_group_name = config .get ('thing_group_name' )
233238 thing_type_name = config .get ('thing_type_name' )
@@ -244,6 +249,7 @@ def process_sqs(config):
244249 process_thing_group (thing_group_name , thing_name )
245250
246251def lambda_handler (event , context ):
252+ """Lambda function main entry point"""
247253 if event .get ('Records' ) is None :
248254 print ("ERROR: Configuration incorrect: no event record on invoke" )
249255 return {
0 commit comments