Beispiel #1
0
def _ensure_lambda_permissions(client_lambda, lmbda, api):
    if not lmbda.get('arn'):
        lambda_name = lmbda.get('name', '(no name provided)')
        print('Lambda function {} could not be found'.format(lambda_name))
        return

    lambda_arn = lmbda.get('arn')
    lambda_alias = lmbda.get('alias')
    lambda_name = lmbda.get('name')

    lambda_region, lambda_account_id = \
        _get_region_and_account_from_lambda_arn(lambda_arn)

    source_arn = 'arn:aws:execute-api:{region}:{accountId}:{apiId}/*/*'.format(
        region=lambda_region, accountId=lambda_account_id, apiId=api['id'])

    if _invoke_lambda_permission_exists(client_lambda, lambda_arn, source_arn):
        print('API already has permission to invoke lambda {}'.format(
            lambda_name))
        return

    print('Adding lambda permission for API Gateway for lambda {}'.format(
        lambda_name))
    _sleep()
    response = client_lambda.add_permission(FunctionName=lambda_name,
                                            StatementId=str(uuid.uuid1()),
                                            Action=INVOKE_FUNCTION_ACTION,
                                            Principal=AMAZON_API_PRINCIPAL,
                                            SourceArn=source_arn,
                                            Qualifier=lambda_alias)

    print(json2table(json.loads(response['Statement'])))
Beispiel #2
0
def list_stacks(awsclient):
    """Print out the list of stacks deployed at AWS cloud.

    :param awsclient:
    :return:
    """
    client_cf = awsclient.get_client('cloudformation')
    response = client_cf.list_stacks(
        StackStatusFilter=[
            'CREATE_IN_PROGRESS', 'CREATE_COMPLETE', 'ROLLBACK_IN_PROGRESS',
            'ROLLBACK_COMPLETE', 'DELETE_IN_PROGRESS', 'DELETE_FAILED',
            'UPDATE_IN_PROGRESS', 'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS',
            'UPDATE_COMPLETE', 'UPDATE_ROLLBACK_IN_PROGRESS',
            'UPDATE_ROLLBACK_FAILED',
            'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS',
            'UPDATE_ROLLBACK_COMPLETE',
        ]
    )
    result = {}
    stack_sum = 0
    for summary in response['StackSummaries']:
        result['StackName'] = summary["StackName"]
        result['CreationTime'] = summary['CreationTime']
        result['StackStatus'] = summary['StackStatus']
        print(json2table(result))
        stack_sum += 1
    print('listed %s stacks' % str(stack_sum))
Beispiel #3
0
def export_to_swagger(awsclient,
                      api_name,
                      stage_name,
                      api_description,
                      lambdas,
                      custom_hostname=False,
                      custom_base_path=False):
    """Export the API design as swagger file. 
    
    :param api_name: 
    :param stage_name: 
    :param api_description: 
    :param lambdas: 
    :param custom_hostname: 
    :param custom_base_path: 
    """
    print('Exporting to swagger...')

    api = _api_by_name(awsclient, api_name)
    if api is not None:

        print(json2table(api))
        api_id = api['id']
        client_api = awsclient.get_client('apigateway')
        template_variables = _template_variables_to_dict(
            client_api, api_name, api_description, stage_name, api_id, lambdas,
            custom_hostname, custom_base_path)
        content = _compile_template(SWAGGER_FILE, template_variables)
        swagger_file = open('swagger_export.yaml', 'w')

        swagger_file.write(content)
    else:
        print('API name unknown')
Beispiel #4
0
def _create_deployment(awsclient,
                       api_name,
                       stage_name,
                       cache_cluster_enabled=False,
                       cache_cluster_size='0.5'):
    client_api = awsclient.get_client('apigateway')
    print('create deployment')

    api = _api_by_name(awsclient, api_name)

    if api is not None:
        request = {
            'restApiId': api['id'],
            'stageName': stage_name,
            'description': 'TO BE FILLED',
            'cacheClusterEnabled': cache_cluster_enabled
        }
        if cache_cluster_enabled:
            request['cacheClusterSize'] = cache_cluster_size
        _sleep()
        response = client_api.create_deployment(**request)

        print(json2table(response))
    else:
        print('API name unknown')
