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