Exemplo n.º 1
0
def test_configurations():
    """Test for method for get, fetch, delete."""

    # region for our tests
    region = 'us-east-1'

    # create dummy ec2 instance so we can figure out account id
    client = boto3.client('ec2', region_name=region)
    client.run_instances(ImageId='ami-123abc', MinCount=1, MaxCount=5)

    # create a mock table
    mocks.create_dynamodb(region)
    ctx = utils.MockContext()

    # make sure we successfully created the table
    dynamodb = boto3.resource('dynamodb', region_name=region)
    table = dynamodb.Table('ebs_snapshot_configuration')
    assert table.table_status == "ACTIVE"

    # put some data in
    config_data = {
        "match": {
            "instance-id": "i-abc12345",
            "tag:plant": "special_flower",
            "tag:Name": "legacy_server"
        },
        "snapshot": {
            "retention": "6 days",
            "minimum": 6,
            "frequency": "13 hours"
        }
    }

    # put it in the table, be sure it succeeded
    response = dynamo.store_configuration(region, 'foo', '111122223333', config_data)
    assert response != {}

    # now list everything, be sure it was present
    fetched_configurations = dynamo.list_configurations(ctx, region)
    assert fetched_configurations == [config_data]

    # now get that specific one
    specific_config = dynamo.get_configuration(ctx, region, 'foo', '111122223333')
    assert specific_config == config_data

    # be sure another get for invalid item returns none
    missing_config = dynamo.get_configuration(ctx, region, 'abc', '111122223333')
    assert missing_config is None

    # be sure it returns in a list
    fetched_configurations = dynamo.list_ids(ctx, region)
    assert 'foo' in fetched_configurations

    # now delete it and confirm both list and get return nothing
    dynamo.delete_configuration(region, 'foo', '111122223333')
    specific_config = dynamo.get_configuration(ctx, region, 'foo', '111122223333')
    assert specific_config is None
    fetched_configurations = dynamo.list_configurations(ctx, region)
    assert fetched_configurations == []
Exemplo n.º 2
0
def shell_configure(*args):
    """Get, set, or delete configuration in DynamoDB."""

    # lazy retrieve the account id one way or another
    if args[0].aws_account_id is None:
        aws_account_id = utils.get_owner_id(CTX)[0]
    else:
        aws_account_id = args[0].aws_account_id
    LOG.debug("Account: %s", aws_account_id)

    object_id = args[0].object_id
    action = args[0].conf_action
    installed_region = args[0].conf_toolregion
    extra = args[0].extra

    if action == 'check':
        LOG.debug('Sanity checking configurations for %s', aws_account_id)
        findings = deploy.sanity_check(
            CTX,
            installed_region,
            aws_account_id=aws_account_id) or []

        if extra:
            prefix = '{},{}'.format(extra, aws_account_id)
        else:
            prefix = '{}'.format(aws_account_id)

        for f in findings:
            print("{}: {}".format(prefix, f))

    elif action == 'list':
        LOG.info('Listing all object keys for %s', aws_account_id)
        list_results = dynamo.list_ids(
            CTX,
            installed_region,
            aws_account_id=aws_account_id)
        if list_results is None or len(list_results) == 0:
            print('No configurations found')
        else:
            print("aws_account_id,id")
            for r in list_results:
                print("{},{}".format(aws_account_id, r))
    elif action == 'get':
        if object_id is None:
            raise Exception('must provide an object key id')
        else:
            LOG.debug("Object key: %s", object_id)

        LOG.info('Retrieving %s', args[0])

        single_result = dynamo.get_configuration(
            CTX,
            installed_region,
            object_id=object_id,
            aws_account_id=aws_account_id)
        if single_result is None:
            print('No configuration found')
        else:
            print(json.dumps(single_result))
    elif action == 'set':
        if object_id is None:
            raise Exception('must provide an object key id')
        else:
            LOG.debug("Object key: %s", object_id)

        config = json.loads(args[0].configuration_json)
        LOG.debug("Configuration: %s", config)
        dynamo.store_configuration(installed_region, object_id, aws_account_id, config)
        print('Saved to key {} under account {}'
              .format(object_id, aws_account_id))
    elif action == 'del':
        print(dynamo.delete_configuration(
            installed_region,
            object_id=object_id,
            aws_account_id=aws_account_id))
    else:
        # should never get here, from argparse
        raise Exception('invalid parameters', args)

    LOG.info('Function shell_configure completed')