blob: 269160ecdd3b3b5a754ddc1953f40361424b8b45 [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15from oslo_serialization import jsonutils as json
16from six.moves.urllib import parse as urllib
17
18from tempest.lib.api_schema.response.compute.v2_1 import hosts as schema
19from tempest.lib.common import rest_client
20
21
22class HostsClient(rest_client.RestClient):
23
24 def list_hosts(self, **params):
25 """List all hosts."""
26
27 url = 'os-hosts'
28 if params:
29 url += '?%s' % urllib.urlencode(params)
30
31 resp, body = self.get(url)
32 body = json.loads(body)
33 self.validate_response(schema.list_hosts, resp, body)
34 return rest_client.ResponseBody(resp, body)
35
36 def show_host(self, hostname):
37 """Show detail information for the host."""
38
39 resp, body = self.get("os-hosts/%s" % hostname)
40 body = json.loads(body)
41 self.validate_response(schema.get_host_detail, resp, body)
42 return rest_client.ResponseBody(resp, body)
43
44 def update_host(self, hostname, **kwargs):
45 """Update a host.
46
47 Available params: see http://developer.openstack.org/
48 api-ref-compute-v2.1.html#enablehost
49 """
50
51 request_body = {
52 'status': None,
53 'maintenance_mode': None,
54 }
55 request_body.update(**kwargs)
56 request_body = json.dumps(request_body)
57
58 resp, body = self.put("os-hosts/%s" % hostname, request_body)
59 body = json.loads(body)
60 self.validate_response(schema.update_host, resp, body)
61 return rest_client.ResponseBody(resp, body)
62
63 def startup_host(self, hostname):
64 """Startup a host."""
65
66 resp, body = self.get("os-hosts/%s/startup" % hostname)
67 body = json.loads(body)
68 self.validate_response(schema.startup_host, resp, body)
69 return rest_client.ResponseBody(resp, body)
70
71 def shutdown_host(self, hostname):
72 """Shutdown a host."""
73
74 resp, body = self.get("os-hosts/%s/shutdown" % hostname)
75 body = json.loads(body)
76 self.validate_response(schema.shutdown_host, resp, body)
77 return rest_client.ResponseBody(resp, body)
78
79 def reboot_host(self, hostname):
80 """Reboot a host."""
81
82 resp, body = self.get("os-hosts/%s/reboot" % hostname)
83 body = json.loads(body)
84 self.validate_response(schema.reboot_host, resp, body)
85 return rest_client.ResponseBody(resp, body)