blob: 519798eb7197c2f7b47fd7af502dc1654f1987cc [file] [log] [blame]
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
3# Copyright 2013 IBM Corp.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import urllib
18
19from lxml import etree
20from tempest.common.rest_client import RestClientXML
Lingxian Kong4b398fd2013-10-04 17:14:14 +080021from tempest.services.compute.xml.common import Document
22from tempest.services.compute.xml.common import Element
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080023from tempest.services.compute.xml.common import xml_to_json
24
25
26class HostsClientXML(RestClientXML):
27
28 def __init__(self, config, username, password, auth_url, tenant_name=None):
29 super(HostsClientXML, self).__init__(config, username, password,
30 auth_url, tenant_name)
31 self.service = self.config.compute.catalog_type
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
40 resp, body = self.get(url, self.headers)
41 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
48 resp, body = self.get("os-hosts/%s" % str(hostname), self.headers)
49 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),
61 str(Document(request_body)),
62 self.headers)
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
70 resp, body = self.get("os-hosts/%s/startup" % str(hostname),
71 self.headers)
72 node = etree.fromstring(body)
73 body = [xml_to_json(x) for x in node.getchildren()]
74 return resp, body
75
76 def shutdown_host(self, hostname):
77 """Shutdown a host."""
78
79 resp, body = self.get("os-hosts/%s/shutdown" % str(hostname),
80 self.headers)
81 node = etree.fromstring(body)
82 body = [xml_to_json(x) for x in node.getchildren()]
83 return resp, body
84
85 def reboot_host(self, hostname):
86 """Reboot a host."""
87
88 resp, body = self.get("os-hosts/%s/reboot" % str(hostname),
89 self.headers)
90 node = etree.fromstring(body)
91 body = [xml_to_json(x) for x in node.getchildren()]
92 return resp, body