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 1 commit
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
62 changes: 61 additions & 1 deletion boto3/resources/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,66 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the @rtype: notation?

@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, so we need to take that into account and
# search for the same resource type.
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
break
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this else coming from? It does not look in line with any if statement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's from the for loop. If it never breaks, then the else is executed. I've since removed it to simplify the code and fix a potential case I hadn't considered so it should now be easier to understand.

# 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 +383,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
51 changes: 51 additions & 0 deletions tests/unit/resources/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,54 @@ 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'}
]
}
}
}
}

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('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('Queue', dir(message))
self.assertNotIn('Message', dir(message))