From 72b1862c97afbfc5bcd90a2568afc0c7aacf4d79 Mon Sep 17 00:00:00 2001 From: Chris Yeoh Date: Wed, 4 Dec 2013 23:09:57 +1030 Subject: [PATCH] Adds flavor access support for Nova V3 API Adds the remaining flavors support for the Nova V3 API. Partially implements blueprint v3-api Change-Id: I46cdec8999f74af37a9ca4280d123907124bad39 --- novaclient/tests/v3/fakes.py | 6 +++ novaclient/tests/v3/test_flavor_access.py | 63 +++++++++++++++++++++++ novaclient/v3/client.py | 2 + novaclient/v3/flavor_access.py | 45 ++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 novaclient/tests/v3/test_flavor_access.py create mode 100644 novaclient/v3/flavor_access.py diff --git a/novaclient/tests/v3/fakes.py b/novaclient/tests/v3/fakes.py index f0f4a2a89..4069776ff 100644 --- a/novaclient/tests/v3/fakes.py +++ b/novaclient/tests/v3/fakes.py @@ -105,3 +105,9 @@ class FakeHTTPClient(fakes_v1_1.FakeHTTPClient): ] return (200, {}, flavors) + + # + # Flavor access + # + get_flavors_2_flavor_access = ( + fakes_v1_1.FakeHTTPClient.get_flavors_2_os_flavor_access) diff --git a/novaclient/tests/v3/test_flavor_access.py b/novaclient/tests/v3/test_flavor_access.py new file mode 100644 index 000000000..5b64887d1 --- /dev/null +++ b/novaclient/tests/v3/test_flavor_access.py @@ -0,0 +1,63 @@ +# Copyright 2012 OpenStack Foundation +# Copyright 2013 IBM Corp. +# All Rights Reserved. +# +# 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 novaclient.tests import utils +from novaclient.tests.v3 import fakes +from novaclient.v3 import flavor_access + + +cs = fakes.FakeClient() + + +class FlavorAccessTest(utils.TestCase): + + def test_list_access_by_flavor_private(self): + kwargs = {'flavor': cs.flavors.get(2)} + r = cs.flavor_access.list(**kwargs) + cs.assert_called('GET', '/flavors/2/flavor-access') + for access_list in r: + self.assertTrue(isinstance(access_list, + flavor_access.FlavorAccess)) + + def test_add_tenant_access(self): + flavor = cs.flavors.get(2) + tenant = 'proj2' + r = cs.flavor_access.add_tenant_access(flavor, tenant) + + body = { + "add_tenant_access": { + "tenant_id": "proj2" + } + } + + cs.assert_called('POST', '/flavors/2/action', body) + for a in r: + self.assertTrue(isinstance(a, flavor_access.FlavorAccess)) + + def test_remove_tenant_access(self): + flavor = cs.flavors.get(2) + tenant = 'proj2' + r = cs.flavor_access.remove_tenant_access(flavor, tenant) + + body = { + "remove_tenant_access": { + "tenant_id": "proj2" + } + } + + cs.assert_called('POST', '/flavors/2/action', body) + for a in r: + self.assertTrue(isinstance(a, flavor_access.FlavorAccess)) diff --git a/novaclient/v3/client.py b/novaclient/v3/client.py index 1ee1cdccc..8aba63f34 100644 --- a/novaclient/v3/client.py +++ b/novaclient/v3/client.py @@ -15,6 +15,7 @@ # under the License. from novaclient import client +from novaclient.v3 import flavor_access from novaclient.v3 import flavors from novaclient.v3 import hosts @@ -53,6 +54,7 @@ class Client(object): #TODO(bnemec): Add back in v3 extensions self.hosts = hosts.HostManager(self) self.flavors = flavors.FlavorManager(self) + self.flavor_access = flavor_access.FlavorAccessManager(self) # Add in any extensions... if extensions: diff --git a/novaclient/v3/flavor_access.py b/novaclient/v3/flavor_access.py new file mode 100644 index 000000000..6896dac36 --- /dev/null +++ b/novaclient/v3/flavor_access.py @@ -0,0 +1,45 @@ +# Copyright 2012 OpenStack Foundation +# Copyright 2013 IBM Corp. +# All Rights Reserved. +# +# 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. + +"""Flavor access interface.""" + +from novaclient import base +from novaclient.v1_1 import flavor_access + + +class FlavorAccess(flavor_access.FlavorAccess): + pass + + +class FlavorAccessManager(flavor_access.FlavorAccessManager): + """ + Manage :class:`FlavorAccess` resources. + """ + resource_class = FlavorAccess + + def _list_by_flavor(self, flavor): + return self._list('/flavors/%s/flavor-access' % base.getid(flavor), + 'flavor_access') + + def add_tenant_access(self, flavor, tenant): + """Add a tenant to the given flavor access list.""" + info = {'tenant_id': tenant} + return self._action('add_tenant_access', flavor, info) + + def remove_tenant_access(self, flavor, tenant): + """Remove a tenant from the given flavor access list.""" + info = {'tenant_id': tenant} + return self._action('remove_tenant_access', flavor, info)