Proxy create method

This change introduces a common create API that can be applied to every
proxy method that revolves around creating a resource. In particular,
the change applies that base method to creating a server.

The method takes the attributes to create a resource and then calls the
create method on it.

As with the other similar changes to add base proxy methods, the
duplicate verification test will be removed once this API is applied to
the rest of the service proxies.

Change-Id: Ie718af8e81ec942c6d1dfa0f0b05becec88849d8
This commit is contained in:
Brian Curtin
2015-03-26 13:21:13 -05:00
parent 3c95ae8186
commit c7ca54b012
5 changed files with 65 additions and 5 deletions

View File

@@ -144,8 +144,17 @@ class Proxy(proxy.BaseProxy):
"""
return limits.Limits().get(self.session)
def create_server(self, **data):
return server.Server(data).create(self.session)
def create_server(self, **attrs):
"""Create a new server from attributes
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.compute.v2.server.Server`,
comprised of the properties on the Server class.
:returns: The results of server creation
:rtype: :class:`~openstack.compute.v2.server.Server`
"""
return self._create(server.Server, **attrs)
def delete_server(self, value, ignore_missing=True):
"""Delete a server

View File

@@ -102,3 +102,18 @@ class BaseProxy(object):
res = resource_type.existing(id=resource.Resource.get_id(value))
res.update_attrs(attrs)
return res.update(self.session)
def _create(self, resource_type, **attrs):
"""Create a resource from attributes
:param resource_type: The type of resource to create.
:type resource_type: :class:`~openstack.resource.Resource`
:param **attrs: Attributes from which to create a Resource object.
These attributes will be used in conjunction with
``resource_type``.
:returns: The result of the ``create``
:rtype: :class:`~openstack.resource.Resource`
"""
res = resource_type.new(**attrs)
return res.create(self.session)

View File

@@ -162,9 +162,13 @@ class TestComputeProxy(test_proxy_base.TestProxyBase):
self.verify_list('openstack.compute.v2.server_ip.ServerIP.list',
self.proxy.list_server_ips)
def test_server_create(self):
self.verify_create('openstack.compute.v2.server.Server.create',
self.proxy.create_server)
def test_server_create_attrs(self):
kwargs = {"x": 1, "y": 2, "z": 3}
self.verify_create2('openstack.proxy.BaseProxy._create',
self.proxy.create_server,
method_kwargs=kwargs,
expected_args=[server.Server],
expected_kwargs=kwargs)
def test_server_delete(self):
self.verify_delete2(server.Server, self.proxy.delete_server, False)

View File

@@ -26,6 +26,10 @@ class UpdateableResource(resource.Resource):
allow_update = True
class CreateableResource(resource.Resource):
allow_create = True
class Test_check_resource(testtools.TestCase):
def setUp(self):
@@ -165,3 +169,27 @@ class TestProxyUpdate(testtools.TestCase):
def test_update_id(self):
self._test_update(self.fake_id)
class TestProxyCreate(testtools.TestCase):
def setUp(self):
super(TestProxyCreate, self).setUp()
self.session = mock.Mock()
self.fake_result = "fake_result"
self.res = mock.Mock(spec=CreateableResource)
self.res.create = mock.Mock(return_value=self.fake_result)
self.sot = proxy.BaseProxy(self.session)
def test_create_attributes(self):
CreateableResource.new = mock.Mock(return_value=self.res)
attrs = {"x": 1, "y": 2, "z": 3}
rv = self.sot._create(CreateableResource, **attrs)
self.assertEqual(rv, self.fake_result)
CreateableResource.new.assert_called_once_with(**attrs)
self.res.create.assert_called_once_with(self.session)

View File

@@ -73,6 +73,10 @@ class TestProxyBase(base.TestCase):
self._verify(mock_method, test_method, expected_result="result",
**kwargs)
def verify_create2(self, mock_method, test_method, **kwargs):
self._verify2(mock_method, test_method, expected_result="result",
**kwargs)
def verify_delete(self, mock_method, test_method, **kwargs):
self._verify(mock_method, test_method, **kwargs)