Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 | |
| 15 | from oslo_serialization import jsonutils as json |
| 16 | from six.moves.urllib import parse as urllib |
| 17 | |
| 18 | from tempest.lib.api_schema.response.compute.v2_1 import hosts as schema |
| 19 | from tempest.lib.common import rest_client |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 20 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 21 | |
| 22 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 23 | class HostsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 24 | |
| 25 | def list_hosts(self, **params): |
zhufl | 8d4134b | 2017-03-08 15:04:20 +0800 | [diff] [blame] | 26 | """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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 32 | |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 53 | For a full list of available parameters, please refer to the official |
| 54 | API reference: |
zhufl | 8d4134b | 2017-03-08 15:04:20 +0800 | [diff] [blame] | 55 | https://developer.openstack.org/api-ref/compute/#update-host-status |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 56 | """ |
| 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 Ohmichi | 12b28e9 | 2016-04-06 10:43:51 -0700 | [diff] [blame] | 70 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 75 | """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 Ohmichi | 12b28e9 | 2016-04-06 10:43:51 -0700 | [diff] [blame] | 82 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 87 | """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 Ohmichi | 12b28e9 | 2016-04-06 10:43:51 -0700 | [diff] [blame] | 94 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 99 | """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) |