Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [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 | |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 15 | import json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 16 | |
| 17 | from six.moves.urllib import parse as urllib |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 18 | |
Haiwei Xu | 047e759 | 2015-03-23 13:14:27 +0900 | [diff] [blame] | 19 | from tempest.api_schema.response.compute.v2_1 import hosts as schema |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 20 | from tempest.common import service_client |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 22 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 23 | class HostsClient(service_client.ServiceClient): |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 24 | |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 25 | def list_hosts(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 26 | """Lists all hosts.""" |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 27 | |
| 28 | url = 'os-hosts' |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 29 | if params: |
| 30 | url += '?%s' % urllib.urlencode(params) |
| 31 | |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 32 | resp, body = self.get(url) |
| 33 | body = json.loads(body) |
Haiwei Xu | 54432cf | 2014-03-17 22:58:11 +0900 | [diff] [blame] | 34 | self.validate_response(schema.list_hosts, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 35 | return service_client.ResponseBodyList(resp, body['hosts']) |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 36 | |
Ken'ichi Ohmichi | 394e6e4 | 2015-06-11 04:20:37 +0000 | [diff] [blame] | 37 | def show_host(self, hostname): |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 38 | """Show detail information for the host.""" |
| 39 | |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 40 | resp, body = self.get("os-hosts/%s" % hostname) |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 41 | body = json.loads(body) |
Haiwei Xu | 047e759 | 2015-03-23 13:14:27 +0900 | [diff] [blame] | 42 | self.validate_response(schema.get_host_detail, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 43 | return service_client.ResponseBodyList(resp, body['host']) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 44 | |
| 45 | def update_host(self, hostname, **kwargs): |
| 46 | """Update a host.""" |
| 47 | |
| 48 | request_body = { |
| 49 | 'status': None, |
| 50 | 'maintenance_mode': None, |
| 51 | } |
| 52 | request_body.update(**kwargs) |
| 53 | request_body = json.dumps(request_body) |
| 54 | |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 55 | resp, body = self.put("os-hosts/%s" % hostname, request_body) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 56 | body = json.loads(body) |
Haiwei Xu | 047e759 | 2015-03-23 13:14:27 +0900 | [diff] [blame] | 57 | self.validate_response(schema.update_host, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 58 | return service_client.ResponseBody(resp, body) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 59 | |
| 60 | def startup_host(self, hostname): |
| 61 | """Startup a host.""" |
| 62 | |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 63 | resp, body = self.get("os-hosts/%s/startup" % hostname) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 64 | body = json.loads(body) |
Haiwei Xu | 047e759 | 2015-03-23 13:14:27 +0900 | [diff] [blame] | 65 | self.validate_response(schema.startup_host, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 66 | return service_client.ResponseBody(resp, body['host']) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 67 | |
| 68 | def shutdown_host(self, hostname): |
| 69 | """Shutdown a host.""" |
| 70 | |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 71 | resp, body = self.get("os-hosts/%s/shutdown" % hostname) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 72 | body = json.loads(body) |
Haiwei Xu | 047e759 | 2015-03-23 13:14:27 +0900 | [diff] [blame] | 73 | self.validate_response(schema.shutdown_host, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 74 | return service_client.ResponseBody(resp, body['host']) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 75 | |
| 76 | def reboot_host(self, hostname): |
| 77 | """reboot a host.""" |
| 78 | |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 79 | resp, body = self.get("os-hosts/%s/reboot" % hostname) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 80 | body = json.loads(body) |
Haiwei Xu | 047e759 | 2015-03-23 13:14:27 +0900 | [diff] [blame] | 81 | self.validate_response(schema.reboot_host, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 82 | return service_client.ResponseBody(resp, body['host']) |