blob: a486801eee17dc3ea727a1267f7e3de3b7ec92d9 [file] [log] [blame]
ivan-zhu09111942013-08-01 08:09:16 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack Foundation
4# Copyright 2013 Hewlett-Packard Development Company, L.P.
ivan-zhu8f992be2013-07-31 14:56:58 +08005# Copyright 2013 IBM Corp
ivan-zhu09111942013-08-01 08:09:16 +08006# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19
20import json
21import time
22import urllib
23
24from tempest.common.rest_client import RestClient
25from tempest.common import waiters
26from tempest import exceptions
27
28
ivan-zhu8f992be2013-07-31 14:56:58 +080029class ServersV3ClientJSON(RestClient):
ivan-zhu09111942013-08-01 08:09:16 +080030
ivan-zhu8f992be2013-07-31 14:56:58 +080031 def __init__(self, config, username, password, auth_url,
32 tenant_name=None, auth_version='v2'):
33 super(ServersV3ClientJSON, self).__init__(config, username, password,
34 auth_url, tenant_name,
35 auth_version=auth_version)
36 self.service = self.config.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.
48 personality: A list of dictionaries for files to be injected into
49 the server.
50 security_groups: A list of security group dicts.
51 networks: A list of network dicts with UUID and fixed_ip.
52 user_data: User data for instance.
53 availability_zone: Availability zone in which to launch instance.
ivan-zhu8f992be2013-07-31 14:56:58 +080054 access_ip_v4: The IPv4 access address for the server.
55 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +080056 min_count: Count of minimum number of instances to launch.
57 max_count: Count of maximum number of instances to launch.
58 disk_config: Determines if user or admin controls disk configuration.
59 return_reservation_id: Enable/Disable the return of reservation id
60 """
61 post_body = {
62 'name': name,
ivan-zhu8f992be2013-07-31 14:56:58 +080063 'image_ref': image_ref,
64 'flavor_ref': flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +080065 }
66
ivan-zhu2ca89b32013-08-07 22:37:32 +080067 for option in ['personality', 'admin_password', 'key_name', 'networks',
68 ('os-security-groups:security_groups',
69 'security_groups'),
ivan-zhu8f992be2013-07-31 14:56:58 +080070 ('os-user-data:user_data', 'user_data'),
71 ('os-availability-zone:availability_zone',
72 'availability_zone'),
ivan-zhu2ca89b32013-08-07 22:37:32 +080073 ('os-access-ips:access_ip_v4', 'access_ip_v4'),
74 ('os-access-ips:access_ip_v6', 'access_ip_v6'),
ivan-zhu8f992be2013-07-31 14:56:58 +080075 ('os-multiple-create:min_count', 'min_count'),
76 ('os-multiple-create:max_count', 'max_count'),
77 ('metadata', 'meta'),
78 ('os-disk-config:disk_config', 'disk_config'),
79 ('os-multiple-create:return_reservation_id',
80 'return_reservation_id')]:
ivan-zhu09111942013-08-01 08:09:16 +080081 if isinstance(option, tuple):
82 post_param = option[0]
83 key = option[1]
84 else:
85 post_param = option
86 key = option
87 value = kwargs.get(key)
88 if value is not None:
89 post_body[post_param] = value
90 post_body = json.dumps({'server': post_body})
91 resp, body = self.post('servers', post_body, self.headers)
92
93 body = json.loads(body)
94 # NOTE(maurosr): this deals with the case of multiple server create
95 # with return reservation id set True
96 if 'reservation_id' in body:
97 return resp, body
98 return resp, body['server']
99
ivan-zhu8f992be2013-07-31 14:56:58 +0800100 def update_server(self, server_id, name=None, meta=None, access_ip_v4=None,
101 access_ip_v6=None, disk_config=None):
ivan-zhu09111942013-08-01 08:09:16 +0800102 """
103 Updates the properties of an existing server.
104 server_id: The id of an existing server.
105 name: The name of the server.
106 personality: A list of files to be injected into the server.
ivan-zhu8f992be2013-07-31 14:56:58 +0800107 access_ip_v4: The IPv4 access address for the server.
108 access_ip_v6: The IPv6 access address for the server.
ivan-zhu09111942013-08-01 08:09:16 +0800109 """
110
111 post_body = {}
112
113 if meta is not None:
114 post_body['metadata'] = meta
115
116 if name is not None:
117 post_body['name'] = name
118
ivan-zhu8f992be2013-07-31 14:56:58 +0800119 if access_ip_v4 is not None:
120 post_body['access_ip_v4'] = access_ip_v4
ivan-zhu09111942013-08-01 08:09:16 +0800121
ivan-zhu8f992be2013-07-31 14:56:58 +0800122 if access_ip_v6 is not None:
123 post_body['access_ip_v6'] = access_ip_v6
ivan-zhu09111942013-08-01 08:09:16 +0800124
125 if disk_config is not None:
ivan-zhu50969522013-08-26 11:10:39 +0800126 post_body['os-disk-config:disk_config'] = disk_config
ivan-zhu09111942013-08-01 08:09:16 +0800127
128 post_body = json.dumps({'server': post_body})
129 resp, body = self.put("servers/%s" % str(server_id),
130 post_body, self.headers)
131 body = json.loads(body)
132 return resp, body['server']
133
134 def get_server(self, server_id):
135 """Returns the details of an existing server."""
136 resp, body = self.get("servers/%s" % str(server_id))
137 body = json.loads(body)
138 return resp, body['server']
139
140 def delete_server(self, server_id):
141 """Deletes the given server."""
142 return self.delete("servers/%s" % str(server_id))
143
144 def list_servers(self, params=None):
145 """Lists all servers for a user."""
146
147 url = 'servers'
148 if params:
149 url += '?%s' % urllib.urlencode(params)
150
151 resp, body = self.get(url)
152 body = json.loads(body)
153 return resp, body
154
155 def list_servers_with_detail(self, params=None):
156 """Lists all servers in detail for a user."""
157
158 url = 'servers/detail'
159 if params:
160 url += '?%s' % urllib.urlencode(params)
161
162 resp, body = self.get(url)
163 body = json.loads(body)
164 return resp, body
165
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900166 def wait_for_server_status(self, server_id, status, extra_timeout=0):
ivan-zhu09111942013-08-01 08:09:16 +0800167 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900168 return waiters.wait_for_server_status(self, server_id, status,
169 extra_timeout=extra_timeout)
ivan-zhu09111942013-08-01 08:09:16 +0800170
171 def wait_for_server_termination(self, server_id, ignore_error=False):
172 """Waits for server to reach termination."""
173 start_time = int(time.time())
174 while True:
175 try:
176 resp, body = self.get_server(server_id)
177 except exceptions.NotFound:
178 return
179
180 server_status = body['status']
181 if server_status == 'ERROR' and not ignore_error:
182 raise exceptions.BuildErrorException(server_id=server_id)
183
184 if int(time.time()) - start_time >= self.build_timeout:
185 raise exceptions.TimeoutException
186
187 time.sleep(self.build_interval)
188
189 def list_addresses(self, server_id):
190 """Lists all addresses for a server."""
191 resp, body = self.get("servers/%s/ips" % str(server_id))
192 body = json.loads(body)
193 return resp, body['addresses']
194
195 def list_addresses_by_network(self, server_id, network_id):
196 """Lists all addresses of a specific network type for a server."""
197 resp, body = self.get("servers/%s/ips/%s" %
198 (str(server_id), network_id))
199 body = json.loads(body)
200 return resp, body
201
202 def action(self, server_id, action_name, response_key, **kwargs):
203 post_body = json.dumps({action_name: kwargs})
204 resp, body = self.post('servers/%s/action' % str(server_id),
205 post_body, self.headers)
206 if response_key is not None:
207 body = json.loads(body)[response_key]
208 return resp, body
209
ivan-zhu2ca89b32013-08-07 22:37:32 +0800210 def create_backup(self, server_id, backup_type, rotation, name):
211 """Backup a server instance."""
212 return self.action(server_id, "create_backup", None,
213 backup_type=backup_type,
214 rotation=rotation,
215 name=name)
216
ivan-zhu8f992be2013-07-31 14:56:58 +0800217 def change_password(self, server_id, admin_password):
ivan-zhu09111942013-08-01 08:09:16 +0800218 """Changes the root password for the server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800219 return self.action(server_id, 'change_password', None,
220 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800221
222 def reboot(self, server_id, reboot_type):
223 """Reboots a server."""
224 return self.action(server_id, 'reboot', None, type=reboot_type)
225
226 def rebuild(self, server_id, image_ref, **kwargs):
227 """Rebuilds a server with a new image."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800228 kwargs['image_ref'] = image_ref
ivan-zhu09111942013-08-01 08:09:16 +0800229 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800230 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800231 del kwargs['disk_config']
232 return self.action(server_id, 'rebuild', 'server', **kwargs)
233
234 def resize(self, server_id, flavor_ref, **kwargs):
235 """Changes the flavor of a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800236 kwargs['flavor_ref'] = flavor_ref
ivan-zhu09111942013-08-01 08:09:16 +0800237 if 'disk_config' in kwargs:
ivan-zhu8f992be2013-07-31 14:56:58 +0800238 kwargs['os-disk-config:disk_config'] = kwargs['disk_config']
ivan-zhu09111942013-08-01 08:09:16 +0800239 del kwargs['disk_config']
240 return self.action(server_id, 'resize', None, **kwargs)
241
242 def confirm_resize(self, server_id, **kwargs):
243 """Confirms the flavor change for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800244 return self.action(server_id, 'confirm_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800245
246 def revert_resize(self, server_id, **kwargs):
247 """Reverts a server back to its original flavor."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800248 return self.action(server_id, 'revert_resize', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800249
ivan-zhu8f992be2013-07-31 14:56:58 +0800250 def create_image(self, server_id, name, meta=None):
251 """Creates an image of the original server."""
252
253 post_body = {
254 'create_image': {
255 'name': name,
256 }
257 }
258
259 if meta is not None:
260 post_body['create_image']['metadata'] = meta
261
262 post_body = json.dumps(post_body)
263 resp, body = self.post('servers/%s/action' % str(server_id),
264 post_body, self.headers)
265 return resp, body
ivan-zhu09111942013-08-01 08:09:16 +0800266
267 def list_server_metadata(self, server_id):
268 resp, body = self.get("servers/%s/metadata" % str(server_id))
269 body = json.loads(body)
270 return resp, body['metadata']
271
272 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
273 if no_metadata_field:
274 post_body = ""
275 else:
276 post_body = json.dumps({'metadata': meta})
277 resp, body = self.put('servers/%s/metadata' % str(server_id),
278 post_body, self.headers)
279 body = json.loads(body)
280 return resp, body['metadata']
281
282 def update_server_metadata(self, server_id, meta):
283 post_body = json.dumps({'metadata': meta})
284 resp, body = self.post('servers/%s/metadata' % str(server_id),
285 post_body, self.headers)
286 body = json.loads(body)
287 return resp, body['metadata']
288
289 def get_server_metadata_item(self, server_id, key):
290 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
291 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800292 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800293
294 def set_server_metadata_item(self, server_id, key, meta):
ivan-zhu8f992be2013-07-31 14:56:58 +0800295 post_body = json.dumps({'metadata': meta})
ivan-zhu09111942013-08-01 08:09:16 +0800296 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
297 post_body, self.headers)
298 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800299 return resp, body['metadata']
ivan-zhu09111942013-08-01 08:09:16 +0800300
301 def delete_server_metadata_item(self, server_id, key):
302 resp, body = self.delete("servers/%s/metadata/%s" %
303 (str(server_id), key))
304 return resp, body
305
306 def stop(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800307 return self.action(server_id, 'stop', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800308
309 def start(self, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800310 return self.action(server_id, 'start', None, **kwargs)
ivan-zhu09111942013-08-01 08:09:16 +0800311
312 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
313 """Attaches a volume to a server instance."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800314 return self.action(server_id, 'attach', None, volume_id=volume_id,
315 device=device)
ivan-zhu09111942013-08-01 08:09:16 +0800316
317 def detach_volume(self, server_id, volume_id):
318 """Detaches a volume from a server instance."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800319 return self.action(server_id, 'detach', None, volume_id=volume_id)
ivan-zhu09111942013-08-01 08:09:16 +0800320
ivan-zhu09111942013-08-01 08:09:16 +0800321 def live_migrate_server(self, server_id, dest_host, use_block_migration):
322 """This should be called with administrator privileges ."""
323
324 migrate_params = {
325 "disk_over_commit": False,
326 "block_migration": use_block_migration,
327 "host": dest_host
328 }
329
ivan-zhu8f992be2013-07-31 14:56:58 +0800330 req_body = json.dumps({'migrate_live': migrate_params})
ivan-zhu09111942013-08-01 08:09:16 +0800331
332 resp, body = self.post("servers/%s/action" % str(server_id),
333 req_body, self.headers)
334 return resp, body
335
336 def migrate_server(self, server_id, **kwargs):
337 """Migrates a server to a new host."""
338 return self.action(server_id, 'migrate', None, **kwargs)
339
340 def lock_server(self, server_id, **kwargs):
341 """Locks the given server."""
342 return self.action(server_id, 'lock', None, **kwargs)
343
344 def unlock_server(self, server_id, **kwargs):
345 """UNlocks the given server."""
346 return self.action(server_id, 'unlock', None, **kwargs)
347
348 def suspend_server(self, server_id, **kwargs):
349 """Suspends the provded server."""
350 return self.action(server_id, 'suspend', None, **kwargs)
351
352 def resume_server(self, server_id, **kwargs):
353 """Un-suspends the provded server."""
354 return self.action(server_id, 'resume', None, **kwargs)
355
356 def pause_server(self, server_id, **kwargs):
357 """Pauses the provded server."""
358 return self.action(server_id, 'pause', None, **kwargs)
359
360 def unpause_server(self, server_id, **kwargs):
361 """Un-pauses the provded server."""
362 return self.action(server_id, 'unpause', None, **kwargs)
363
364 def reset_state(self, server_id, state='error'):
365 """Resets the state of a server to active/error."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800366 return self.action(server_id, 'reset_state', None, state=state)
ivan-zhu09111942013-08-01 08:09:16 +0800367
ivan-zhu2ca89b32013-08-07 22:37:32 +0800368 def shelve_server(self, server_id, **kwargs):
369 """Shelves the provided server."""
370 return self.action(server_id, 'shelve', None, **kwargs)
371
372 def unshelve_server(self, server_id, **kwargs):
373 """Un-shelves the provided server."""
374 return self.action(server_id, 'unshelve', None, **kwargs)
375
ivan-zhu09111942013-08-01 08:09:16 +0800376 def get_console_output(self, server_id, length):
ivan-zhu8f992be2013-07-31 14:56:58 +0800377 return self.action(server_id, 'get_console_output', 'output',
ivan-zhu09111942013-08-01 08:09:16 +0800378 length=length)
379
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +0900380 def rescue_server(self, server_id, admin_password=None):
ivan-zhu09111942013-08-01 08:09:16 +0800381 """Rescue the provided server."""
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +0900382 return self.action(server_id, 'rescue', None,
383 admin_password=admin_password)
ivan-zhu09111942013-08-01 08:09:16 +0800384
385 def unrescue_server(self, server_id):
386 """Unrescue the provided server."""
387 return self.action(server_id, 'unrescue', None)
388
389 def get_server_diagnostics(self, server_id):
390 """Get the usage data for a server."""
ivan-zhu8f992be2013-07-31 14:56:58 +0800391 resp, body = self.get("servers/%s/os-server-diagnostics" %
392 str(server_id))
ivan-zhu09111942013-08-01 08:09:16 +0800393 return resp, json.loads(body)
394
395 def list_instance_actions(self, server_id):
396 """List the provided server action."""
397 resp, body = self.get("servers/%s/os-instance-actions" %
398 str(server_id))
399 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800400 return resp, body['instance_actions']
ivan-zhu09111942013-08-01 08:09:16 +0800401
402 def get_instance_action(self, server_id, request_id):
403 """Returns the action details of the provided server."""
404 resp, body = self.get("servers/%s/os-instance-actions/%s" %
405 (str(server_id), str(request_id)))
406 body = json.loads(body)
ivan-zhu8f992be2013-07-31 14:56:58 +0800407 return resp, body['instance_action']
ivan-zhu2ca89b32013-08-07 22:37:32 +0800408
409 def force_delete_server(self, server_id, **kwargs):
410 """Force delete a server."""
411 return self.action(server_id, 'force_delete', None, **kwargs)
412
413 def restore_soft_deleted_server(self, server_id, **kwargs):
414 """Restore a soft-deleted server."""
415 return self.action(server_id, 'restore', None, **kwargs)