blob: 752af68178810226f453533921a8ca33f3554b49 [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
Matthew Treinish21905512015-07-13 10:33:35 -040015from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040016from six.moves.urllib import parse as urllib
Mate Lakat99ee9142012-09-14 12:34:46 +010017
Haiwei Xu047e7592015-03-23 13:14:27 +090018from tempest.api_schema.response.compute.v2_1 import hosts as schema
David Kranz0a735172015-01-16 10:51:18 -050019from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050020
Mate Lakat99ee9142012-09-14 12:34:46 +010021
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000022class HostsClient(service_client.ServiceClient):
Mate Lakat99ee9142012-09-14 12:34:46 +010023
Ken'ichi Ohmichi118776d2015-07-01 08:15:00 +000024 def list_hosts(self, **params):
Sean Daguef237ccb2013-01-04 15:19:14 -050025 """Lists all hosts."""
Mate Lakat99ee9142012-09-14 12:34:46 +010026
27 url = 'os-hosts'
Liu, Zhi Kuna9d10822013-07-01 17:48:17 +080028 if params:
29 url += '?%s' % urllib.urlencode(params)
30
Mate Lakat99ee9142012-09-14 12:34:46 +010031 resp, body = self.get(url)
32 body = json.loads(body)
Haiwei Xu54432cf2014-03-17 22:58:11 +090033 self.validate_response(schema.list_hosts, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050034 return service_client.ResponseBodyList(resp, body['hosts'])
Zhu Zhuff150052013-09-17 17:48:39 +080035
Ken'ichi Ohmichi394e6e42015-06-11 04:20:37 +000036 def show_host(self, hostname):
Zhu Zhuff150052013-09-17 17:48:39 +080037 """Show detail information for the host."""
38
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000039 resp, body = self.get("os-hosts/%s" % hostname)
Zhu Zhuff150052013-09-17 17:48:39 +080040 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090041 self.validate_response(schema.get_host_detail, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050042 return service_client.ResponseBodyList(resp, body['host'])
Lingxian Kong4b398fd2013-10-04 17:14:14 +080043
44 def update_host(self, hostname, **kwargs):
45 """Update a host."""
46
47 request_body = {
48 'status': None,
49 'maintenance_mode': None,
50 }
51 request_body.update(**kwargs)
52 request_body = json.dumps(request_body)
53
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000054 resp, body = self.put("os-hosts/%s" % hostname, request_body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080055 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090056 self.validate_response(schema.update_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050057 return service_client.ResponseBody(resp, body)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080058
59 def startup_host(self, hostname):
60 """Startup a host."""
61
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000062 resp, body = self.get("os-hosts/%s/startup" % hostname)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080063 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090064 self.validate_response(schema.startup_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050065 return service_client.ResponseBody(resp, body['host'])
Lingxian Kong4b398fd2013-10-04 17:14:14 +080066
67 def shutdown_host(self, hostname):
68 """Shutdown a host."""
69
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000070 resp, body = self.get("os-hosts/%s/shutdown" % hostname)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080071 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090072 self.validate_response(schema.shutdown_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050073 return service_client.ResponseBody(resp, body['host'])
Lingxian Kong4b398fd2013-10-04 17:14:14 +080074
75 def reboot_host(self, hostname):
76 """reboot a host."""
77
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000078 resp, body = self.get("os-hosts/%s/reboot" % hostname)
Lingxian Kong4b398fd2013-10-04 17:14:14 +080079 body = json.loads(body)
Haiwei Xu047e7592015-03-23 13:14:27 +090080 self.validate_response(schema.reboot_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050081 return service_client.ResponseBody(resp, body['host'])