blob: 1b93b006c1b95c3e5ea5187818e9a8b3a21d20b5 [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):
26 """List all hosts."""
27
28 url = 'os-hosts'
29 if params:
30 url += '?%s' % urllib.urlencode(params)
31
32 resp, body = self.get(url)
33 body = json.loads(body)
34 self.validate_response(schema.list_hosts, resp, body)
35 return rest_client.ResponseBody(resp, body)
36
37 def show_host(self, hostname):
38 """Show detail information for the host."""
39
40 resp, body = self.get("os-hosts/%s" % hostname)
41 body = json.loads(body)
42 self.validate_response(schema.get_host_detail, resp, body)
43 return rest_client.ResponseBody(resp, body)
44
45 def update_host(self, hostname, **kwargs):
46 """Update a host.
47
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090048 For a full list of available parameters, please refer to the official
49 API reference:
50 http://developer.openstack.org/api-ref-compute-v2.1.html#enablehost
Matthew Treinish9e26ca82016-02-23 11:43:20 -050051 """
52
53 request_body = {
54 'status': None,
55 'maintenance_mode': None,
56 }
57 request_body.update(**kwargs)
58 request_body = json.dumps(request_body)
59
60 resp, body = self.put("os-hosts/%s" % hostname, request_body)
61 body = json.loads(body)
62 self.validate_response(schema.update_host, resp, body)
63 return rest_client.ResponseBody(resp, body)
64
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -070065 def startup_host(self, hostname): # noqa
66 # NOTE: This noqa is for passing T110 check and we cannot rename
67 # to keep backwards compatibility. Actually, the root problem
68 # of this is a wrong API design. GET operation should not change
69 # resource status, but current API does that.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050070 """Startup a host."""
71
72 resp, body = self.get("os-hosts/%s/startup" % hostname)
73 body = json.loads(body)
74 self.validate_response(schema.startup_host, resp, body)
75 return rest_client.ResponseBody(resp, body)
76
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -070077 def shutdown_host(self, hostname): # noqa
78 # NOTE: This noqa is for passing T110 check and we cannot rename
79 # to keep backwards compatibility. Actually, the root problem
80 # of this is a wrong API design. GET operation should not change
81 # resource status, but current API does that.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050082 """Shutdown a host."""
83
84 resp, body = self.get("os-hosts/%s/shutdown" % hostname)
85 body = json.loads(body)
86 self.validate_response(schema.shutdown_host, resp, body)
87 return rest_client.ResponseBody(resp, body)
88
Ken'ichi Ohmichi12b28e92016-04-06 10:43:51 -070089 def reboot_host(self, hostname): # noqa
90 # NOTE: This noqa is for passing T110 check and we cannot rename
91 # to keep backwards compatibility. Actually, the root problem
92 # of this is a wrong API design. GET operation should not change
93 # resource status, but current API does that.
Matthew Treinish9e26ca82016-02-23 11:43:20 -050094 """Reboot a host."""
95
96 resp, body = self.get("os-hosts/%s/reboot" % hostname)
97 body = json.loads(body)
98 self.validate_response(schema.reboot_host, resp, body)
99 return rest_client.ResponseBody(resp, body)