blob: b74cd040b3907a58c5ee2d653b19b71389633f66 [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
vponomaryov960eeb42014-02-22 18:25:25 +020018from tempest.common import rest_client
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
vponomaryov960eeb42014-02-22 18:25:25 +020027class HostsClientXML(rest_client.RestClient):
28 TYPE = "xml"
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080029
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000030 def __init__(self, auth_provider):
31 super(HostsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000032 self.service = CONF.compute.catalog_type
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080033
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
vponomaryovf4c27f92014-02-18 10:56:42 +020041 resp, body = self.get(url)
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080042 node = etree.fromstring(body)
43 body = [xml_to_json(x) for x in node.getchildren()]
44 return resp, body
Zhu Zhuff150052013-09-17 17:48:39 +080045
46 def show_host_detail(self, hostname):
47 """Show detail information for the host."""
48
vponomaryovf4c27f92014-02-18 10:56:42 +020049 resp, body = self.get("os-hosts/%s" % str(hostname))
Zhu Zhuff150052013-09-17 17:48:39 +080050 node = etree.fromstring(body)
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090051 body = [xml_to_json(node)]
Zhu Zhuff150052013-09-17 17:48:39 +080052 return resp, body
Lingxian Kong4b398fd2013-10-04 17:14:14 +080053
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090054 def update_host(self, hostname, **kwargs):
Lingxian Kong4b398fd2013-10-04 17:14:14 +080055 """Update a host."""
56
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090057 request_body = Element("updates")
Lingxian Kong4b398fd2013-10-04 17:14:14 +080058 if kwargs:
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090059 for k, v in kwargs.iteritems():
60 request_body.append(Element(k, v))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080061 resp, body = self.put("os-hosts/%s" % str(hostname),
vponomaryovf4c27f92014-02-18 10:56:42 +020062 str(Document(request_body)))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080063 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
vponomaryovf4c27f92014-02-18 10:56:42 +020070 resp, body = self.get("os-hosts/%s/startup" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080071 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
vponomaryovf4c27f92014-02-18 10:56:42 +020078 resp, body = self.get("os-hosts/%s/shutdown" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080079 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
vponomaryovf4c27f92014-02-18 10:56:42 +020086 resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080087 node = etree.fromstring(body)
88 body = [xml_to_json(x) for x in node.getchildren()]
89 return resp, body