Example #1
0
    def test_get_batch(self):

        # get from non existing table
        get_requests = dynamodb_consts.batch_get_config(
            str(uuid.uuid1()), dynamodb_test_consts.PRIMARY_KEY_NAME,
            ['a', 'b'])
        self.assertRaises(dynamodb_exceptions.BatchGetException,
                          self._dynamodb_wrapper.batch_get, get_requests)

        # get non existing key from existing table
        table_name = self._create_table_and_verify_its_existness()
        get_requests = dynamodb_consts.batch_get_config(
            table_name, dynamodb_test_consts.PRIMARY_KEY_NAME,
            [str(uuid.uuid1())])
        self.assertEqual(len(self._get_response_content(get_requests)), 0)

        # get an existing key
        self._dynamodb_wrapper.batch_put(
            table_name, dynamodb_test_consts.VALID_ITEMS_TO_PUT)
        items_to_get = list(
            dynamodb_test_consts.VALID_ITEMS_TO_PUT[0].values())
        items_to_get.append(str(uuid.uuid1()))

        get_requests = dynamodb_consts.batch_get_config(
            table_name, dynamodb_test_consts.PRIMARY_KEY_NAME, items_to_get)
        get_output = self._get_response_content(get_requests)[0]
        self.assertEqual(
            get_output[dynamodb_test_consts.PRIMARY_KEY_NAME],
            dynamodb_test_consts.VALID_ITEMS_TO_PUT[0][
                dynamodb_test_consts.PRIMARY_KEY_NAME])
Example #2
0
    def test_put_item(self):
        primary_key_name = 'Y'
        number_value = 10
        item_to_put = {
            dynamodb_test_consts.PRIMARY_KEY_NAME: primary_key_name,
            'num': number_value
        }

        # Put item without condition
        self.assertRaises(dynamodb_exceptions.PutItemException,
                          self._dynamodb_wrapper.put_item, str(uuid.uuid1()),
                          item_to_put)

        # Put item without condition
        table_name = self._create_table_and_verify_its_existness()
        self._dynamodb_wrapper.put_item(table_name, item_to_put)

        get_requests = dynamodb_consts.batch_get_config(
            table_name, dynamodb_test_consts.PRIMARY_KEY_NAME,
            [primary_key_name])
        self.assertDictEqual(
            self._get_response_content(get_requests)[0], item_to_put)

        self._dynamodb_wrapper.put_item(
            table_name,
            item_to_put,
            condition=dynamodb_conditions.Condition.is_equal(
                'num', number_value))
        self.assertDictEqual(
            self._get_response_content(get_requests)[0], item_to_put)

        self.assertRaises(
            dynamodb_exceptions.PutItemException,
            self._dynamodb_wrapper.put_item, table_name, item_to_put,
            dynamodb_conditions.Condition.is_equal('num', number_value * 12))
Example #3
0
    def test_delete_batch(self):
        keys_to_delete = dynamodb_test_consts.VALID_ITEMS_TO_PUT
        self.assertRaises(dynamodb_exceptions.BatchDeleteException,
                          self._dynamodb_wrapper.batch_delete,
                          str(uuid.uuid1()), keys_to_delete)

        table_name = self._create_table_and_verify_its_existness()

        # Test no existing keys from existing table
        self._dynamodb_wrapper.batch_delete(table_name, keys_to_delete)

        # Test deletion existing keys from existing tables
        items_to_put = copy.copy(dynamodb_test_consts.VALID_ITEMS_TO_PUT)
        items_to_put[0]['b'] = 'bb'
        self._dynamodb_wrapper.batch_put(table_name, items_to_put)
        get_requests = dynamodb_consts.batch_get_config(
            table_name, dynamodb_test_consts.PRIMARY_KEY_NAME,
            items_to_put[0].values())

        pre_deletion_get_response_content = self._get_response_content(
            get_requests)
        for response in pre_deletion_get_response_content:
            self.assertIn(response, items_to_put)

        # test deletion with partly existing keys
        partly_existing_keys = []
        for key_value in [
                str(uuid.uuid1()),
                items_to_put[0][dynamodb_test_consts.PRIMARY_KEY_NAME]
        ]:
            partly_existing_keys.append(
                {dynamodb_test_consts.PRIMARY_KEY_NAME: key_value})

        self._dynamodb_wrapper.batch_delete(table_name, partly_existing_keys)
        self.assertEqual(0, len(self._get_response_content(get_requests)))
Example #4
0
    def get_current_ref_count(self, unique_id, routing_type):
        routing_id_value = self.get_formatted_routing_id(
            unique_id, routing_type)
        batch_get_config = dynamodb_consts.batch_get_config(
            self._table_name,
            ref_count_config_consts.REF_COUNT_TABLE_PRIMARY_KEY_NAME,
            [routing_id_value])
        for response in self._dynamodb_wrapper.batch_get(
                batch_get_config)['Responses'][self._table_name]:
            if response[ref_count_config_consts.
                        REF_COUNT_TABLE_PRIMARY_KEY_NAME] == routing_id_value:
                return int(response[
                    ref_count_config_consts.RefCountTableConfig.counter.value])

        self._logger.log_and_raise(ref_count_exceptions.CounterNotFound,
                                   table_name=self._table_name,
                                   primary_key=ref_count_config_consts.
                                   REF_COUNT_TABLE_PRIMARY_KEY_NAME,
                                   routing_id=routing_id_value)
