Best Python code snippet using localstack_python
ec2_key.py
Source:ec2_key.py
...141 name_in_use = True142 while name_in_use:143 random_name = "ansible-" + str(uuid.uuid4())144 name_in_use = find_key_pair(module, ec2_client, random_name)145 temp_key = import_key_pair(module, ec2_client, random_name, key_material)146 delete_key_pair(module, ec2_client, random_name, finish_task=False)147 return temp_key['KeyFingerprint']148def find_key_pair(module, ec2_client, name):149 try:150 key = ec2_client.describe_key_pairs(KeyNames=[name])['KeyPairs'][0]151 except ClientError as err:152 if err.response['Error']['Code'] == "InvalidKeyPair.NotFound":153 return None154 module.fail_json_aws(err, msg="error finding keypair")155 return key156def create_key_pair(module, ec2_client, name, key_material, force):157 key = find_key_pair(module, ec2_client, name)158 if key:159 if key_material and force:160 new_fingerprint = get_key_fingerprint(module, ec2_client, key_material)161 if key['KeyFingerprint'] != new_fingerprint:162 if not module.check_mode:163 delete_key_pair(module, ec2_client, name, finish_task=False)164 key = import_key_pair(module, ec2_client, name, key_material)165 key_data = extract_key_data(key)166 module.exit_json(changed=True, key=key_data, msg="key pair updated")167 key_data = extract_key_data(key)168 module.exit_json(changed=False, key=key_data, msg="key pair already exists")169 else:170 # key doesn't exist, create it now171 key_data = None172 if not module.check_mode:173 if key_material:174 key = import_key_pair(module, ec2_client, name, key_material)175 else:176 try:177 key = ec2_client.create_key_pair(KeyName=name)178 except ClientError as err:179 module.fail_json_aws(err, msg="error creating key")180 key_data = extract_key_data(key)181 module.exit_json(changed=True, key=key_data, msg="key pair created")182def import_key_pair(module, ec2_client, name, key_material):183 try:184 key = ec2_client.import_key_pair(KeyName=name, PublicKeyMaterial=to_bytes(key_material))185 except ClientError as err:186 module.fail_json_aws(err, msg="error importing key")187 return key188def delete_key_pair(module, ec2_client, name, finish_task=True):189 key = find_key_pair(module, ec2_client, name)190 if key:191 if not module.check_mode:192 try:193 ec2_client.delete_key_pair(KeyName=name)194 except ClientError as err:195 module.fail_json_aws(err, msg="error deleting key")196 if not finish_task:197 return198 module.exit_json(changed=True, key=None, msg="key deleted")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!