def test_update_rest_api_stage_without_patch_operations(self, *args):

        # Setup

        mock_stage = {'MockStage': ''}

        Custom_ServiceApi.api_gateway.get_stage.return_value = mock_stage

        # Execute

        Custom_ServiceApi.update_rest_api_stage(REST_API_ID, FULL_PROPS)

        # Verify

        kwargs = {
            'restApiId': REST_API_ID,
            'stageName': Custom_ServiceApi.STAGE_NAME,
        }

        Custom_ServiceApi.api_gateway.get_stage.assert_called_once_with(
            **kwargs)

        Custom_ServiceApi.get_rest_api_stage_update_patch_operations.assert_called_once_with(
            mock_stage, FULL_PROPS)

        self.assertFalse(Custom_ServiceApi.api_gateway.update_stage.called)
    def test_Update_with_minimal_properties(self, *args):

        # Setup
        id_data = {
            'AbstractRoleMappings': {
                LOGICAL_RESOURCE_ID: ROLE_NAME
            },
            'RestApiId': REST_API_ID
        }

        physical_resource_id = '{}-{}::{}'.format(
            MockResourceGroupInfo.MOCK_STACK_NAME, LOGICAL_RESOURCE_ID,
            json.dumps(id_data, sort_keys=True))

        event = {
            'RequestType': 'Update',
            'StackId': MockResourceGroupInfo.MOCK_STACK_ARN,
            'LogicalResourceId': LOGICAL_RESOURCE_ID,
            'PhysicalResourceId': physical_resource_id,
            'ResourceProperties': MINIMAL_RESOURCE_PROPERTIES
        }

        expected_tags = {
            constant.STACK_ID_TAG: MockResourceGroupInfo.MOCK_STACK_ARN,
            constant.PROJECT_NAME_TAG: MockResourceGroupInfo.MOCK_PROJECT_NAME
        }

        context = {}

        expected_data = {'Url': EXPECTED_URL}

        # Execute

        Custom_ServiceApi.handler(event, context)

        # Validate

        role_utils.get_access_control_role_arn.assert_called_once_with(
            id_data, LOGICAL_RESOURCE_ID)

        Custom_ServiceApi.get_configured_swagger_content.assert_called_once_with(
            RESOURCE_GROUP_INFO, PROPS_MATCHER, ROLE_ARN,
            REST_API_RESOURCE_NAME)

        Custom_ServiceApi.update_api_gateway.assert_called_once_with(
            REST_API_ID, PROPS_MATCHER, SWAGGER_CONTENT)

        custom_resource_response.success_response.assert_called_once_with(
            expected_data, physical_resource_id)

        Custom_ServiceApi.update_api_gateway_tags.assert_called_once_with(
            REST_API_ID, event, expected_tags)

        Custom_ServiceApi.register_service_interfaces.assert_called_once_with(
            RESOURCE_GROUP_INFO,
            'https://{rest_api_id}.execute-api.{region}.amazonaws.com/{stage_name}'
            .format(rest_api_id=REST_API_ID,
                    region=RESOURCE_GROUP_INFO.region,
                    stage_name=Custom_ServiceApi.STAGE_NAME), SWAGGER_CONTENT)
    def test_Create_with_full_properties(self, *args):

        # Setup
        event = {
            'RequestType': 'Create',
            'StackId': MockResourceGroupInfo.MOCK_STACK_ARN,
            'LogicalResourceId': LOGICAL_RESOURCE_ID,
            'ResourceProperties': FULL_RESOURCE_PROPERTIES
        }

        expected_tags = {
            constant.STACK_ID_TAG: MockResourceGroupInfo.MOCK_STACK_ARN,
            constant.PROJECT_NAME_TAG: MockResourceGroupInfo.MOCK_PROJECT_NAME
        }

        context = {}

        expected_data = {'Url': EXPECTED_URL}

        # Execute

        with mock.patch('resource_manager_common.stack_info.StackInfoManager',
                        return_value=STACK_MANAGER) as mock_stack_info_manager:
            Custom_ServiceApi.handler(event, context)

            # Validate

            role_utils.create_access_control_role.assert_called_once_with(
                mock_stack_info_manager.return_value,
                {'RestApiId': REST_API_ID},
                MockResourceGroupInfo.MOCK_STACK_ARN, LOGICAL_RESOURCE_ID,
                Custom_ServiceApi.API_GATEWAY_SERVICE_NAME)

            Custom_ServiceApi.get_configured_swagger_content.assert_called_once_with(
                RESOURCE_GROUP_INFO, PROPS_MATCHER, ROLE_ARN,
                REST_API_RESOURCE_NAME)

            Custom_ServiceApi.create_api_gateway.assert_called_once_with(
                REST_API_RESOURCE_NAME, PROPS_MATCHER, SWAGGER_CONTENT)

            Custom_ServiceApi.update_api_gateway_tags.assert_called_once_with(
                REST_API_ID, event, expected_tags)

            expected_physical_resource_id = '{}-{}::{}'.format(
                MockResourceGroupInfo.MOCK_STACK_NAME, LOGICAL_RESOURCE_ID,
                json.dumps({"RestApiId": REST_API_ID}))

            custom_resource_response.success_response.assert_called_once_with(
                expected_data, expected_physical_resource_id)

            Custom_ServiceApi.register_service_interfaces.assert_called_once_with(
                RESOURCE_GROUP_INFO,
                'https://{rest_api_id}.execute-api.{region}.amazonaws.com/{stage_name}'
                .format(rest_api_id=REST_API_ID,
                        region=RESOURCE_GROUP_INFO.region,
                        stage_name=Custom_ServiceApi.STAGE_NAME),
                SWAGGER_CONTENT)
    def test_delete_api_gateway(self, *args):

        # Execute

        Custom_ServiceApi.delete_api_gateway(REST_API_ID)

        # Verify

        Custom_ServiceApi.delete_rest_api.assert_called_once_with(REST_API_ID)
