Best Python code snippet using localstack_python
acc_alias.py
Source:acc_alias.py
...40 except Exception as err:41 error_message = boto_exception(err)42 module.fail_json(msg=str(err))43 return changed44def delete_account_alias(module, client, alias):45 """46 Remove the alias form the exisiting name47 """48 changed = False49 try:50 resp = client.delete_account_alias(AccountAlias=alias)51 changed = True52 except Exception as err:53 error_message = boto_exception(err)54 module.fail_json(msg=str(err))55 return changed56def get_user_id(module, client):57 """58 Get User id by taking module and client as parameters59 """60 try:61 resp = client.get_user()62 user_id = resp["User"]["UserId"]63 except Exception as err:64 error_message = boto_exception(err)65 module.fail_json(msg=error_message)66 return user_id67def main():68 argument_spec = dict(69 aws_account_alias=dict(default=None, required=False),70 aws_account_state=dict(default=None, required=True, choices=['present', 'absent']),71 aws_access_key=dict(default=None, required=False),72 aws_secret_key=dict(default=None, required=False),73 ) 74 module = AnsibleModule(argument_spec=argument_spec) 75 76 aws_account_alias = module.params.get('aws_account_alias')77 aws_account_state = module.params.get('aws_account_state')78 aws_access_key = module.params.get('aws_access_key')79 aws_secret_key = module.params.get('aws_secret_key')80 if not aws_access_key:81 try:82 aws_access_key = os.environ["AWS_ACCESS_KEY_ID"]83 except:84 module.fail_json(msg="Empty value for required argument: aws_access_key")85 if not aws_secret_key:86 try:87 aws_secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]88 except:89 module.fail_json(msg="Empty value for required argument: aws_secret_key")90 client = aws_client(91 SERVICE,92 aws_access_key_id=aws_access_key,93 aws_secret_access_key=aws_secret_key94 )95 current_account_alias = get_account_alias(module, client)96 changed = False97 user_id = get_user_id(module, client)98 if aws_account_alias:99 if aws_account_alias != current_account_alias and aws_account_state=="present":100 changed = set_account_alias(module, client, aws_account_alias)101 current_account_alias = aws_account_alias102 elif aws_account_alias == current_account_alias and aws_account_state=="absent":103 changed = delete_account_alias(module, client, aws_account_alias)104 current_account_alias = get_account_alias(module, client)105 106 # Set module exit Output107 module.exit_json(108 changed=changed,109 aws_account_id=get_user_id(module, client),110 aws_account_alias=current_account_alias111 )112 113if __name__ == '__main__':...
test_account_alias.py
Source:test_account_alias.py
1"""2Tests the account alias configuration lambda3"""4import unittest5import boto36from botocore.stub import Stubber7from botocore.exceptions import ClientError8from mock import Mock9from aws_xray_sdk import global_sdk_config10from ..configure_account_alias import (11 create_account_alias,12 ensure_account_has_alias,13)14global_sdk_config.set_sdk_enabled(False)15class SuccessTestCase(unittest.TestCase):16 @staticmethod17 def test_account_alias_exists_already():18 test_account = {"account_id": 123456789012, "alias": "MyCoolAlias"}19 iam_client = Mock()20 iam_client.list_account_aliases.return_value = {21 "AccountAliases": ["MyCoolAlias"],22 }23 ensure_account_has_alias(test_account, iam_client)24 iam_client.list_account_aliases.assert_called_once_with()25 iam_client.delete_account_alias.assert_not_called()26 iam_client.create_account_alias.assert_not_called()27 @staticmethod28 def test_account_alias_another_alias_exists():29 test_account = {"account_id": 123456789012, "alias": "MyCoolAlias"}30 iam_client = Mock()31 iam_client.list_account_aliases.return_value = {32 "AccountAliases": ["AnotherCoolAlias"],33 }34 ensure_account_has_alias(test_account, iam_client)35 iam_client.list_account_aliases.assert_called_once_with()36 iam_client.delete_account_alias.assert_called_once_with(37 AccountAlias='AnotherCoolAlias',38 )39 iam_client.create_account_alias.assert_called_once_with(40 AccountAlias='MyCoolAlias',41 )42 @staticmethod43 def test_account_alias_no_aliases_yet():44 test_account = {"account_id": 123456789012, "alias": "MyCoolAlias"}45 iam_client = Mock()46 iam_client.list_account_aliases.return_value = {47 "AccountAliases": [],48 }49 ensure_account_has_alias(test_account, iam_client)50 iam_client.list_account_aliases.assert_called_once_with()51 iam_client.delete_account_alias.assert_not_called()52 iam_client.create_account_alias.assert_called_once_with(53 AccountAlias='MyCoolAlias',54 )55class FailureTestCase(unittest.TestCase):56 # pylint: disable=W010657 def test_account_alias_when_nonunique(self):58 test_account = {"account_id": 123456789012, "alias": "nonunique"}59 iam_client = boto3.client("iam")60 stubber = Stubber(iam_client)61 stubber.add_client_error(62 'create_account_alias',63 'EntityAlreadyExistsException',64 f"An error occurred (EntityAlreadyExists) when calling the CreateAccountAlias operation: The account alias {test_account.get('alias')} already exists."65 )66 stubber.activate()67 with self.assertRaises(ClientError) as _error:68 create_account_alias(test_account, iam_client)69 self.assertRegex(70 str(_error.exception),71 r'.*The account alias nonunique already exists.*'...
delete_account_alias.py
Source:delete_account_alias.py
...17# Create IAM client18iam = boto3.client('iam')1920# Delete an account alias21iam.delete_account_alias(22 AccountAlias='ALIAS'23)24 2526# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]27# snippet-sourcedescription:[delete_account_alias.py demonstrates how to delete an alias for your IAM Account.]28# snippet-keyword:[Python]29# snippet-keyword:[AWS SDK for Python (Boto3)]30# snippet-keyword:[Code Sample]31# snippet-keyword:[AWS Identity and Access Management (IAM)]32# snippet-service:[iam]33# snippet-sourcetype:[full-example]34# snippet-sourcedate:[]35# snippet-sourceauthor:[jschwarzwalder (AWS)]
...
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!!