ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +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 | |
| 15 | import json |
| 16 | import urllib |
| 17 | |
| 18 | from tempest.common.rest_client import RestClient |
| 19 | |
| 20 | |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame] | 21 | class HostsV3ClientJSON(RestClient): |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 22 | |
| 23 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame] | 24 | super(HostsV3ClientJSON, self).__init__(config, username, password, |
| 25 | auth_url, tenant_name) |
| 26 | self.service = self.config.compute.catalog_v3_type |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 27 | |
| 28 | def list_hosts(self, params=None): |
| 29 | """Lists all hosts.""" |
| 30 | |
| 31 | url = 'os-hosts' |
| 32 | if params: |
| 33 | url += '?%s' % urllib.urlencode(params) |
| 34 | |
| 35 | resp, body = self.get(url) |
| 36 | body = json.loads(body) |
| 37 | return resp, body['hosts'] |
| 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'] |
| 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) |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame] | 54 | request_body = json.dumps({'host': request_body}) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 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'] |