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