Beispiel #5
0
def list_apis(awsclient):
    """List APIs in account."""
    client_api = awsclient.get_client('apigateway')

    apis = client_api.get_rest_apis()['items']

    for api in apis:
        print(json2table(api))
Beispiel #6
0
def test_json2table_exception():
    data = json.dumps({
        'sth': 'here',
        'number': 1.1,
        'ResponseMetadata': 'bla'
    })
    actual = json2table(data)
    assert actual == data
Beispiel #7
0
def _create_api(awsclient, api_name, api_description):
    client_api = awsclient.get_client('apigateway')

    print('creating API')

    response = client_api.create_rest_api(name=api_name,
                                          description=api_description)

    print(json2table(response))
Beispiel #8
0
def delete_api(awsclient, api_name):
    """Delete the API.

    :param api_name:
    """
    _sleep()
    client_api = awsclient.get_client('apigateway')

    print('deleting api: %s' % api_name)
    api = _api_by_name(awsclient, api_name)

    if api is not None:
        print(json2table(api))

        response = client_api.delete_rest_api(restApiId=api['id'])

        print(json2table(response))
    else:
        print('API name unknown')
Beispiel #9
0
def list_api_keys(awsclient):
    """Print the defined API keys.
    """
    _sleep()
    client_api = awsclient.get_client('apigateway')
    print('listing api keys')

    response = client_api.get_api_keys()['items']

    for item in response:
        print(json2table(item))
Beispiel #10
0
def delete_api_key(awsclient, api_key):
    """Remove API key.

    :param api_key:
    """
    _sleep()
    client_api = awsclient.get_client('apigateway')
    print('delete api key: %s' % api_key)

    response = client_api.delete_api_key(apiKey=api_key)

    print(json2table(response))
Beispiel #11
0
def _lambda_add_s3_event_source(awsclient, arn, event, bucket, prefix, suffix):
    """Use only prefix OR suffix

    :param arn:
    :param event:
    :param bucket:
    :param prefix:
    :param suffix:
    :return:
    """
    json_data = {
        'LambdaFunctionConfigurations': [{
            'LambdaFunctionArn': arn,
            'Id': str(uuid.uuid1()),
            'Events': [event]
        }]
    }

    filter_rules = build_filter_rules(prefix, suffix)

    json_data['LambdaFunctionConfigurations'][0].update(
        {'Filter': {
            'Key': {
                'FilterRules': filter_rules
            }
        }})
    # http://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-notification-configuration.html
    # http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
    client_s3 = awsclient.get_client('s3')

    bucket_configurations = client_s3.get_bucket_notification_configuration(
        Bucket=bucket)
    bucket_configurations.pop('ResponseMetadata')

    if 'LambdaFunctionConfigurations' in bucket_configurations:
        bucket_configurations['LambdaFunctionConfigurations'].append(
            json_data['LambdaFunctionConfigurations'][0])
    else:
        bucket_configurations['LambdaFunctionConfigurations'] = json_data[
            'LambdaFunctionConfigurations']

    response = client_s3.put_bucket_notification_configuration(
        Bucket=bucket, NotificationConfiguration=bucket_configurations)
    # TODO don't return a table, but success state
    return json2table(response)
Beispiel #12
0
def _import_from_swagger(awsclient, api_name, api_description, stage_name,
                         lambdas):
    client_api = awsclient.get_client('apigateway')

    print('Import from swagger file')

    api = _api_by_name(awsclient, api_name)
    if api is None:
        #print(json2table(api))
        api_id = False
        template_variables = _template_variables_to_dict(
            client_api, api_name, api_description, stage_name, api_id, lambdas)
        swagger_body = _compile_template(SWAGGER_FILE, template_variables)
        _sleep()
        response_swagger = client_api.import_rest_api(failOnWarnings=True,
                                                      body=swagger_body)
        print(json2table(response_swagger))
    else:
        print('API already taken')
