Add v3 HostManager

Some of the host code required changes to work with the v3 API.
This change adds a v3-compatible HostManager that inherits from
the 1_1 HostManager to make use of the functions that didn't
need to change.

bp v3-api

Change-Id: I3ef7ab4619d49c20ab1bdeb1185ad28b411d37a1
This commit is contained in:
Ben Nemec 2013-08-15 17:13:19 -05:00
parent 615ad3de54
commit 57a22ec99c
4 changed files with 150 additions and 1 deletions

View File

@ -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'}})

View File

@ -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')

View File

@ -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:

35
novaclient/v3/hosts.py Normal file
View File

@ -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')