blob: 82ded4cafa584ed7590549cfdfeaa1edc35a4744 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
nithya-ganesane6a36c82013-02-15 14:38:27 +00002# Copyright 2013 Hewlett-Packard Development Company, L.P.
dwallecke62b9f02012-10-10 23:34:42 -05003# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
Daryl Wallecke5b83d42011-11-10 14:39:02 -060017import json
Daryl Wallecke5b83d42011-11-10 14:39:02 -060018import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Daryl Wallecke5b83d42011-11-10 14:39:02 -060020
Ghanshyam7fa397c2014-04-01 19:32:38 +090021from tempest.api_schema.compute import servers as common_schema
Ken'ichi Ohmichi02604582014-03-14 16:23:41 +090022from tempest.api_schema.compute.v2 import servers as schema
Haiwei Xuab924622014-03-05 04:33:51 +090023from tempest.common import rest_client
Attila Fazekas0abbc952013-07-01 19:19:42 +020024from tempest.common import waiters
Matthew Treinish684d8992014-01-30 16:27:40 +000025from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest import exceptions
27
Matthew Treinish684d8992014-01-30 16:27:40 +000028CONF = config.CONF
29
Daryl Wallecke5b83d42011-11-10 14:39:02 -060030
Haiwei Xuab924622014-03-05 04:33:51 +090031class ServersClientJSON(rest_client.RestClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060032
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000033 def __init__(self, auth_provider):
34 super(ServersClientJSON, self).__init__(auth_provider)
35
Matthew Treinish684d8992014-01-30 16:27:40 +000036 self.service = CONF.compute.catalog_type
Daryl Wallecke5b83d42011-11-10 14:39:02 -060037
Rohit Karajgi95446a22011-12-12 06:21:43 -080038 def create_server(self, name, image_ref, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060039 """
40 Creates an instance of a server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080041 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:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060045 adminPass: Sets the initial root password.
chris fattarsia6c2a732012-05-02 17:00:19 -070046 key_name: Key name of keypair that was created earlier.
David Kranz28e79de2012-04-12 15:49:41 -040047 meta: A dictionary of values to be used as metadata.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060048 personality: A list of dictionaries for files to be injected into
49 the server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080050 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.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060054 accessIPv4: The IPv4 access address for the server.
55 accessIPv6: The IPv6 access address for the server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080056 min_count: Count of minimum number of instances to launch.
57 max_count: Count of maximum number of instances to launch.
Daryl Wallecke36d5002012-03-28 09:56:10 -050058 disk_config: Determines if user or admin controls disk configuration.
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050059 return_reservation_id: Enable/Disable the return of reservation id
Daryl Wallecke5b83d42011-11-10 14:39:02 -060060 """
Daryl Wallecke5b83d42011-11-10 14:39:02 -060061 post_body = {
62 'name': name,
63 'imageRef': image_ref,
David Kranz28e79de2012-04-12 15:49:41 -040064 'flavorRef': flavor_ref
Daryl Wallecke5b83d42011-11-10 14:39:02 -060065 }
66
chris fattarsia6c2a732012-05-02 17:00:19 -070067 for option in ['personality', 'adminPass', 'key_name',
Zhongyue Luoe0884a32012-09-25 17:24:17 +080068 'security_groups', 'networks', 'user_data',
69 'availability_zone', 'accessIPv4', 'accessIPv6',
70 'min_count', 'max_count', ('metadata', 'meta'),
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050071 ('OS-DCF:diskConfig', 'disk_config'),
72 'return_reservation_id']:
David Kranz28e79de2012-04-12 15:49:41 -040073 if isinstance(option, tuple):
74 post_param = option[0]
75 key = option[1]
76 else:
77 post_param = option
78 key = option
79 value = kwargs.get(key)
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080080 if value is not None:
David Kranz28e79de2012-04-12 15:49:41 -040081 post_body[post_param] = value
raiesmh08cbe21b02014-03-12 17:04:44 +053082 post_body = {'server': post_body}
83
84 if 'sched_hints' in kwargs:
85 hints = {'os:scheduler_hints': kwargs.get('sched_hints')}
86 post_body = dict(post_body.items() + hints.items())
87 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020088 resp, body = self.post('servers', post_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -050089
Daryl Wallecke5b83d42011-11-10 14:39:02 -060090 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050091 # NOTE(maurosr): this deals with the case of multiple server create
92 # with return reservation id set True
93 if 'reservation_id' in body:
94 return resp, body
Ken'ichi Ohmichi02604582014-03-14 16:23:41 +090095 self.validate_response(schema.create_server, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060096 return resp, body['server']
97
98 def update_server(self, server_id, name=None, meta=None, accessIPv4=None,
ivan-zhu72d7d5b2013-10-16 17:30:58 +080099 accessIPv6=None, disk_config=None):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600100 """
101 Updates the properties of an existing server.
102 server_id: The id of an existing server.
103 name: The name of the server.
104 personality: A list of files to be injected into the server.
105 accessIPv4: The IPv4 access address for the server.
106 accessIPv6: The IPv6 access address for the server.
107 """
108
109 post_body = {}
110
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800111 if meta is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600112 post_body['metadata'] = meta
113
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800114 if name is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600115 post_body['name'] = name
116
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800117 if accessIPv4 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600118 post_body['accessIPv4'] = accessIPv4
119
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800120 if accessIPv6 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600121 post_body['accessIPv6'] = accessIPv6
122
ivan-zhu72d7d5b2013-10-16 17:30:58 +0800123 if disk_config is not None:
124 post_body['OS-DCF:diskConfig'] = disk_config
125
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600126 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)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600128 body = json.loads(body)
129 return resp, body['server']
130
131 def get_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500132 """Returns the details of an existing server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700133 resp, body = self.get("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600134 body = json.loads(body)
135 return resp, body['server']
136
137 def delete_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500138 """Deletes the given server."""
Ghanshyam4ac02742014-04-17 19:15:47 +0900139 resp, body = self.delete("servers/%s" % str(server_id))
140 self.validate_response(common_schema.delete_server, resp, body)
141 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600142
143 def list_servers(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500144 """Lists all servers for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600145
146 url = 'servers'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500147 if params:
148 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600149
chris fattarsi5098fa22012-04-17 13:27:00 -0700150 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600151 body = json.loads(body)
152 return resp, body
153
154 def list_servers_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500155 """Lists all servers in detail for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600156
157 url = 'servers/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500158 if params:
159 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600160
chris fattarsi5098fa22012-04-17 13:27:00 -0700161 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600162 body = json.loads(body)
163 return resp, body
164
Zhi Kun Liue5401762013-09-11 20:45:48 +0800165 def wait_for_server_status(self, server_id, status, extra_timeout=0,
166 raise_on_error=True):
Sean Daguef237ccb2013-01-04 15:19:14 -0500167 """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,
Zhi Kun Liue5401762013-09-11 20:45:48 +0800169 extra_timeout=extra_timeout,
170 raise_on_error=raise_on_error)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600171
Jay Pipes051075a2012-04-28 17:39:37 -0400172 def wait_for_server_termination(self, server_id, ignore_error=False):
Sean Daguef237ccb2013-01-04 15:19:14 -0500173 """Waits for server to reach termination."""
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700174 start_time = int(time.time())
175 while True:
176 try:
177 resp, body = self.get_server(server_id)
178 except exceptions.NotFound:
179 return
180
181 server_status = body['status']
Jay Pipes051075a2012-04-28 17:39:37 -0400182 if server_status == 'ERROR' and not ignore_error:
Jay Pipes444c3e62012-10-04 19:26:35 -0400183 raise exceptions.BuildErrorException(server_id=server_id)
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700184
185 if int(time.time()) - start_time >= self.build_timeout:
186 raise exceptions.TimeoutException
187
188 time.sleep(self.build_interval)
189
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600190 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500191 """Lists all addresses for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700192 resp, body = self.get("servers/%s/ips" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600193 body = json.loads(body)
194 return resp, body['addresses']
195
196 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500197 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700198 resp, body = self.get("servers/%s/ips/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800199 (str(server_id), network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600200 body = json.loads(body)
201 return resp, body
202
Ghanshyamd6d30402014-04-02 15:28:37 +0900203 def action(self, server_id, action_name, response_key,
204 schema=None, **kwargs):
Attila Fazekasd4299282013-02-22 13:25:23 +0100205 post_body = json.dumps({action_name: kwargs})
206 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200207 post_body)
Attila Fazekasd4299282013-02-22 13:25:23 +0100208 if response_key is not None:
Ghanshyamd6d30402014-04-02 15:28:37 +0900209 body = json.loads(body)
210 # Check for Schema as 'None' because if we donot have any server
211 # action schema implemented yet then they can pass 'None' to skip
212 # the validation.Once all server action has their schema
213 # implemented then, this check can be removed if every actions are
214 # supposed to validate their response.
215 if schema is not None:
216 self.validate_response(schema, resp, body)
217 body = body[response_key]
Attila Fazekasd4299282013-02-22 13:25:23 +0100218 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600219
ivan-zhuccc89462013-10-31 16:53:12 +0800220 def create_backup(self, server_id, backup_type, rotation, name):
221 """Backup a server instance."""
222 return self.action(server_id, "createBackup", None,
223 backup_type=backup_type,
224 rotation=rotation,
225 name=name)
226
Attila Fazekasd4299282013-02-22 13:25:23 +0100227 def change_password(self, server_id, adminPass):
228 """Changes the root password for the server."""
229 return self.action(server_id, 'changePassword', None,
230 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600231
ivan-zhu6de5b042013-10-24 17:21:48 +0800232 def get_password(self, server_id):
233 resp, body = self.get("servers/%s/os-server-password" %
234 str(server_id))
235 body = json.loads(body)
Ghanshyam7fa397c2014-04-01 19:32:38 +0900236 self.validate_response(common_schema.get_password, resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800237 return resp, body
238
239 def delete_password(self, server_id):
240 """
241 Removes the encrypted server password from the metadata server
242 Note that this does not actually change the instance server
243 password.
244 """
245 return self.delete("servers/%s/os-server-password" %
246 str(server_id))
247
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600248 def reboot(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500249 """Reboots a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100250 return self.action(server_id, 'reboot', None, type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600251
Attila Fazekasd4299282013-02-22 13:25:23 +0100252 def rebuild(self, server_id, image_ref, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500253 """Rebuilds a server with a new image."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100254 kwargs['imageRef'] = image_ref
255 if 'disk_config' in kwargs:
256 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
257 del kwargs['disk_config']
258 return self.action(server_id, 'rebuild', 'server', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600259
Attila Fazekasd4299282013-02-22 13:25:23 +0100260 def resize(self, server_id, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600261 """Changes the flavor of a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100262 kwargs['flavorRef'] = flavor_ref
263 if 'disk_config' in kwargs:
264 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
265 del kwargs['disk_config']
266 return self.action(server_id, 'resize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600267
Attila Fazekasd4299282013-02-22 13:25:23 +0100268 def confirm_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500269 """Confirms the flavor change for a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100270 return self.action(server_id, 'confirmResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600271
Attila Fazekasd4299282013-02-22 13:25:23 +0100272 def revert_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500273 """Reverts a server back to its original flavor."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100274 return self.action(server_id, 'revertResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600275
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600276 def list_server_metadata(self, server_id):
chris fattarsi5098fa22012-04-17 13:27:00 -0700277 resp, body = self.get("servers/%s/metadata" % str(server_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600278 body = json.loads(body)
Ghanshyama8f66532014-04-23 17:18:14 +0900279 self.validate_response(common_schema.list_server_metadata, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600280 return resp, body['metadata']
281
Chris Yeohd0b52e72013-09-17 22:38:59 +0930282 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
283 if no_metadata_field:
284 post_body = ""
285 else:
286 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800287 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200288 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600289 body = json.loads(body)
Haiwei Xu3d2b7af2014-04-12 02:50:26 +0900290 self.validate_response(common_schema.set_server_metadata, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600291 return resp, body['metadata']
292
293 def update_server_metadata(self, server_id, meta):
294 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800295 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200296 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600297 body = json.loads(body)
298 return resp, body['metadata']
299
300 def get_server_metadata_item(self, server_id, key):
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800301 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600302 body = json.loads(body)
303 return resp, body['meta']
304
305 def set_server_metadata_item(self, server_id, key, meta):
306 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800307 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200308 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600309 body = json.loads(body)
310 return resp, body['meta']
311
312 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700313 resp, body = self.delete("servers/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800314 (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600315 return resp, body
Dan Smithc18d8c62012-07-02 08:09:26 -0700316
Attila Fazekasd4299282013-02-22 13:25:23 +0100317 def stop(self, server_id, **kwargs):
318 return self.action(server_id, 'os-stop', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700319
Attila Fazekasd4299282013-02-22 13:25:23 +0100320 def start(self, server_id, **kwargs):
321 return self.action(server_id, 'os-start', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700322
323 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500324 """Attaches a volume to a server instance."""
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900325 post_body = json.dumps({
326 'volumeAttachment': {
327 'volumeId': volume_id,
328 'device': device,
329 }
330 })
Dan Smithc18d8c62012-07-02 08:09:26 -0700331 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200332 post_body)
Ghanshyam385c4e72014-03-27 11:42:25 +0900333 body = json.loads(body)
334 self.validate_response(schema.attach_volume, resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700335 return resp, body
336
337 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500338 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700339 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
340 (server_id, volume_id))
Ghanshyam385c4e72014-03-27 11:42:25 +0900341 self.validate_response(schema.detach_volume, resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700342 return resp, body
sapan-kona775cf632012-06-22 00:32:36 +0530343
Attila Fazekasd4299282013-02-22 13:25:23 +0100344 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500345 """Adds a security group to the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100346 return self.action(server_id, 'addSecurityGroup', None, name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530347
Attila Fazekasd4299282013-02-22 13:25:23 +0100348 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500349 """Removes a security group from the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100350 return self.action(server_id, 'removeSecurityGroup', None, name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100351
352 def live_migrate_server(self, server_id, dest_host, use_block_migration):
Sean Daguef237ccb2013-01-04 15:19:14 -0500353 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100354
355 migrate_params = {
356 "disk_over_commit": False,
357 "block_migration": use_block_migration,
358 "host": dest_host
359 }
360
361 req_body = json.dumps({'os-migrateLive': migrate_params})
362
vponomaryovf4c27f92014-02-18 10:56:42 +0200363 resp, body = self.post("servers/%s/action" % str(server_id), req_body)
Mate Lakat99ee9142012-09-14 12:34:46 +0100364 return resp, body
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600365
Attila Fazekasd4299282013-02-22 13:25:23 +0100366 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500367 """Migrates a server to a new host."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100368 return self.action(server_id, 'migrate', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600369
Attila Fazekasd4299282013-02-22 13:25:23 +0100370 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500371 """Locks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100372 return self.action(server_id, 'lock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600373
Attila Fazekasd4299282013-02-22 13:25:23 +0100374 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500375 """UNlocks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100376 return self.action(server_id, 'unlock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600377
Attila Fazekasd4299282013-02-22 13:25:23 +0100378 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900379 """Suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100380 return self.action(server_id, 'suspend', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600381
Attila Fazekasd4299282013-02-22 13:25:23 +0100382 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900383 """Un-suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100384 return self.action(server_id, 'resume', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600385
Attila Fazekasd4299282013-02-22 13:25:23 +0100386 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900387 """Pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100388 return self.action(server_id, 'pause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600389
Attila Fazekasd4299282013-02-22 13:25:23 +0100390 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900391 """Un-pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100392 return self.action(server_id, 'unpause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600393
Attila Fazekasd4299282013-02-22 13:25:23 +0100394 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500395 """Resets the state of a server to active/error."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100396 return self.action(server_id, 'os-resetState', None, state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100397
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900398 def shelve_server(self, server_id, **kwargs):
399 """Shelves the provided server."""
400 return self.action(server_id, 'shelve', None, **kwargs)
401
402 def unshelve_server(self, server_id, **kwargs):
403 """Un-shelves the provided server."""
404 return self.action(server_id, 'unshelve', None, **kwargs)
405
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900406 def shelve_offload_server(self, server_id, **kwargs):
407 """Shelve-offload the provided server."""
408 return self.action(server_id, 'shelveOffload', None, **kwargs)
409
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100410 def get_console_output(self, server_id, length):
Attila Fazekasd4299282013-02-22 13:25:23 +0100411 return self.action(server_id, 'os-getConsoleOutput', 'output',
412 length=length)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200413
414 def list_virtual_interfaces(self, server_id):
415 """
416 List the virtual interfaces used in an instance.
417 """
418 resp, body = self.get('/'.join(['servers', server_id,
419 'os-virtual-interfaces']))
Ghanshyam08ce58d2014-04-04 14:51:14 +0900420 body = json.loads(body)
421 self.validate_response(schema.list_virtual_interfaces, resp, body)
422 return resp, body
nithya-ganesane6a36c82013-02-15 14:38:27 +0000423
Yuiko Takada7835d502014-01-15 10:15:03 +0000424 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000425 """Rescue the provided server."""
Yuiko Takada7835d502014-01-15 10:15:03 +0000426 return self.action(server_id, 'rescue', None, **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000427
428 def unrescue_server(self, server_id):
429 """Unrescue the provided server."""
430 return self.action(server_id, 'unrescue', None)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900431
Zhu Zhuda070852013-09-25 08:07:57 -0500432 def get_server_diagnostics(self, server_id):
433 """Get the usage data for a server."""
434 resp, body = self.get("servers/%s/diagnostics" % str(server_id))
435 return resp, json.loads(body)
436
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900437 def list_instance_actions(self, server_id):
438 """List the provided server action."""
439 resp, body = self.get("servers/%s/os-instance-actions" %
440 str(server_id))
441 body = json.loads(body)
442 return resp, body['instanceActions']
443
444 def get_instance_action(self, server_id, request_id):
445 """Returns the action details of the provided server."""
446 resp, body = self.get("servers/%s/os-instance-actions/%s" %
447 (str(server_id), str(request_id)))
448 body = json.loads(body)
449 return resp, body['instanceAction']
Lingxian Kongaecc1092013-10-03 16:18:46 +0800450
451 def force_delete_server(self, server_id, **kwargs):
452 """Force delete a server."""
453 return self.action(server_id, 'forceDelete', None, **kwargs)
454
455 def restore_soft_deleted_server(self, server_id, **kwargs):
456 """Restore a soft-deleted server."""
457 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900458
459 def reset_network(self, server_id, **kwargs):
460 """Resets the Network of a server"""
461 return self.action(server_id, 'resetNetwork', None, **kwargs)
462
463 def inject_network_info(self, server_id, **kwargs):
464 """Inject the Network Info into server"""
465 return self.action(server_id, 'injectNetworkInfo', None, **kwargs)
Ghanshyam86d1a572014-03-05 10:19:25 +0900466
467 def get_vnc_console(self, server_id, console_type):
468 """Get URL of VNC console."""
469 return self.action(server_id, "os-getVNCConsole",
Ghanshyamd6d30402014-04-02 15:28:37 +0900470 "console", common_schema.get_vnc_console,
471 type=console_type)