blob: f51879d7962b271cdacf4e8d0508545d482a28c6 [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
Mate Lakat99ee9142012-09-14 12:34:46 +010017import json
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080018import urllib
Mate Lakat99ee9142012-09-14 12:34:46 +010019
Matthew Treinisha83a16e2012-12-07 13:44:02 -050020from tempest.common.rest_client import RestClient
21
Mate Lakat99ee9142012-09-14 12:34:46 +010022
23class HostsClientJSON(RestClient):
24
25 def __init__(self, config, username, password, auth_url, tenant_name=None):
26 super(HostsClientJSON, self).__init__(config, username, password,
27 auth_url, tenant_name)
28 self.service = self.config.compute.catalog_type
29
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080030 def list_hosts(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050031 """Lists all hosts."""
Mate Lakat99ee9142012-09-14 12:34:46 +010032
33 url = 'os-hosts'
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080034 if params:
35 url += '?%s' % urllib.urlencode(params)
36
Mate Lakat99ee9142012-09-14 12:34:46 +010037 resp, body = self.get(url)
38 body = json.loads(body)
39 return resp, body['hosts']
Zhu Zhuff150052013-09-17 17:48:39 +080040
41 def show_host_detail(self, hostname):
42 """Show detail information for the host."""
43
44 resp, body = self.get("os-hosts/%s" % str(hostname))
45 body = json.loads(body)
46 return resp, body['host']
Lingxian Kong4b398fd2013-10-04 17:14:14 +080047
48 def update_host(self, hostname, **kwargs):
49 """Update a host."""
50
51 request_body = {
52 'status': None,
53 'maintenance_mode': None,
54 }
55 request_body.update(**kwargs)
56 request_body = json.dumps(request_body)
57
58 resp, body = self.put("os-hosts/%s" % str(hostname), request_body,
59 self.headers)
60 body = json.loads(body)
61 return resp, body
62
63 def startup_host(self, hostname):
64 """Startup a host."""
65
66 resp, body = self.get("os-hosts/%s/startup" % str(hostname))
67 body = json.loads(body)
68 return resp, body['host']
69
70 def shutdown_host(self, hostname):
71 """Shutdown a host."""
72
73 resp, body = self.get("os-hosts/%s/shutdown" % str(hostname))
74 body = json.loads(body)
75 return resp, body['host']
76
77 def reboot_host(self, hostname):
78 """reboot a host."""
79
80 resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
81 body = json.loads(body)
82 return resp, body['host']