diff --git a/novaclient/tests/v3/fakes.py b/novaclient/tests/v3/fakes.py index 8b7f4d2d5..a8c3788ab 100644 --- a/novaclient/tests/v3/fakes.py +++ b/novaclient/tests/v3/fakes.py @@ -29,4 +29,30 @@ class FakeClient(fakes.FakeClient, client.Client): class FakeHTTPClient(fakes_v1_1.FakeHTTPClient): - pass + # + # Hosts + # + def put_os_hosts_sample_host_1(self, body, **kw): + return (200, {}, {'host': {'host': 'sample-host_1', + 'status': 'enabled'}}) + + def put_os_hosts_sample_host_2(self, body, **kw): + return (200, {}, {'host': {'host': 'sample-host_2', + 'maintenance_mode': 'on_maintenance'}}) + + def put_os_hosts_sample_host_3(self, body, **kw): + return (200, {}, {'host': {'host': 'sample-host_3', + 'status': 'enabled', + 'maintenance_mode': 'on_maintenance'}}) + + def get_os_hosts_sample_host_reboot(self, **kw): + return (200, {}, {'host': {'host': 'sample_host', + 'power_action': 'reboot'}}) + + def get_os_hosts_sample_host_startup(self, **kw): + return (200, {}, {'host': {'host': 'sample_host', + 'power_action': 'startup'}}) + + def get_os_hosts_sample_host_shutdown(self, **kw): + return (200, {}, {'host': {'host': 'sample_host', + 'power_action': 'shutdown'}}) diff --git a/novaclient/tests/v3/test_hosts.py b/novaclient/tests/v3/test_hosts.py new file mode 100644 index 000000000..15bda0e85 --- /dev/null +++ b/novaclient/tests/v3/test_hosts.py @@ -0,0 +1,83 @@ +# Copyright 2013 OpenStack Foundation +# +# 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.v3 import hosts +from novaclient.tests.v3 import fakes +from novaclient.tests import utils + + +cs = fakes.FakeClient() + + +class HostsTest(utils.TestCase): + + def test_describe_resource(self): + hs = cs.hosts.get('host') + cs.assert_called('GET', '/os-hosts/host') + for h in hs: + self.assertTrue(isinstance(h, hosts.Host)) + + def test_list_host(self): + hs = cs.hosts.list() + cs.assert_called('GET', '/os-hosts') + for h in hs: + self.assertTrue(isinstance(h, hosts.Host)) + self.assertEqual(h.zone, 'nova1') + + def test_list_host_with_zone(self): + hs = cs.hosts.list('nova') + cs.assert_called('GET', '/os-hosts?zone=nova') + for h in hs: + self.assertTrue(isinstance(h, hosts.Host)) + self.assertEqual(h.zone, 'nova') + + def test_update_enable(self): + host = cs.hosts.get('sample_host')[0] + values = {"status": "enabled"} + result = host.update(values) + cs.assert_called('PUT', '/os-hosts/sample_host', {"host": values}) + self.assertTrue(isinstance(result, hosts.Host)) + + def test_update_maintenance(self): + host = cs.hosts.get('sample_host')[0] + values = {"maintenance_mode": "enable"} + result = host.update(values) + cs.assert_called('PUT', '/os-hosts/sample_host', {"host": values}) + self.assertTrue(isinstance(result, hosts.Host)) + + def test_update_both(self): + host = cs.hosts.get('sample_host')[0] + values = {"status": "enabled", + "maintenance_mode": "enable"} + result = host.update(values) + cs.assert_called('PUT', '/os-hosts/sample_host', {"host": values}) + self.assertTrue(isinstance(result, hosts.Host)) + + def test_host_startup(self): + host = cs.hosts.get('sample_host')[0] + result = host.startup() + cs.assert_called( + 'GET', '/os-hosts/sample_host/startup') + + def test_host_reboot(self): + host = cs.hosts.get('sample_host')[0] + result = host.reboot() + cs.assert_called( + 'GET', '/os-hosts/sample_host/reboot') + + def test_host_shutdown(self): + host = cs.hosts.get('sample_host')[0] + result = host.shutdown() + cs.assert_called( + 'GET', '/os-hosts/sample_host/shutdown') diff --git a/novaclient/v3/client.py b/novaclient/v3/client.py index 8a8d814f9..091aa0901 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 hosts class Client(object): @@ -45,7 +46,11 @@ class Client(object): http_log_debug=False, auth_system='keystone', auth_plugin=None, cacert=None, tenant_id=None): + self.projectid = project_id + self.tenant_id = tenant_id + self.os_cache = os_cache or not no_cache #TODO(bnemec): Add back in v3 extensions + self.hosts = hosts.HostManager(self) # Add in any extensions... if extensions: diff --git a/novaclient/v3/hosts.py b/novaclient/v3/hosts.py new file mode 100644 index 000000000..5327a2bf3 --- /dev/null +++ b/novaclient/v3/hosts.py @@ -0,0 +1,35 @@ +# Copyright 2013 OpenStack Foundation +# +# 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. + +""" V3 API versions of the Hosts interface. + +Inherits from the 1.1 code because a lot of the functionality is shared. +""" + +from novaclient.v1_1 import hosts + + +Host = hosts.Host + + +class HostManager(hosts.HostManager): + def update(self, host, values): + """Update status or maintenance mode for the host.""" + body = dict(host=values) + return self._update("/os-hosts/%s" % host, body, response_key='host') + + def host_action(self, host, action): + """Perform an action on a host.""" + url = '/os-hosts/{0}/{1}'.format(host, action) + return self._get(url, response_key='host')