blob: 13abe182fae74bd51114b18bba100342a715cd68 [file] [log] [blame]
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +08001# 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
15import urllib
16
17from lxml import etree
18from tempest.common.rest_client import RestClientXML
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
Lingxian Kong4b398fd2013-10-04 17:14:14 +080020from tempest.services.compute.xml.common import Document
21from tempest.services.compute.xml.common import Element
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080022from tempest.services.compute.xml.common import xml_to_json
23
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080026
27class HostsClientXML(RestClientXML):
28
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(HostsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_type
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080032
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
vponomaryovf4c27f92014-02-18 10:56:42 +020040 resp, body = self.get(url)
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080041 node = etree.fromstring(body)
42 body = [xml_to_json(x) for x in node.getchildren()]
43 return resp, body
Zhu Zhuff150052013-09-17 17:48:39 +080044
45 def show_host_detail(self, hostname):
46 """Show detail information for the host."""
47
vponomaryovf4c27f92014-02-18 10:56:42 +020048 resp, body = self.get("os-hosts/%s" % str(hostname))
Zhu Zhuff150052013-09-17 17:48:39 +080049 node = etree.fromstring(body)
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090050 body = [xml_to_json(node)]
Zhu Zhuff150052013-09-17 17:48:39 +080051 return resp, body
Lingxian Kong4b398fd2013-10-04 17:14:14 +080052
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090053 def update_host(self, hostname, **kwargs):
Lingxian Kong4b398fd2013-10-04 17:14:14 +080054 """Update a host."""
55
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090056 request_body = Element("updates")
Lingxian Kong4b398fd2013-10-04 17:14:14 +080057 if kwargs:
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090058 for k, v in kwargs.iteritems():
59 request_body.append(Element(k, v))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080060 resp, body = self.put("os-hosts/%s" % str(hostname),
vponomaryovf4c27f92014-02-18 10:56:42 +020061 str(Document(request_body)))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080062 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
vponomaryovf4c27f92014-02-18 10:56:42 +020069 resp, body = self.get("os-hosts/%s/startup" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080070 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
vponomaryovf4c27f92014-02-18 10:56:42 +020077 resp, body = self.get("os-hosts/%s/shutdown" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080078 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
vponomaryovf4c27f92014-02-18 10:56:42 +020085 resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080086 node = etree.fromstring(body)
87 body = [xml_to_json(x) for x in node.getchildren()]
88 return resp, body