blob: 389e6a477e2281f32bd06c088f4803da8d8129e1 [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
22from tempest.common.rest_client import RestClient
23from tempest.common import waiters
Matthew Treinish684d8992014-01-30 16:27:40 +000024from tempest import config
ivan-zhu09111942013-08-01 08:09:16 +080025from tempest import exceptions
26
Matthew Treinish684d8992014-01-30 16:27:40 +000027CONF = config.CONF
28
ivan-zhu09111942013-08-01 08:09:16 +080029
ivan-zhu8f992be2013-07-31 14:56:58 +080030class ServersV3ClientJSON(RestClient):
ivan-zhu09111942013-08-01 08:09:16 +080031
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000032 def __init__(self, auth_provider):
33 super(ServersV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000034 self.service = CONF.compute.catalog_v3_type
ivan-zhu09111942013-08-01 08:09:16 +080035
36 def create_server(self, name, image_ref, flavor_ref, **kwargs):
37 """
38 Creates an instance of a server.
39 name (Required): The name of the server.
40 image_ref (Required): Reference to the image used to build the server.
41 flavor_ref (Required): The flavor used to build the server.
42 Following optional keyword arguments are accepted:
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +090043 admin_password: Sets the initial root password.
ivan-zhu09111942013-08-01 08:09:16 +080044 key_name: Key name of keypair that was created earlier.
45 meta: A dictionary of values to be used as metadata.
ivan-zhu09111942013-08-01 08:09:16 +080046 security_groups: A list of security group dicts.
47 networks: A list of network dicts with UUID and fixed_ip.
48 user_data: User data for instance.
49 availability_zone: Availability zone in which to launch instance.
ivan-zhu8f992be2013-07-31 14:56:58 +080050 access_ip_v4: The IPv4 access address for the server.
51 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +080052 min_count: Count of minimum number of instances to launch.
53 max_count: Count of maximum number of instances to launch.
54 disk_config: Determines if user or admin controls disk configuration.
55 return_reservation_id: Enable/Disable the return of reservation id
56 """
57 post_body = {
58 'name': name,
ivan-zhu8f992be2013-07-31 14:56:58 +080059 'image_ref': image_ref,
60 'flavor_ref': flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +080061 }
62
ivan-zhu82094252013-11-21 10:16:11 +080063 for option in ['admin_password', 'key_name', 'networks',
ivan-zhu2ca89b32013-08-07 22:37:32 +080064 ('os-security-groups:security_groups',
65 'security_groups'),
ivan-zhu8f992be2013-07-31 14:56:58 +080066 ('os-user-data:user_data', 'user_data'),
67 ('os-availability-zone:availability_zone',
68 'availability_zone'),
ivan-zhu2ca89b32013-08-07 22:37:32 +080069 ('os-access-ips:access_ip_v4', 'access_ip_v4'),
70 ('os-access-ips:access_ip_v6', 'access_ip_v6'),
ivan-zhu8f992be2013-07-31 14:56:58 +080071 ('os-multiple-create:min_count', 'min_count'),
72 ('os-multiple-create:max_count', 'max_count'),
73 ('metadata', 'meta'),
74 ('os-disk-config:disk_config', 'disk_config'),
75 ('os-multiple-create:return_reservation_id',
Yuiko Takada7835d502014-01-15 10:15:03 +000076 'return_reservation_id')]:
ivan-zhu09111942013-08-01 08:09:16 +080077 if isinstance(option, tuple):
78 post_param = option[0]
79 key = option[1]
80 else:
81 post_param = option
82 key = option
83 value = kwargs.get(key)
84 if value is not None:
85 post_body[post_param] = value
86 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020087 resp, body = self.post('servers', post_body)
ivan-zhu09111942013-08-01 08:09:16 +080088
89 body = json.loads(body)
90 # NOTE(maurosr): this deals with the case of multiple server create
91 # with return reservation id set True
ivan-zhuf0bf3012013-11-25 16:03:42 +080092 if 'servers_reservation' in body:
93 return resp, body['servers_reservation']
ivan-zhu09111942013-08-01 08:09:16 +080094 return resp, body['server']
95
ivan-zhu8f992be2013-07-31 14:56:58 +080096 def update_server(self, server_id, name=None, meta=None, access_ip_v4=None,
97 access_ip_v6=None, disk_config=None):
ivan-zhu09111942013-08-01 08:09:16 +080098 """
99 Updates the properties of an existing server.
100 server_id: The id of an existing server.
101 name: The name of the server.
ivan-zhu8f992be2013-07-31 14:56:58 +0800102 access_ip_v4: The IPv4 access address for the server.
103 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +0800104 """
105
106 post_body = {}
107
108 if meta is not None:
109 post_body['metadata'] = meta
110
111 if name is not None:
112 post_body['name'] = name
113
ivan-zhu8f992be2013-07-31 14:56:58 +0800114 if access_ip_v4 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800115 post_body['os-access-ips:access_ip_v4'] = access_ip_v4
ivan-zhu09111942013-08-01 08:09:16 +0800116
ivan-zhu8f992be2013-07-31 14:56:58 +0800117 if access_ip_v6 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800118 post_body['os-access-ips:access_ip_v6'] = access_ip_v6
ivan-zhu09111942013-08-01 08:09:16 +0800119
120 if disk_config is not None:
ivan-zhu50969522013-08-26 11:10:39 +0800121 post_body['os-disk-config:disk_config'] = disk_config
ivan-zhu09111942013-08-01 08:09:16 +0800122
123 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +0200124 resp, body = self.put("servers/%s" % str(server_id), post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800125 body = json.loads(body)
126 return resp, body['server']
127
128 def get_server(self, server_id):
129 """Returns the details of an existing server."""
130 resp, body = self.get("servers/%s" % str(server_id))
131 body = json.loads(body)
132 return resp, body['server']
133
134 def delete_server(self, server_id):
135 """Deletes the given server."""
136 return self.delete("servers/%s" % str(server_id))
137
138 def list_servers(self, params=None):
139 """Lists all servers for a user."""
140
141 url = 'servers'
142 if params:
143 url += '?%s' % urllib.urlencode(params)
144
145 resp, body = self.get(url)
146 body = json.loads(body)
147 return resp, body
148
149 def list_servers_with_detail(self, params=None):
150 """Lists all servers in detail for a user."""
151
152 url = 'servers/detail'
153 if params:
154 url += '?%s' % urllib.urlencode(params)
155
156 resp, body = self.get(url)
157 body = json.loads(body)
158 return resp, body
159
Zhi Kun Liude892722013-12-30 15:27:52 +0800160 def wait_for_server_status(self, server_id, status, extra_timeout=0,
161 raise_on_error=True):
ivan-zhu09111942013-08-01 08:09:16 +0800162 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900163 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liude892722013-12-30 15:27:52 +0800164 extra_timeout=extra_timeout,
165 raise_on_error=raise_on_error)
ivan-zhu09111942013-08-01 08:09:16 +0800166
167 def wait_for_server_termination(self, server_id, ignore_error=False):
168 """Waits for server to reach termination."""
169 start_time = int(time.time())
170 while True:
171 try:
172 resp, body = self.get_server(server_id)
173 except exceptions.NotFound:
174 return
175
176 server_status = body['status']
177 if server_status == 'ERROR' and not ignore_error:
178 raise exceptions.BuildErrorException(server_id=server_id)
179
180 if int(time.time()) - start_time >= self.build_timeout:
181 raise exceptions.TimeoutException
182
183 time.sleep(self.build_interval)
184
185 def list_addresses(self, server_id):
186 """Lists all addresses for a server."""
187 resp, body = self.get("servers/%s/ips" % str(server_id))
188 body = json.loads(body)
189 return resp, body['addresses']
190
191 def list_addresses_by_network(self, server_id, network_id):
192 """Lists all addresses of a specific network type for a server."""
193 resp, body = self.get("servers/%s/ips/%s" %
194 (str(server_id), network_id))
195 body = json.loads(body)
196 return resp, body
197
198 def action(self, server_id, action_name, response_key, **kwargs):
199 post_body = json.dumps({action_name: kwargs})
200 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200201 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800202 if response_key is not None:
203 body = json.loads(body)[response_key]
204 return resp, body
205
ivan-zhu2ca89b32013-08-07 22:37:32 +0800206 def create_backup(self, server_id, backup_type, rotation, name):
207 """Backup a server instance."""
208 return self.action(server_id, "create_backup", None,
209 backup_type=backup_type,
210 rotation=rotation,
211 name=name)
212
ivan-zhu8f992be2013-07-31 14:56:58 +0800213 def change_password(self, server_id, admin_password):
ivan-zhu09111942013-08-01 08:09:16 +0800214 """Changes the root password for the server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800215 return self.action(server_id, 'change_password', None,
216 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800217
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800218 def get_password(self, server_id):
219 resp, body = self.get("servers/%s/os-server-password" %
220 str(server_id))
221 body = json.loads(body)
222 return resp, body
223
224 def delete_password(self, server_id):
225 """
226 Removes the encrypted server password from the metadata server
227 Note that this does not actually change the instance server
228 password.
229 """
230 return self.delete("servers/%s/os-server-password" %
231 str(server_id))
232
ivan-zhu09111942013-08-01 08:09:16 +0800233 def reboot(self, server_id, reboot_type):
234 """Reboots a server."""
235 return self.action(server_id, 'reboot', None, type=reboot_type)
236
237 def rebuild(self, server_id, image_ref, **kwargs):
238 """Rebuilds a server with a new image."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800239 kwargs['image_ref'] = image_ref
ivan-zhu09111942013-08-01 08:09:16 +0800240 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800241 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800242 del kwargs['disk_config']
243 return self.action(server_id, 'rebuild', 'server', **kwargs)
244
245 def resize(self, server_id, flavor_ref, **kwargs):
246 """Changes the flavor of a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800247 kwargs['flavor_ref'] = flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +0800248 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800249 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800250 del kwargs['disk_config']
251 return self.action(server_id, 'resize', None, **kwargs)
252
253 def confirm_resize(self, server_id, **kwargs):
254 """Confirms the flavor change for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800255 return self.action(server_id, 'confirm_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800256
257 def revert_resize(self, server_id, **kwargs):
258 """Reverts a server back to its original flavor."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800259 return self.action(server_id, 'revert_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800260
ivan-zhu8f992be2013-07-31 14:56:58 +0800261 def create_image(self, server_id, name, meta=None):
262 """Creates an image of the original server."""
263
264 post_body = {
265 'create_image': {
266 'name': name,
267 }
268 }
269
270 if meta is not None:
271 post_body['create_image']['metadata'] = meta
272
273 post_body = json.dumps(post_body)
274 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200275 post_body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800276 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800277
278 def list_server_metadata(self, server_id):
279 resp, body = self.get("servers/%s/metadata" % str(server_id))
280 body = json.loads(body)
281 return resp, body['metadata']
282
283 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
284 if no_metadata_field:
285 post_body = ""
286 else:
287 post_body = json.dumps({'metadata': meta})
288 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200289 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800290 body = json.loads(body)
291 return resp, body['metadata']
292
293 def update_server_metadata(self, server_id, meta):
294 post_body = json.dumps({'metadata': meta})
295 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200296 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800297 body = json.loads(body)
298 return resp, body['metadata']
299
300 def get_server_metadata_item(self, server_id, key):
301 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
302 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800303 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800304
305 def set_server_metadata_item(self, server_id, key, meta):
ivan-zhu8f992be2013-07-31 14:56:58 +0800306 post_body = json.dumps({'metadata': meta})
ivan-zhu09111942013-08-01 08:09:16 +0800307 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200308 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800309 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800310 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800311
312 def delete_server_metadata_item(self, server_id, key):
313 resp, body = self.delete("servers/%s/metadata/%s" %
314 (str(server_id), key))
315 return resp, body
316
317 def stop(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800318 return self.action(server_id, 'stop', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800319
320 def start(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800321 return self.action(server_id, 'start', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800322
323 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
324 """Attaches a volume to a server instance."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800325 return self.action(server_id, 'attach', None, volume_id=volume_id,
326 device=device)
ivan-zhu09111942013-08-01 08:09:16 +0800327
328 def detach_volume(self, server_id, volume_id):
329 """Detaches a volume from a server instance."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800330 return self.action(server_id, 'detach', None, volume_id=volume_id)
ivan-zhu09111942013-08-01 08:09:16 +0800331
ivan-zhu09111942013-08-01 08:09:16 +0800332 def live_migrate_server(self, server_id, dest_host, use_block_migration):
333 """This should be called with administrator privileges ."""
334
335 migrate_params = {
336 "disk_over_commit": False,
337 "block_migration": use_block_migration,
338 "host": dest_host
339 }
340
ivan-zhu8f992be2013-07-31 14:56:58 +0800341 req_body = json.dumps({'migrate_live': migrate_params})
ivan-zhu09111942013-08-01 08:09:16 +0800342
343 resp, body = self.post("servers/%s/action" % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200344 req_body)
ivan-zhu09111942013-08-01 08:09:16 +0800345 return resp, body
346
347 def migrate_server(self, server_id, **kwargs):
348 """Migrates a server to a new host."""
349 return self.action(server_id, 'migrate', None, **kwargs)
350
351 def lock_server(self, server_id, **kwargs):
352 """Locks the given server."""
353 return self.action(server_id, 'lock', None, **kwargs)
354
355 def unlock_server(self, server_id, **kwargs):
356 """UNlocks the given server."""
357 return self.action(server_id, 'unlock', None, **kwargs)
358
359 def suspend_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800360 """Suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800361 return self.action(server_id, 'suspend', None, **kwargs)
362
363 def resume_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800364 """Un-suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800365 return self.action(server_id, 'resume', None, **kwargs)
366
367 def pause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800368 """Pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800369 return self.action(server_id, 'pause', None, **kwargs)
370
371 def unpause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800372 """Un-pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800373 return self.action(server_id, 'unpause', None, **kwargs)
374
375 def reset_state(self, server_id, state='error'):
376 """Resets the state of a server to active/error."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800377 return self.action(server_id, 'reset_state', None, state=state)
ivan-zhu09111942013-08-01 08:09:16 +0800378
ivan-zhu2ca89b32013-08-07 22:37:32 +0800379 def shelve_server(self, server_id, **kwargs):
380 """Shelves the provided server."""
381 return self.action(server_id, 'shelve', None, **kwargs)
382
383 def unshelve_server(self, server_id, **kwargs):
384 """Un-shelves the provided server."""
385 return self.action(server_id, 'unshelve', None, **kwargs)
386
ivan-zhu09111942013-08-01 08:09:16 +0800387 def get_console_output(self, server_id, length):
ivan-zhu8f992be2013-07-31 14:56:58 +0800388 return self.action(server_id, 'get_console_output', 'output',
ivan-zhu09111942013-08-01 08:09:16 +0800389 length=length)
390
Yuiko Takada7835d502014-01-15 10:15:03 +0000391 def rescue_server(self, server_id, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800392 """Rescue the provided server."""
Yuiko Takada7835d502014-01-15 10:15:03 +0000393 return self.action(server_id, 'rescue', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800394
395 def unrescue_server(self, server_id):
396 """Unrescue the provided server."""
397 return self.action(server_id, 'unrescue', None)
398
399 def get_server_diagnostics(self, server_id):
400 """Get the usage data for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800401 resp, body = self.get("servers/%s/os-server-diagnostics" %
402 str(server_id))
ivan-zhu09111942013-08-01 08:09:16 +0800403 return resp, json.loads(body)
404
405 def list_instance_actions(self, server_id):
406 """List the provided server action."""
407 resp, body = self.get("servers/%s/os-instance-actions" %
408 str(server_id))
409 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800410 return resp, body['instance_actions']
ivan-zhu09111942013-08-01 08:09:16 +0800411
412 def get_instance_action(self, server_id, request_id):
413 """Returns the action details of the provided server."""
414 resp, body = self.get("servers/%s/os-instance-actions/%s" %
415 (str(server_id), str(request_id)))
416 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800417 return resp, body['instance_action']
ivan-zhu2ca89b32013-08-07 22:37:32 +0800418
419 def force_delete_server(self, server_id, **kwargs):
420 """Force delete a server."""
421 return self.action(server_id, 'force_delete', None, **kwargs)
422
423 def restore_soft_deleted_server(self, server_id, **kwargs):
424 """Restore a soft-deleted server."""
425 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900426
427 def get_vnc_console(self, server_id, type):
428 """Get URL of VNC console."""
429 post_body = json.dumps({
430 "get_vnc_console": {
431 "type": type
432 }
433 })
434 resp, body = self.post('servers/%s/action' % str(server_id),
435 post_body)
436 body = json.loads(body)
437 return resp, body['console']
Ghanshyam0549f7b2014-03-04 19:07:52 +0900438
439 def reset_network(self, server_id, **kwargs):
440 """Resets the Network of a server"""
441 return self.action(server_id, 'reset_network', None, **kwargs)
442
443 def inject_network_info(self, server_id, **kwargs):
444 """Inject the Network Info into server"""
445 return self.action(server_id, 'inject_network_info', None, **kwargs)