Generate the Blockade API endpoints.

def generate_api_resources():
    """Generate the Blockade API endpoints."""
    client = boto3.client('apigateway', region_name=PRIMARY_REGION)
    matches = [x for x in client.get_rest_apis().get('items', list())
               if x['name'] == API_GATEWAY]
    match = matches.pop()

    iam = boto3.resource('iam')
    account_id = iam.CurrentUser().arn.split(':')[4]

    for label in API_GATEWAY_RESOURCES:
        schema = API_GATEWAY_RESOURCE_SCHEMA[label]
        existing = get_api_gateway_resource(schema['resource']['path'])
        if existing:
            logger.debug("[#] API resource %s already created" % (label))
            continue

        resource_id = get_api_gateway_resource('/')
        if schema['admin']:
            resource_id = get_api_gateway_resource('admin')

        logger.debug("[#] Adding %s endpoint" % (label))
        schema = API_GATEWAY_RESOURCE_SCHEMA[label]

        logger.debug("[#] Creating %s resource" % (schema['resource']['path']))
        resource = client.create_resource(
            restApiId=match.get('id'),
            parentId=resource_id,
            pathPart=schema['resource']['path']
        )
        logger.debug("[#] Created %s resource" % (schema['resource']['path']))

        logger.debug("[#] Creating %s method" % (schema['resource']['method']))
        method = client.put_method(
            restApiId=match.get('id'),
            resourceId=resource.get('id'),
            httpMethod=schema['resource']['method'],
            authorizationType='NONE'
        )
        logger.debug("[#] Created %s method" % (schema['resource']['method']))

        logger.debug("[#] Creating %s integration" % (label))
        uri = "arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/"
        uri += "arn:aws:lambda:{region}:{id}:function:{func}/invocations"
        integration = client.put_integration(
            restApiId=match.get('id'),
            resourceId=resource.get('id'),
            httpMethod=schema['resource']['method'],
            type='AWS',
            integrationHttpMethod='POST',
            uri=uri.format(id=account_id, region=PRIMARY_REGION, func=label),
            requestTemplates=schema['request']['template'],
            passthroughBehavior='WHEN_NO_TEMPLATES'
        )
        logger.debug("[#] Created %s integration" % (label))

        logger.debug("[#] Creating %s response method" % (label))
        method_reponse = client.put_method_response(
            restApiId=match.get('id'),
            resourceId=resource.get('id'),
            httpMethod=schema['resource']['method'],
            statusCode='200'
        )
        logger.debug("[#] Created %s response method" % (label))

        logger.debug("[#] Creating %s integration response" % (label))
        integration_response = client.put_integration_response(
            restApiId=match.get('id'),
            resourceId=resource.get('id'),
            httpMethod=schema['resource']['method'],
            statusCode='200',
            responseTemplates=schema['response']['template']
        )
        logger.debug("[#] Created %s integration response" % (label))

        logger.debug("[#] Generating %s permissions" % (label))
        aws_lambda = boto3.client('lambda', region_name=PRIMARY_REGION)

        if not schema['admin']:
            arn = "arn:aws:execute-api:{region}:{account_id}:{rest_id}/*/{method}/{path}"
        else:
            arn = "arn:aws:execute-api:{region}:{account_id}:{rest_id}/*/{method}/admin/{path}"
        aws_lambda.add_permission(
            FunctionName=label,
            StatementId='BlockadeBootstrap-%d' % (random.randint(0, 9999)),
            Action='lambda:InvokeFunction',
            Principal='apigateway.amazonaws.com',
            SourceArn=arn.format(region=PRIMARY_REGION, account_id=account_id,
                                 rest_id=match.get('id'),
                                 method=schema['resource']['method'],
                                 path=schema['resource']['path'])
        )
        logger.debug("[#] Generated %s permissions" % (label))

    logger.debug("[#] Creating production deployment")
    deployment = client.create_deployment(
        restApiId=match.get('id'),
        stageName='prod'
    )
    url = "https://{rest_id}.execute-api.{region}.amazonaws.com/prod/"
    url = url.format(rest_id=match.get('id'), region=PRIMARY_REGION)
    logger.debug("[#] Deployment is accessible: %s" % (url))

    return url