blob: 1fdd9075ca61ff3115aba3599942e81f79da8bd0 [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
Ghanshyamee9af302016-02-25 06:12:43 +090020from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050021
22
Ghanshyamee9af302016-02-25 06:12:43 +090023class HostsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050024
25 def list_hosts(self, **params):
zhufl8d4134b2017-03-08 15:04:20 +080026 """List all hosts.
27
28 For a full list of available parameters, please refer to the official
29 API reference:
30 https://developer.openstack.org/api-ref/compute/#list-hosts
31 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050032
33 url = 'os-hosts'
34 if params:
35 url += '?%s' % urllib.urlencode(params)
36
37 resp, body = self.get(url)
38 body = json.loads(body)
39 self.validate_response(schema.list_hosts, resp, body)
40 return rest_client.ResponseBody(resp, body)
41
42 def show_host(self, hostname):
43 """Show detail information for the host."""
44
45 resp, body = self.get("os-hosts/%s" % hostname)
46 body = json.loads(body)
47 self.validate_response(schema.get_host_detail, resp, body)
48 return rest_client.ResponseBody(resp, body)
49
50 def update_host(self, hostname, **kwargs):
51 """Update a host.
52
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090053 For a full list of available parameters, please refer to the official
54 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080055 https://developer.openstack.org/api-ref/compute/#update-host-status
Matthew Treinish9e26ca82016-02-23 11:43:20 -050056 """
57
58 request_body = {
59 'status': None,
60 'maintenance_mode': None,
61 }
62 request_body.update(**kwargs)
63 request_body = json.dumps(request_body)
64
65 resp, body = self.put("os-hosts/%s" % hostname, request_body)
66 body = json.loads(body)
67 self.validate_response(schema.update_host, resp, body)
68 return rest_client.ResponseBody(resp, body)
69
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -070070 def startup_host(self, hostname): # noqa
71 # NOTE: This noqa is for passing T110 check and we cannot rename
72 # to keep backwards compatibility. Actually, the root problem
73 # of this is a wrong API design. GET operation should not change
74 # resource status, but current API does that.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050075 """Startup a host."""
76
77 resp, body = self.get("os-hosts/%s/startup" % hostname)
78 body = json.loads(body)
79 self.validate_response(schema.startup_host, resp, body)
80 return rest_client.ResponseBody(resp, body)
81
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -070082 def shutdown_host(self, hostname): # noqa
83 # NOTE: This noqa is for passing T110 check and we cannot rename
84 # to keep backwards compatibility. Actually, the root problem
85 # of this is a wrong API design. GET operation should not change
86 # resource status, but current API does that.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050087 """Shutdown a host."""
88
89 resp, body = self.get("os-hosts/%s/shutdown" % hostname)
90 body = json.loads(body)
91 self.validate_response(schema.shutdown_host, resp, body)
92 return rest_client.ResponseBody(resp, body)
93
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -070094 def reboot_host(self, hostname): # noqa
95 # NOTE: This noqa is for passing T110 check and we cannot rename
96 # to keep backwards compatibility. Actually, the root problem
97 # of this is a wrong API design. GET operation should not change
98 # resource status, but current API does that.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050099 """Reboot a host."""
100
101 resp, body = self.get("os-hosts/%s/reboot" % hostname)
102 body = json.loads(body)
103 self.validate_response(schema.reboot_host, resp, body)
104 return rest_client.ResponseBody(resp, body)