def test_can_resolve_multiple_vars(self): assert self.resolve_vars( { 'foo': Variable('myvar'), 'bar': Variable('myvar') }, {'myvar': 'value'}) == { 'foo': 'value', 'bar': 'value' }
def test_can_plan_rest_api(self): function = create_function_resource('function_name') rest_api = models.RestAPI( resource_name='rest_api', swagger_doc={'swagger': '2.0'}, api_gateway_stage='api', lambda_function=function, ) plan = self.determine_plan(rest_api) self.assert_loads_needed_variables(plan) assert plan[4:] == [ models.APICall( method_name='import_rest_api', params={'swagger_document': { 'swagger': '2.0' }}, output_var='rest_api_id', ), models.RecordResourceVariable( resource_type='rest_api', resource_name='rest_api', name='rest_api_id', variable_name='rest_api_id', ), models.APICall(method_name='deploy_rest_api', params={ 'rest_api_id': Variable('rest_api_id'), 'api_gateway_stage': 'api' }), models.APICall( method_name='add_permission_for_apigateway_if_needed', params={ 'function_name': 'appname-dev-function_name', 'region_name': Variable('region_name'), 'account_id': Variable('account_id'), 'rest_api_id': Variable('rest_api_id'), }), models.StoreValue( name='rest_api_url', value=StringFormat( 'https://{rest_api_id}.execute-api.{region_name}' '.amazonaws.com/api/', ['rest_api_id', 'region_name'], ), ), models.RecordResourceVariable(resource_type='rest_api', resource_name='rest_api', name='rest_api_url', variable_name='rest_api_url'), ] assert list( self.last_plan.messages.values()) == ['Creating Rest API\n']
def test_deeply_nested(self): nested = { 'a': { 'b': { 'c': { 'd': [{ 'e': { 'f': Variable('foo') } }], } } } } variables = {'foo': 'value'} assert self.resolve_vars(nested, variables) == { 'a': { 'b': { 'c': { 'd': [{ 'e': { 'f': 'value' } }], } } } }
def test_can_resolve_nested_variable_refs(self): assert self.resolve_vars({'foo': { 'bar': Variable('myvar') }}, {'myvar': 'value'}) == { 'foo': { 'bar': 'value' } }
def test_can_plan_s3_event(self): function = create_function_resource('function_name') bucket_event = models.S3BucketNotification( resource_name='function_name-s3event', bucket='mybucket', events=['s3:ObjectCreated:*'], prefix=None, suffix=None, lambda_function=function, ) plan = self.determine_plan(bucket_event) self.assert_apicall_equals( plan[0], models.APICall( method_name='add_permission_for_s3_event', params={ 'bucket': 'mybucket', 'function_arn': Variable('function_name_lambda_arn'), }, )) self.assert_apicall_equals( plan[1], models.APICall( method_name='connect_s3_bucket_to_lambda', params={ 'bucket': 'mybucket', 'function_arn': Variable('function_name_lambda_arn'), 'events': ['s3:ObjectCreated:*'], 'prefix': None, 'suffix': None, }, )) assert plan[2] == models.RecordResourceValue( resource_type='s3_event', resource_name='function_name-s3event', name='bucket', value='mybucket', ) assert plan[3] == models.RecordResourceVariable( resource_type='s3_event', resource_name='function_name-s3event', name='lambda_arn', variable_name='function_name_lambda_arn', )
def test_can_plan_scheduled_event(self): function = create_function_resource('function_name') event = models.ScheduledEvent( resource_name='bar', rule_name='myrulename', schedule_expression='rate(5 minutes)', lambda_function=function, ) plan = self.determine_plan(event) assert len(plan) == 4 self.assert_apicall_equals( plan[0], models.APICall( method_name='get_or_create_rule_arn', params={ 'rule_name': 'myrulename', 'schedule_expression': 'rate(5 minutes)', }, output_var='rule-arn', )) self.assert_apicall_equals( plan[1], models.APICall(method_name='connect_rule_to_lambda', params={ 'rule_name': 'myrulename', 'function_arn': Variable('function_name_lambda_arn') })) self.assert_apicall_equals( plan[2], models.APICall( method_name='add_permission_for_scheduled_event', params={ 'rule_arn': Variable('rule-arn'), 'function_arn': Variable('function_name_lambda_arn'), }, )) assert plan[3] == models.RecordResourceValue( resource_type='cloudwatch_event', resource_name='bar', name='rule_name', value='myrulename', )
def test_can_call_builtin_function(self): self.execute([ StoreValue(name='my_arn', value='arn:aws:lambda:us-west-2:123:function:name'), BuiltinFunction( function_name='parse_arn', args=[Variable('my_arn')], output_var='result', ) ]) assert self.executor.variables['result'] == { 'account_id': '123', 'region': 'us-west-2', 'service': 'lambda' }
def test_can_reference_stored_results_in_api_calls(self): params = { 'name': Variable('role_name'), 'trust_policy': {'trust': 'policy'}, 'policy': {'iam': 'policy'} } call = APICall('create_role', params) self.mock_client.create_role.return_value = 'myrole:arn' self.executor.variables['role_name'] = 'myrole-name' self.execute([call]) self.mock_client.create_role.assert_called_with( name='myrole-name', trust_policy={'trust': 'policy'}, policy={'iam': 'policy'}, )
def assert_loads_needed_variables(self, plan): # Parse arn and store region/account id for future # API calls. assert plan[0:4] == [ models.BuiltinFunction( 'parse_arn', [Variable('function_name_lambda_arn')], output_var='parsed_lambda_arn', ), models.JPSearch('account_id', input_var='parsed_lambda_arn', output_var='account_id'), models.JPSearch('region', input_var='parsed_lambda_arn', output_var='region_name'), # Verify we copy the function arn as needed. models.CopyVariable(from_var='function_name_lambda_arn', to_var='api_handler_lambda_arn'), ]
def _delete_s3_event(self, resource_values): # type: (Dict[str, Any]) -> ResourceValueType bucket = resource_values['bucket'] function_arn = resource_values['lambda_arn'] return { 'instructions': ( models.BuiltinFunction('parse_arn', [function_arn], output_var='parsed_lambda_arn'), models.JPSearch('account_id', input_var='parsed_lambda_arn', output_var='account_id'), models.APICall( method_name='disconnect_s3_bucket_from_lambda', params={'bucket': bucket, 'function_arn': function_arn} ), models.APICall( method_name='remove_permission_for_s3_event', params={'bucket': bucket, 'function_arn': function_arn, 'account_id': Variable('account_id')} ), ) }
def test_can_resolve_vars_in_list(self): assert self.resolve_vars({'foo': [0, 1, Variable('myvar')]}, {'myvar': 2}) == { 'foo': [0, 1, 2] }
def test_resolve_top_level_vars(self): assert self.resolve_vars({'foo': Variable('myvar')}, {'myvar': 'value'}) == { 'foo': 'value' }
def test_can_update_rest_api(self): function = create_function_resource('function_name') rest_api = models.RestAPI( resource_name='rest_api', swagger_doc={'swagger': '2.0'}, api_gateway_stage='api', lambda_function=function, ) self.remote_state.declare_resource_exists(rest_api) self.remote_state.deployed_values['rest_api'] = { 'rest_api_id': 'my_rest_api_id', } plan = self.determine_plan(rest_api) self.assert_loads_needed_variables(plan) assert plan[4:] == [ models.StoreValue(name='rest_api_id', value='my_rest_api_id'), models.RecordResourceVariable( resource_type='rest_api', resource_name='rest_api', name='rest_api_id', variable_name='rest_api_id', ), models.APICall( method_name='update_api_from_swagger', params={ 'rest_api_id': Variable('rest_api_id'), 'swagger_document': { 'swagger': '2.0' }, }, ), models.APICall( method_name='deploy_rest_api', params={ 'rest_api_id': Variable('rest_api_id'), 'api_gateway_stage': 'api' }, ), models.APICall( method_name='add_permission_for_apigateway_if_needed', params={ 'function_name': 'appname-dev-function_name', 'region_name': Variable('region_name'), 'account_id': Variable('account_id'), 'rest_api_id': Variable('rest_api_id') }, ), models.APICall( method_name='add_permission_for_apigateway_if_needed', params={ 'rest_api_id': Variable("rest_api_id"), 'region_name': Variable("region_name"), 'account_id': Variable("account_id"), 'function_name': 'appname-dev-function_name' }, output_var=None), models.StoreValue( name='rest_api_url', value=StringFormat( 'https://{rest_api_id}.execute-api.{region_name}' '.amazonaws.com/api/', ['rest_api_id', 'region_name'], ), ), models.RecordResourceVariable(resource_type='rest_api', resource_name='rest_api', name='rest_api_url', variable_name='rest_api_url'), ]