Skip to content

Commit

Permalink
Add batch actions and waiters to resource model.
Browse files Browse the repository at this point in the history
This adds definitions to the internal resource model to handle batch actions
and waiters, both of which are recent additions to the resource JSON format.
This change is a prerequisite to adding support for these features in the
resource class factory. Tests are added for the new model features.
  • Loading branch information
danielgtaylor committed Nov 24, 2014
1 parent 65c7973 commit 496008b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
71 changes: 66 additions & 5 deletions boto3/resources/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ def __init__(self, name, definition, resource_defs):
self.path = definition.get('path')


class Request(object):

class ItemWithParameters(object):
"""
A service operation action request.
An item which has parameters exposed via the ``params`` property.
A request has an operation and parameters, while a waiter has
a name, a low-level waiter name and parameters.
:type definition: dict
:param definition: The JSON definition
"""
def __init__(self, definition):
self._definition = definition

#: (``string``) The name of the low-level service operation
self.operation = definition.get('operation')

@property
def params(self):
"""
Expand Down Expand Up @@ -121,6 +121,39 @@ def __init__(self, target, source_type, source):
self.source = source


class Request(ItemWithParameters):
"""
A service operation action request.
:type definition: dict
:param definition: The JSON definition
"""
def __init__(self, definition):
super(Request, self).__init__(definition)

#: (``string``) The name of the low-level service operation
self.operation = definition.get('operation')


class Waiter(ItemWithParameters):
"""
An event waiter specification.
:type name: string
:param name: Name of the waiter
:type definition: dict
:param definition: The JSON definition
"""
def __init__(self, name, definition):
super(Waiter, self).__init__(definition)

#: (``string``) The name of this waiter
self.name = name

#: (``string``) The name of the underlying event waiter
self.waiter_name = definition.get('waiterName')


class ResponseResource(object):
"""
A resource response to create after performing an action.
Expand Down Expand Up @@ -284,6 +317,20 @@ def actions(self):

return actions

@property
def batch_actions(self):
"""
Get a list of batch actions for this resource.
:type: list(:py:class:`Action`)
"""
actions = []

for name, item in self._definition.get('batchActions', {}).items():
actions.append(Action(name, item, self._resource_defs))

return actions

@property
def references(self):
"""
Expand Down Expand Up @@ -355,3 +402,17 @@ def collections(self):
collections.append(Collection(name, item, self._resource_defs))

return collections

@property
def waiters(self):
"""
Get a list of waiters for this resource.
:type: list(:py:class:`Waiter`)
"""
waiters = []

for name, item in self._definition.get('waiters', {}).items():
waiters.append(Waiter(name, item))

return waiters
1 change: 1 addition & 0 deletions docs/source/reference/core/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Resource Model
.. automodule:: boto3.resources.model
:members:
:undoc-members:
:inherited-members:

Request Parameters
------------------
Expand Down
45 changes: 44 additions & 1 deletion tests/unit/resources/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# language governing permissions and limitations under the License.

from boto3.resources.model import ResourceModel, Action, SubResourceList,\
Collection
Collection, Waiter
from tests import BaseTestCase


Expand Down Expand Up @@ -101,6 +101,28 @@ def test_resource_load_action(self):
self.assertEqual(model.load.request.operation, 'GetFrobInfo')
self.assertEqual(model.load.path, '$')

def test_resource_batch_action(self):
model = ResourceModel('test', {
'batchActions': {
'Delete': {
'request': {
'operation': 'DeleteObjects',
'params': [
{'target': 'Bucket', 'sourceType': 'identifier',
'source': 'BucketName'}
]
}
}
}
}, {})

self.assertIsInstance(model.batch_actions, list)

action = model.batch_actions[0]
self.assertIsInstance(action, Action)
self.assertEqual(action.request.operation, 'DeleteObjects')
self.assertEqual(action.request.params[0].target, 'Bucket')

def test_sub_resources(self):
model = ResourceModel('test', {
'subResources': {
Expand Down Expand Up @@ -223,3 +245,24 @@ def test_resource_collections(self):
self.assertEqual(model.collections[0].resource.type, 'Frob')
self.assertEqual(model.collections[0].resource.model.name, 'Frob')
self.assertEqual(model.collections[0].path, 'FrobList[]')

def test_waiter(self):
model = ResourceModel('test', {
'waiters': {
'Exists': {
'waiterName': 'ObjectExists',
'params': [
{'target': 'Bucket', 'sourceType': 'identifier',
'source': 'BucketName'}
]
}
}
}, {})

self.assertIsInstance(model.waiters, list)

waiter = model.waiters[0]
self.assertIsInstance(waiter, Waiter)
self.assertEqual(waiter.name, 'Exists')
self.assertEqual(waiter.waiter_name, 'ObjectExists')
self.assertEqual(waiter.params[0].target, 'Bucket')

0 comments on commit 496008b

Please sign in to comment.