Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all resources on a service resource. #60

Merged
merged 5 commits into from
Feb 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boto3/resources/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _load_has_relations(self, attrs, service_name, resource_name,
# This is a sub-resource class you can create
# by passing in an identifier, e.g. s3.Bucket(name).
name = subresource.resource.type
attrs[name] = self._create_class_partial(
attrs[subresource.name] = self._create_class_partial(
name, subresource, service_name, resource_name, model,
resource_defs, service_model)

Expand Down
65 changes: 64 additions & 1 deletion boto3/resources/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,69 @@ def batch_actions(self):

return actions

def _get_has_definition(self):
"""
Get a ``has`` relationship definition from a model, where the
service resource model is treated special in that it contains
a relationship to every resource defined for the service. This
allows things like ``s3.Object('bucket-name', 'key')`` to
work even though the JSON doesn't define it explicitly.

:rtype: dict
:return: Mapping of names to subresource and reference
definitions.
"""
if self.name not in self._resource_defs:
# This is the service resource, so let us expose all of
# the defined resources as subresources.
definition = {}

for name, resource_def in self._resource_defs.items():
# It's possible for the service to have renamed a
# resource or to have defined multiple names that
# point to the same resource type, so we need to
# take that into account.
found = False
has_items = self._definition.get('has', {}).items()
for has_name, has_def in has_items:
if has_def.get('resource', {}).get('type') == name:
definition[has_name] = has_def
found = True

if not found:
# Create a relationship definition and attach it
# to the model, such that all identifiers must be
# supplied by the user. It will look something like:
#
# {
# 'resource': {
# 'type': 'ResourceName',
# 'identifiers': [
# {'target': 'Name1', 'source': 'input'},
# {'target': 'Name2', 'source': 'input'},
# ...
# ]
# }
# }
#
fake_has = {
'resource': {
'type': name,
'identifiers': []
}
}

for identifier in resource_def.get('identifiers', []):
fake_has['resource']['identifiers'].append({
'target': identifier['name'], 'source': 'input'
})

definition[name] = fake_has
else:
definition = self._definition.get('has', {})

return definition

def _get_related_resources(self, subresources):
"""
Get a list of sub-resources or references.
Expand All @@ -323,7 +386,7 @@ def _get_related_resources(self, subresources):
"""
resources = []

for name, definition in self._definition.get('has', {}).items():
for name, definition in self._get_has_definition().items():
action = Action(name, definition, self._resource_defs)

data_required = False
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ def test_s3_resource_waiter(self):
obj.wait_until_exists()

# List objects and make sure ours is present
self.assertIn('test.txt', [o.key for o in bucket.objects.all()])
self.assertIn('test.txt', [o.key for o in bucket.objects.all()])

def test_can_create_object_directly(self):
obj = self.s3.Object(self.bucket_name, 'test.txt')

self.assertEqual(obj.bucket_name, self.bucket_name)
self.assertEqual(obj.key, 'test.txt')
61 changes: 61 additions & 0 deletions tests/unit/resources/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,64 @@ def test_dangling_resource_inequality(self):

self.assertNotEqual(q1, q2)
self.assertNotEqual(q1, m)


class TestServiceResourceSubresources(TestResourceFactory):
def setUp(self):
super(TestServiceResourceSubresources, self).setUp()

self.model = {
'has': {
'QueueObject': {
'resource': {
'type': 'Queue',
'identifiers': [
{'target': 'Url', 'source': 'input'}
]
}
},
'PriorityQueue': {
'resource': {
'type': 'Queue',
'identifiers': [
{'target': 'Url', 'source': 'input'}
]
}
}
}
}

self.defs = {
'Queue': {
'identifiers': [
{'name': 'Url'}
]
},
'Message': {
'identifiers': [
{'name': 'QueueUrl'},
{'name': 'ReceiptHandle'}
]
}
}

def test_subresource_custom_name(self):
resource = self.load('test', 'test', self.model, self.defs, None)()

self.assertTrue(hasattr(resource, 'QueueObject'))

def test_contains_all_subresources(self):
resource = self.load('test', 'test', self.model, self.defs, None)()

self.assertIn('QueueObject', dir(resource))
self.assertIn('PriorityQueue', dir(resource))
self.assertIn('Message', dir(resource))

def test_subresource_missing_all_subresources(self):
resource = self.load('test', 'test', self.model, self.defs, None)()
message = resource.Message('url', 'handle')

self.assertNotIn('QueueObject', dir(message))
self.assertNotIn('PriorityQueue', dir(message))
self.assertNotIn('Queue', dir(message))
self.assertNotIn('Message', dir(message))
13 changes: 11 additions & 2 deletions tests/unit/resources/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ def test_resource_batch_action(self):
def test_sub_resources(self):
model = ResourceModel('test', {
'has': {
'Frob': {
'RedFrob': {
'resource': {
'type': 'Frob',
'identifiers': [
{'target': 'Id', 'source': 'input'}
]
}
},
'GreenFrob': {
'resource': {
'type': 'Frob',
'identifiers': [
Expand All @@ -141,11 +149,12 @@ def test_sub_resources(self):
})

self.assertIsInstance(model.subresources, list)
self.assertEqual(len(model.subresources), 2)

action = model.subresources[0]
resource = action.resource

self.assertEqual(action.name, 'Frob')
self.assertIn(action.name, ['RedFrob', 'GreenFrob'])
self.assertEqual(resource.identifiers[0].target, 'Id')
self.assertEqual(resource.identifiers[0].source, 'input')
self.assertEqual(resource.type, 'Frob')
Expand Down