Example #5
0
    def test_Delete(self, *args):

        # Setup

        id_data = {
            'AbstractRoleMappings': {
                LOGICAL_RESOURCE_ID: ROLE_NAME
            },
            'RestApiId': REST_API_ID
        }

        physical_resource_id = '{}-{}::{}'.format(
            MockResourceGroupInfo.MOCK_STACK_NAME, LOGICAL_RESOURCE_ID,
            json.dumps(id_data, sort_keys=True))

        event = {
            'RequestType': 'Delete',
            'StackId': MockResourceGroupInfo.MOCK_STACK_ARN,
            'LogicalResourceId': LOGICAL_RESOURCE_ID,
            'PhysicalResourceId': physical_resource_id
        }

        context = {}

        expected_data = {}

        # Execute

        Custom_ServiceApi.handler(event, context)

        # Validate

        role_utils.delete_access_control_role.assert_called_once_with(
            {'AbstractRoleMappings': {
                LOGICAL_RESOURCE_ID: ROLE_NAME
            }}, LOGICAL_RESOURCE_ID)

        Custom_ServiceApi.delete_api_gateway(REST_API_ID)

        expected_physical_resource_id = '{}-{}::{}'.format(
            MockResourceGroupInfo.MOCK_STACK_NAME, LOGICAL_RESOURCE_ID,
            json.dumps(
                {'AbstractRoleMappings': {
                    LOGICAL_RESOURCE_ID: ROLE_NAME
                }},
                sort_keys=True))

        custom_resource_response.success_response.assert_called_once_with(
            expected_data, expected_physical_resource_id)

        Custom_ServiceApi.unregister_service_interfaces.assert_called_once_with(
            RESOURCE_GROUP_INFO,
            'https://{rest_api_id}.execute-api.{region}.amazonaws.com/{stage_name}'
            .format(rest_api_id=REST_API_ID,
                    region=RESOURCE_GROUP_INFO.region,
                    stage_name=Custom_ServiceApi.STAGE_NAME))
    def test_delete_rest_api(self, *args):

        # Execute

        Custom_ServiceApi.delete_rest_api(REST_API_ID)

        # Verify

        kwargs = {'restApiId': REST_API_ID}

        Custom_ServiceApi.api_gateway.delete_rest_api(**kwargs)
    def test_update_api_case_path_mappings(self, *args):

        # Setup

        Custom_ServiceApi.api_gateway.get_domain_names.return_value = {
            'position': '',
            'items': [{
                'domainName': CUSTOM_DOMAIN_NAME_A
            }]
        }

        # Execute

        response = Custom_ServiceApi.update_api_case_path_mappings(
            REST_API_ID, MockResourceGroupInfo.MOCK_REGION,
            CUSTOM_DOMAIN_NAME_B)

        # Verify
        args = ()
        Custom_ServiceApi.api_gateway.get_domain_names.assert_called_once_with(
            *args)

        args = (REST_API_ID, MockResourceGroupInfo.MOCK_REGION,
                CUSTOM_DOMAIN_NAME_A)
        Custom_ServiceApi.delete_api_case_path_mappings.assert_called_once_with(
            *args)

        args = (REST_API_ID, MockResourceGroupInfo.MOCK_REGION,
                CUSTOM_DOMAIN_NAME_B)
        Custom_ServiceApi.add_api_case_path_mappings.assert_called_once_with(
            *args)

        self.assertEqual(response, CUSTOM_DOMAIN_NAME_A)
    def test_import_rest_api(self, *args):

        # Execute

        Custom_ServiceApi.import_rest_api(SWAGGER_CONTENT)

        # Verify

        kwargs = {
            "failOnWarnings": True,
            "body": SWAGGER_CONTENT,
            'baseBackoff': 1.5,
            'maxBackoff': 90.0
        }

        Custom_ServiceApi.api_gateway.import_rest_api.assert_called_once_with(
            **kwargs)
    def test_put_rest_api(self, *args):

        # Execute

        Custom_ServiceApi.put_rest_api(REST_API_ID, SWAGGER_CONTENT)

        # Verify

        kwargs = {
            "failOnWarnings": True,
            "body": SWAGGER_CONTENT,
            "mode": "overwrite",
            "restApiId": REST_API_ID
        }

        Custom_ServiceApi.api_gateway.put_rest_api.assert_called_once_with(
            **kwargs)
    def test_create_rest_api_deployment(self, *args):

        # Execute

        Custom_ServiceApi.create_rest_api_deployment(REST_API_ID,
                                                     SWAGGER_DIGEST)

        # Verify

        kwargs = {
            'restApiId': REST_API_ID,
            'description': SWAGGER_DIGEST,
            'stageName': Custom_ServiceApi.STAGE_NAME
        }

        Custom_ServiceApi.api_gateway.create_deployment.assert_called_once_with(
            **kwargs)
    def test_configure_swagger_content(self, *args):
        # Execute
        result = Custom_ServiceApi.configure_swagger_content(
            RESOURCE_GROUP_INFO, self.PROPS, ROLE_ARN, REST_API_RESOURCE_NAME,
            self.INPUT_SWAGGER_CONTENT)

        # Verify
        self.assertEqual(self.OUTPUT_SWAGGER_CONTENT, result)
