blob: 89e282dfc5aa06f667f82e8ed88ea6366a50d5e7 [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020022from tempest.api_schema.response.compute import servers as common_schema
23from tempest.api_schema.response.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']
Ghanshyam7f989102014-07-29 14:23:26 +090099 if CONF.compute_feature_enabled.enable_instance_password:
100 create_schema = schema.create_server_with_admin_pass
101 else:
102 create_schema = schema.create_server
103 self.validate_response(create_schema, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800104 return resp, body['server']
105
ivan-zhu8f992be2013-07-31 14:56:58 +0800106 def update_server(self, server_id, name=None, meta=None, access_ip_v4=None,
107 access_ip_v6=None, disk_config=None):
ivan-zhu09111942013-08-01 08:09:16 +0800108 """
109 Updates the properties of an existing server.
110 server_id: The id of an existing server.
111 name: The name of the server.
ivan-zhu8f992be2013-07-31 14:56:58 +0800112 access_ip_v4: The IPv4 access address for the server.
113 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +0800114 """
115
116 post_body = {}
117
118 if meta is not None:
119 post_body['metadata'] = meta
120
121 if name is not None:
122 post_body['name'] = name
123
ivan-zhu8f992be2013-07-31 14:56:58 +0800124 if access_ip_v4 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800125 post_body['os-access-ips:access_ip_v4'] = access_ip_v4
ivan-zhu09111942013-08-01 08:09:16 +0800126
ivan-zhu8f992be2013-07-31 14:56:58 +0800127 if access_ip_v6 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800128 post_body['os-access-ips:access_ip_v6'] = access_ip_v6
ivan-zhu09111942013-08-01 08:09:16 +0800129
130 if disk_config is not None:
ivan-zhu50969522013-08-26 11:10:39 +0800131 post_body['os-disk-config:disk_config'] = disk_config
ivan-zhu09111942013-08-01 08:09:16 +0800132
133 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +0200134 resp, body = self.put("servers/%s" % str(server_id), post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800135 body = json.loads(body)
Ken'ichi Ohmichi29cd5122014-04-28 11:04:52 +0900136 self.validate_response(schema.update_server, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800137 return resp, body['server']
138
139 def get_server(self, server_id):
140 """Returns the details of an existing server."""
141 resp, body = self.get("servers/%s" % str(server_id))
142 body = json.loads(body)
Ken'ichi Ohmichi21e4fc72014-05-08 16:46:23 +0900143 self.validate_response(schema.get_server, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800144 return resp, body['server']
145
146 def delete_server(self, server_id):
147 """Deletes the given server."""
Ghanshyam4ac02742014-04-17 19:15:47 +0900148 resp, body = self.delete("servers/%s" % str(server_id))
149 self.validate_response(common_schema.delete_server, resp, body)
150 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800151
152 def list_servers(self, params=None):
153 """Lists all servers for a user."""
154
155 url = 'servers'
156 if params:
157 url += '?%s' % urllib.urlencode(params)
158
159 resp, body = self.get(url)
160 body = json.loads(body)
Ghanshyam623c38f2014-04-21 17:16:21 +0900161 self.validate_response(common_schema.list_servers, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800162 return resp, body
163
164 def list_servers_with_detail(self, params=None):
165 """Lists all servers in detail for a user."""
166
167 url = 'servers/detail'
168 if params:
169 url += '?%s' % urllib.urlencode(params)
170
171 resp, body = self.get(url)
172 body = json.loads(body)
Ghanshyam51744862014-06-13 12:56:24 +0900173 self.validate_response(schema.list_servers_detail, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800174 return resp, body
175
Zhi Kun Liude892722013-12-30 15:27:52 +0800176 def wait_for_server_status(self, server_id, status, extra_timeout=0,
177 raise_on_error=True):
ivan-zhu09111942013-08-01 08:09:16 +0800178 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900179 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liude892722013-12-30 15:27:52 +0800180 extra_timeout=extra_timeout,
181 raise_on_error=raise_on_error)
ivan-zhu09111942013-08-01 08:09:16 +0800182
183 def wait_for_server_termination(self, server_id, ignore_error=False):
184 """Waits for server to reach termination."""
185 start_time = int(time.time())
186 while True:
187 try:
188 resp, body = self.get_server(server_id)
189 except exceptions.NotFound:
190 return
191
192 server_status = body['status']
193 if server_status == 'ERROR' and not ignore_error:
194 raise exceptions.BuildErrorException(server_id=server_id)
195
196 if int(time.time()) - start_time >= self.build_timeout:
197 raise exceptions.TimeoutException
198
199 time.sleep(self.build_interval)
200
201 def list_addresses(self, server_id):
202 """Lists all addresses for a server."""
203 resp, body = self.get("servers/%s/ips" % str(server_id))
204 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900205 self.validate_response(schema.list_addresses, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800206 return resp, body['addresses']
207
208 def list_addresses_by_network(self, server_id, network_id):
209 """Lists all addresses of a specific network type for a server."""
210 resp, body = self.get("servers/%s/ips/%s" %
211 (str(server_id), network_id))
212 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900213 self.validate_response(schema.list_addresses_by_network, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800214 return resp, body
215
Ghanshyam997c9092014-04-03 19:00:20 +0900216 def action(self, server_id, action_name, response_key,
217 schema=common_schema.server_actions_common_schema, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800218 post_body = json.dumps({action_name: kwargs})
219 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200220 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800221 if response_key is not None:
Ghanshyam7ee264a2014-04-02 16:37:57 +0900222 body = json.loads(body)
223 # Check for Schema as 'None' because if we do not have any server
224 # action schema implemented yet then they can pass 'None' to skip
225 # the validation.Once all server action has their schema
226 # implemented then, this check can be removed if every actions are
227 # supposed to validate their response.
228 # TODO(GMann): Remove the below 'if' check once all server actions
229 # schema are implemented.
230 if schema is not None:
231 self.validate_response(schema, resp, body)
232 body = body[response_key]
Ghanshyam997c9092014-04-03 19:00:20 +0900233 else:
234 self.validate_response(schema, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800235 return resp, body
236
ivan-zhu2ca89b32013-08-07 22:37:32 +0800237 def create_backup(self, server_id, backup_type, rotation, name):
238 """Backup a server instance."""
239 return self.action(server_id, "create_backup", None,
240 backup_type=backup_type,
241 rotation=rotation,
242 name=name)
243
ivan-zhu8f992be2013-07-31 14:56:58 +0800244 def change_password(self, server_id, admin_password):
ivan-zhu09111942013-08-01 08:09:16 +0800245 """Changes the root password for the server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900246 return self.action(server_id, 'change_password',
247 None, schema.server_actions_change_password,
ivan-zhu8f992be2013-07-31 14:56:58 +0800248 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800249
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800250 def get_password(self, server_id):
251 resp, body = self.get("servers/%s/os-server-password" %
252 str(server_id))
253 body = json.loads(body)
Ghanshyam7fa397c2014-04-01 19:32:38 +0900254 self.validate_response(common_schema.get_password, resp, body)
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800255 return resp, body
256
257 def delete_password(self, server_id):
258 """
259 Removes the encrypted server password from the metadata server
260 Note that this does not actually change the instance server
261 password.
262 """
Ghanshyam997c9092014-04-03 19:00:20 +0900263 resp, body = self.delete("servers/%s/os-server-password" %
264 str(server_id))
265 self.validate_response(common_schema.server_actions_delete_password,
266 resp, body)
267 return resp, body
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800268
ivan-zhu09111942013-08-01 08:09:16 +0800269 def reboot(self, server_id, reboot_type):
270 """Reboots a server."""
271 return self.action(server_id, 'reboot', None, type=reboot_type)
272
273 def rebuild(self, server_id, image_ref, **kwargs):
274 """Rebuilds a server with a new image."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800275 kwargs['image_ref'] = image_ref
ivan-zhu09111942013-08-01 08:09:16 +0800276 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800277 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800278 del kwargs['disk_config']
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900279 if CONF.compute_feature_enabled.enable_instance_password:
280 rebuild_schema = schema.rebuild_server_with_admin_pass
281 else:
282 rebuild_schema = schema.rebuild_server
283 return self.action(server_id, 'rebuild', 'server',
284 rebuild_schema, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800285
286 def resize(self, server_id, flavor_ref, **kwargs):
287 """Changes the flavor of a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800288 kwargs['flavor_ref'] = flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +0800289 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800290 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800291 del kwargs['disk_config']
292 return self.action(server_id, 'resize', None, **kwargs)
293
294 def confirm_resize(self, server_id, **kwargs):
295 """Confirms the flavor change for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800296 return self.action(server_id, 'confirm_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800297
298 def revert_resize(self, server_id, **kwargs):
299 """Reverts a server back to its original flavor."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800300 return self.action(server_id, 'revert_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800301
ivan-zhu8f992be2013-07-31 14:56:58 +0800302 def create_image(self, server_id, name, meta=None):
303 """Creates an image of the original server."""
304
305 post_body = {
306 'create_image': {
307 'name': name,
308 }
309 }
310
311 if meta is not None:
312 post_body['create_image']['metadata'] = meta
313
314 post_body = json.dumps(post_body)
315 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200316 post_body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800317 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800318
319 def list_server_metadata(self, server_id):
320 resp, body = self.get("servers/%s/metadata" % str(server_id))
321 body = json.loads(body)
Ghanshyama8f66532014-04-23 17:18:14 +0900322 self.validate_response(common_schema.list_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800323 return resp, body['metadata']
324
325 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
326 if no_metadata_field:
327 post_body = ""
328 else:
329 post_body = json.dumps({'metadata': meta})
330 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200331 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800332 body = json.loads(body)
Haiwei Xu3d2b7af2014-04-12 02:50:26 +0900333 self.validate_response(common_schema.set_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800334 return resp, body['metadata']
335
336 def update_server_metadata(self, server_id, meta):
337 post_body = json.dumps({'metadata': meta})
338 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200339 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800340 body = json.loads(body)
Ghanshyame8421062014-06-02 15:58:21 +0900341 self.validate_response(schema.update_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800342 return resp, body['metadata']
343
344 def get_server_metadata_item(self, server_id, key):
345 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
346 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900347 self.validate_response(schema.set_get_server_metadata_item,
348 resp, body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800349 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800350
351 def set_server_metadata_item(self, server_id, key, meta):
ivan-zhu8f992be2013-07-31 14:56:58 +0800352 post_body = json.dumps({'metadata': meta})
ivan-zhu09111942013-08-01 08:09:16 +0800353 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200354 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800355 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900356 self.validate_response(schema.set_get_server_metadata_item,
357 resp, body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800358 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800359
360 def delete_server_metadata_item(self, server_id, key):
361 resp, body = self.delete("servers/%s/metadata/%s" %
362 (str(server_id), key))
Ghanshyameaaa6a42014-04-25 18:38:21 +0900363 self.validate_response(common_schema.delete_server_metadata_item,
364 resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800365 return resp, body
366
367 def stop(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800368 return self.action(server_id, 'stop', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800369
370 def start(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800371 return self.action(server_id, 'start', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800372
373 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
374 """Attaches a volume to a server instance."""
Ghanshyam385c4e72014-03-27 11:42:25 +0900375 resp, body = self.action(server_id, 'attach', None,
376 volume_id=volume_id, device=device)
377 self.validate_response(schema.attach_detach_volume, resp, body)
378 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800379
380 def detach_volume(self, server_id, volume_id):
381 """Detaches a volume from a server instance."""
Ghanshyam385c4e72014-03-27 11:42:25 +0900382 resp, body = self.action(server_id, 'detach', None,
383 volume_id=volume_id)
384 self.validate_response(schema.attach_detach_volume, resp, body)
385 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800386
ivan-zhu09111942013-08-01 08:09:16 +0800387 def live_migrate_server(self, server_id, dest_host, use_block_migration):
388 """This should be called with administrator privileges ."""
389
390 migrate_params = {
391 "disk_over_commit": False,
392 "block_migration": use_block_migration,
393 "host": dest_host
394 }
395
ivan-zhu8f992be2013-07-31 14:56:58 +0800396 req_body = json.dumps({'migrate_live': migrate_params})
ivan-zhu09111942013-08-01 08:09:16 +0800397
398 resp, body = self.post("servers/%s/action" % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200399 req_body)
Ghanshyam997c9092014-04-03 19:00:20 +0900400 self.validate_response(common_schema.server_actions_common_schema,
401 resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800402 return resp, body
403
404 def migrate_server(self, server_id, **kwargs):
405 """Migrates a server to a new host."""
406 return self.action(server_id, 'migrate', None, **kwargs)
407
408 def lock_server(self, server_id, **kwargs):
409 """Locks the given server."""
410 return self.action(server_id, 'lock', None, **kwargs)
411
412 def unlock_server(self, server_id, **kwargs):
413 """UNlocks the given server."""
414 return self.action(server_id, 'unlock', None, **kwargs)
415
416 def suspend_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800417 """Suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800418 return self.action(server_id, 'suspend', None, **kwargs)
419
420 def resume_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800421 """Un-suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800422 return self.action(server_id, 'resume', None, **kwargs)
423
424 def pause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800425 """Pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800426 return self.action(server_id, 'pause', None, **kwargs)
427
428 def unpause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800429 """Un-pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800430 return self.action(server_id, 'unpause', None, **kwargs)
431
432 def reset_state(self, server_id, state='error'):
433 """Resets the state of a server to active/error."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800434 return self.action(server_id, 'reset_state', None, state=state)
ivan-zhu09111942013-08-01 08:09:16 +0800435
ivan-zhu2ca89b32013-08-07 22:37:32 +0800436 def shelve_server(self, server_id, **kwargs):
437 """Shelves the provided server."""
438 return self.action(server_id, 'shelve', None, **kwargs)
439
440 def unshelve_server(self, server_id, **kwargs):
441 """Un-shelves the provided server."""
442 return self.action(server_id, 'unshelve', None, **kwargs)
443
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900444 def shelve_offload_server(self, server_id, **kwargs):
445 """Shelve-offload the provided server."""
446 return self.action(server_id, 'shelve_offload', None, **kwargs)
447
ivan-zhu09111942013-08-01 08:09:16 +0800448 def get_console_output(self, server_id, length):
Matt Riedemann20e18dd2014-06-10 09:13:23 -0700449 if length is None:
450 # NOTE(mriedem): -1 means optional/unlimited in the nova v3 API.
451 length = -1
ivan-zhu8f992be2013-07-31 14:56:58 +0800452 return self.action(server_id, 'get_console_output', 'output',
Ghanshyam7ee264a2014-04-02 16:37:57 +0900453 common_schema.get_console_output, length=length)
ivan-zhu09111942013-08-01 08:09:16 +0800454
Yuiko Takada7835d502014-01-15 10:15:03 +0000455 def rescue_server(self, server_id, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800456 """Rescue the provided server."""
Ghanshyam88457912014-07-25 16:02:12 +0900457 post_body = json.dumps({'rescue': kwargs})
458 resp, body = self.post('servers/%s/action' % str(server_id),
459 post_body)
460 if CONF.compute_feature_enabled.enable_instance_password:
461 rescue_schema = schema.rescue_server_with_admin_pass
462 else:
463 rescue_schema = schema.rescue_server
464 body = json.loads(body)
465 self.validate_response(rescue_schema, resp, body)
466 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800467
468 def unrescue_server(self, server_id):
469 """Unrescue the provided server."""
470 return self.action(server_id, 'unrescue', None)
471
472 def get_server_diagnostics(self, server_id):
473 """Get the usage data for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800474 resp, body = self.get("servers/%s/os-server-diagnostics" %
475 str(server_id))
ivan-zhu09111942013-08-01 08:09:16 +0800476 return resp, json.loads(body)
477
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200478 def list_server_actions(self, server_id):
ivan-zhu09111942013-08-01 08:09:16 +0800479 """List the provided server action."""
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200480 resp, body = self.get("servers/%s/os-server-actions" %
ivan-zhu09111942013-08-01 08:09:16 +0800481 str(server_id))
482 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900483 self.validate_response(schema.list_server_actions, resp, body)
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200484 return resp, body['server_actions']
ivan-zhu09111942013-08-01 08:09:16 +0800485
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200486 def get_server_action(self, server_id, request_id):
ivan-zhu09111942013-08-01 08:09:16 +0800487 """Returns the action details of the provided server."""
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200488 resp, body = self.get("servers/%s/os-server-actions/%s" %
ivan-zhu09111942013-08-01 08:09:16 +0800489 (str(server_id), str(request_id)))
490 body = json.loads(body)
Ghanshyam4b41dfd2014-07-17 13:40:38 +0900491 self.validate_response(schema.get_server_action, resp, body)
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200492 return resp, body['server_action']
ivan-zhu2ca89b32013-08-07 22:37:32 +0800493
494 def force_delete_server(self, server_id, **kwargs):
495 """Force delete a server."""
496 return self.action(server_id, 'force_delete', None, **kwargs)
497
498 def restore_soft_deleted_server(self, server_id, **kwargs):
499 """Restore a soft-deleted server."""
500 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900501
502 def get_vnc_console(self, server_id, type):
503 """Get URL of VNC console."""
504 post_body = json.dumps({
505 "get_vnc_console": {
506 "type": type
507 }
508 })
509 resp, body = self.post('servers/%s/action' % str(server_id),
510 post_body)
511 body = json.loads(body)
Ghanshyamd6d30402014-04-02 15:28:37 +0900512 self.validate_response(common_schema.get_vnc_console, resp, body)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900513 return resp, body['console']
Ghanshyam0549f7b2014-03-04 19:07:52 +0900514
515 def reset_network(self, server_id, **kwargs):
516 """Resets the Network of a server"""
517 return self.action(server_id, 'reset_network', None, **kwargs)
518
519 def inject_network_info(self, server_id, **kwargs):
520 """Inject the Network Info into server"""
521 return self.action(server_id, 'inject_network_info', None, **kwargs)
Ghanshyam70876d02014-03-11 11:40:18 +0900522
523 def get_spice_console(self, server_id, console_type):
524 """Get URL of Spice console."""
525 return self.action(server_id, "get_spice_console"
Ghanshyam7ee264a2014-04-02 16:37:57 +0900526 "console", None, type=console_type)
Ghanshyam70876d02014-03-11 11:40:18 +0900527
528 def get_rdp_console(self, server_id, console_type):
529 """Get URL of RDP console."""
530 return self.action(server_id, "get_rdp_console"
Ghanshyam7ee264a2014-04-02 16:37:57 +0900531 "console", None, type=console_type)