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 |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
| 20 | |
| 21 | CONF = config.CONF |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 22 | |
| 23 | |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame] | 24 | class HostsV3ClientJSON(RestClient): |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 25 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 26 | def __init__(self, auth_provider): |
| 27 | super(HostsV3ClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | self.service = CONF.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 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 58 | resp, body = self.put("os-hosts/%s" % str(hostname), request_body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 59 | body = json.loads(body) |
| 60 | return resp, body |
| 61 | |
| 62 | def startup_host(self, hostname): |
| 63 | """Startup a host.""" |
| 64 | |
| 65 | resp, body = self.get("os-hosts/%s/startup" % str(hostname)) |
| 66 | body = json.loads(body) |
| 67 | return resp, body['host'] |
| 68 | |
| 69 | def shutdown_host(self, hostname): |
| 70 | """Shutdown a host.""" |
| 71 | |
| 72 | resp, body = self.get("os-hosts/%s/shutdown" % str(hostname)) |
| 73 | body = json.loads(body) |
| 74 | return resp, body['host'] |
| 75 | |
| 76 | def reboot_host(self, hostname): |
| 77 | """reboot a host.""" |
| 78 | |
| 79 | resp, body = self.get("os-hosts/%s/reboot" % str(hostname)) |
| 80 | body = json.loads(body) |
| 81 | return resp, body['host'] |