Unify Manager._update behaviour

Now _update call usually returns an instance of self.resource_class.
This simplifies the code and makes novaclient closer to keystoneclient.

Also, update hosts and services API according to changes on nova.
(If50a6b6e20f9b3fe66d486bb9b15d3eb4b62daf9).

Change-Id: I447e49e5fce0afba8a9c1a5df6dfa7200cc93e18
This commit is contained in:
Alessio Ababilov 2012-12-22 20:12:20 +04:00
parent fba20df12c
commit 6307310466
15 changed files with 99 additions and 94 deletions

@ -136,12 +136,9 @@ class Manager(utils.HookableMixin):
if cache:
cache.write("%s\n" % val)
def _get(self, url, response_key=None):
def _get(self, url, response_key):
_resp, body = self.api.client.get(url)
if response_key:
return self.resource_class(self, body[response_key], loaded=True)
else:
return self.resource_class(self, body, loaded=True)
return self.resource_class(self, body[response_key], loaded=True)
def _create(self, url, body, response_key, return_raw=False, **kwargs):
self.run_hooks('modify_body_for_create', body, **kwargs)
@ -156,10 +153,13 @@ class Manager(utils.HookableMixin):
def _delete(self, url):
_resp, _body = self.api.client.delete(url)
def _update(self, url, body, **kwargs):
def _update(self, url, body, response_key=None, **kwargs):
self.run_hooks('modify_body_for_update', body, **kwargs)
_resp, body = self.api.client.put(url, body=body)
return body
if response_key:
return self.resource_class(self, body[response_key])
else:
return body
class ManagerWithFind(Manager):

@ -49,7 +49,7 @@ class AgentsManager(base.ManagerWithFind):
'version': version,
'url': url,
'md5hash': md5hash}}
return self._update('/os-agents/%s' % id, body)
return self._update('/os-agents/%s' % id, body, 'agent')
def create(self, os, architecture, version,
url, md5hash, hypervisor):

@ -62,9 +62,9 @@ class AggregateManager(base.ManagerWithFind):
def update(self, aggregate, values):
"""Update the name and/or availability zone."""
body = {'aggregate': values}
result = self._update("/os-aggregates/%s" % base.getid(aggregate),
body)
return self.resource_class(self, result["aggregate"])
return self._update("/os-aggregates/%s" % base.getid(aggregate),
body,
"aggregate")
def add_host(self, aggregate, host):
"""Add a host into the Host Aggregate."""

@ -59,9 +59,9 @@ class FloatingIPDNSDomainManager(base.ManagerWithFind):
body = {'domain_entry':
{'scope': 'private',
'availability_zone': availability_zone}}
return self._update('/os-floating-ip-dns/%s' % _quote_domain(fqdomain),
body)
body,
'domain_entry')
def create_public(self, fqdomain, project):
"""Add or modify a public DNS domain."""
@ -70,7 +70,8 @@ class FloatingIPDNSDomainManager(base.ManagerWithFind):
'project': project}}
return self._update('/os-floating-ip-dns/%s' % _quote_domain(fqdomain),
body)
body,
'domain_entry')
def delete(self, fqdomain):
"""Delete the specified domain"""
@ -115,7 +116,8 @@ class FloatingIPDNSEntryManager(base.ManagerWithFind):
return self._update("/os-floating-ip-dns/%s/entries/%s" %
(_quote_domain(domain), name),
body)
body,
"dns_entry")
def modify_ip(self, domain, name, ip):
"""Add a new DNS entry."""
@ -125,7 +127,8 @@ class FloatingIPDNSEntryManager(base.ManagerWithFind):
return self._update("/os-floating-ip-dns/%s/entries/%s" %
(_quote_domain(domain), name),
body)
body,
"dns_entry")
def delete(self, domain, name):
"""Delete entry specified by name and domain."""

@ -54,13 +54,12 @@ class HostManager(base.ManagerWithFind):
def update(self, host, values):
"""Update status or maintenance mode for the host."""
result = self._update("/os-hosts/%s" % host, values)
return self.resource_class(self, result)
return self._update("/os-hosts/%s" % host, {"host": values}, "host")
def host_action(self, host, action):
"""Performs an action on a host."""
url = "/os-hosts/%s/%s" % (host, action)
return self._get(url)
return self._create(url, None, "host")
def list_all(self, zone=None):
url = '/os-hosts'

@ -25,7 +25,7 @@ class QuotaClassSet(base.Resource):
return self.class_name
def update(self, *args, **kwargs):
self.manager.update(self.class_name, *args, **kwargs)
return self.manager.update(self.class_name, *args, **kwargs)
class QuotaClassSetManager(base.ManagerWithFind):
@ -62,4 +62,6 @@ class QuotaClassSetManager(base.ManagerWithFind):
if body['quota_class_set'][key] is None:
body['quota_class_set'].pop(key)
self._update('/os-quota-class-sets/%s' % (class_name), body)
return self._update('/os-quota-class-sets/%s' % (class_name),
body,
'quota_class_set')

