blob: a6f3cc10afc69c9bbfc69e9118409ca9cae6d39b [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
Matthew Treinish89128142015-04-23 10:44:30 -040016
17from six.moves.urllib import parse as urllib
Mate Lakat99ee9142012-09-14 12:34:46 +010018
Haiwei Xu047e7592015-03-23 13:14:27 +090019from tempest.api_schema.response.compute.v2_1 import hosts as schema
David Kranz0a735172015-01-16 10:51:18 -050020from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021
Mate Lakat99ee9142012-09-14 12:34:46 +010022
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000023class HostsClient(service_client.ServiceClient):
Mate Lakat99ee9142012-09-14 12:34:46 +010024
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080025 def list_hosts(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050026 """Lists all hosts."""
Mate Lakat99ee9142012-09-14 12:34:46 +010027
28 url = 'os-hosts'
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080029 if params:
30 url += '?%s' % urllib.urlencode(params)
31
Mate Lakat99ee9142012-09-14 12:34:46 +010032 resp, body = self.get(url)
33 body = json.loads(body)
Haiwei Xu54432cf2014-03-17 22:58:11 +090034 self.validate_response(schema.list_hosts, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050035 return service_client.ResponseBodyList(resp, body['hosts'])
Zhu Zhuff150052013-09-17 17:48:39 +080036
Ken'ichi Ohmichi394e6e42015-06-11 04:20:37 +000037 def show_host(self, hostname):
Zhu Zhuff150052013-09-17 17:48:39 +080038 """Show detail information for the host."""
39
40 resp, body = self.get("os-hosts/%s" % str(hostname))
41 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090042 self.validate_response(schema.get_host_detail, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050043 return service_client.ResponseBodyList(resp, body['host'])
Lingxian Kong4b398fd2013-10-04 17:14:14 +080044
45 def update_host(self, hostname, **kwargs):
46 """Update a host."""
47
48 request_body = {
49 'status': None,
50 'maintenance_mode': None,
51 }
52 request_body.update(**kwargs)
53 request_body = json.dumps(request_body)
54
vponomaryovf4c27f92014-02-18 10:56:42 +020055 resp, body = self.put("os-hosts/%s" % str(hostname), request_body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080056 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090057 self.validate_response(schema.update_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050058 return service_client.ResponseBody(resp, body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080059
60 def startup_host(self, hostname):
61 """Startup a host."""
62
63 resp, body = self.get("os-hosts/%s/startup" % str(hostname))
64 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090065 self.validate_response(schema.startup_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050066 return service_client.ResponseBody(resp, body['host'])
Lingxian Kong4b398fd2013-10-04 17:14:14 +080067
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)
Haiwei Xu047e7592015-03-23 13:14:27 +090073 self.validate_response(schema.shutdown_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050074 return service_client.ResponseBody(resp, body['host'])
Lingxian Kong4b398fd2013-10-04 17:14:14 +080075
76 def reboot_host(self, hostname):
77 """reboot a host."""
78
79 resp, body = self.get("os-hosts/%s/reboot" % str(hostname))
80 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090081 self.validate_response(schema.reboot_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050082 return service_client.ResponseBody(resp, body['host'])