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): |
| 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 | |
| 48 | Available params: see http://developer.openstack.org/ |
| 49 | api-ref-compute-v2.1.html#enablehost |
| 50 | """ |
| 51 | |
| 52 | request_body = { |
| 53 | 'status': None, |
| 54 | 'maintenance_mode': None, |
| 55 | } |
| 56 | request_body.update(**kwargs) |
| 57 | request_body = json.dumps(request_body) |
| 58 | |
| 59 | resp, body = self.put("os-hosts/%s" % hostname, request_body) |
| 60 | body = json.loads(body) |
| 61 | self.validate_response(schema.update_host, resp, body) |
| 62 | return rest_client.ResponseBody(resp, body) |
| 63 | |
| 64 | def startup_host(self, hostname): |
| 65 | """Startup a host.""" |
| 66 | |
| 67 | resp, body = self.get("os-hosts/%s/startup" % hostname) |
| 68 | body = json.loads(body) |
| 69 | self.validate_response(schema.startup_host, resp, body) |
| 70 | return rest_client.ResponseBody(resp, body) |
| 71 | |
| 72 | def shutdown_host(self, hostname): |
| 73 | """Shutdown a host.""" |
| 74 | |
| 75 | resp, body = self.get("os-hosts/%s/shutdown" % hostname) |
| 76 | body = json.loads(body) |
| 77 | self.validate_response(schema.shutdown_host, resp, body) |
| 78 | return rest_client.ResponseBody(resp, body) |
| 79 | |
| 80 | def reboot_host(self, hostname): |
| 81 | """Reboot a host.""" |
| 82 | |
| 83 | resp, body = self.get("os-hosts/%s/reboot" % hostname) |
| 84 | body = json.loads(body) |
| 85 | self.validate_response(schema.reboot_host, resp, body) |
| 86 | return rest_client.ResponseBody(resp, body) |