Example #5
0
    def test_put_batch(self):
        items_to_put = copy.copy(dynamodb_test_consts.VALID_ITEMS_TO_PUT)
        new_key_name = 'b'
        items_to_put[0][new_key_name] = 'bb'

        # put in non exists table
        self.assertRaises(dynamodb_exceptions.BatchPutException,
                          self._dynamodb_wrapper.batch_put, str(uuid.uuid1()),
                          items_to_put)

        table_name = self._create_table_and_verify_its_existness()
        self._dynamodb_wrapper.batch_put(table_name, items_to_put)
        get_requests = dynamodb_consts.batch_get_config(
            table_name, dynamodb_test_consts.PRIMARY_KEY_NAME,
            [dynamodb_test_consts.DEFAULT_PRIMARY_KEY_VALUE, new_key_name])
        output = self._get_response_content(get_requests)
        self.assertEqual(len(output), 1)
        original_b_value = output[0][new_key_name]

        # Test update an existing key
        items_to_put[0][new_key_name] = 'bla'
        self._dynamodb_wrapper.batch_put(table_name, items_to_put)
        updated_b_value = self._get_response_content(
            get_requests)[0][new_key_name]
        self.assertNotEqual(original_b_value, updated_b_value)
        self.assertEqual(updated_b_value, items_to_put[0][new_key_name])

        self.assertRaises(
            dynamodb_exceptions.BatchPutException,
            self._dynamodb_wrapper.batch_put, table_name,
            dynamodb_test_consts.ITEMS_TO_PUT_WITHOUT_PRIMARY_KEY)

        self.assertRaises(
            dynamodb_exceptions.BatchPutException,
            self._dynamodb_wrapper.batch_put, table_name,
            dynamodb_test_consts.ITEMS_TO_PUT_WITH_MISMATCH_PRIMARY_KEY_VALUE)
Example #6
0
def hello_world(aws_access_key=None,
                aws_secret_key=None,
                region_name=Regions.n_virginia.value):
    logger = taco.logger.logger.get_logger(name='hello_world')

    ssm = ssm_wrapper.SSMWrapper(logger=logger)
    if aws_secret_key is None:
        aws_secret_key = ssm.aws_secret_key

    if aws_access_key is None:
        aws_access_key = ssm.aws_access_key

    wrappers_default_kwargs = {
        'logger': logger,
        'aws_access_key': aws_access_key,
        'aws_secret_key': aws_secret_key,
        'region_name': region_name
    }

    sqs = sqs_wrapper.SQSWrapper(**wrappers_default_kwargs)
    s3 = s3_wrapper.S3Wrapper(**wrappers_default_kwargs)
    dynamo = dynamodb_wrapper.DynamoDBWrapper(**wrappers_default_kwargs)

    # SQS create queue
    queue_name = 'queue_name'
    creating_sqs_config = sqs_configs.SQSCreationConfig(
        queue_name, enable_dead_letter_queue=True, force_clean_queue=False)
    sqs.create_queue(creating_sqs_config)

    # SQS send message
    sqs.send_message(
        queue_name,
        data='with all this romantic atmosphere Disaster\'s in the air')

    # read a message
    read_message = sqs.read_message(queue_name=queue_name,
                                    delete_after_receive=True)

    # S3 create bucket
    bucket_name = str(uuid.uuid1().hex)
    s3.create_bucket(bucket_name, region_name=Regions.ohio.value)

    # upload the data to s3
    file_path = 'hello_world'
    s3.upload_data_to_file(bucket_name=bucket_name,
                           file_path=file_path,
                           data=read_message.data)

    # update file metadata
    new_metadata = {'a': 'b'}
    s3.update_file_metadata(bucket_name=bucket_name,
                            file_path=file_path,
                            new_metadata=new_metadata)

    print(s3.get_file_data(bucket_name=bucket_name, file_path=file_path))
    print(s3.get_file_metadata(bucket_name=bucket_name, file_path=file_path))

    # DynamoDB create table
    table_name = 'table_name'
    primary_key_name = 'primary_key_name'
    attribute_definitions = [
        dynamodb_consts.property_schema(
            primary_key_name, dynamodb_consts.AttributeTypes.string_type.value)
    ]
    primary_keys = [
        dynamodb_consts.property_schema(
            primary_key_name, dynamodb_consts.PrimaryKeyTypes.hash_type.value)
    ]
    table_config = table_creation_config.TableCreationConfig(
        table_name=table_name,
        primary_keys=primary_keys,
        attribute_definitions=attribute_definitions)
    dynamo.create_table(table_config)

    # Add raw to table
    primary_key_value = 'primary_key_value'
    item_to_put = {
        primary_key_name: primary_key_value,
        'another random key': 25
    }
    dynamo.put_item(table_name, item_to_put)

    # Read from Dynamo
    get_requests = dynamodb_consts.batch_get_config(table_name,
                                                    primary_key_name,
                                                    [primary_key_value])
    print(dynamo.batch_get(get_requests))

    # delete the bucket and the sqs
    s3.delete_bucket(bucket_name=bucket_name, delete_non_empty_bucket=True)
    sqs.delete_queue(queue_name=queue_name)
    dynamo.delete_table(table_name=table_name)