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 | |
| 15 | import urllib |
| 16 | |
| 17 | from lxml import etree |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 18 | from tempest.common import rest_client |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 19 | from tempest.common import xml_utils |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 21 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | CONF = config.CONF |
| 23 | |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 24 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 25 | class HostsClientXML(rest_client.RestClient): |
| 26 | TYPE = "xml" |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 27 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(HostsClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_type |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 31 | |
| 32 | def list_hosts(self, params=None): |
| 33 | """Lists all hosts.""" |
| 34 | |
| 35 | url = 'os-hosts' |
| 36 | if params: |
| 37 | url += '?%s' % urllib.urlencode(params) |
| 38 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 39 | resp, body = self.get(url) |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 40 | node = etree.fromstring(body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 41 | body = [xml_utils.xml_to_json(x) for x in node.getchildren()] |
Liu, Zhi Kun | a9d1082 | 2013-07-01 17:48:17 +0800 | [diff] [blame] | 42 | return resp, body |
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 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 47 | resp, body = self.get("os-hosts/%s" % str(hostname)) |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 48 | node = etree.fromstring(body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 49 | body = [xml_utils.xml_to_json(node)] |
Zhu Zhu | ff15005 | 2013-09-17 17:48:39 +0800 | [diff] [blame] | 50 | return resp, body |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 51 | |
Ken'ichi Ohmichi | a1aa44c | 2013-12-06 20:48:24 +0900 | [diff] [blame] | 52 | def update_host(self, hostname, **kwargs): |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 53 | """Update a host.""" |
| 54 | |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 55 | request_body = xml_utils.Element("updates") |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 56 | if kwargs: |
Ken'ichi Ohmichi | a1aa44c | 2013-12-06 20:48:24 +0900 | [diff] [blame] | 57 | for k, v in kwargs.iteritems(): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 58 | request_body.append(xml_utils.Element(k, v)) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 59 | resp, body = self.put("os-hosts/%s" % str(hostname), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 60 | str(xml_utils.Document(request_body))) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 61 | node = etree.fromstring(body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 62 | body = [xml_utils.xml_to_json(x) for x in node.getchildren()] |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 63 | return resp, body |
| 64 | |
| 65 | def startup_host(self, hostname): |
| 66 | """Startup a host.""" |
| 67 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 68 | resp, body = self.get("os-hosts/%s/startup" % str(hostname)) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 69 | node = etree.fromstring(body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 70 | body = [xml_utils.xml_to_json(x) for x in node.getchildren()] |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 71 | return resp, body |
| 72 | |
| 73 | def shutdown_host(self, hostname): |
| 74 | """Shutdown a host.""" |
| 75 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 76 | resp, body = self.get("os-hosts/%s/shutdown" % str(hostname)) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 77 | node = etree.fromstring(body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 78 | body = [xml_utils.xml_to_json(x) for x in node.getchildren()] |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 79 | return resp, body |
| 80 | |
| 81 | def reboot_host(self, hostname): |
| 82 | """Reboot a host.""" |
| 83 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 84 | resp, body = self.get("os-hosts/%s/reboot" % str(hostname)) |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 85 | node = etree.fromstring(body) |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 86 | body = [xml_utils.xml_to_json(x) for x in node.getchildren()] |
Lingxian Kong | 4b398fd | 2013-10-04 17:14:14 +0800 | [diff] [blame] | 87 | return resp, body |