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