| 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 | |
| Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 18 | from tempest.common.rest_client import RestClient |
| 19 | |
| Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 20 | |
| 21 | class HostsClientJSON(RestClient): |
| 22 | |
| 23 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 24 | super(HostsClientJSON, self).__init__(config, username, password, |
| 25 | auth_url, tenant_name) |
| 26 | self.service = self.config.compute.catalog_type |
| 27 | |
| Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 28 | def list_hosts(self, params=None): |
| Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 29 | """Lists all hosts.""" |
| Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 30 | |
| 31 | url = 'os-hosts' |
| Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 32 | if params: |
| 33 | url += '?%s' % urllib.urlencode(params) |
| 34 | |
| Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 35 | resp, body = self.get(url) |
| 36 | body = json.loads(body) |
| 37 | return resp, body['hosts'] |
| Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 38 | |
| 39 | def show_host_detail(self, hostname): |
| 40 | """Show detail information for the host.""" |
| 41 | |
| 42 | resp, body = self.get("os-hosts/%s" % str(hostname)) |
| 43 | body = json.loads(body) |
| 44 | return resp, body['host'] |
| Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 45 | |
| 46 | def update_host(self, hostname, **kwargs): |
| 47 | """Update a host.""" |
| 48 | |
| 49 | request_body = { |
| 50 | 'status': None, |
| 51 | 'maintenance_mode': None, |
| 52 | } |
| 53 | request_body.update(**kwargs) |
| 54 | request_body = json.dumps(request_body) |
| 55 | |
| 56 | resp, body = self.put("os-hosts/%s" % str(hostname), request_body, |
| 57 | self.headers) |
| 58 | body = json.loads(body) |
| 59 | return resp, body |
| 60 | |
| 61 | def startup_host(self, hostname): |
| 62 | """Startup a host.""" |
| 63 | |
| 64 | resp, body = self.get("os-hosts/%s/startup" % str(hostname)) |
| 65 | body = json.loads(body) |
| 66 | return resp, body['host'] |
| 67 | |
| 68 | def shutdown_host(self, hostname): |
| 69 | """Shutdown a host.""" |
| 70 | |
| 71 | resp, body = self.get("os-hosts/%s/shutdown" % str(hostname)) |
| 72 | body = json.loads(body) |
| 73 | return resp, body['host'] |
| 74 | |
| 75 | def reboot_host(self, hostname): |
| 76 | """reboot a host.""" |
| 77 | |
| 78 | resp, body = self.get("os-hosts/%s/reboot" % str(hostname)) |
| 79 | body = json.loads(body) |
| 80 | return resp, body['host'] |