@ -25,7 +25,7 @@ class QuotaSet(base.Resource):
return self.tenant_id
def update(self, *args, **kwargs):
self.manager.update(self.tenant_id, *args, **kwargs)
return self.manager.update(self.tenant_id, *args, **kwargs)
class QuotaSetManager(base.ManagerWithFind):
@ -63,7 +63,7 @@ class QuotaSetManager(base.ManagerWithFind):
if body['quota_set'][key] is None:
body['quota_set'].pop(key)
self._update('/os-quota-sets/%s' % (tenant_id), body)
return self._update('/os-quota-sets/%s' % tenant_id, body, 'quota_set')
def defaults(self, tenant_id):
return self._get('/os-quota-sets/%s/defaults' % tenant_id,

@ -525,7 +525,7 @@ class ServerManager(local_base.BootingManagerWithFind):
},
}
self._update("/servers/%s" % base.getid(server), body)
return self._update("/servers/%s" % base.getid(server), body, "server")
def change_password(self, server, password):
"""

@ -34,29 +34,28 @@ class Service(base.Resource):
class ServiceManager(base.ManagerWithFind):
resource_class = Service
def list(self, host=None, service=None):
def list(self, host=None, binary=None):
"""
Describes cpu/memory/hdd info for host.
:param host: destination host name.
"""
url = "/os-services"
filters = []
if host:
url = "/os-services?host=%s" % host
if service:
url = "/os-services?service=%s" % service
if host and service:
url = "/os-services?host=%s&service=%s" % (host, service)
filters.append("host=%s" % host)
if binary:
filters.append("binary=%s" % binary)
if filters:
url = "%s?%s" % (url, "&".join(filters))
return self._list(url, "services")
def enable(self, host, service):
"""Enable the service specified by hostname and servicename"""
body = {"host": host, "service": service}
result = self._update("/os-services/enable", body)
return self.resource_class(self, result)
def enable(self, host, binary):
"""Enable the service specified by hostname and binary"""
body = {"host": host, "binary": binary}
return self._update("/os-services/enable", body, "service")
def disable(self, host, service):
"""Enable the service specified by hostname and servicename"""
body = {"host": host, "service": service}
result = self._update("/os-services/disable", body)
return self.resource_class(self, result)
def disable(self, host, binary):
"""Enable the service specified by hostname and binary"""
body = {"host": host, "binary": binary}
return self._update("/os-services/disable", body, "service")

@ -2077,7 +2077,7 @@ def do_agent_modify(cs, args):
"""Modify an existing agent build."""
result = cs.agents.update(args.id, args.version,
args.url, args.md5hash)
utils.print_dict(result['agent'])
utils.print_dict(result._info)
def do_aggregate_list(cs, args):

@ -363,7 +363,7 @@ class FakeHTTPClient(base_client.HTTPClient):
def put_servers_1234(self, body, **kw):
assert body.keys() == ['server']
fakes.assert_has_keys(body['server'], optional=['name', 'adminPass'])
return (204, {}, None)
return (204, {}, body)
def delete_servers_1234(self, **kw):
return (202, {}, None)
@ -711,12 +711,12 @@ class FakeHTTPClient(base_client.HTTPClient):
else:
fakes.assert_has_keys(body['domain_entry'],
required=['project', 'scope'])
return (205, {}, None)
return (205, {}, body)
def put_os_floating_ip_dns_testdomain_entries_testname(self, body, **kw):
fakes.assert_has_keys(body['dns_entry'],
required=['ip', 'dns_type'])
return (205, {}, None)
return (205, {}, body)
def delete_os_floating_ip_dns_testdomain(self, **kw):
return (200, {}, None)
@ -1080,7 +1080,7 @@ class FakeHTTPClient(base_client.HTTPClient):
#
def get_os_services(self, **kw):
host = kw.get('host', 'host1')
service = kw.get('service', 'nova-compute')
service = kw.get('binary', 'nova-compute')
return (200, {}, {'services':
[{'binary': service,
'host': host,
@ -1097,12 +1097,14 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def put_os_services_enable(self, body, **kw):
return (200, {}, {'host': body['host'], 'service': body['service'],
'disabled': False})
return (200, {}, {'service': {'host': body['host'],
'binary': body['binary'],
'disabled': False}})
def put_os_services_disable(self, body, **kw):
return (200, {}, {'host': body['host'], 'service': body['service'],
'disabled': True})
return (200, {}, {'service': {'host': body['host'],
'binary': body['binary'],
'disabled': True}})
#
# Fixed IPs
@ -1134,10 +1136,10 @@ class FakeHTTPClient(base_client.HTTPClient):
def get_os_hosts(self, **kw):
zone = kw.get('zone', 'nova1')
return (200, {}, {'hosts':
[{'host': 'host1',
[{'host_name': 'host1',
'service': 'nova-compute',
'zone': zone},
{'host': 'host1',
{'host_name': 'host1',
'service': 'nova-cert',
'zone': zone}]})
@ -1145,34 +1147,34 @@ class FakeHTTPClient(base_client.HTTPClient):
return (200, {}, {'host': [{'resource': {'host': 'sample_host'}}], })
def put_os_hosts_sample_host_1(self, body, **kw):
return (200, {}, {'host': 'sample-host_1',
'status': 'enabled'})
return (200, {}, {'host': {'host_name': 'sample-host_1',
'status': 'enabled'}})
def put_os_hosts_sample_host_2(self, body, **kw):
return (200, {}, {'host': 'sample-host_2',
'maintenance_mode': 'on_maintenance'})
return (200, {}, {'host': {'host_name': 'sample-host_2',
'maintenance_mode': 'on_maintenance'}})
def put_os_hosts_sample_host_3(self, body, **kw):
return (200, {}, {'host': 'sample-host_3',
'status': 'enabled',
'maintenance_mode': 'on_maintenance'})
return (200, {}, {'host': {'host_name': 'sample-host_3',
'status': 'enabled',
'maintenance_mode': 'on_maintenance'}})
def get_os_hosts_sample_host_startup(self, **kw):
return (200, {}, {'host': 'sample_host',
'power_action': 'startup'})
def post_os_hosts_sample_host_startup(self, **kw):
return (200, {}, {'host': {'host_name': 'sample_host',
'power_action': 'startup'}})
def get_os_hosts_sample_host_reboot(self, **kw):
return (200, {}, {'host': 'sample_host',
'power_action': 'reboot'})
def post_os_hosts_sample_host_reboot(self, **kw):
return (200, {}, {'host': {'host_name': 'sample_host',
'power_action': 'reboot'}})
def get_os_hosts_sample_host_shutdown(self, **kw):
return (200, {}, {'host': 'sample_host',
'power_action': 'shutdown'})
def post_os_hosts_sample_host_shutdown(self, **kw):
return (200, {}, {'host': {'host_name': 'sample_host',
'power_action': 'shutdown'}})
def put_os_hosts_sample_host(self, body, **kw):
result = {'host': 'dummy'}
result.update(body)
return (200, {}, result)
result = {'host_name': 'dummy'}
result.update(body['host'])
return (200, {}, {'host': result})
def get_os_hypervisors(self, **kw):
return (200, {}, {"hypervisors": [

@ -65,4 +65,4 @@ class AgentsTest(utils.TestCase):
"version": "8.0",
"md5hash": "add6bb58e139be103324d04d82d8f546"}}
cs.assert_called('PUT', '/os-agents/1', body)
self.assertEqual(1, ag['agent']['id'])
self.assertEqual(1, ag.id)

@ -29,14 +29,14 @@ class HostsTest(utils.TestCase):
host = cs.hosts.get('sample_host')[0]
values = {"status": "enabled"}
result = host.update(values)
cs.assert_called('PUT', '/os-hosts/sample_host', 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', values)
cs.assert_called('PUT', '/os-hosts/sample_host', {'host': values})
self.assertTrue(isinstance(result, hosts.Host))
def test_update_both(self):
@ -44,23 +44,23 @@ class HostsTest(utils.TestCase):
values = {"status": "enabled",
"maintenance_mode": "enable"}
result = host.update(values)
cs.assert_called('PUT', '/os-hosts/sample_host', 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')
cs.assert_called('POST', '/os-hosts/sample_host/startup')
self.assertTrue(isinstance(result, hosts.Host))
def test_host_reboot(self):
host = cs.hosts.get('sample_host')[0]
result = host.reboot()
cs.assert_called('GET', '/os-hosts/sample_host/reboot')
cs.assert_called('POST', '/os-hosts/sample_host/reboot')
self.assertTrue(isinstance(result, hosts.Host))
def test_host_shutdown(self):
host = cs.hosts.get('sample_host')[0]
result = host.shutdown()
cs.assert_called('GET', '/os-hosts/sample_host/shutdown')
cs.assert_called('POST', '/os-hosts/sample_host/shutdown')
self.assertTrue(isinstance(result, hosts.Host))

@ -39,30 +39,30 @@ class ServicesTest(utils.TestCase):
[self.assertEqual(s.binary, 'nova-compute') for s in svs]
[self.assertEqual(s.host, 'host2') for s in svs]
def test_list_services_with_service(self):
svs = cs.services.list(service='nova-cert')
cs.assert_called('GET', '/os-services?service=nova-cert')
def test_list_services_with_binary(self):
svs = cs.services.list(binary='nova-cert')
cs.assert_called('GET', '/os-services?binary=nova-cert')
[self.assertTrue(isinstance(s, services.Service)) for s in svs]
[self.assertEqual(s.binary, 'nova-cert') for s in svs]
[self.assertEqual(s.host, 'host1') for s in svs]
def test_list_services_with_host_service(self):
def test_list_services_with_host_binary(self):
svs = cs.services.list('host2', 'nova-cert')
cs.assert_called('GET', '/os-services?host=host2&service=nova-cert')
cs.assert_called('GET', '/os-services?host=host2&binary=nova-cert')
[self.assertTrue(isinstance(s, services.Service)) for s in svs]
[self.assertEqual(s.binary, 'nova-cert') for s in svs]
[self.assertEqual(s.host, 'host2') for s in svs]
def test_services_enable(self):
service = cs.services.enable('host1', 'nova-cert')
values = {"host": "host1", 'service': 'nova-cert'}
values = {"host": "host1", 'binary': 'nova-cert'}
cs.assert_called('PUT', '/os-services/enable', values)
self.assertTrue(isinstance(service, services.Service))
self.assertFalse(service.disabled)
def test_services_disable(self):
service = cs.services.disable('host1', 'nova-cert')
values = {"host": "host1", 'service': 'nova-cert'}
values = {"host": "host1", 'binary': 'nova-cert'}
cs.assert_called('PUT', '/os-services/disable', values)
self.assertTrue(isinstance(service, services.Service))
self.assertTrue(service.disabled)

@ -578,20 +578,20 @@ class ShellTest(utils.TestCase):
def test_services_list_with_servicename(self):
self.run_command('service-list --servicename nova-cert')
self.assert_called('GET', '/os-services?service=nova-cert')
self.assert_called('GET', '/os-services?binary=nova-cert')
def test_services_list_with_host_servicename(self):
self.run_command('service-list --host host1 --servicename nova-cert')
self.assert_called('GET', '/os-services?host=host1&service=nova-cert')
self.assert_called('GET', '/os-services?host=host1&binary=nova-cert')
def test_services_enable(self):
self.run_command('service-enable host1 nova-cert')
body = {'host': 'host1', 'service': 'nova-cert'}
body = {'host': 'host1', 'binary': 'nova-cert'}
self.assert_called('PUT', '/os-services/enable', body)
def test_services_disable(self):
self.run_command('service-disable host1 nova-cert')
body = {'host': 'host1', 'service': 'nova-cert'}
body = {'host': 'host1', 'binary': 'nova-cert'}
self.assert_called('PUT', '/os-services/disable', body)
def test_fixed_ips_get(self):
@ -618,31 +618,31 @@ class ShellTest(utils.TestCase):
def test_host_update_status(self):
self.run_command('host-update sample-host_1 --status enabled')
body = {'status': 'enabled'}
body = {'host': {'status': 'enabled'}}
self.assert_called('PUT', '/os-hosts/sample-host_1', body)
def test_host_update_maintenance(self):
self.run_command('host-update sample-host_2 --maintenance enable')
body = {'maintenance_mode': 'enable'}
body = {'host': {'maintenance_mode': 'enable'}}
self.assert_called('PUT', '/os-hosts/sample-host_2', body)
def test_host_update_multiple_settings(self):
self.run_command('host-update sample-host_3 '
'--status disabled --maintenance enable')
body = {'status': 'disabled', 'maintenance_mode': 'enable'}
body = {'host': {'status': 'disabled', 'maintenance_mode': 'enable'}}
self.assert_called('PUT', '/os-hosts/sample-host_3', body)
def test_host_startup(self):
self.run_command('host-action sample-host --action startup')
self.assert_called('GET', '/os-hosts/sample-host/startup')
self.assert_called('POST', '/os-hosts/sample-host/startup')
def test_host_shutdown(self):
self.run_command('host-action sample-host --action shutdown')
self.assert_called('GET', '/os-hosts/sample-host/shutdown')
self.assert_called('POST', '/os-hosts/sample-host/shutdown')
def test_host_reboot(self):
self.run_command('host-action sample-host --action reboot')
self.assert_called('GET', '/os-hosts/sample-host/reboot')
self.assert_called('POST', '/os-hosts/sample-host/reboot')
def test_coverage_start(self):
self.run_command('coverage-start')