Best Python code snippet using localstack_python
alicloud_ecs.py
Source:alicloud_ecs.py
...75 else:76 raise AnsibleACSError(77 "Unknown problem connecting to region %s for acs module %s." % (region, acs_module.__name__))78 return conn79def get_assume_role(params):80 """ Return new params """81 sts_params = get_acs_connection_info(params)82 assume_role = {}83 if params.get('assume_role'):84 assume_role['alicloud_assume_role_arn'] = params['assume_role'].get('role_arn')85 assume_role['alicloud_assume_role_session_name'] = params['assume_role'].get('session_name')86 assume_role['alicloud_assume_role_session_expiration'] = params['assume_role'].get('session_expiration')87 assume_role['alicloud_assume_role_policy'] = params['assume_role'].get('policy')88 assume_role_params = {89 'role_arn': params.get('alicloud_assume_role_arn') if params.get('alicloud_assume_role_arn') else assume_role.get('alicloud_assume_role_arn'),90 'role_session_name': params.get('alicloud_assume_role_session_name') if params.get('alicloud_assume_role_session_name')91 else assume_role.get('alicloud_assume_role_session_name'),92 'duration_seconds': params.get('alicloud_assume_role_session_expiration') if params.get('alicloud_assume_role_session_expiration')93 else assume_role.get('alicloud_assume_role_session_expiration', 3600),94 'policy': assume_role.get('alicloud_assume_role_policy', {})95 }96 try:97 sts = connect_to_acs(footmark.sts, params.get('alicloud_region'), **sts_params).assume_role(**assume_role_params).read()98 sts_params['acs_access_key_id'], sts_params['acs_secret_access_key'], sts_params['security_token'] \99 = sts['access_key_id'], sts['access_key_secret'], sts['security_token']100 except AnsibleACSError as e:101 params.fail_json(msg=str(e))102 return sts_params103def get_profile(params):104 if not params['alicloud_access_key'] and not params['ecs_role_name'] and params['profile']:105 path = params['shared_credentials_file'] if params['shared_credentials_file'] else os.getenv('HOME') + '/.aliyun/config.json'106 auth = {}107 with open(path, 'r') as f:108 for pro in json.load(f)['profiles']:109 if params['profile'] == pro['name']:110 auth = pro111 if auth:112 if auth['mode'] == 'AK' and auth.get('access_key_id') and auth.get('access_key_secret'):113 params['alicloud_access_key'] = auth.get('access_key_id')114 params['alicloud_secret_key'] = auth.get('access_key_secret')115 params['alicloud_region'] = auth.get('region_id')116 params = get_acs_connection_info(params)117 elif auth['mode'] == 'StsToken' and auth.get('access_key_id') and auth.get('access_key_secret') and auth.get('sts_token'):118 params['alicloud_access_key'] = auth.get('access_key_id')119 params['alicloud_secret_key'] = auth.get('access_key_secret')120 params['security_token'] = auth.get('sts_token')121 params['alicloud_region'] = auth.get('region_id')122 params = get_acs_connection_info(params)123 elif auth['mode'] == 'EcsRamRole':124 params['ecs_role_name'] = auth.get('ram_role_name')125 params['alicloud_region'] = auth.get('region_id')126 params = get_acs_connection_info(params)127 elif auth['mode'] == 'RamRoleArn' and auth.get('ram_role_arn'):128 params['alicloud_access_key'] = auth.get('access_key_id')129 params['alicloud_secret_key'] = auth.get('access_key_secret')130 params['security_token'] = auth.get('sts_token')131 params['ecs_role_name'] = auth.get('ram_role_name')132 params['alicloud_assume_role_arn'] = auth.get('ram_role_arn')133 params['alicloud_assume_role_session_name'] = auth.get('ram_session_name')134 params['alicloud_assume_role_session_expiration'] = auth.get('expired_seconds')135 params['alicloud_region'] = auth.get('region_id')136 params = get_assume_role(params)137 elif params.get('alicloud_assume_role_arn') or params.get('assume_role'):138 params = get_assume_role(params)139 else:140 params = get_acs_connection_info(params)141 return params142def ecs_connect(module):143 """ Return an ecs connection"""144 ecs_params = get_profile(module.params)145 # If we have a region specified, connect to its endpoint.146 region = module.params.get('alicloud_region')147 if region:148 try:149 ecs = connect_to_acs(footmark.ecs, region, **ecs_params)150 except AnsibleACSError as e:151 module.fail_json(msg=str(e))152 # Otherwise, no region so we fallback to the old connection method...
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!!