blob: 11258a61bda28ee039b3d73ec3ff49b85de1978e [file] [log] [blame]
ivan-zhu09111942013-08-01 08:09:16 +08001# Copyright 2012 OpenStack Foundation
2# Copyright 2013 Hewlett-Packard Development Company, L.P.
ivan-zhu8f992be2013-07-31 14:56:58 +08003# Copyright 2013 IBM Corp
ivan-zhu09111942013-08-01 08:09:16 +08004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import json
19import time
20import urllib
21
Ghanshyam7fa397c2014-04-01 19:32:38 +090022from tempest.api_schema.compute import servers as common_schema
Ken'ichi Ohmichi02604582014-03-14 16:23:41 +090023from tempest.api_schema.compute.v3 import servers as schema
Haiwei Xu16ee2c82014-03-05 04:59:18 +090024from tempest.common import rest_client
ivan-zhu09111942013-08-01 08:09:16 +080025from tempest.common import waiters
Matthew Treinish684d8992014-01-30 16:27:40 +000026from tempest import config
ivan-zhu09111942013-08-01 08:09:16 +080027from tempest import exceptions
28
Matthew Treinish684d8992014-01-30 16:27:40 +000029CONF = config.CONF
30
ivan-zhu09111942013-08-01 08:09:16 +080031
Haiwei Xu16ee2c82014-03-05 04:59:18 +090032class ServersV3ClientJSON(rest_client.RestClient):
ivan-zhu09111942013-08-01 08:09:16 +080033
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000034 def __init__(self, auth_provider):
35 super(ServersV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000036 self.service = CONF.compute.catalog_v3_type
ivan-zhu09111942013-08-01 08:09:16 +080037
38 def create_server(self, name, image_ref, flavor_ref, **kwargs):
39 """
40 Creates an instance of a server.
41 name (Required): The name of the server.
42 image_ref (Required): Reference to the image used to build the server.
43 flavor_ref (Required): The flavor used to build the server.
44 Following optional keyword arguments are accepted:
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +090045 admin_password: Sets the initial root password.
ivan-zhu09111942013-08-01 08:09:16 +080046 key_name: Key name of keypair that was created earlier.
47 meta: A dictionary of values to be used as metadata.
ivan-zhu09111942013-08-01 08:09:16 +080048 security_groups: A list of security group dicts.
49 networks: A list of network dicts with UUID and fixed_ip.
50 user_data: User data for instance.
51 availability_zone: Availability zone in which to launch instance.
ivan-zhu8f992be2013-07-31 14:56:58 +080052 access_ip_v4: The IPv4 access address for the server.
53 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +080054 min_count: Count of minimum number of instances to launch.
55 max_count: Count of maximum number of instances to launch.
56 disk_config: Determines if user or admin controls disk configuration.
57 return_reservation_id: Enable/Disable the return of reservation id
Andrey Pavlov2a2fc412014-06-09 11:02:53 +040058 block_device_mapping: Block device mapping for the server.
ivan-zhu09111942013-08-01 08:09:16 +080059 """
60 post_body = {
61 'name': name,
ivan-zhu8f992be2013-07-31 14:56:58 +080062 'image_ref': image_ref,
63 'flavor_ref': flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +080064 }
65
ivan-zhu82094252013-11-21 10:16:11 +080066 for option in ['admin_password', 'key_name', 'networks',
ivan-zhu2ca89b32013-08-07 22:37:32 +080067 ('os-security-groups:security_groups',
68 'security_groups'),
ivan-zhu8f992be2013-07-31 14:56:58 +080069 ('os-user-data:user_data', 'user_data'),
70 ('os-availability-zone:availability_zone',
71 'availability_zone'),
ivan-zhu2ca89b32013-08-07 22:37:32 +080072 ('os-access-ips:access_ip_v4', 'access_ip_v4'),
73 ('os-access-ips:access_ip_v6', 'access_ip_v6'),
ivan-zhu8f992be2013-07-31 14:56:58 +080074 ('os-multiple-create:min_count', 'min_count'),
75 ('os-multiple-create:max_count', 'max_count'),
76 ('metadata', 'meta'),
77 ('os-disk-config:disk_config', 'disk_config'),
78 ('os-multiple-create:return_reservation_id',
Andrey Pavlov2a2fc412014-06-09 11:02:53 +040079 'return_reservation_id'),
80 ('os-block-device-mapping:block_device_mapping',
81 'block_device_mapping')]:
ivan-zhu09111942013-08-01 08:09:16 +080082 if isinstance(option, tuple):
83 post_param = option[0]
84 key = option[1]
85 else:
86 post_param = option
87 key = option
88 value = kwargs.get(key)
89 if value is not None:
90 post_body[post_param] = value
91 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020092 resp, body = self.post('servers', post_body)
ivan-zhu09111942013-08-01 08:09:16 +080093
94 body = json.loads(body)
95 # NOTE(maurosr): this deals with the case of multiple server create
96 # with return reservation id set True
ivan-zhuf0bf3012013-11-25 16:03:42 +080097 if 'servers_reservation' in body:
98 return resp, body['servers_reservation']
Ken'ichi Ohmichi02604582014-03-14 16:23:41 +090099 self.validate_response(schema.create_server, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800100 return resp, body['server']
101
ivan-zhu8f992be2013-07-31 14:56:58 +0800102 def update_server(self, server_id, name=None, meta=None, access_ip_v4=None,
103 access_ip_v6=None, disk_config=None):
ivan-zhu09111942013-08-01 08:09:16 +0800104 """
105 Updates the properties of an existing server.
106 server_id: The id of an existing server.
107 name: The name of the server.
ivan-zhu8f992be2013-07-31 14:56:58 +0800108 access_ip_v4: The IPv4 access address for the server.
109 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +0800110 """
111
112 post_body = {}
113
114 if meta is not None:
115 post_body['metadata'] = meta
116
117 if name is not None:
118 post_body['name'] = name
119
ivan-zhu8f992be2013-07-31 14:56:58 +0800120 if access_ip_v4 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800121 post_body['os-access-ips:access_ip_v4'] = access_ip_v4
ivan-zhu09111942013-08-01 08:09:16 +0800122
ivan-zhu8f992be2013-07-31 14:56:58 +0800123 if access_ip_v6 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800124 post_body['os-access-ips:access_ip_v6'] = access_ip_v6
ivan-zhu09111942013-08-01 08:09:16 +0800125
126 if disk_config is not None:
ivan-zhu50969522013-08-26 11:10:39 +0800127 post_body['os-disk-config:disk_config'] = disk_config
ivan-zhu09111942013-08-01 08:09:16 +0800128
129 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +0200130 resp, body = self.put("servers/%s" % str(server_id), post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800131 body = json.loads(body)
Ken'ichi Ohmichi29cd5122014-04-28 11:04:52 +0900132 self.validate_response(schema.update_server, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800133 return resp, body['server']
134
135 def get_server(self, server_id):
136 """Returns the details of an existing server."""
137 resp, body = self.get("servers/%s" % str(server_id))
138 body = json.loads(body)
139 return resp, body['server']
140
141 def delete_server(self, server_id):
142 """Deletes the given server."""
Ghanshyam4ac02742014-04-17 19:15:47 +0900143 resp, body = self.delete("servers/%s" % str(server_id))
144 self.validate_response(common_schema.delete_server, resp, body)
145 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800146
147 def list_servers(self, params=None):
148 """Lists all servers for a user."""
149
150 url = 'servers'
151 if params:
152 url += '?%s' % urllib.urlencode(params)
153
154 resp, body = self.get(url)
155 body = json.loads(body)
Ghanshyam623c38f2014-04-21 17:16:21 +0900156 self.validate_response(common_schema.list_servers, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800157 return resp, body
158
159 def list_servers_with_detail(self, params=None):
160 """Lists all servers in detail for a user."""
161
162 url = 'servers/detail'
163 if params:
164 url += '?%s' % urllib.urlencode(params)
165
166 resp, body = self.get(url)
167 body = json.loads(body)
168 return resp, body
169
Zhi Kun Liude892722013-12-30 15:27:52 +0800170 def wait_for_server_status(self, server_id, status, extra_timeout=0,
171 raise_on_error=True):
ivan-zhu09111942013-08-01 08:09:16 +0800172 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900173 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liude892722013-12-30 15:27:52 +0800174 extra_timeout=extra_timeout,
175 raise_on_error=raise_on_error)
ivan-zhu09111942013-08-01 08:09:16 +0800176
177 def wait_for_server_termination(self, server_id, ignore_error=False):
178 """Waits for server to reach termination."""
179 start_time = int(time.time())
180 while True:
181 try:
182 resp, body = self.get_server(server_id)
183 except exceptions.NotFound:
184 return
185
186 server_status = body['status']
187 if server_status == 'ERROR' and not ignore_error:
188 raise exceptions.BuildErrorException(server_id=server_id)
189
190 if int(time.time()) - start_time >= self.build_timeout:
191 raise exceptions.TimeoutException
192
193 time.sleep(self.build_interval)
194
195 def list_addresses(self, server_id):
196 """Lists all addresses for a server."""
197 resp, body = self.get("servers/%s/ips" % str(server_id))
198 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900199 self.validate_response(schema.list_addresses, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800200 return resp, body['addresses']
201
202 def list_addresses_by_network(self, server_id, network_id):
203 """Lists all addresses of a specific network type for a server."""
204 resp, body = self.get("servers/%s/ips/%s" %
205 (str(server_id), network_id))
206 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900207 self.validate_response(schema.list_addresses_by_network, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800208 return resp, body
209
Ghanshyam997c9092014-04-03 19:00:20 +0900210 def action(self, server_id, action_name, response_key,
211 schema=common_schema.server_actions_common_schema, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800212 post_body = json.dumps({action_name: kwargs})
213 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200214 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800215 if response_key is not None:
Ghanshyam7ee264a2014-04-02 16:37:57 +0900216 body = json.loads(body)
217 # Check for Schema as 'None' because if we do not have any server
218 # action schema implemented yet then they can pass 'None' to skip
219 # the validation.Once all server action has their schema
220 # implemented then, this check can be removed if every actions are
221 # supposed to validate their response.
222 # TODO(GMann): Remove the below 'if' check once all server actions
223 # schema are implemented.
224 if schema is not None:
225 self.validate_response(schema, resp, body)
226 body = body[response_key]
Ghanshyam997c9092014-04-03 19:00:20 +0900227 else:
228 self.validate_response(schema, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800229 return resp, body
230
ivan-zhu2ca89b32013-08-07 22:37:32 +0800231 def create_backup(self, server_id, backup_type, rotation, name):
232 """Backup a server instance."""
233 return self.action(server_id, "create_backup", None,
234 backup_type=backup_type,
235 rotation=rotation,
236 name=name)
237
ivan-zhu8f992be2013-07-31 14:56:58 +0800238 def change_password(self, server_id, admin_password):
ivan-zhu09111942013-08-01 08:09:16 +0800239 """Changes the root password for the server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900240 return self.action(server_id, 'change_password',
241 None, schema.server_actions_change_password,
ivan-zhu8f992be2013-07-31 14:56:58 +0800242 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800243
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800244 def get_password(self, server_id):
245 resp, body = self.get("servers/%s/os-server-password" %
246 str(server_id))
247 body = json.loads(body)
Ghanshyam7fa397c2014-04-01 19:32:38 +0900248 self.validate_response(common_schema.get_password, resp, body)
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800249 return resp, body
250
251 def delete_password(self, server_id):
252 """
253 Removes the encrypted server password from the metadata server
254 Note that this does not actually change the instance server
255 password.
256 """
Ghanshyam997c9092014-04-03 19:00:20 +0900257 resp, body = self.delete("servers/%s/os-server-password" %
258 str(server_id))
259 self.validate_response(common_schema.server_actions_delete_password,
260 resp, body)
261 return resp, body
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800262
ivan-zhu09111942013-08-01 08:09:16 +0800263 def reboot(self, server_id, reboot_type):
264 """Reboots a server."""
265 return self.action(server_id, 'reboot', None, type=reboot_type)
266
267 def rebuild(self, server_id, image_ref, **kwargs):
268 """Rebuilds a server with a new image."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800269 kwargs['image_ref'] = image_ref
ivan-zhu09111942013-08-01 08:09:16 +0800270 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800271 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800272 del kwargs['disk_config']
Ghanshyam7ee264a2014-04-02 16:37:57 +0900273 return self.action(server_id, 'rebuild', 'server', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800274
275 def resize(self, server_id, flavor_ref, **kwargs):
276 """Changes the flavor of a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800277 kwargs['flavor_ref'] = flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +0800278 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800279 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800280 del kwargs['disk_config']
281 return self.action(server_id, 'resize', None, **kwargs)
282
283 def confirm_resize(self, server_id, **kwargs):
284 """Confirms the flavor change for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800285 return self.action(server_id, 'confirm_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800286
287 def revert_resize(self, server_id, **kwargs):
288 """Reverts a server back to its original flavor."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800289 return self.action(server_id, 'revert_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800290
ivan-zhu8f992be2013-07-31 14:56:58 +0800291 def create_image(self, server_id, name, meta=None):
292 """Creates an image of the original server."""
293
294 post_body = {
295 'create_image': {
296 'name': name,
297 }
298 }
299
300 if meta is not None:
301 post_body['create_image']['metadata'] = meta
302
303 post_body = json.dumps(post_body)
304 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200305 post_body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800306 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800307
308 def list_server_metadata(self, server_id):
309 resp, body = self.get("servers/%s/metadata" % str(server_id))
310 body = json.loads(body)
Ghanshyama8f66532014-04-23 17:18:14 +0900311 self.validate_response(common_schema.list_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800312 return resp, body['metadata']
313
314 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
315 if no_metadata_field:
316 post_body = ""
317 else:
318 post_body = json.dumps({'metadata': meta})
319 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200320 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800321 body = json.loads(body)
Haiwei Xu3d2b7af2014-04-12 02:50:26 +0900322 self.validate_response(common_schema.set_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800323 return resp, body['metadata']
324
325 def update_server_metadata(self, server_id, meta):
326 post_body = json.dumps({'metadata': meta})
327 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200328 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800329 body = json.loads(body)
Ghanshyame8421062014-06-02 15:58:21 +0900330 self.validate_response(schema.update_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800331 return resp, body['metadata']
332
333 def get_server_metadata_item(self, server_id, key):
334 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
335 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900336 self.validate_response(schema.set_get_server_metadata_item,
337 resp, body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800338 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800339
340 def set_server_metadata_item(self, server_id, key, meta):
ivan-zhu8f992be2013-07-31 14:56:58 +0800341 post_body = json.dumps({'metadata': meta})
ivan-zhu09111942013-08-01 08:09:16 +0800342 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200343 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800344 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900345 self.validate_response(schema.set_get_server_metadata_item,
346 resp, body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800347 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800348
349 def delete_server_metadata_item(self, server_id, key):
350 resp, body = self.delete("servers/%s/metadata/%s" %
351 (str(server_id), key))
Ghanshyameaaa6a42014-04-25 18:38:21 +0900352 self.validate_response(common_schema.delete_server_metadata_item,
353 resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800354 return resp, body
355
356 def stop(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800357 return self.action(server_id, 'stop', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800358
359 def start(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800360 return self.action(server_id, 'start', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800361
362 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
363 """Attaches a volume to a server instance."""
Ghanshyam385c4e72014-03-27 11:42:25 +0900364 resp, body = self.action(server_id, 'attach', None,
365 volume_id=volume_id, device=device)
366 self.validate_response(schema.attach_detach_volume, resp, body)
367 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800368
369 def detach_volume(self, server_id, volume_id):
370 """Detaches a volume from a server instance."""
Ghanshyam385c4e72014-03-27 11:42:25 +0900371 resp, body = self.action(server_id, 'detach', None,
372 volume_id=volume_id)
373 self.validate_response(schema.attach_detach_volume, resp, body)
374 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800375
ivan-zhu09111942013-08-01 08:09:16 +0800376 def live_migrate_server(self, server_id, dest_host, use_block_migration):
377 """This should be called with administrator privileges ."""
378
379 migrate_params = {
380 "disk_over_commit": False,
381 "block_migration": use_block_migration,
382 "host": dest_host
383 }
384
ivan-zhu8f992be2013-07-31 14:56:58 +0800385 req_body = json.dumps({'migrate_live': migrate_params})
ivan-zhu09111942013-08-01 08:09:16 +0800386
387 resp, body = self.post("servers/%s/action" % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200388 req_body)
Ghanshyam997c9092014-04-03 19:00:20 +0900389 self.validate_response(common_schema.server_actions_common_schema,
390 resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800391 return resp, body
392
393 def migrate_server(self, server_id, **kwargs):
394 """Migrates a server to a new host."""
395 return self.action(server_id, 'migrate', None, **kwargs)
396
397 def lock_server(self, server_id, **kwargs):
398 """Locks the given server."""
399 return self.action(server_id, 'lock', None, **kwargs)
400
401 def unlock_server(self, server_id, **kwargs):
402 """UNlocks the given server."""
403 return self.action(server_id, 'unlock', None, **kwargs)
404
405 def suspend_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800406 """Suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800407 return self.action(server_id, 'suspend', None, **kwargs)
408
409 def resume_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800410 """Un-suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800411 return self.action(server_id, 'resume', None, **kwargs)
412
413 def pause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800414 """Pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800415 return self.action(server_id, 'pause', None, **kwargs)
416
417 def unpause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800418 """Un-pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800419 return self.action(server_id, 'unpause', None, **kwargs)
420
421 def reset_state(self, server_id, state='error'):
422 """Resets the state of a server to active/error."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800423 return self.action(server_id, 'reset_state', None, state=state)
ivan-zhu09111942013-08-01 08:09:16 +0800424
ivan-zhu2ca89b32013-08-07 22:37:32 +0800425 def shelve_server(self, server_id, **kwargs):
426 """Shelves the provided server."""
427 return self.action(server_id, 'shelve', None, **kwargs)
428
429 def unshelve_server(self, server_id, **kwargs):
430 """Un-shelves the provided server."""
431 return self.action(server_id, 'unshelve', None, **kwargs)
432
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900433 def shelve_offload_server(self, server_id, **kwargs):
434 """Shelve-offload the provided server."""
435 return self.action(server_id, 'shelve_offload', None, **kwargs)
436
ivan-zhu09111942013-08-01 08:09:16 +0800437 def get_console_output(self, server_id, length):
Matt Riedemann20e18dd2014-06-10 09:13:23 -0700438 if length is None:
439 # NOTE(mriedem): -1 means optional/unlimited in the nova v3 API.
440 length = -1
ivan-zhu8f992be2013-07-31 14:56:58 +0800441 return self.action(server_id, 'get_console_output', 'output',
Ghanshyam7ee264a2014-04-02 16:37:57 +0900442 common_schema.get_console_output, length=length)
ivan-zhu09111942013-08-01 08:09:16 +0800443
Yuiko Takada7835d502014-01-15 10:15:03 +0000444 def rescue_server(self, server_id, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800445 """Rescue the provided server."""
Ghanshyam7ee264a2014-04-02 16:37:57 +0900446 return self.action(server_id, 'rescue', 'admin_password',
447 None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800448
449 def unrescue_server(self, server_id):
450 """Unrescue the provided server."""
451 return self.action(server_id, 'unrescue', None)
452
453 def get_server_diagnostics(self, server_id):
454 """Get the usage data for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800455 resp, body = self.get("servers/%s/os-server-diagnostics" %
456 str(server_id))
ivan-zhu09111942013-08-01 08:09:16 +0800457 return resp, json.loads(body)
458
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200459 def list_server_actions(self, server_id):
ivan-zhu09111942013-08-01 08:09:16 +0800460 """List the provided server action."""
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200461 resp, body = self.get("servers/%s/os-server-actions" %
ivan-zhu09111942013-08-01 08:09:16 +0800462 str(server_id))
463 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900464 self.validate_response(schema.list_server_actions, resp, body)
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200465 return resp, body['server_actions']
ivan-zhu09111942013-08-01 08:09:16 +0800466
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200467 def get_server_action(self, server_id, request_id):
ivan-zhu09111942013-08-01 08:09:16 +0800468 """Returns the action details of the provided server."""
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200469 resp, body = self.get("servers/%s/os-server-actions/%s" %
ivan-zhu09111942013-08-01 08:09:16 +0800470 (str(server_id), str(request_id)))
471 body = json.loads(body)
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200472 return resp, body['server_action']
ivan-zhu2ca89b32013-08-07 22:37:32 +0800473
474 def force_delete_server(self, server_id, **kwargs):
475 """Force delete a server."""
476 return self.action(server_id, 'force_delete', None, **kwargs)
477
478 def restore_soft_deleted_server(self, server_id, **kwargs):
479 """Restore a soft-deleted server."""
480 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900481
482 def get_vnc_console(self, server_id, type):
483 """Get URL of VNC console."""
484 post_body = json.dumps({
485 "get_vnc_console": {
486 "type": type
487 }
488 })
489 resp, body = self.post('servers/%s/action' % str(server_id),
490 post_body)
491 body = json.loads(body)
Ghanshyamd6d30402014-04-02 15:28:37 +0900492 self.validate_response(common_schema.get_vnc_console, resp, body)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900493 return resp, body['console']
Ghanshyam0549f7b2014-03-04 19:07:52 +0900494
495 def reset_network(self, server_id, **kwargs):
496 """Resets the Network of a server"""
497 return self.action(server_id, 'reset_network', None, **kwargs)
498
499 def inject_network_info(self, server_id, **kwargs):
500 """Inject the Network Info into server"""
501 return self.action(server_id, 'inject_network_info', None, **kwargs)
Ghanshyam70876d02014-03-11 11:40:18 +0900502
503 def get_spice_console(self, server_id, console_type):
504 """Get URL of Spice console."""
505 return self.action(server_id, "get_spice_console"
Ghanshyam7ee264a2014-04-02 16:37:57 +0900506 "console", None, type=console_type)
Ghanshyam70876d02014-03-11 11:40:18 +0900507
508 def get_rdp_console(self, server_id, console_type):
509 """Get URL of RDP console."""
510 return self.action(server_id, "get_rdp_console"
Ghanshyam7ee264a2014-04-02 16:37:57 +0900511 "console", None, type=console_type)