def test_failed_with_physical_resource_id(self):

        event = {
            'StackId': 'test-stack-id',
            'RequestId': 'test-request-id',
            'LogicalResourceId': 'test-logical-resource-id',
            'PhysicalResourceId': 'test-physical-resource-id',
            'ResponseURL': 'https://test-host/test-path'
        }

        context = {
        }

        reason = 'test-reason'

        with mock.patch('httplib.HTTPSConnection') as mock_HTTPSConnection:

            mock_connection = mock_HTTPSConnection.return_value
            mock_getresponse = mock_connection.getresponse

            mock_response = mock.MagicMock()
            mock_response.status = httplib.OK
            mock_getresponse.return_value = mock_response

            mock_request = mock_connection.request

            custom_resource_response.fail(event, context, reason)

            mock_HTTPSConnection.assert_called_with('test-host')
            mock_request.assert_called_with('PUT', '/test-path', '{"Status": "FAILED", "StackId": "test-stack-id", "Reason": "test-reason", "PhysicalResourceId": "test-physical-resource-id", "RequestId": "test-request-id", "LogicalResourceId": "test-logical-resource-id"}')
def handler(event, context):
    try:
        handler = handlers.get(event['ResourceType'], unknown_handler)   
        handler(event, context)
    except ValidationError as e:
        custom_resource_response.fail(event, context, str(e))
    except Exception as e:
        print 'Unexpected error occured when processing event {} with context {}. {}'.format(event, context, traceback.format_exc())
        custom_resource_response.fail(event, context, 'Unexpected error occured. Details can be found in the CloudWatch log group {} stream {}'.format(
            context.log_group_name,
            context.log_stream_name))
Esempio n. 3
0
def handler(event, context):
    try:
        handler = handlers.get(event['ResourceType'], unknown_handler)
        handler(event, context)
    except ValidationError as e:
        custom_resource_response.fail(event, context, str(e))
    except Exception as e:
        print 'Unexpected error occured when processing event {} with context {}. {}'.format(
            event, context, traceback.format_exc())
        custom_resource_response.fail(
            event, context,
            'Unexpected error occured. Details can be found in the CloudWatch log group {} stream {}'
            .format(context.log_group_name, context.log_stream_name))
Esempio n. 4
0
    def test_retry(self):

        event = {
            'StackId': 'test-stack-id',
            'RequestId': 'test-request-id',
            'LogicalResourceId': 'test-logical-resource-id',
            'PhysicalResourceId': 'test-physical-resource-id',
            'ResponseURL': 'https://test-host/test-path'
        }

        context = {}

        reason = 'test-reason'

        with mock.patch('httplib.HTTPSConnection') as mock_HTTPSConnection:

            mock_connection = mock_HTTPSConnection.return_value
            mock_getresponse = mock_connection.getresponse

            mock_failed_response = mock.MagicMock()
            mock_failed_response.status = httplib.INTERNAL_SERVER_ERROR
            mock_ok_response = mock.MagicMock()
            mock_ok_response.status = httplib.OK
            mock_exception_response = RuntimeError('test')
            mock_getresponse.side_effect = [
                mock_failed_response, mock_exception_response, mock_ok_response
            ]

            mock_request = mock_connection.request

            custom_resource_response.fail(event, context, reason)

            mock_HTTPSConnection.assert_called_with('test-host')
            mock_HTTPSConnection.assert_called_with('test-host')
            mock_HTTPSConnection.assert_called_with('test-host')
            self.assertEquals(mock_HTTPSConnection.call_count, 3)

            mock_request.assert_called_with(
                'PUT', '/test-path',
                '{"Status": "FAILED", "StackId": "test-stack-id", "Reason": "test-reason", "PhysicalResourceId": "test-physical-resource-id", "RequestId": "test-request-id", "LogicalResourceId": "test-logical-resource-id"}'
            )
            mock_request.assert_called_with(
                'PUT', '/test-path',
                '{"Status": "FAILED", "StackId": "test-stack-id", "Reason": "test-reason", "PhysicalResourceId": "test-physical-resource-id", "RequestId": "test-request-id", "LogicalResourceId": "test-logical-resource-id"}'
            )
            mock_request.assert_called_with(
                'PUT', '/test-path',
                '{"Status": "FAILED", "StackId": "test-stack-id", "Reason": "test-reason", "PhysicalResourceId": "test-physical-resource-id", "RequestId": "test-request-id", "LogicalResourceId": "test-logical-resource-id"}'
            )
            self.assertEquals(mock_request.call_count, 3)
Esempio n. 5
0
def unknown_handler(event, context):
    custom_resource_response.fail(
        event, context,
        'Unknown resource type {}'.format(event['ResourceType']))
Esempio n. 6
0
def handler(event, context):
    try:

        resource_type = event.get('ResourceType', None)
        if resource_type is None:
            raise RuntimeError('No ResourceType specified.')

        module_name = resource_type.replace('Custom::', '') + 'ResourceHandler'

        module = sys.modules.get(module_name, None)

        if module is None:

            # First check for handler module in same directory as this module,
            # if not found, check for module in the resource group provided
            # directories.

            module_file_name = module_name + '.py'
            module_file_path = os.path.join(os.path.dirname(__file__),
                                            module_file_name)

            if os.path.isfile(module_file_path):

                module = __load_module(module_name,
                                       os.path.dirname(module_file_path))

            elif os.path.isdir(PLUGIN_DIRECTORY_PATH):

                plugin_directory_names = [
                    item for item in os.listdir(PLUGIN_DIRECTORY_PATH)
                    if os.path.isdir(os.path.join(PLUGIN_DIRECTORY_PATH, item))
                ]

                for plugin_directory_name in plugin_directory_names:
                    module_file_path = os.path.join(PLUGIN_DIRECTORY_PATH,
                                                    plugin_directory_name,
                                                    module_file_name)
                    if os.path.isfile(module_file_path):
                        module = __load_module(
                            module_name, os.path.dirname(module_file_path))
                        break

        if module is None:
            raise RuntimeError(
                'No handler module found for the {} resource type.'.format(
                    resource_type))

        if not hasattr(module, 'handler'):
            raise RuntimeError(
                'No handler function found for the {} resource type.'.format(
                    resource_type))

        print 'Using {}'.format(module)

        module.handler(event, context)

    except ValidationError as e:
        custom_resource_response.fail(event, context, str(e))
    except Exception as e:
        print 'Unexpected error occured when processing event {} with context {}. {}'.format(
            event, context, traceback.format_exc())
        custom_resource_response.fail(
            event, context,
            'Unexpected error occured. Details can be found in the CloudWatch log group {} stream {}'
            .format(context.log_group_name, context.log_stream_name))
def unknown_handler(event, context):
    custom_resource_response.fail(event, context, 'Unknown resource type {}'.format(event['ResourceType']))