blob: 6a0d9b26ec0e8cbbc699340b3770fd0eb96b274d [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
58 """
59 post_body = {
60 'name': name,
ivan-zhu8f992be2013-07-31 14:56:58 +080061 'image_ref': image_ref,
62 'flavor_ref': flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +080063 }
64
ivan-zhu82094252013-11-21 10:16:11 +080065 for option in ['admin_password', 'key_name', 'networks',
ivan-zhu2ca89b32013-08-07 22:37:32 +080066 ('os-security-groups:security_groups',
67 'security_groups'),
ivan-zhu8f992be2013-07-31 14:56:58 +080068 ('os-user-data:user_data', 'user_data'),
69 ('os-availability-zone:availability_zone',
70 'availability_zone'),
ivan-zhu2ca89b32013-08-07 22:37:32 +080071 ('os-access-ips:access_ip_v4', 'access_ip_v4'),
72 ('os-access-ips:access_ip_v6', 'access_ip_v6'),
ivan-zhu8f992be2013-07-31 14:56:58 +080073 ('os-multiple-create:min_count', 'min_count'),
74 ('os-multiple-create:max_count', 'max_count'),
75 ('metadata', 'meta'),
76 ('os-disk-config:disk_config', 'disk_config'),
77 ('os-multiple-create:return_reservation_id',
Yuiko Takada7835d502014-01-15 10:15:03 +000078 'return_reservation_id')]:
ivan-zhu09111942013-08-01 08:09:16 +080079 if isinstance(option, tuple):
80 post_param = option[0]
81 key = option[1]
82 else:
83 post_param = option
84 key = option
85 value = kwargs.get(key)
86 if value is not None:
87 post_body[post_param] = value
88 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020089 resp, body = self.post('servers', post_body)
ivan-zhu09111942013-08-01 08:09:16 +080090
91 body = json.loads(body)
92 # NOTE(maurosr): this deals with the case of multiple server create
93 # with return reservation id set True
ivan-zhuf0bf3012013-11-25 16:03:42 +080094 if 'servers_reservation' in body:
95 return resp, body['servers_reservation']
Ken'ichi Ohmichi02604582014-03-14 16:23:41 +090096 self.validate_response(schema.create_server, resp, body)
ivan-zhu09111942013-08-01 08:09:16 +080097 return resp, body['server']
98
ivan-zhu8f992be2013-07-31 14:56:58 +080099 def update_server(self, server_id, name=None, meta=None, access_ip_v4=None,
100 access_ip_v6=None, disk_config=None):
ivan-zhu09111942013-08-01 08:09:16 +0800101 """
102 Updates the properties of an existing server.
103 server_id: The id of an existing server.
104 name: The name of the server.
ivan-zhu8f992be2013-07-31 14:56:58 +0800105 access_ip_v4: The IPv4 access address for the server.
106 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +0800107 """
108
109 post_body = {}
110
111 if meta is not None:
112 post_body['metadata'] = meta
113
114 if name is not None:
115 post_body['name'] = name
116
ivan-zhu8f992be2013-07-31 14:56:58 +0800117 if access_ip_v4 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800118 post_body['os-access-ips:access_ip_v4'] = access_ip_v4
ivan-zhu09111942013-08-01 08:09:16 +0800119
ivan-zhu8f992be2013-07-31 14:56:58 +0800120 if access_ip_v6 is not None:
ivan-zhuf0bf3012013-11-25 16:03:42 +0800121 post_body['os-access-ips:access_ip_v6'] = access_ip_v6
ivan-zhu09111942013-08-01 08:09:16 +0800122
123 if disk_config is not None:
ivan-zhu50969522013-08-26 11:10:39 +0800124 post_body['os-disk-config:disk_config'] = disk_config
ivan-zhu09111942013-08-01 08:09:16 +0800125
126 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +0200127 resp, body = self.put("servers/%s" % str(server_id), post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800128 body = json.loads(body)
129 return resp, body['server']
130
131 def get_server(self, server_id):
132 """Returns the details of an existing server."""
133 resp, body = self.get("servers/%s" % str(server_id))
134 body = json.loads(body)
135 return resp, body['server']
136
137 def delete_server(self, server_id):
138 """Deletes the given server."""
139 return self.delete("servers/%s" % str(server_id))
140
141 def list_servers(self, params=None):
142 """Lists all servers for a user."""
143
144 url = 'servers'
145 if params:
146 url += '?%s' % urllib.urlencode(params)
147
148 resp, body = self.get(url)
149 body = json.loads(body)
150 return resp, body
151
152 def list_servers_with_detail(self, params=None):
153 """Lists all servers in detail for a user."""
154
155 url = 'servers/detail'
156 if params:
157 url += '?%s' % urllib.urlencode(params)
158
159 resp, body = self.get(url)
160 body = json.loads(body)
161 return resp, body
162
Zhi Kun Liude892722013-12-30 15:27:52 +0800163 def wait_for_server_status(self, server_id, status, extra_timeout=0,
164 raise_on_error=True):
ivan-zhu09111942013-08-01 08:09:16 +0800165 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900166 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liude892722013-12-30 15:27:52 +0800167 extra_timeout=extra_timeout,
168 raise_on_error=raise_on_error)
ivan-zhu09111942013-08-01 08:09:16 +0800169
170 def wait_for_server_termination(self, server_id, ignore_error=False):
171 """Waits for server to reach termination."""
172 start_time = int(time.time())
173 while True:
174 try:
175 resp, body = self.get_server(server_id)
176 except exceptions.NotFound:
177 return
178
179 server_status = body['status']
180 if server_status == 'ERROR' and not ignore_error:
181 raise exceptions.BuildErrorException(server_id=server_id)
182
183 if int(time.time()) - start_time >= self.build_timeout:
184 raise exceptions.TimeoutException
185
186 time.sleep(self.build_interval)
187
188 def list_addresses(self, server_id):
189 """Lists all addresses for a server."""
190 resp, body = self.get("servers/%s/ips" % str(server_id))
191 body = json.loads(body)
192 return resp, body['addresses']
193
194 def list_addresses_by_network(self, server_id, network_id):
195 """Lists all addresses of a specific network type for a server."""
196 resp, body = self.get("servers/%s/ips/%s" %
197 (str(server_id), network_id))
198 body = json.loads(body)
199 return resp, body
200
201 def action(self, server_id, action_name, response_key, **kwargs):
202 post_body = json.dumps({action_name: kwargs})
203 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200204 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800205 if response_key is not None:
206 body = json.loads(body)[response_key]
207 return resp, body
208
ivan-zhu2ca89b32013-08-07 22:37:32 +0800209 def create_backup(self, server_id, backup_type, rotation, name):
210 """Backup a server instance."""
211 return self.action(server_id, "create_backup", None,
212 backup_type=backup_type,
213 rotation=rotation,
214 name=name)
215
ivan-zhu8f992be2013-07-31 14:56:58 +0800216 def change_password(self, server_id, admin_password):
ivan-zhu09111942013-08-01 08:09:16 +0800217 """Changes the root password for the server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800218 return self.action(server_id, 'change_password', None,
219 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800220
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800221 def get_password(self, server_id):
222 resp, body = self.get("servers/%s/os-server-password" %
223 str(server_id))
224 body = json.loads(body)
Ghanshyam7fa397c2014-04-01 19:32:38 +0900225 self.validate_response(common_schema.get_password, resp, body)
ivan-zhuaf8c4e62014-01-22 17:09:42 +0800226 return resp, body
227
228 def delete_password(self, server_id):
229 """
230 Removes the encrypted server password from the metadata server
231 Note that this does not actually change the instance server
232 password.
233 """
234 return self.delete("servers/%s/os-server-password" %
235 str(server_id))
236
ivan-zhu09111942013-08-01 08:09:16 +0800237 def reboot(self, server_id, reboot_type):
238 """Reboots a server."""
239 return self.action(server_id, 'reboot', None, type=reboot_type)
240
241 def rebuild(self, server_id, image_ref, **kwargs):
242 """Rebuilds a server with a new image."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800243 kwargs['image_ref'] = image_ref
ivan-zhu09111942013-08-01 08:09:16 +0800244 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800245 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800246 del kwargs['disk_config']
247 return self.action(server_id, 'rebuild', 'server', **kwargs)
248
249 def resize(self, server_id, flavor_ref, **kwargs):
250 """Changes the flavor of a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800251 kwargs['flavor_ref'] = flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +0800252 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800253 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800254 del kwargs['disk_config']
255 return self.action(server_id, 'resize', None, **kwargs)
256
257 def confirm_resize(self, server_id, **kwargs):
258 """Confirms the flavor change for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800259 return self.action(server_id, 'confirm_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800260
261 def revert_resize(self, server_id, **kwargs):
262 """Reverts a server back to its original flavor."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800263 return self.action(server_id, 'revert_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800264
ivan-zhu8f992be2013-07-31 14:56:58 +0800265 def create_image(self, server_id, name, meta=None):
266 """Creates an image of the original server."""
267
268 post_body = {
269 'create_image': {
270 'name': name,
271 }
272 }
273
274 if meta is not None:
275 post_body['create_image']['metadata'] = meta
276
277 post_body = json.dumps(post_body)
278 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200279 post_body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800280 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800281
282 def list_server_metadata(self, server_id):
283 resp, body = self.get("servers/%s/metadata" % str(server_id))
284 body = json.loads(body)
285 return resp, body['metadata']
286
287 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
288 if no_metadata_field:
289 post_body = ""
290 else:
291 post_body = json.dumps({'metadata': meta})
292 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200293 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800294 body = json.loads(body)
295 return resp, body['metadata']
296
297 def update_server_metadata(self, server_id, meta):
298 post_body = json.dumps({'metadata': meta})
299 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200300 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800301 body = json.loads(body)
302 return resp, body['metadata']
303
304 def get_server_metadata_item(self, server_id, key):
305 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
306 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800307 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800308
309 def set_server_metadata_item(self, server_id, key, meta):
ivan-zhu8f992be2013-07-31 14:56:58 +0800310 post_body = json.dumps({'metadata': meta})
ivan-zhu09111942013-08-01 08:09:16 +0800311 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200312 post_body)
ivan-zhu09111942013-08-01 08:09:16 +0800313 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800314 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800315
316 def delete_server_metadata_item(self, server_id, key):
317 resp, body = self.delete("servers/%s/metadata/%s" %
318 (str(server_id), key))
319 return resp, body
320
321 def stop(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800322 return self.action(server_id, 'stop', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800323
324 def start(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800325 return self.action(server_id, 'start', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800326
327 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
328 """Attaches a volume to a server instance."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800329 return self.action(server_id, 'attach', None, volume_id=volume_id,
330 device=device)
ivan-zhu09111942013-08-01 08:09:16 +0800331
332 def detach_volume(self, server_id, volume_id):
333 """Detaches a volume from a server instance."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800334 return self.action(server_id, 'detach', None, volume_id=volume_id)
ivan-zhu09111942013-08-01 08:09:16 +0800335
ivan-zhu09111942013-08-01 08:09:16 +0800336 def live_migrate_server(self, server_id, dest_host, use_block_migration):
337 """This should be called with administrator privileges ."""
338
339 migrate_params = {
340 "disk_over_commit": False,
341 "block_migration": use_block_migration,
342 "host": dest_host
343 }
344
ivan-zhu8f992be2013-07-31 14:56:58 +0800345 req_body = json.dumps({'migrate_live': migrate_params})
ivan-zhu09111942013-08-01 08:09:16 +0800346
347 resp, body = self.post("servers/%s/action" % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200348 req_body)
ivan-zhu09111942013-08-01 08:09:16 +0800349 return resp, body
350
351 def migrate_server(self, server_id, **kwargs):
352 """Migrates a server to a new host."""
353 return self.action(server_id, 'migrate', None, **kwargs)
354
355 def lock_server(self, server_id, **kwargs):
356 """Locks the given server."""
357 return self.action(server_id, 'lock', None, **kwargs)
358
359 def unlock_server(self, server_id, **kwargs):
360 """UNlocks the given server."""
361 return self.action(server_id, 'unlock', None, **kwargs)
362
363 def suspend_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800364 """Suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800365 return self.action(server_id, 'suspend', None, **kwargs)
366
367 def resume_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800368 """Un-suspends the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800369 return self.action(server_id, 'resume', None, **kwargs)
370
371 def pause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800372 """Pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800373 return self.action(server_id, 'pause', None, **kwargs)
374
375 def unpause_server(self, server_id, **kwargs):
Zhi Kun Liude892722013-12-30 15:27:52 +0800376 """Un-pauses the provided server."""
ivan-zhu09111942013-08-01 08:09:16 +0800377 return self.action(server_id, 'unpause', None, **kwargs)
378
379 def reset_state(self, server_id, state='error'):
380 """Resets the state of a server to active/error."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800381 return self.action(server_id, 'reset_state', None, state=state)
ivan-zhu09111942013-08-01 08:09:16 +0800382
ivan-zhu2ca89b32013-08-07 22:37:32 +0800383 def shelve_server(self, server_id, **kwargs):
384 """Shelves the provided server."""
385 return self.action(server_id, 'shelve', None, **kwargs)
386
387 def unshelve_server(self, server_id, **kwargs):
388 """Un-shelves the provided server."""
389 return self.action(server_id, 'unshelve', None, **kwargs)
390
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900391 def shelve_offload_server(self, server_id, **kwargs):
392 """Shelve-offload the provided server."""
393 return self.action(server_id, 'shelve_offload', None, **kwargs)
394
ivan-zhu09111942013-08-01 08:09:16 +0800395 def get_console_output(self, server_id, length):
ivan-zhu8f992be2013-07-31 14:56:58 +0800396 return self.action(server_id, 'get_console_output', 'output',
ivan-zhu09111942013-08-01 08:09:16 +0800397 length=length)
398
Yuiko Takada7835d502014-01-15 10:15:03 +0000399 def rescue_server(self, server_id, **kwargs):
ivan-zhu09111942013-08-01 08:09:16 +0800400 """Rescue the provided server."""
Yuiko Takada7835d502014-01-15 10:15:03 +0000401 return self.action(server_id, 'rescue', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800402
403 def unrescue_server(self, server_id):
404 """Unrescue the provided server."""
405 return self.action(server_id, 'unrescue', None)
406
407 def get_server_diagnostics(self, server_id):
408 """Get the usage data for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800409 resp, body = self.get("servers/%s/os-server-diagnostics" %
410 str(server_id))
ivan-zhu09111942013-08-01 08:09:16 +0800411 return resp, json.loads(body)
412
413 def list_instance_actions(self, server_id):
414 """List the provided server action."""
415 resp, body = self.get("servers/%s/os-instance-actions" %
416 str(server_id))
417 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800418 return resp, body['instance_actions']
ivan-zhu09111942013-08-01 08:09:16 +0800419
420 def get_instance_action(self, server_id, request_id):
421 """Returns the action details of the provided server."""
422 resp, body = self.get("servers/%s/os-instance-actions/%s" %
423 (str(server_id), str(request_id)))
424 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800425 return resp, body['instance_action']
ivan-zhu2ca89b32013-08-07 22:37:32 +0800426
427 def force_delete_server(self, server_id, **kwargs):
428 """Force delete a server."""
429 return self.action(server_id, 'force_delete', None, **kwargs)
430
431 def restore_soft_deleted_server(self, server_id, **kwargs):
432 """Restore a soft-deleted server."""
433 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann41c17572014-02-27 18:52:56 +0900434
435 def get_vnc_console(self, server_id, type):
436 """Get URL of VNC console."""
437 post_body = json.dumps({
438 "get_vnc_console": {
439 "type": type
440 }
441 })
442 resp, body = self.post('servers/%s/action' % str(server_id),
443 post_body)
444 body = json.loads(body)
445 return resp, body['console']
Ghanshyam0549f7b2014-03-04 19:07:52 +0900446
447 def reset_network(self, server_id, **kwargs):
448 """Resets the Network of a server"""
449 return self.action(server_id, 'reset_network', None, **kwargs)
450
451 def inject_network_info(self, server_id, **kwargs):
452 """Inject the Network Info into server"""
453 return self.action(server_id, 'inject_network_info', None, **kwargs)