Beispiel #13
0
def describe_change_set(awsclient, change_set_name, stack_name):
    """Print out the change_set to console.
    This needs to run create_change_set first.

    :param awsclient:
    :param change_set_name:
    :param stack_name:
    """
    client = awsclient.get_client('cloudformation')

    status = None
    while status not in ['CREATE_COMPLETE', 'FAILED']:
        response = client.describe_change_set(
            ChangeSetName=change_set_name,
            StackName=stack_name)
        status = response['Status']
        # print('##### %s' % status)
        if status == 'FAILED':
            print(response['StatusReason'])
        elif status == 'CREATE_COMPLETE':
            for change in response['Changes']:
                print(json2table(change['ResourceChange']))
Beispiel #14
0
def test_json2table_create_lambda_response():
    response = OrderedDict([
        ('CodeSha256', 'CwEvufZaAmNgUnlA6yTJGi8p8MNR+mNcCNYPOIwsTNM='),
        ('FunctionName', 'jenkins-gcdt-lifecycle-for-ramuda'),
        ('CodeSize', 430078), ('MemorySize', 256),
        ('FunctionArn',
         'arn:aws:lambda:eu-west-1:644239850139:function:jenkins-gcdt-lifecycle-for-ramuda'
         ), ('Version', '13'),
        ('Role',
         'arn:aws:iam::644239850139:role/lambda/dp-dev-store-redshift-cdn-lo-LambdaCdnRedshiftLoad-DD2S84CZFGT4'
         ), ('Timeout', 300), ('LastModified', '2016-08-23T15:27:07.658+0000'),
        ('Handler', 'handler.handle'), ('Runtime', 'python2.7'),
        ('Description', 'lambda test for ramuda')
    ])

    expected_file = here('resources/expected/expected_json2table.txt')
    with open(expected_file) as efile:
        expected = efile.read()
        if not PY3:
            expected = expected.decode('utf-8')
    actual = json2table(response)  # .encode('utf-8')
    assert actual == expected
Beispiel #15
0
def _wire_api_key(awsclient, api_name, api_key, stage_name):
    client_api = awsclient.get_client('apigateway')
    print('updating api key')

    api = _api_by_name(awsclient, api_name)

    if api is not None:
        _sleep()
        response = client_api.update_api_key(apiKey=api_key,
                                             patchOperations=[
                                                 {
                                                     'op':
                                                     'add',
                                                     'path':
                                                     '/stages',
                                                     'value':
                                                     "%s/%s" %
                                                     (api['id'], stage_name)
                                                 },
                                             ])

        print(json2table(response))
    else:
        print('API name unknown')
Beispiel #16
0
def _update_from_swagger(awsclient, api_name, api_description, stage_name,
                         lambdas):
    client_api = awsclient.get_client('apigateway')

    print('update from swagger file')

    api = _api_by_name(awsclient, api_name)

    if api is not None:
        api_id = api['id']
        template_variables = _template_variables_to_dict(
            client_api, api_name, api_description, stage_name, api_id, lambdas)
        filled_swagger_file = _compile_template(SWAGGER_FILE,
                                                template_variables)

        _sleep()
        response_swagger = client_api.put_rest_api(restApiId=api['id'],
                                                   mode='overwrite',
                                                   failOnWarnings=True,
                                                   body=filled_swagger_file)
    else:
        print('API name unknown')

    print(json2table(response_swagger))
Beispiel #17
0
def test_json2table():
    data = {'sth': 'here', 'number': 1.1, 'ResponseMetadata': 'bla'}
    expected = u'\u2552\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2564\u2550\u2550\u2550\u2550\u2550\u2550\u2555\n\u2502 sth    \u2502 here \u2502\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n\u2502 number \u2502 1.1  \u2502\n\u2558\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2567\u2550\u2550\u2550\u2550\u2550\u2550\u255b'
    actual = json2table(data)
    assert actual == expected