blob: 51c449922350b7950e3bc9985cfab0b7e7c80f9b [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)
Ken'ichi Ohmichi21e4fc72014-05-08 16:46:23 +0900139 self.validate_response(schema.get_server, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800140 return resp, body['server']
141
142 def delete_server(self, server_id):
143 """Deletes the given server."""
Ghanshyam4ac02742014-04-17 19:15:47 +0900144 resp, body = self.delete("servers/%s" % str(server_id))
145 self.validate_response(common_schema.delete_server, resp, body)
146 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800147
148 def list_servers(self, params=None):
149 """Lists all servers for a user."""
150
151 url = 'servers'
152 if params:
153 url += '?%s' % urllib.urlencode(params)
154
155 resp, body = self.get(url)
156 body = json.loads(body)
Ghanshyam623c38f2014-04-21 17:16:21 +0900157 self.validate_response(common_schema.list_servers, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800158 return resp, body
159
160 def list_servers_with_detail(self, params=None):
161 """Lists all servers in detail for a user."""
162
163 url = 'servers/detail'
164 if params:
165 url += '?%s' % urllib.urlencode(params)
166
167 resp, body = self.get(url)
168 body = json.loads(body)
Ghanshyam51744862014-06-13 12:56:24 +0900169 self.validate_response(schema.list_servers_detail, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800170 return resp, body
171
Zhi Kun Liude892722013-12-30 15:27:52 +0800172 def wait_for_server_status(self, server_id, status, extra_timeout=0,
173 raise_on_error=True):
ivan-zhu09111942013-08-01 08:09:16 +0800174 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900175 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liude892722013-12-30 15:27:52 +0800176 extra_timeout=extra_timeout,
177 raise_on_error=raise_on_error)
ivan-zhu09111942013-08-01 08:09:16 +0800178
179 def wait_for_server_termination(self, server_id, ignore_error=False):
180 """Waits for server to reach termination."""
181 start_time = int(time.time())
182 while True:
183 try:
184 resp, body = self.get_server(server_id)
185 except exceptions.NotFound:
186 return
187
188 server_status = body['status']
189 if server_status == 'ERROR' and not ignore_error:
190 raise exceptions.BuildErrorException(server_id=server_id)
191
192 if int(time.time()) - start_time >= self.build_timeout:
193 raise exceptions.TimeoutException
194
195 time.sleep(self.build_interval)
196
197 def list_addresses(self, server_id):
198 """Lists all addresses for a server."""
199 resp, body = self.get("servers/%s/ips" % str(server_id))
200 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900201 self.validate_response(schema.list_addresses, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800202 return resp, body['addresses']
203
204 def list_addresses_by_network(self, server_id, network_id):
205 """Lists all addresses of a specific network type for a server."""
206 resp, body = self.get("servers/%s/ips/%s" %
207 (str(server_id), network_id))
208 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900209 self.validate_response(schema.list_addresses_by_network, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800210 return resp, body
211
Ghanshyam997c9092014-04-03 19:00:20 +0900212 def action(self, server_id, action_name, response_key,
213 schema=common_schema.server_actions_common_schema, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800214 post_body = json.dumps({action_name: kwargs})
215 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200216 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800217 if response_key is not None:
Ghanshyam7ee264a2014-04-02 16:37:57 +0900218 body = json.loads(body)
219 # Check for Schema as 'None' because if we do not have any server
220 # action schema implemented yet then they can pass 'None' to skip
221 # the validation.Once all server action has their schema
222 # implemented then, this check can be removed if every actions are
223 # supposed to validate their response.
224 # TODO(GMann): Remove the below 'if' check once all server actions
225 # schema are implemented.
226 if schema is not None:
227 self.validate_response(schema, resp, body)
228 body = body[response_key]
Ghanshyam997c9092014-04-03 19:00:20 +0900229 else:
230 self.validate_response(schema, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800231 return resp, body
232
ivan-zhu2ca89b32013-08-07 22:37:32 +0800233 def create_backup(self, server_id, backup_type, rotation, name):
234 """Backup a server instance."""
235 return self.action(server_id, "create_backup", None,
236 backup_type=backup_type,
237 rotation=rotation,
238 name=name)
239
ivan-zhu8f992be2013-07-31 14:56:58 +0800240 def change_password(self, server_id, admin_password):
ivan-zhu09111942013-08-01 08:09:16 +0800241 """Changes the root password for the server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900242 return self.action(server_id, 'change_password',
243 None, schema.server_actions_change_password,
ivan-zhu8f992be2013-07-31 14:56:58 +0800244 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800245
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800246 def get_password(self, server_id):
247 resp, body = self.get("servers/%s/os-server-password" %
248 str(server_id))
249 body = json.loads(body)
Ghanshyam7fa397c2014-04-01 19:32:38 +0900250 self.validate_response(common_schema.get_password, resp, body)
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800251 return resp, body
252
253 def delete_password(self, server_id):
254 """
255 Removes the encrypted server password from the metadata server
256 Note that this does not actually change the instance server
257 password.
258 """
Ghanshyam997c9092014-04-03 19:00:20 +0900259 resp, body = self.delete("servers/%s/os-server-password" %
260 str(server_id))
261 self.validate_response(common_schema.server_actions_delete_password,
262 resp, body)
263 return resp, body
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800264
ivan-zhu09111942013-08-01 08:09:16 +0800265 def reboot(self, server_id, reboot_type):
266 """Reboots a server."""
267 return self.action(server_id, 'reboot', None, type=reboot_type)
268
269 def rebuild(self, server_id, image_ref, **kwargs):
270 """Rebuilds a server with a new image."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800271 kwargs['image_ref'] = image_ref
ivan-zhu09111942013-08-01 08:09:16 +0800272 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800273 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800274 del kwargs['disk_config']
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900275 if CONF.compute_feature_enabled.enable_instance_password:
276 rebuild_schema = schema.rebuild_server_with_admin_pass
277 else:
278 rebuild_schema = schema.rebuild_server
279 return self.action(server_id, 'rebuild', 'server',
280 rebuild_schema, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800281
282 def resize(self, server_id, flavor_ref, **kwargs):
283 """Changes the flavor of a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800284 kwargs['flavor_ref'] = flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +0800285 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800286 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800287 del kwargs['disk_config']
288 return self.action(server_id, 'resize', None, **kwargs)
289
290 def confirm_resize(self, server_id, **kwargs):
291 """Confirms the flavor change for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800292 return self.action(server_id, 'confirm_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800293
294 def revert_resize(self, server_id, **kwargs):
295 """Reverts a server back to its original flavor."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800296 return self.action(server_id, 'revert_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800297
ivan-zhu8f992be2013-07-31 14:56:58 +0800298 def create_image(self, server_id, name, meta=None):
299 """Creates an image of the original server."""
300
301 post_body = {
302 'create_image': {
303 'name': name,
304 }
305 }
306
307 if meta is not None:
308 post_body['create_image']['metadata'] = meta
309
310 post_body = json.dumps(post_body)
311 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200312 post_body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800313 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800314
315 def list_server_metadata(self, server_id):
316 resp, body = self.get("servers/%s/metadata" % str(server_id))
317 body = json.loads(body)
Ghanshyama8f66532014-04-23 17:18:14 +0900318 self.validate_response(common_schema.list_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800319 return resp, body['metadata']
320
321 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
322 if no_metadata_field:
323 post_body = ""
324 else:
325 post_body = json.dumps({'metadata': meta})
326 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200327 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800328 body = json.loads(body)
Haiwei Xu3d2b7af2014-04-12 02:50:26 +0900329 self.validate_response(common_schema.set_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800330 return resp, body['metadata']
331
332 def update_server_metadata(self, server_id, meta):
333 post_body = json.dumps({'metadata': meta})
334 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200335 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800336 body = json.loads(body)
Ghanshyame8421062014-06-02 15:58:21 +0900337 self.validate_response(schema.update_server_metadata, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800338 return resp, body['metadata']
339
340 def get_server_metadata_item(self, server_id, key):
341 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
342 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900343 self.validate_response(schema.set_get_server_metadata_item,
344 resp, body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800345 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800346
347 def set_server_metadata_item(self, server_id, key, meta):
ivan-zhu8f992be2013-07-31 14:56:58 +0800348 post_body = json.dumps({'metadata': meta})
ivan-zhu09111942013-08-01 08:09:16 +0800349 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200350 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800351 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900352 self.validate_response(schema.set_get_server_metadata_item,
353 resp, body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800354 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800355
356 def delete_server_metadata_item(self, server_id, key):
357 resp, body = self.delete("servers/%s/metadata/%s" %
358 (str(server_id), key))
Ghanshyameaaa6a42014-04-25 18:38:21 +0900359 self.validate_response(common_schema.delete_server_metadata_item,
360 resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800361 return resp, body
362
363 def stop(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800364 return self.action(server_id, 'stop', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800365
366 def start(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800367 return self.action(server_id, 'start', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800368
369 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
370 """Attaches a volume to a server instance."""
Ghanshyam385c4e72014-03-27 11:42:25 +0900371 resp, body = self.action(server_id, 'attach', None,
372 volume_id=volume_id, device=device)
373 self.validate_response(schema.attach_detach_volume, resp, body)
374 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800375
376 def detach_volume(self, server_id, volume_id):
377 """Detaches a volume from a server instance."""
Ghanshyam385c4e72014-03-27 11:42:25 +0900378 resp, body = self.action(server_id, 'detach', None,
379 volume_id=volume_id)
380 self.validate_response(schema.attach_detach_volume, resp, body)
381 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800382
ivan-zhu09111942013-08-01 08:09:16 +0800383 def live_migrate_server(self, server_id, dest_host, use_block_migration):
384 """This should be called with administrator privileges ."""
385
386 migrate_params = {
387 "disk_over_commit": False,
388 "block_migration": use_block_migration,
389 "host": dest_host
390 }
391
ivan-zhu8f992be2013-07-31 14:56:58 +0800392 req_body = json.dumps({'migrate_live': migrate_params})
ivan-zhu09111942013-08-01 08:09:16 +0800393
394 resp, body = self.post("servers/%s/action" % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200395 req_body)
Ghanshyam997c9092014-04-03 19:00:20 +0900396 self.validate_response(common_schema.server_actions_common_schema,
397 resp, body)
ivan-zhu09111942013-08-01 08:09:16 +0800398 return resp, body
399
400 def migrate_server(self, server_id, **kwargs):
401 """Migrates a server to a new host."""
402 return self.action(server_id, 'migrate', None, **kwargs)
403
404 def lock_server(self, server_id, **kwargs):
405 """Locks the given server."""
406 return self.action(server_id, 'lock', None, **kwargs)
407
408 def unlock_server(self, server_id, **kwargs):
409 """UNlocks the given server."""
410 return self.action(server_id, 'unlock', None, **kwargs)
411
412 def suspend_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800413 """Suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800414 return self.action(server_id, 'suspend', None, **kwargs)
415
416 def resume_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800417 """Un-suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800418 return self.action(server_id, 'resume', None, **kwargs)
419
420 def pause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800421 """Pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800422 return self.action(server_id, 'pause', None, **kwargs)
423
424 def unpause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800425 """Un-pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800426 return self.action(server_id, 'unpause', None, **kwargs)
427
428 def reset_state(self, server_id, state='error'):
429 """Resets the state of a server to active/error."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800430 return self.action(server_id, 'reset_state', None, state=state)
ivan-zhu09111942013-08-01 08:09:16 +0800431
ivan-zhu2ca89b32013-08-07 22:37:32 +0800432 def shelve_server(self, server_id, **kwargs):
433 """Shelves the provided server."""
434 return self.action(server_id, 'shelve', None, **kwargs)
435
436 def unshelve_server(self, server_id, **kwargs):
437 """Un-shelves the provided server."""
438 return self.action(server_id, 'unshelve', None, **kwargs)
439
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900440 def shelve_offload_server(self, server_id, **kwargs):
441 """Shelve-offload the provided server."""
442 return self.action(server_id, 'shelve_offload', None, **kwargs)
443
ivan-zhu09111942013-08-01 08:09:16 +0800444 def get_console_output(self, server_id, length):
Matt Riedemann20e18dd2014-06-10 09:13:23 -0700445 if length is None:
446 # NOTE(mriedem): -1 means optional/unlimited in the nova v3 API.
447 length = -1
ivan-zhu8f992be2013-07-31 14:56:58 +0800448 return self.action(server_id, 'get_console_output', 'output',
Ghanshyam7ee264a2014-04-02 16:37:57 +0900449 common_schema.get_console_output, length=length)
ivan-zhu09111942013-08-01 08:09:16 +0800450
Yuiko Takada7835d502014-01-15 10:15:03 +0000451 def rescue_server(self, server_id, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800452 """Rescue the provided server."""
Ghanshyam88457912014-07-25 16:02:12 +0900453 post_body = json.dumps({'rescue': kwargs})
454 resp, body = self.post('servers/%s/action' % str(server_id),
455 post_body)
456 if CONF.compute_feature_enabled.enable_instance_password:
457 rescue_schema = schema.rescue_server_with_admin_pass
458 else:
459 rescue_schema = schema.rescue_server
460 body = json.loads(body)
461 self.validate_response(rescue_schema, resp, body)
462 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800463
464 def unrescue_server(self, server_id):
465 """Unrescue the provided server."""
466 return self.action(server_id, 'unrescue', None)
467
468 def get_server_diagnostics(self, server_id):
469 """Get the usage data for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800470 resp, body = self.get("servers/%s/os-server-diagnostics" %
471 str(server_id))
ivan-zhu09111942013-08-01 08:09:16 +0800472 return resp, json.loads(body)
473
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200474 def list_server_actions(self, server_id):
ivan-zhu09111942013-08-01 08:09:16 +0800475 """List the provided server action."""
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200476 resp, body = self.get("servers/%s/os-server-actions" %
ivan-zhu09111942013-08-01 08:09:16 +0800477 str(server_id))
478 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900479 self.validate_response(schema.list_server_actions, resp, body)
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200480 return resp, body['server_actions']
ivan-zhu09111942013-08-01 08:09:16 +0800481
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200482 def get_server_action(self, server_id, request_id):
ivan-zhu09111942013-08-01 08:09:16 +0800483 """Returns the action details of the provided server."""
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200484 resp, body = self.get("servers/%s/os-server-actions/%s" %
ivan-zhu09111942013-08-01 08:09:16 +0800485 (str(server_id), str(request_id)))
486 body = json.loads(body)
Ghanshyam4b41dfd2014-07-17 13:40:38 +0900487 self.validate_response(schema.get_server_action, resp, body)
Rohan Kanade8ac8c972014-04-09 07:35:56 +0200488 return resp, body['server_action']
ivan-zhu2ca89b32013-08-07 22:37:32 +0800489
490 def force_delete_server(self, server_id, **kwargs):
491 """Force delete a server."""
492 return self.action(server_id, 'force_delete', None, **kwargs)
493
494 def restore_soft_deleted_server(self, server_id, **kwargs):
495 """Restore a soft-deleted server."""
496 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900497
498 def get_vnc_console(self, server_id, type):
499 """Get URL of VNC console."""
500 post_body = json.dumps({
501 "get_vnc_console": {
502 "type": type
503 }
504 })
505 resp, body = self.post('servers/%s/action' % str(server_id),
506 post_body)
507 body = json.loads(body)
Ghanshyamd6d30402014-04-02 15:28:37 +0900508 self.validate_response(common_schema.get_vnc_console, resp, body)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900509 return resp, body['console']
Ghanshyam0549f7b2014-03-04 19:07:52 +0900510
511 def reset_network(self, server_id, **kwargs):
512 """Resets the Network of a server"""
513 return self.action(server_id, 'reset_network', None, **kwargs)
514
515 def inject_network_info(self, server_id, **kwargs):
516 """Inject the Network Info into server"""
517 return self.action(server_id, 'inject_network_info', None, **kwargs)
Ghanshyam70876d02014-03-11 11:40:18 +0900518
519 def get_spice_console(self, server_id, console_type):
520 """Get URL of Spice console."""
521 return self.action(server_id, "get_spice_console"
Ghanshyam7ee264a2014-04-02 16:37:57 +0900522 "console", None, type=console_type)
Ghanshyam70876d02014-03-11 11:40:18 +0900523
524 def get_rdp_console(self, server_id, console_type):
525 """Get URL of RDP console."""
526 return self.action(server_id, "get_rdp_console"
Ghanshyam7ee264a2014-04-02 16:37:57 +0900527 "console", None, type=console_type)