Merge pull request #1 from jistr/ftr/racks_python_api

Racks Python API and _get in base.Manager
This commit is contained in:
Petr Blaho 2013-07-11 06:28:41 -07:00
commit 280016ff6f
6 changed files with 121 additions and 0 deletions

View File

@ -52,6 +52,13 @@ class Manager(object):
if body:
return self.resource_class(self, body)
def _get(self, url, **kwargs):
kwargs.setdefault('expect_single', True)
try:
return self._list(url, **kwargs)[0]
except IndexError:
return None
def _list(self, url, response_key=None, obj_class=None, body=None,
expect_single=False):
resp, body = self.api.json_request('GET', url)

View File

View File

@ -0,0 +1,44 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from tuskarclient.tests import utils as tutils
from tuskarclient.common import base
class ManagerTest(tutils.TestCase):
def setUp(self):
super(ManagerTest, self).setUp()
self.api = mock.Mock()
self.m = base.Manager(self.api)
def test_get(self):
self.m._list = mock.Mock(return_value=['fake_resource'])
got = self.m._get('url', response_key='response_key',
obj_class='obj_class', body='body')
self.assertEqual('fake_resource', got)
self.m._list.assert_called_with('url', response_key='response_key',
obj_class='obj_class',
body='body', expect_single=True)
def test_get_nonexistent(self):
self.m._list = mock.Mock(return_value=[])
got = self.m._get('url', response_key='response_key',
obj_class='obj_class', body='body')
self.assertEqual(None, got)
self.m._list.assert_called_with('url', response_key='response_key',
obj_class='obj_class',
body='body', expect_single=True)

View File

View File

@ -0,0 +1,36 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from tuskarclient.v1_0 import racks
import tuskarclient.tests.utils as tutils
class RackManagerTest(tutils.TestCase):
def setUp(self):
super(RackManagerTest, self).setUp()
self.api = mock.Mock()
self.rm = racks.RackManager(self.api)
def test_get(self):
self.rm._get = mock.Mock(return_value='fake_rack')
self.assertEqual(self.rm.get(42), 'fake_rack')
self.rm._get.assert_called_with('/v1/racks/42')
def test_list(self):
self.rm._list = mock.Mock(return_value=['fake_rack'])
self.assertEqual(self.rm.list(), ['fake_rack'])
self.rm._list.assert_called_with('/v1/racks')

View File

@ -0,0 +1,34 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from tuskarclient.common import base
class Rack(base.Resource):
def __repr__(self):
return "<Rack {0}>".format(self._info)
class RackManager(base.Manager):
resource_class = Rack
@staticmethod
def _path(id=None):
return '/v1/racks/%s' % id if id else '/v1/racks'
def get(self, rack_id):
return self._get(self._path(rack_id))
def list(self):
return self._list(self._path())