Ejemplo n.º 1
0
 def remove_from_db(self, uid: str) -> bool:
     try:
         conn = Connection(host=self.model.Meta.host,
                           region=self.model.Meta.region)
         conn.delete_item(self.model.Meta.table_name,
                          hash_key=uid,
                          range_key=None)  # Note that range key is optional
         return True
     except DeleteError as e:
         logger.error("failed to remove user from db", uid=uid)
         return False
Ejemplo n.º 2
0
"""
Examples using a connection
"""
from pynamodb.connection import Connection

# Get a connection
conn = Connection(host='http://localhost:8000')
print(conn)

# List tables
print(conn.list_tables())

# Describe a table
print(conn.describe_table('Thread'))

# Get an item
print(conn.get_item('Thread', 'hash-key', 'range-key'))

# Put an item
conn.put_item('Thread',
              'hash-key',
              'range-key',
              attributes={
                  'forum_name': 'value',
                  'subject': 'value'
              })

# Delete an item
conn.delete_item('Thread', 'hash-key', 'range-key')
Ejemplo n.º 3
0
def test_connection_integration(ddb_url):
    table_name = 'pynamodb-ci-connection'

    # For use with a fake dynamodb connection
    # See: http://aws.amazon.com/dynamodb/developer-resources/
    conn = Connection(host=ddb_url)

    print(conn)
    print("conn.describe_table...")
    table = None
    try:
        table = conn.describe_table(table_name)
    except TableDoesNotExist:
        params = {
            'read_capacity_units':
            1,
            'write_capacity_units':
            1,
            'attribute_definitions': [{
                'attribute_type': STRING,
                'attribute_name': 'Forum'
            }, {
                'attribute_type': STRING,
                'attribute_name': 'Thread'
            }, {
                'attribute_type': STRING,
                'attribute_name': 'AltKey'
            }, {
                'attribute_type': NUMBER,
                'attribute_name': 'number'
            }],
            'key_schema': [{
                'key_type': HASH,
                'attribute_name': 'Forum'
            }, {
                'key_type': RANGE,
                'attribute_name': 'Thread'
            }],
            'global_secondary_indexes': [{
                'index_name':
                'alt-index',
                'key_schema': [{
                    'KeyType': 'HASH',
                    'AttributeName': 'AltKey'
                }],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                },
                'provisioned_throughput': {
                    'ReadCapacityUnits': 1,
                    'WriteCapacityUnits': 1,
                }
            }],
            'local_secondary_indexes': [{
                'index_name':
                'view-index',
                'key_schema': [{
                    'KeyType': 'HASH',
                    'AttributeName': 'Forum'
                }, {
                    'KeyType': 'RANGE',
                    'AttributeName': 'AltKey'
                }],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                }
            }]
        }
        print("conn.create_table...")
        conn.create_table(table_name, **params)

    while table is None:
        time.sleep(1)
        table = conn.describe_table(table_name)

    while table['TableStatus'] == 'CREATING':
        time.sleep(2)
        table = conn.describe_table(table_name)
    print("conn.list_tables")
    conn.list_tables()
    print("conn.update_table...")

    conn.update_table(table_name,
                      read_capacity_units=table.get(
                          PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
                      write_capacity_units=2)

    table = conn.describe_table(table_name)

    while table['TableStatus'] != 'ACTIVE':
        time.sleep(2)
        table = conn.describe_table(table_name)

    print("conn.put_item")
    conn.put_item(
        table_name,
        'item1-hash',
        range_key='item1-range',
        attributes={'foo': {
            'S': 'bar'
        }},
        condition=NotExists(Path('Forum')),
    )
    conn.get_item(table_name, 'item1-hash', range_key='item1-range')
    conn.delete_item(table_name, 'item1-hash', range_key='item1-range')

    items = []
    for i in range(10):
        items.append({"Forum": "FooForum", "Thread": f"thread-{i}"})
    print("conn.batch_write_items...")
    conn.batch_write_item(table_name, put_items=items)
    print("conn.batch_get_items...")
    data = conn.batch_get_item(table_name, items)
    print("conn.query...")
    conn.query(
        table_name,
        "FooForum",
        range_key_condition=(BeginsWith(Path('Thread'), Value('thread'))),
    )
    print("conn.scan...")
    conn.scan(table_name, )
    print("conn.delete_table...")
    conn.delete_table(table_name)
Ejemplo n.º 4
0
    def test_delete_item(self):
        """
        Connection.delete_item
        """
        conn = Connection(self.region)
        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
            conn.describe_table(self.test_table_name)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpBadRequest(), {}
            self.assertRaises(DeleteError, conn.delete_item, self.test_table_name, "foo", "bar")

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.delete_item(
                self.test_table_name,
                "Amazon DynamoDB",
                "How do I update multiple items?")
            params = {
                'return_consumed_capacity': 'TOTAL',
                'key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'table_name': self.test_table_name}
            self.assertEqual(req.call_args[1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.delete_item(
                self.test_table_name,
                "Amazon DynamoDB",
                "How do I update multiple items?",
                return_values='ALL_NEW'
            )
            params = {
                'return_consumed_capacity': 'TOTAL',
                'key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'table_name': self.test_table_name,
                'return_values': 'ALL_NEW'
            }
            self.assertEqual(req.call_args[1], params)

        self.assertRaises(
            ValueError,
            conn.delete_item,
            self.test_table_name,
            "foo",
            "bar",
            return_values='bad_values')

        self.assertRaises(
            ValueError,
            conn.delete_item,
            self.test_table_name,
            "foo",
            "bar",
            return_consumed_capacity='badvalue')

        self.assertRaises(
            ValueError,
            conn.delete_item,
            self.test_table_name,
            "foo",
            "bar",
            return_item_collection_metrics='badvalue')

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.delete_item(
                self.test_table_name,
                "Amazon DynamoDB",
                "How do I update multiple items?",
                return_consumed_capacity='TOTAL'
            )
            params = {
                'key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'table_name': self.test_table_name,
                'return_consumed_capacity': 'TOTAL'
            }
            self.assertEqual(req.call_args[1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.delete_item(
                self.test_table_name,
                "Amazon DynamoDB",
                "How do I update multiple items?",
                return_item_collection_metrics='SIZE'
            )
            params = {
                'key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'table_name': self.test_table_name,
                'return_item_collection_metrics': 'SIZE',
                'return_consumed_capacity': 'TOTAL'
            }
            self.assertEqual(req.call_args[1], params)

        self.assertRaises(
            ValueError,
            conn.delete_item,
            self.test_table_name,
            "Foo", "Bar",
            expected={'Bad': {'Value': False}}
        )

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.delete_item(
                self.test_table_name,
                "Amazon DynamoDB",
                "How do I update multiple items?",
                expected={'ForumName': {'Exists': False}},
                return_item_collection_metrics='SIZE'
            )
            params = {
                'key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'expected': {
                    'ForumName': {
                        'Exists': False
                    }
                },
                'table_name': self.test_table_name,
                'return_consumed_capacity': 'TOTAL',
                'return_item_collection_metrics': 'SIZE'
            }
            self.assertEqual(req.call_args[1], params)
Ejemplo n.º 5
0
"""
Examples using a connection
"""
from __future__ import print_function
from pynamodb.connection import Connection

# Get a connection
conn = Connection(host='http://localhost:8000')
print(conn)

# List tables
print(conn.list_tables())

# Describe a table
print(conn.describe_table('Thread'))

# Get an item
print(conn.get_item('Thread', 'hash-key', 'range-key'))

# Put an item
conn.put_item('Thread', 'hash-key', 'range-key', attributes={'forum_name': 'value', 'subject': 'value'})

# Delete an item
conn.delete_item('Thread', 'hash-key', 'range-key')
conn.update_table('table_name', **kwargs)

# Modifying items

# The low level API can perform item operations too, such as getting an item:

conn.get_item('table_name', 'hash_key', 'range_key')

# You can put items as well, specifying the keys and any other attributes:
conn.put_item('table_name',
              'hash_key',
              'range_key',
              attributes={'key': 'value'})

# Deleting an item has similar syntax:
conn.delete_item('table_name', 'hash_key', 'range_key')

# AWS Access
# https://pynamodb.readthedocs.io/en/latest/awsaccess.html
'''
PynamoDB uses botocore to interact with the DynamoDB API. Thus, any method of configuration supported by botocore works with PynamoDB. For local development the use of environment variables such as AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY is probably preferable. You can of course use IAM users, as recommended by AWS. In addition EC2 roles will work as well and would be recommended when running on EC2.

As for the permissions granted via IAM, many tasks can be carried out by PynamoDB. So you should construct your policies as required, see the DynamoDB docs for more information.

If for some reason you can’t use conventional AWS configuration methods, you can set the credentials in the Model Meta class:
'''
from pynamodb.models import Model


class MyModel(Model):
    class Meta:
Ejemplo n.º 7
0
print("conn.put_item")
conn.put_item(
    table_name,
    'item1-hash',
    range_key='item1-range',
    attributes={'foo': {'S': 'bar'}},
    expected={'Forum': {'Exists': False}}
)
conn.get_item(
    table_name,
    'item1-hash',
    range_key='item1-range'
)
conn.delete_item(
    table_name,
    'item1-hash',
    range_key='item1-range'
)

items = []
for i in range(10):
    items.append(
        {"Forum": "FooForum", "Thread": "thread-{0}".format(i)}
    )
print("conn.batch_write_items...")
conn.batch_write_item(
    table_name,
    put_items=items
)
print("conn.batch_get_items...")
data = conn.batch_get_item(
while table['TableStatus'] != 'ACTIVE':
    time.sleep(2)
    table = conn.describe_table(table_name)

print("conn.put_item")
conn.put_item(table_name,
              'item1-hash',
              range_key='item1-range',
              attributes={'foo': {
                  'S': 'bar'
              }},
              expected={'Forum': {
                  'Exists': False
              }})
conn.get_item(table_name, 'item1-hash', range_key='item1-range')
conn.delete_item(table_name, 'item1-hash', range_key='item1-range')

items = []
for i in range(10):
    items.append({"Forum": "FooForum", "Thread": "thread-{0}".format(i)})
print("conn.batch_write_items...")
conn.batch_write_item(table_name, put_items=items)
print("conn.batch_get_items...")
data = conn.batch_get_item(table_name, items)
print("conn.query...")
conn.query(table_name,
           "FooForum",
           key_conditions={
               'Thread': {
                   'ComparisonOperator': 'BEGINS_WITH',
                   'AttributeValueList': ['thread']