Example #12
0
    def test_create_api_gateway(self, *args):

        # Execute

        result = Custom_ServiceApi.create_api_gateway(FULL_PROPS,
                                                      SWAGGER_CONTENT)

        # Verify

        Custom_ServiceApi.import_rest_api.assert_called_once_with(
            SWAGGER_CONTENT)

        Custom_ServiceApi.create_rest_api_deployment.assert_called_once_with(
            REST_API_ID, SWAGGER_DIGEST)

        Custom_ServiceApi.update_rest_api_stage(REST_API_ID, FULL_PROPS)

        self.assertEquals(REST_API_ID, result)
    def test_update_api_gateway_without_swagger_changes(self, *args):

        # Execute

        Custom_ServiceApi.update_api_gateway(REST_API_ID, FULL_PROPS,
                                             SWAGGER_CONTENT)

        # Verify

        Custom_ServiceApi.get_rest_api_deployment_id.assert_called_once_with(
            REST_API_ID)

        Custom_ServiceApi.detect_swagger_changes.assert_called_once_with(
            REST_API_ID, REST_API_DEPLOYMENT_ID, SWAGGER_CONTENT)

        Custom_ServiceApi.update_rest_api_stage.assert_called_once_with(
            REST_API_ID, FULL_PROPS)

        self.assertFalse(Custom_ServiceApi.api_gateway.put_rest_api.called)
    def test_get_rest_api_deployment_id(self, *args):

        # Setup

        Custom_ServiceApi.api_gateway.get_stage.return_value = {
            'deploymentId': REST_API_DEPLOYMENT_ID
        }

        # Execute

        Custom_ServiceApi.get_rest_api_deployment_id(REST_API_ID)

        # Verify

        kwargs = {
            'restApiId': REST_API_ID,
            'stageName': Custom_ServiceApi.STAGE_NAME
        }

        Custom_ServiceApi.api_gateway.get_stage.assert_called_once_with(
            **kwargs)
    def test_get_input_swagger_content(self, *args):
        # Setup
        Custom_ServiceApi.s3.get_object.return_value = mock_aws.s3_get_object_response(
            self.INPUT_SWAGGER_CONTENT.encode())

        # Execute
        result = Custom_ServiceApi.get_input_swagger_content(self.PROPS)

        # Verify
        Custom_ServiceApi.s3.get_object.assert_called_once_with(
            Bucket=CONFIGURATION_BUCKET, Key=INPUT_SWAGGER_KEY)

        self.assertEqual(self.INPUT_SWAGGER_CONTENT, result)
    def test_register_service_interfaces(self, *args):

        interface_id = 'test-interface-id'
        interface_metadata = {
            'basePath': '/base-path',
            'paths': ['path'],
            'definitions': ['definition']
        }
        stack_info = mock.MagicMock()
        stack_info.is_project_stack = False
        service_url = 'https://host:port/path'
        swagger = {
            service_interface.INTERFACE_METADATA_OBJECT_NAME: {
                interface_id: interface_metadata
            }
        }
        swagger_content = json.dumps(swagger)

        Custom_ServiceApi.register_service_interfaces(stack_info, service_url,
                                                      swagger_content)

        Custom_ServiceApi.get_interface_swagger.assert_called_once_with(
            swagger, interface_id, interface_metadata['basePath'],
            interface_metadata['paths'], interface_metadata['definitions'])

        Custom_ServiceApi.get_service_directory.assert_called_once_with(
            stack_info)

        Custom_ServiceApi.get_service_directory.return_value.put_service_interfaces.assert_called_once_with(
            stack_info.deployment.deployment_name, service_url, [{
                'InterfaceId':
                interface_id,
                'InterfaceUrl':
                service_url + interface_metadata['basePath'],
                'InterfaceSwagger':
                Custom_ServiceApi.get_interface_swagger.return_value
            }])
    def test_detect_swagger_changes_without_changes(self, *args):

        # Setup

        Custom_ServiceApi.api_gateway.get_deployment.return_value = {
            'description': SWAGGER_DIGEST_A
        }

        # Execute

        result = Custom_ServiceApi.detect_swagger_changes(
            REST_API_ID, REST_API_DEPLOYMENT_ID, SWAGGER_CONTENT)

        # Verify

        self.assertIsNone(result)
    def test_delete_api_case_path_mappings(self, *args):

        # Execute

        response = Custom_ServiceApi.delete_api_case_path_mappings(
            REST_API_ID, MockResourceGroupInfo.MOCK_REGION,
            CUSTOM_DOMAIN_NAME_A)

        # Verify
        base_path = '{region}.{stage_name}.{rest_api_id}'.format(
            region=MockResourceGroupInfo.MOCK_REGION,
            stage_name=Custom_ServiceApi.STAGE_NAME,
            rest_api_id=REST_API_ID)

        kwargs = {'domainName': CUSTOM_DOMAIN_NAME_A, 'basePath': base_path}

        Custom_ServiceApi.api_gateway.get_base_path_mapping.assert_called_once_with(
            **kwargs)

        Custom_ServiceApi.api_gateway.delete_base_path_mapping.assert_called_once_with(
            **kwargs)

        self.assertTrue(response)
    def test_add_api_case_path_mappings(self, *args):

        # Setup
        Custom_ServiceApi.api_gateway.get_base_path_mapping.side_effect = ClientError(
            {'Error': {
                'Code': 'NotFoundException'
            }}, '')

        # Execute

        response = Custom_ServiceApi.add_api_case_path_mappings(
            REST_API_ID, MockResourceGroupInfo.MOCK_REGION,
            CUSTOM_DOMAIN_NAME_A)

        # Verify
        base_path = '{region}.{stage_name}.{rest_api_id}'.format(
            region=MockResourceGroupInfo.MOCK_REGION,
            stage_name=Custom_ServiceApi.STAGE_NAME,
            rest_api_id=REST_API_ID)

        kwargs = {'domainName': CUSTOM_DOMAIN_NAME_A, 'basePath': base_path}

        Custom_ServiceApi.api_gateway.get_base_path_mapping.assert_called_once_with(
            **kwargs)

        kwargs = {
            'domainName': CUSTOM_DOMAIN_NAME_A,
            'basePath': base_path,
            'restApiId': REST_API_ID,
            'stage': Custom_ServiceApi.STAGE_NAME
        }

        Custom_ServiceApi.api_gateway.create_base_path_mapping.assert_called_once_with(
            **kwargs)

        self.assertTrue(response)
    def __test_Create_with_minimal_properties(self, custom_domain_name=''):

        # Setup
        event = {
            'RequestType': 'Create',
            'StackId': MockResourceGroupInfo.MOCK_STACK_ARN,
            'LogicalResourceId': LOGICAL_RESOURCE_ID,
            'ResourceProperties': MINIMAL_RESOURCE_PROPERTIES
        }

        expected_tags = {
            constant.STACK_ID_TAG: MockResourceGroupInfo.MOCK_STACK_ARN,
            constant.PROJECT_NAME_TAG: MockResourceGroupInfo.MOCK_PROJECT_NAME
        }

        context = {}

        expected_data = {
            'Url':
            EXPECTED_ALTERNATIVE_URL if custom_domain_name else EXPECTED_URL
        }

        # Execute

        with mock.patch('resource_manager_common.stack_info.StackInfoManager',
                        return_value=STACK_MANAGER) as mock_stack_info_manager:

            # Setup

            os.environ['CustomDomainName'] = custom_domain_name

            Custom_ServiceApi.handler(event, context)

            # Validate

            role_utils.create_access_control_role.assert_called_once_with(
                mock_stack_info_manager.return_value,
                {'RestApiId': REST_API_ID},
                MockResourceGroupInfo.MOCK_STACK_ARN, LOGICAL_RESOURCE_ID,
                Custom_ServiceApi.API_GATEWAY_SERVICE_NAME)

            Custom_ServiceApi.get_configured_swagger_content.assert_called_once_with(
                RESOURCE_GROUP_INFO, PROPS_MATCHER, ROLE_ARN,
                REST_API_RESOURCE_NAME)

            Custom_ServiceApi.create_api_gateway.assert_called_once_with(
                REST_API_RESOURCE_NAME, PROPS_MATCHER, SWAGGER_CONTENT)

            expected_physical_resource_id = MockResourceGroupInfo.MOCK_STACK_NAME + '-' + LOGICAL_RESOURCE_ID + '::{"RestApiId": "TestRestApiId"}'

            custom_resource_response.success_response.assert_called_once_with(
                expected_data, expected_physical_resource_id)

            Custom_ServiceApi.update_api_gateway_tags.assert_called_once_with(
                REST_API_ID, event, expected_tags)

            if custom_domain_name:
                Custom_ServiceApi.register_service_interfaces.assert_called_once_with(
                    RESOURCE_GROUP_INFO,
                    'https://{custom_domain_name}/{region}.{stage_name}.{rest_api_id}'
                    .format(custom_domain_name=custom_domain_name,
                            region=RESOURCE_GROUP_INFO.region,
                            stage_name=Custom_ServiceApi.STAGE_NAME,
                            rest_api_id=REST_API_ID), SWAGGER_CONTENT)
            else:
                Custom_ServiceApi.register_service_interfaces.assert_called_once_with(
                    RESOURCE_GROUP_INFO,
                    'https://{rest_api_id}.execute-api.{region}.amazonaws.com/{stage_name}'
                    .format(rest_api_id=REST_API_ID,
                            region=RESOURCE_GROUP_INFO.region,
                            stage_name=Custom_ServiceApi.STAGE_NAME),
                    SWAGGER_CONTENT)