ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | # |
| 3 | # Copyright 2013 IBM Corp. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import json |
| 18 | import urllib |
| 19 | |
| 20 | from tempest.common.rest_client import RestClient |
| 21 | |
| 22 | |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame^] | 23 | class HostsV3ClientJSON(RestClient): |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 24 | |
| 25 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame^] | 26 | super(HostsV3ClientJSON, self).__init__(config, username, password, |
| 27 | auth_url, tenant_name) |
| 28 | self.service = self.config.compute.catalog_v3_type |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 29 | |
| 30 | def list_hosts(self, params=None): |
| 31 | """Lists all hosts.""" |
| 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 | return resp, body['hosts'] |
| 40 | |
| 41 | def show_host_detail(self, hostname): |
| 42 | """Show detail information for the host.""" |
| 43 | |
| 44 | resp, body = self.get("os-hosts/%s" % str(hostname)) |
| 45 | body = json.loads(body) |
| 46 | return resp, body['host'] |
| 47 | |
| 48 | def update_host(self, hostname, **kwargs): |
| 49 | """Update a host.""" |
| 50 | |
| 51 | request_body = { |
| 52 | 'status': None, |
| 53 | 'maintenance_mode': None, |
| 54 | } |
| 55 | request_body.update(**kwargs) |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame^] | 56 | request_body = json.dumps({'host': request_body}) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 57 | |
| 58 | resp, body = self.put("os-hosts/%s" % str(hostname), request_body, |
| 59 | self.headers) |
| 60 | body = json.loads(body) |
| 61 | return resp, body |
| 62 | |
| 63 | def startup_host(self, hostname): |
| 64 | """Startup a host.""" |
| 65 | |
| 66 | resp, body = self.get("os-hosts/%s/startup" % str(hostname)) |
| 67 | body = json.loads(body) |
| 68 | return resp, body['host'] |
| 69 | |
| 70 | def shutdown_host(self, hostname): |
| 71 | """Shutdown a host.""" |
| 72 | |
| 73 | resp, body = self.get("os-hosts/%s/shutdown" % str(hostname)) |
| 74 | body = json.loads(body) |
| 75 | return resp, body['host'] |
| 76 | |
| 77 | def reboot_host(self, hostname): |
| 78 | """reboot a host.""" |
| 79 | |
| 80 | resp, body = self.get("os-hosts/%s/reboot" % str(hostname)) |
| 81 | body = json.loads(body) |
| 82 | return resp, body['host'] |