blob: e148572bfc904e316f5c51aa2ab68af24b2cf387 [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
Haiwei Xu54432cf2014-03-17 22:58:11 +090018from tempest.api_schema.compute import hosts as schema
Haiwei Xu03a0e9d2014-03-28 02:22:50 +090019from tempest.api_schema.compute.v2 import hosts as v2_schema
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
22
23CONF = config.CONF
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024
Mate Lakat99ee9142012-09-14 12:34:46 +010025
Haiwei Xuab924622014-03-05 04:33:51 +090026class HostsClientJSON(rest_client.RestClient):
Mate Lakat99ee9142012-09-14 12:34:46 +010027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(HostsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
Mate Lakat99ee9142012-09-14 12:34:46 +010031
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080032 def list_hosts(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050033 """Lists all hosts."""
Mate Lakat99ee9142012-09-14 12:34:46 +010034
35 url = 'os-hosts'
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080036 if params:
37 url += '?%s' % urllib.urlencode(params)
38
Mate Lakat99ee9142012-09-14 12:34:46 +010039 resp, body = self.get(url)
40 body = json.loads(body)
Haiwei Xu54432cf2014-03-17 22:58:11 +090041 self.validate_response(schema.list_hosts, resp, body)
Mate Lakat99ee9142012-09-14 12:34:46 +010042 return resp, body['hosts']
Zhu Zhuff150052013-09-17 17:48:39 +080043
44 def show_host_detail(self, hostname):
45 """Show detail information for the host."""
46
47 resp, body = self.get("os-hosts/%s" % str(hostname))
48 body = json.loads(body)
Haiwei Xud99dd482014-03-19 05:28:36 +090049 self.validate_response(schema.show_host_detail, resp, body)
Zhu Zhuff150052013-09-17 17:48:39 +080050 return resp, body['host']
Lingxian Kong4b398fd2013-10-04 17:14:14 +080051
52 def update_host(self, hostname, **kwargs):
53 """Update a host."""
54
55 request_body = {
56 'status': None,
57 'maintenance_mode': None,
58 }
59 request_body.update(**kwargs)
60 request_body = json.dumps(request_body)
61
vponomaryovf4c27f92014-02-18 10:56:42 +020062 resp, body = self.put("os-hosts/%s" % str(hostname), request_body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080063 body = json.loads(body)
64 return resp, body
65
66 def startup_host(self, hostname):
67 """Startup a host."""
68
69 resp, body = self.get("os-hosts/%s/startup" % str(hostname))
70 body = json.loads(body)
Haiwei Xu03a0e9d2014-03-28 02:22:50 +090071 self.validate_response(v2_schema.startup_host, resp, body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080072 return resp, body['host']
73
74 def shutdown_host(self, hostname):
75 """Shutdown a host."""
76
77 resp, body = self.get("os-hosts/%s/shutdown" % str(hostname))
78 body = json.loads(body)
Haiwei Xu11e69da2014-03-28 04:00:45 +090079 self.validate_response(v2_schema.shutdown_host, resp, body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080080 return resp, body['host']
81
82 def reboot_host(self, hostname):
83 """reboot a host."""
84
85 resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
86 body = json.loads(body)
Haiwei Xu11e69da2014-03-28 04:00:45 +090087 self.validate_response(v2_schema.reboot_host, resp, body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080088 return resp, body['host']