blob: 1d2c7b2cf8f8d674d9f706ce3a81cb1a10fd9123 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
nithya-ganesane6a36c82013-02-15 14:38:27 +00002# Copyright 2013 Hewlett-Packard Development Company, L.P.
dwallecke62b9f02012-10-10 23:34:42 -05003# All Rights Reserved.
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
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000017import copy
18
Matthew Treinish21905512015-07-13 10:33:35 -040019from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040020from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090021
ghanshyamaa93b4b2015-03-20 11:03:44 +090022from tempest.api_schema.response.compute.v2_1 import servers as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000023from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024
Daryl Wallecke5b83d42011-11-10 14:39:02 -060025
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000026class ServersClient(service_client.ServiceClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060027
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090028 def __init__(self, auth_provider, service, region,
29 enable_instance_password=True, **kwargs):
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000030 super(ServersClient, self).__init__(
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090031 auth_provider, service, region, **kwargs)
32 self.enable_instance_password = enable_instance_password
33
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000034 def create_server(self, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060035 """
36 Creates an instance of a server.
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000037 Most parameters except the following are passed to the API without
38 any changes.
39 :param disk_config: The name is changed to OS-DCF:diskConfig
40 :param scheduler_hints: The name is changed to os:scheduler_hints and
41 the parameter is set in the same level as the parameter 'server'.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060042 """
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000043 body = copy.deepcopy(kwargs)
44 if body.get('disk_config'):
45 body['OS-DCF:diskConfig'] = body.pop('disk_config')
Daryl Wallecke5b83d42011-11-10 14:39:02 -060046
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000047 hints = None
48 if body.get('scheduler_hints'):
49 hints = {'os:scheduler_hints': body.pop('scheduler_hints')}
Joseph Lanouxeef192f2014-08-01 14:32:53 +000050
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000051 post_body = {'server': body}
raiesmh08cbe21b02014-03-12 17:04:44 +053052
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000053 if hints:
raiesmh08cbe21b02014-03-12 17:04:44 +053054 post_body = dict(post_body.items() + hints.items())
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000055
raiesmh08cbe21b02014-03-12 17:04:44 +053056 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020057 resp, body = self.post('servers', post_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -050058
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050060 # NOTE(maurosr): this deals with the case of multiple server create
61 # with return reservation id set True
62 if 'reservation_id' in body:
David Kranz0fb14292015-02-11 15:55:20 -050063 return service_client.ResponseBody(resp, body)
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090064 if self.enable_instance_password:
Ghanshyam7f989102014-07-29 14:23:26 +090065 create_schema = schema.create_server_with_admin_pass
66 else:
67 create_schema = schema.create_server
68 self.validate_response(create_schema, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090069 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060070
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000071 def update_server(self, server_id, **kwargs):
72 """Updates the properties of an existing server.
73 Most parameters except the following are passed to the API without
74 any changes.
75 :param disk_config: The name is changed to OS-DCF:diskConfig
Daryl Wallecke5b83d42011-11-10 14:39:02 -060076 """
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000077 if kwargs.get('disk_config'):
78 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
Daryl Wallecke5b83d42011-11-10 14:39:02 -060079
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000080 post_body = json.dumps({'server': kwargs})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000081 resp, body = self.put("servers/%s" % server_id, post_body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060082 body = json.loads(body)
Ken'ichi Ohmichi29cd5122014-04-28 11:04:52 +090083 self.validate_response(schema.update_server, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090084 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060085
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +000086 def show_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Returns the details of an existing server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000088 resp, body = self.get("servers/%s" % server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060089 body = json.loads(body)
Ken'ichi Ohmichi21e4fc72014-05-08 16:46:23 +090090 self.validate_response(schema.get_server, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090091 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060092
93 def delete_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Deletes the given server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000095 resp, body = self.delete("servers/%s" % server_id)
ghanshyam274327a2015-03-23 10:29:45 +090096 self.validate_response(schema.delete_server, resp, body)
David Kranz0fb14292015-02-11 15:55:20 -050097 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060098
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +000099 def list_servers(self, detail=False, **params):
Sean Daguef237ccb2013-01-04 15:19:14 -0500100 """Lists all servers for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600101
102 url = 'servers'
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000103 _schema = schema.list_servers
104
105 if detail:
106 url += '/detail'
107 _schema = schema.list_servers_detail
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500108 if params:
109 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600110
chris fattarsi5098fa22012-04-17 13:27:00 -0700111 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600112 body = json.loads(body)
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000113 self.validate_response(_schema, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500114 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600115
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600116 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Lists all addresses for a server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000118 resp, body = self.get("servers/%s/ips" % server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600119 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900120 self.validate_response(schema.list_addresses, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900121 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600122
123 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700125 resp, body = self.get("servers/%s/ips/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000126 (server_id, network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600127 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900128 self.validate_response(schema.list_addresses_by_network, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500129 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600130
ghanshyam0f825252015-08-25 16:02:50 +0900131 def action(self, server_id, action_name,
ghanshyam274327a2015-03-23 10:29:45 +0900132 schema=schema.server_actions_common_schema,
ghanshyam0f825252015-08-25 16:02:50 +0900133 **kwargs):
Attila Fazekasd4299282013-02-22 13:25:23 +0100134 post_body = json.dumps({action_name: kwargs})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000135 resp, body = self.post('servers/%s/action' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200136 post_body)
ghanshyam0f825252015-08-25 16:02:50 +0900137 if body:
Ghanshyamd6d30402014-04-02 15:28:37 +0900138 body = json.loads(body)
ghanshyam0f825252015-08-25 16:02:50 +0900139 self.validate_response(schema, resp, body)
140 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600141
ivan-zhuccc89462013-10-31 16:53:12 +0800142 def create_backup(self, server_id, backup_type, rotation, name):
143 """Backup a server instance."""
ghanshyam0f825252015-08-25 16:02:50 +0900144 return self.action(server_id, "createBackup",
ivan-zhuccc89462013-10-31 16:53:12 +0800145 backup_type=backup_type,
146 rotation=rotation,
147 name=name)
148
Attila Fazekasd4299282013-02-22 13:25:23 +0100149 def change_password(self, server_id, adminPass):
150 """Changes the root password for the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900151 return self.action(server_id, 'changePassword',
Attila Fazekasd4299282013-02-22 13:25:23 +0100152 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600153
ivan-zhu6de5b042013-10-24 17:21:48 +0800154 def get_password(self, server_id):
155 resp, body = self.get("servers/%s/os-server-password" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000156 server_id)
ivan-zhu6de5b042013-10-24 17:21:48 +0800157 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900158 self.validate_response(schema.get_password, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500159 return service_client.ResponseBody(resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800160
161 def delete_password(self, server_id):
162 """
163 Removes the encrypted server password from the metadata server
164 Note that this does not actually change the instance server
165 password.
166 """
Ghanshyam997c9092014-04-03 19:00:20 +0900167 resp, body = self.delete("servers/%s/os-server-password" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000168 server_id)
ghanshyam274327a2015-03-23 10:29:45 +0900169 self.validate_response(schema.server_actions_delete_password,
Ghanshyam997c9092014-04-03 19:00:20 +0900170 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500171 return service_client.ResponseBody(resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800172
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000173 def reboot_server(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500174 """Reboots a server."""
ghanshyam0f825252015-08-25 16:02:50 +0900175 return self.action(server_id, 'reboot', type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600176
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000177 def rebuild_server(self, server_id, image_ref, **kwargs):
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000178 """Rebuilds a server with a new image.
179 Most parameters except the following are passed to the API without
180 any changes.
181 :param disk_config: The name is changed to OS-DCF:diskConfig
182 """
Attila Fazekasd4299282013-02-22 13:25:23 +0100183 kwargs['imageRef'] = image_ref
184 if 'disk_config' in kwargs:
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000185 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +0900186 if self.enable_instance_password:
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900187 rebuild_schema = schema.rebuild_server_with_admin_pass
188 else:
189 rebuild_schema = schema.rebuild_server
ghanshyam0f825252015-08-25 16:02:50 +0900190 return self.action(server_id, 'rebuild',
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900191 rebuild_schema, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600192
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000193 def resize_server(self, server_id, flavor_ref, **kwargs):
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000194 """Changes the flavor of a server.
195 Most parameters except the following are passed to the API without
196 any changes.
197 :param disk_config: The name is changed to OS-DCF:diskConfig
198 """
Attila Fazekasd4299282013-02-22 13:25:23 +0100199 kwargs['flavorRef'] = flavor_ref
200 if 'disk_config' in kwargs:
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000201 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
ghanshyam0f825252015-08-25 16:02:50 +0900202 return self.action(server_id, 'resize', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600203
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000204 def confirm_resize_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500205 """Confirms the flavor change for a server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900206 return self.action(server_id, 'confirmResize',
ghanshyam0f825252015-08-25 16:02:50 +0900207 schema.server_actions_confirm_resize,
Ghanshyam997c9092014-04-03 19:00:20 +0900208 **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600209
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000210 def revert_resize_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500211 """Reverts a server back to its original flavor."""
ghanshyam0f825252015-08-25 16:02:50 +0900212 return self.action(server_id, 'revertResize', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600213
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600214 def list_server_metadata(self, server_id):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000215 resp, body = self.get("servers/%s/metadata" % server_id)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600216 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900217 self.validate_response(schema.list_server_metadata, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900218 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600219
Chris Yeohd0b52e72013-09-17 22:38:59 +0930220 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
221 if no_metadata_field:
222 post_body = ""
223 else:
224 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000225 resp, body = self.put('servers/%s/metadata' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200226 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600227 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900228 self.validate_response(schema.set_server_metadata, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900229 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600230
231 def update_server_metadata(self, server_id, meta):
232 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000233 resp, body = self.post('servers/%s/metadata' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200234 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600235 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900236 self.validate_response(schema.update_server_metadata,
Ghanshyame8421062014-06-02 15:58:21 +0900237 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900238 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600239
240 def get_server_metadata_item(self, server_id, key):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000241 resp, body = self.get("servers/%s/metadata/%s" % (server_id, key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600242 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900243 self.validate_response(schema.set_get_server_metadata_item,
244 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900245 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600246
247 def set_server_metadata_item(self, server_id, key, meta):
248 post_body = json.dumps({'meta': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000249 resp, body = self.put('servers/%s/metadata/%s' % (server_id, key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200250 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600251 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900252 self.validate_response(schema.set_get_server_metadata_item,
253 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900254 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600255
256 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700257 resp, body = self.delete("servers/%s/metadata/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000258 (server_id, key))
ghanshyam274327a2015-03-23 10:29:45 +0900259 self.validate_response(schema.delete_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900260 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500261 return service_client.ResponseBody(resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700262
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000263 def stop_server(self, server_id, **kwargs):
ghanshyam0f825252015-08-25 16:02:50 +0900264 return self.action(server_id, 'os-stop', **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700265
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000266 def start_server(self, server_id, **kwargs):
ghanshyam0f825252015-08-25 16:02:50 +0900267 return self.action(server_id, 'os-start', **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700268
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000269 def attach_volume(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500270 """Attaches a volume to a server instance."""
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000271 post_body = json.dumps({'volumeAttachment': kwargs})
Dan Smithc18d8c62012-07-02 08:09:26 -0700272 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200273 post_body)
Ghanshyam385c4e72014-03-27 11:42:25 +0900274 body = json.loads(body)
275 self.validate_response(schema.attach_volume, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900276 return service_client.ResponseBody(resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700277
278 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500279 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700280 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
281 (server_id, volume_id))
Ghanshyam385c4e72014-03-27 11:42:25 +0900282 self.validate_response(schema.detach_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500283 return service_client.ResponseBody(resp, body)
sapan-kona775cf632012-06-22 00:32:36 +0530284
Ghanshyam5c2a5582014-04-14 17:16:57 +0900285 def get_volume_attachment(self, server_id, attach_id):
286 """Return details about the given volume attachment."""
287 resp, body = self.get('servers/%s/os-volume_attachments/%s' % (
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000288 server_id, attach_id))
Ghanshyam5c2a5582014-04-14 17:16:57 +0900289 body = json.loads(body)
290 self.validate_response(schema.get_volume_attachment, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900291 return service_client.ResponseBody(resp, body)
Ghanshyam5c2a5582014-04-14 17:16:57 +0900292
293 def list_volume_attachments(self, server_id):
294 """Returns the list of volume attachments for a given instance."""
295 resp, body = self.get('servers/%s/os-volume_attachments' % (
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000296 server_id))
Ghanshyam5c2a5582014-04-14 17:16:57 +0900297 body = json.loads(body)
298 self.validate_response(schema.list_volume_attachments, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900299 return service_client.ResponseBody(resp, body)
Ghanshyam5c2a5582014-04-14 17:16:57 +0900300
Attila Fazekasd4299282013-02-22 13:25:23 +0100301 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500302 """Adds a security group to the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900303 return self.action(server_id, 'addSecurityGroup', name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530304
Attila Fazekasd4299282013-02-22 13:25:23 +0100305 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500306 """Removes a security group from the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900307 return self.action(server_id, 'removeSecurityGroup', name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100308
Ken'ichi Ohmichi86f58932015-08-18 04:16:15 +0000309 def live_migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500310 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100311
Ken'ichi Ohmichi86f58932015-08-18 04:16:15 +0000312 req_body = json.dumps({'os-migrateLive': kwargs})
Mate Lakat99ee9142012-09-14 12:34:46 +0100313
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000314 resp, body = self.post("servers/%s/action" % server_id, req_body)
ghanshyam274327a2015-03-23 10:29:45 +0900315 self.validate_response(schema.server_actions_common_schema,
Ghanshyam997c9092014-04-03 19:00:20 +0900316 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500317 return service_client.ResponseBody(resp, body)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600318
Attila Fazekasd4299282013-02-22 13:25:23 +0100319 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500320 """Migrates a server to a new host."""
ghanshyam0f825252015-08-25 16:02:50 +0900321 return self.action(server_id, 'migrate', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600322
Attila Fazekasd4299282013-02-22 13:25:23 +0100323 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500324 """Locks the given server."""
ghanshyam0f825252015-08-25 16:02:50 +0900325 return self.action(server_id, 'lock', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600326
Attila Fazekasd4299282013-02-22 13:25:23 +0100327 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500328 """UNlocks the given server."""
ghanshyam0f825252015-08-25 16:02:50 +0900329 return self.action(server_id, 'unlock', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600330
Attila Fazekasd4299282013-02-22 13:25:23 +0100331 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900332 """Suspends the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900333 return self.action(server_id, 'suspend', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600334
Attila Fazekasd4299282013-02-22 13:25:23 +0100335 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900336 """Un-suspends the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900337 return self.action(server_id, 'resume', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600338
Attila Fazekasd4299282013-02-22 13:25:23 +0100339 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900340 """Pauses the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900341 return self.action(server_id, 'pause', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600342
Attila Fazekasd4299282013-02-22 13:25:23 +0100343 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900344 """Un-pauses the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900345 return self.action(server_id, 'unpause', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600346
Attila Fazekasd4299282013-02-22 13:25:23 +0100347 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500348 """Resets the state of a server to active/error."""
ghanshyam0f825252015-08-25 16:02:50 +0900349 return self.action(server_id, 'os-resetState', state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100350
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900351 def shelve_server(self, server_id, **kwargs):
352 """Shelves the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900353 return self.action(server_id, 'shelve', **kwargs)
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900354
355 def unshelve_server(self, server_id, **kwargs):
356 """Un-shelves the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900357 return self.action(server_id, 'unshelve', **kwargs)
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900358
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900359 def shelve_offload_server(self, server_id, **kwargs):
360 """Shelve-offload the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900361 return self.action(server_id, 'shelveOffload', **kwargs)
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900362
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100363 def get_console_output(self, server_id, length):
Davanum Srinivas8a841c72014-06-19 18:33:14 -0400364 kwargs = {'length': length} if length else {}
ghanshyam0f825252015-08-25 16:02:50 +0900365 return self.action(server_id, 'os-getConsoleOutput',
ghanshyam274327a2015-03-23 10:29:45 +0900366 schema.get_console_output,
David Kranzae99b9a2015-02-16 13:37:01 -0500367 **kwargs)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200368
369 def list_virtual_interfaces(self, server_id):
370 """
371 List the virtual interfaces used in an instance.
372 """
373 resp, body = self.get('/'.join(['servers', server_id,
374 'os-virtual-interfaces']))
Ghanshyam08ce58d2014-04-04 14:51:14 +0900375 body = json.loads(body)
376 self.validate_response(schema.list_virtual_interfaces, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500377 return service_client.ResponseBody(resp, body)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000378
Yuiko Takada7835d502014-01-15 10:15:03 +0000379 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000380 """Rescue the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900381 return self.action(server_id, 'rescue',
David Kranzae99b9a2015-02-16 13:37:01 -0500382 schema.rescue_server,
David Kranzae99b9a2015-02-16 13:37:01 -0500383 **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000384
385 def unrescue_server(self, server_id):
386 """Unrescue the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900387 return self.action(server_id, 'unrescue')
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900388
Zhu Zhuda070852013-09-25 08:07:57 -0500389 def get_server_diagnostics(self, server_id):
390 """Get the usage data for a server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000391 resp, body = self.get("servers/%s/diagnostics" % server_id)
David Kranzae99b9a2015-02-16 13:37:01 -0500392 return service_client.ResponseBody(resp, json.loads(body))
Zhu Zhuda070852013-09-25 08:07:57 -0500393
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900394 def list_instance_actions(self, server_id):
395 """List the provided server action."""
396 resp, body = self.get("servers/%s/os-instance-actions" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000397 server_id)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900398 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900399 self.validate_response(schema.list_instance_actions, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900400 return service_client.ResponseBody(resp, body)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900401
402 def get_instance_action(self, server_id, request_id):
403 """Returns the action details of the provided server."""
404 resp, body = self.get("servers/%s/os-instance-actions/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000405 (server_id, request_id))
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900406 body = json.loads(body)
Ghanshyam4b41dfd2014-07-17 13:40:38 +0900407 self.validate_response(schema.get_instance_action, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900408 return service_client.ResponseBody(resp, body)
Lingxian Kongaecc1092013-10-03 16:18:46 +0800409
410 def force_delete_server(self, server_id, **kwargs):
411 """Force delete a server."""
ghanshyam0f825252015-08-25 16:02:50 +0900412 return self.action(server_id, 'forceDelete', **kwargs)
Lingxian Kongaecc1092013-10-03 16:18:46 +0800413
414 def restore_soft_deleted_server(self, server_id, **kwargs):
415 """Restore a soft-deleted server."""
ghanshyam0f825252015-08-25 16:02:50 +0900416 return self.action(server_id, 'restore', **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900417
418 def reset_network(self, server_id, **kwargs):
419 """Resets the Network of a server"""
ghanshyam0f825252015-08-25 16:02:50 +0900420 return self.action(server_id, 'resetNetwork', **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900421
422 def inject_network_info(self, server_id, **kwargs):
423 """Inject the Network Info into server"""
ghanshyam0f825252015-08-25 16:02:50 +0900424 return self.action(server_id, 'injectNetworkInfo', **kwargs)
Ghanshyam86d1a572014-03-05 10:19:25 +0900425
426 def get_vnc_console(self, server_id, console_type):
427 """Get URL of VNC console."""
428 return self.action(server_id, "os-getVNCConsole",
ghanshyam0f825252015-08-25 16:02:50 +0900429 schema.get_vnc_console,
Ghanshyamd6d30402014-04-02 15:28:37 +0900430 type=console_type)
ghanshyam4c7d2a02015-09-14 16:05:13 +0900431
432 def add_fixed_ip(self, server_id, **kwargs):
433 """Add a fixed IP to input server instance."""
434 return self.action(server_id, 'addFixedIp', **kwargs)
435
436 def remove_fixed_ip(self, server_id, **kwargs):
437 """Remove input fixed IP from input server instance."""
438 return self.action(server_id, 'removeFixedIp', **kwargs)