blob: 23a7dd66ff1c70a921388490eea5f02d61498145 [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 Treinish28f164c2014-03-04 18:55:06 +000019from tempest.common import xml_utils
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080021
Matthew Treinish684d8992014-01-30 16:27:40 +000022CONF = config.CONF
23
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080024
vponomaryov960eeb42014-02-22 18:25:25 +020025class HostsClientXML(rest_client.RestClient):
26 TYPE = "xml"
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(HostsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080031
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
vponomaryovf4c27f92014-02-18 10:56:42 +020039 resp, body = self.get(url)
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080040 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000041 body = [xml_utils.xml_to_json(x) for x in node.getchildren()]
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080042 return resp, body
Zhu Zhuff150052013-09-17 17:48:39 +080043
44 def show_host_detail(self, hostname):
45 """Show detail information for the host."""
46
vponomaryovf4c27f92014-02-18 10:56:42 +020047 resp, body = self.get("os-hosts/%s" % str(hostname))
Zhu Zhuff150052013-09-17 17:48:39 +080048 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000049 body = [xml_utils.xml_to_json(node)]
Zhu Zhuff150052013-09-17 17:48:39 +080050 return resp, body
Lingxian Kong4b398fd2013-10-04 17:14:14 +080051
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090052 def update_host(self, hostname, **kwargs):
Lingxian Kong4b398fd2013-10-04 17:14:14 +080053 """Update a host."""
54
Matthew Treinish28f164c2014-03-04 18:55:06 +000055 request_body = xml_utils.Element("updates")
Lingxian Kong4b398fd2013-10-04 17:14:14 +080056 if kwargs:
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +090057 for k, v in kwargs.iteritems():
Matthew Treinish28f164c2014-03-04 18:55:06 +000058 request_body.append(xml_utils.Element(k, v))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080059 resp, body = self.put("os-hosts/%s" % str(hostname),
Matthew Treinish28f164c2014-03-04 18:55:06 +000060 str(xml_utils.Document(request_body)))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080061 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000062 body = [xml_utils.xml_to_json(x) for x in node.getchildren()]
Lingxian Kong4b398fd2013-10-04 17:14:14 +080063 return resp, body
64
65 def startup_host(self, hostname):
66 """Startup a host."""
67
vponomaryovf4c27f92014-02-18 10:56:42 +020068 resp, body = self.get("os-hosts/%s/startup" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080069 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000070 body = [xml_utils.xml_to_json(x) for x in node.getchildren()]
Lingxian Kong4b398fd2013-10-04 17:14:14 +080071 return resp, body
72
73 def shutdown_host(self, hostname):
74 """Shutdown a host."""
75
vponomaryovf4c27f92014-02-18 10:56:42 +020076 resp, body = self.get("os-hosts/%s/shutdown" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080077 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000078 body = [xml_utils.xml_to_json(x) for x in node.getchildren()]
Lingxian Kong4b398fd2013-10-04 17:14:14 +080079 return resp, body
80
81 def reboot_host(self, hostname):
82 """Reboot a host."""
83
vponomaryovf4c27f92014-02-18 10:56:42 +020084 resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
Lingxian Kong4b398fd2013-10-04 17:14:14 +080085 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000086 body = [xml_utils.xml_to_json(x) for x in node.getchildren()]
Lingxian Kong4b398fd2013-10-04 17:14:14 +080087 return resp, body