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 |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 16 | import urllib |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 17 | |
Haiwei Xu | 54432cf | 2014-03-17 22:58:11 +0900 | [diff] [blame] | 18 | from tempest.api_schema.compute import hosts as schema |
Haiwei Xu | 03a0e9d | 2014-03-28 02:22:50 +0900 | [diff] [blame] | 19 | from tempest.api_schema.compute.v2 import hosts as v2_schema |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | from tempest import config |
| 22 | |
| 23 | CONF = config.CONF |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 25 | |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 26 | class HostsClientJSON(rest_client.RestClient): |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 27 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(HostsClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_type |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 31 | |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 32 | def list_hosts(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 33 | """Lists all hosts.""" |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 34 | |
| 35 | url = 'os-hosts' |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 36 | if params: |
| 37 | url += '?%s' % urllib.urlencode(params) |
| 38 | |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 39 | resp, body = self.get(url) |
| 40 | body = json.loads(body) |
Haiwei Xu | 54432cf | 2014-03-17 22:58:11 +0900 | [diff] [blame] | 41 | self.validate_response(schema.list_hosts, resp, body) |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 42 | return resp, body['hosts'] |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 43 | |
| 44 | def show_host_detail(self, hostname): |
| 45 | """Show detail information for the host.""" |
| 46 | |
| 47 | resp, body = self.get("os-hosts/%s" % str(hostname)) |
| 48 | body = json.loads(body) |
Haiwei Xu | d99dd48 | 2014-03-19 05:28:36 +0900 | [diff] [blame] | 49 | self.validate_response(schema.show_host_detail, resp, body) |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 50 | return resp, body['host'] |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 51 | |
| 52 | def update_host(self, hostname, **kwargs): |
| 53 | """Update a host.""" |
| 54 | |
| 55 | request_body = { |
| 56 | 'status': None, |
| 57 | 'maintenance_mode': None, |
| 58 | } |
| 59 | request_body.update(**kwargs) |
| 60 | request_body = json.dumps(request_body) |
| 61 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 62 | resp, body = self.put("os-hosts/%s" % str(hostname), request_body) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 63 | body = json.loads(body) |
| 64 | return resp, body |
| 65 | |
| 66 | def startup_host(self, hostname): |
| 67 | """Startup a host.""" |
| 68 | |
| 69 | resp, body = self.get("os-hosts/%s/startup" % str(hostname)) |
| 70 | body = json.loads(body) |
Haiwei Xu | 03a0e9d | 2014-03-28 02:22:50 +0900 | [diff] [blame] | 71 | self.validate_response(v2_schema.startup_host, resp, body) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 72 | return resp, body['host'] |
| 73 | |
| 74 | def shutdown_host(self, hostname): |
| 75 | """Shutdown a host.""" |
| 76 | |
| 77 | resp, body = self.get("os-hosts/%s/shutdown" % str(hostname)) |
| 78 | body = json.loads(body) |
Haiwei Xu | 11e69da | 2014-03-28 04:00:45 +0900 | [diff] [blame] | 79 | self.validate_response(v2_schema.shutdown_host, resp, body) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 80 | return resp, body['host'] |
| 81 | |
| 82 | def reboot_host(self, hostname): |
| 83 | """reboot a host.""" |
| 84 | |
| 85 | resp, body = self.get("os-hosts/%s/reboot" % str(hostname)) |
| 86 | body = json.loads(body) |
Haiwei Xu | 11e69da | 2014-03-28 04:00:45 +0900 | [diff] [blame] | 87 | self.validate_response(v2_schema.reboot_host, resp, body) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 88 | return resp, body['host'] |