blob: e386ec2327808712e963261cd8ad46e48566f62e [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
Haiwei Xuab924622014-03-05 04:33:51 +090021from tempest.common import rest_client
Attila Fazekas0abbc952013-07-01 19:19:42 +020022from tempest.common import waiters
Matthew Treinish684d8992014-01-30 16:27:40 +000023from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest import exceptions
25
Matthew Treinish684d8992014-01-30 16:27:40 +000026CONF = config.CONF
27
Daryl Wallecke5b83d42011-11-10 14:39:02 -060028
Haiwei Xuab924622014-03-05 04:33:51 +090029class ServersClientJSON(rest_client.RestClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060030
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(ServersClientJSON, self).__init__(auth_provider)
33
Matthew Treinish684d8992014-01-30 16:27:40 +000034 self.service = CONF.compute.catalog_type
Daryl Wallecke5b83d42011-11-10 14:39:02 -060035
Rohit Karajgi95446a22011-12-12 06:21:43 -080036 def create_server(self, name, image_ref, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060037 """
38 Creates an instance of a server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080039 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:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060043 adminPass: Sets the initial root password.
chris fattarsia6c2a732012-05-02 17:00:19 -070044 key_name: Key name of keypair that was created earlier.
David Kranz28e79de2012-04-12 15:49:41 -040045 meta: A dictionary of values to be used as metadata.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060046 personality: A list of dictionaries for files to be injected into
47 the server.
Rohit Karajgi95446a22011-12-12 06:21:43 -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.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060052 accessIPv4: The IPv4 access address for the server.
53 accessIPv6: The IPv6 access address for the server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080054 min_count: Count of minimum number of instances to launch.
55 max_count: Count of maximum number of instances to launch.
Daryl Wallecke36d5002012-03-28 09:56:10 -050056 disk_config: Determines if user or admin controls disk configuration.
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050057 return_reservation_id: Enable/Disable the return of reservation id
Daryl Wallecke5b83d42011-11-10 14:39:02 -060058 """
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059 post_body = {
60 'name': name,
61 'imageRef': image_ref,
David Kranz28e79de2012-04-12 15:49:41 -040062 'flavorRef': flavor_ref
Daryl Wallecke5b83d42011-11-10 14:39:02 -060063 }
64
chris fattarsia6c2a732012-05-02 17:00:19 -070065 for option in ['personality', 'adminPass', 'key_name',
Zhongyue Luoe0884a32012-09-25 17:24:17 +080066 'security_groups', 'networks', 'user_data',
67 'availability_zone', 'accessIPv4', 'accessIPv6',
68 'min_count', 'max_count', ('metadata', 'meta'),
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050069 ('OS-DCF:diskConfig', 'disk_config'),
70 'return_reservation_id']:
David Kranz28e79de2012-04-12 15:49:41 -040071 if isinstance(option, tuple):
72 post_param = option[0]
73 key = option[1]
74 else:
75 post_param = option
76 key = option
77 value = kwargs.get(key)
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080078 if value is not None:
David Kranz28e79de2012-04-12 15:49:41 -040079 post_body[post_param] = value
Daryl Wallecke5b83d42011-11-10 14:39:02 -060080 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020081 resp, body = self.post('servers', post_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -050082
Daryl Wallecke5b83d42011-11-10 14:39:02 -060083 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050084 # NOTE(maurosr): this deals with the case of multiple server create
85 # with return reservation id set True
86 if 'reservation_id' in body:
87 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -060088 return resp, body['server']
89
90 def update_server(self, server_id, name=None, meta=None, accessIPv4=None,
ivan-zhu72d7d5b2013-10-16 17:30:58 +080091 accessIPv6=None, disk_config=None):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060092 """
93 Updates the properties of an existing server.
94 server_id: The id of an existing server.
95 name: The name of the server.
96 personality: A list of files to be injected into the server.
97 accessIPv4: The IPv4 access address for the server.
98 accessIPv6: The IPv6 access address for the server.
99 """
100
101 post_body = {}
102
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800103 if meta is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600104 post_body['metadata'] = meta
105
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800106 if name is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600107 post_body['name'] = name
108
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800109 if accessIPv4 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600110 post_body['accessIPv4'] = accessIPv4
111
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800112 if accessIPv6 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600113 post_body['accessIPv6'] = accessIPv6
114
ivan-zhu72d7d5b2013-10-16 17:30:58 +0800115 if disk_config is not None:
116 post_body['OS-DCF:diskConfig'] = disk_config
117
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600118 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +0200119 resp, body = self.put("servers/%s" % str(server_id), post_body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600120 body = json.loads(body)
121 return resp, body['server']
122
123 def get_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Returns the details of an existing server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700125 resp, body = self.get("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600126 body = json.loads(body)
127 return resp, body['server']
128
129 def delete_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500130 """Deletes the given server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700131 return self.delete("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600132
133 def list_servers(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500134 """Lists all servers for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600135
136 url = 'servers'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500137 if params:
138 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600139
chris fattarsi5098fa22012-04-17 13:27:00 -0700140 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600141 body = json.loads(body)
142 return resp, body
143
144 def list_servers_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500145 """Lists all servers in detail for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600146
147 url = 'servers/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500148 if params:
149 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600150
chris fattarsi5098fa22012-04-17 13:27:00 -0700151 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600152 body = json.loads(body)
153 return resp, body
154
Zhi Kun Liue5401762013-09-11 20:45:48 +0800155 def wait_for_server_status(self, server_id, status, extra_timeout=0,
156 raise_on_error=True):
Sean Daguef237ccb2013-01-04 15:19:14 -0500157 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900158 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liue5401762013-09-11 20:45:48 +0800159 extra_timeout=extra_timeout,
160 raise_on_error=raise_on_error)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600161
Jay Pipes051075a2012-04-28 17:39:37 -0400162 def wait_for_server_termination(self, server_id, ignore_error=False):
Sean Daguef237ccb2013-01-04 15:19:14 -0500163 """Waits for server to reach termination."""
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700164 start_time = int(time.time())
165 while True:
166 try:
167 resp, body = self.get_server(server_id)
168 except exceptions.NotFound:
169 return
170
171 server_status = body['status']
Jay Pipes051075a2012-04-28 17:39:37 -0400172 if server_status == 'ERROR' and not ignore_error:
Jay Pipes444c3e62012-10-04 19:26:35 -0400173 raise exceptions.BuildErrorException(server_id=server_id)
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700174
175 if int(time.time()) - start_time >= self.build_timeout:
176 raise exceptions.TimeoutException
177
178 time.sleep(self.build_interval)
179
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600180 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500181 """Lists all addresses for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700182 resp, body = self.get("servers/%s/ips" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600183 body = json.loads(body)
184 return resp, body['addresses']
185
186 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500187 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700188 resp, body = self.get("servers/%s/ips/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800189 (str(server_id), network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600190 body = json.loads(body)
191 return resp, body
192
Attila Fazekasd4299282013-02-22 13:25:23 +0100193 def action(self, server_id, action_name, response_key, **kwargs):
194 post_body = json.dumps({action_name: kwargs})
195 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200196 post_body)
Attila Fazekasd4299282013-02-22 13:25:23 +0100197 if response_key is not None:
198 body = json.loads(body)[response_key]
199 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600200
ivan-zhuccc89462013-10-31 16:53:12 +0800201 def create_backup(self, server_id, backup_type, rotation, name):
202 """Backup a server instance."""
203 return self.action(server_id, "createBackup", None,
204 backup_type=backup_type,
205 rotation=rotation,
206 name=name)
207
Attila Fazekasd4299282013-02-22 13:25:23 +0100208 def change_password(self, server_id, adminPass):
209 """Changes the root password for the server."""
210 return self.action(server_id, 'changePassword', None,
211 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600212
ivan-zhu6de5b042013-10-24 17:21:48 +0800213 def get_password(self, server_id):
214 resp, body = self.get("servers/%s/os-server-password" %
215 str(server_id))
216 body = json.loads(body)
217 return resp, body
218
219 def delete_password(self, server_id):
220 """
221 Removes the encrypted server password from the metadata server
222 Note that this does not actually change the instance server
223 password.
224 """
225 return self.delete("servers/%s/os-server-password" %
226 str(server_id))
227
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600228 def reboot(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500229 """Reboots a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100230 return self.action(server_id, 'reboot', None, type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600231
Attila Fazekasd4299282013-02-22 13:25:23 +0100232 def rebuild(self, server_id, image_ref, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500233 """Rebuilds a server with a new image."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100234 kwargs['imageRef'] = image_ref
235 if 'disk_config' in kwargs:
236 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
237 del kwargs['disk_config']
238 return self.action(server_id, 'rebuild', 'server', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600239
Attila Fazekasd4299282013-02-22 13:25:23 +0100240 def resize(self, server_id, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600241 """Changes the flavor of a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100242 kwargs['flavorRef'] = flavor_ref
243 if 'disk_config' in kwargs:
244 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
245 del kwargs['disk_config']
246 return self.action(server_id, 'resize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600247
Attila Fazekasd4299282013-02-22 13:25:23 +0100248 def confirm_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500249 """Confirms the flavor change for a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100250 return self.action(server_id, 'confirmResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600251
Attila Fazekasd4299282013-02-22 13:25:23 +0100252 def revert_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500253 """Reverts a server back to its original flavor."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100254 return self.action(server_id, 'revertResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600255
Attila Fazekasd4299282013-02-22 13:25:23 +0100256 def create_image(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500257 """Creates an image of the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100258 return self.action(server_id, 'createImage', None, name=name)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600259
260 def list_server_metadata(self, server_id):
chris fattarsi5098fa22012-04-17 13:27:00 -0700261 resp, body = self.get("servers/%s/metadata" % str(server_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600262 body = json.loads(body)
263 return resp, body['metadata']
264
Chris Yeohd0b52e72013-09-17 22:38:59 +0930265 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
266 if no_metadata_field:
267 post_body = ""
268 else:
269 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800270 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200271 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600272 body = json.loads(body)
273 return resp, body['metadata']
274
275 def update_server_metadata(self, server_id, meta):
276 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800277 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200278 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600279 body = json.loads(body)
280 return resp, body['metadata']
281
282 def get_server_metadata_item(self, server_id, key):
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800283 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600284 body = json.loads(body)
285 return resp, body['meta']
286
287 def set_server_metadata_item(self, server_id, key, meta):
288 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800289 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200290 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600291 body = json.loads(body)
292 return resp, body['meta']
293
294 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700295 resp, body = self.delete("servers/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800296 (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600297 return resp, body
Dan Smithc18d8c62012-07-02 08:09:26 -0700298
Attila Fazekasd4299282013-02-22 13:25:23 +0100299 def stop(self, server_id, **kwargs):
300 return self.action(server_id, 'os-stop', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700301
Attila Fazekasd4299282013-02-22 13:25:23 +0100302 def start(self, server_id, **kwargs):
303 return self.action(server_id, 'os-start', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700304
305 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500306 """Attaches a volume to a server instance."""
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900307 post_body = json.dumps({
308 'volumeAttachment': {
309 'volumeId': volume_id,
310 'device': device,
311 }
312 })
Dan Smithc18d8c62012-07-02 08:09:26 -0700313 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200314 post_body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700315 return resp, body
316
317 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500318 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700319 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
320 (server_id, volume_id))
321 return resp, body
sapan-kona775cf632012-06-22 00:32:36 +0530322
Attila Fazekasd4299282013-02-22 13:25:23 +0100323 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500324 """Adds a security group to the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100325 return self.action(server_id, 'addSecurityGroup', None, name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530326
Attila Fazekasd4299282013-02-22 13:25:23 +0100327 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500328 """Removes a security group from the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100329 return self.action(server_id, 'removeSecurityGroup', None, name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100330
331 def live_migrate_server(self, server_id, dest_host, use_block_migration):
Sean Daguef237ccb2013-01-04 15:19:14 -0500332 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100333
334 migrate_params = {
335 "disk_over_commit": False,
336 "block_migration": use_block_migration,
337 "host": dest_host
338 }
339
340 req_body = json.dumps({'os-migrateLive': migrate_params})
341
vponomaryovf4c27f92014-02-18 10:56:42 +0200342 resp, body = self.post("servers/%s/action" % str(server_id), req_body)
Mate Lakat99ee9142012-09-14 12:34:46 +0100343 return resp, body
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600344
Attila Fazekasd4299282013-02-22 13:25:23 +0100345 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500346 """Migrates a server to a new host."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100347 return self.action(server_id, 'migrate', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600348
Attila Fazekasd4299282013-02-22 13:25:23 +0100349 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500350 """Locks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100351 return self.action(server_id, 'lock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600352
Attila Fazekasd4299282013-02-22 13:25:23 +0100353 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500354 """UNlocks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100355 return self.action(server_id, 'unlock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600356
Attila Fazekasd4299282013-02-22 13:25:23 +0100357 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900358 """Suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100359 return self.action(server_id, 'suspend', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600360
Attila Fazekasd4299282013-02-22 13:25:23 +0100361 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900362 """Un-suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100363 return self.action(server_id, 'resume', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600364
Attila Fazekasd4299282013-02-22 13:25:23 +0100365 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900366 """Pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100367 return self.action(server_id, 'pause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600368
Attila Fazekasd4299282013-02-22 13:25:23 +0100369 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900370 """Un-pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100371 return self.action(server_id, 'unpause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600372
Attila Fazekasd4299282013-02-22 13:25:23 +0100373 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500374 """Resets the state of a server to active/error."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100375 return self.action(server_id, 'os-resetState', None, state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100376
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900377 def shelve_server(self, server_id, **kwargs):
378 """Shelves the provided server."""
379 return self.action(server_id, 'shelve', None, **kwargs)
380
381 def unshelve_server(self, server_id, **kwargs):
382 """Un-shelves the provided server."""
383 return self.action(server_id, 'unshelve', None, **kwargs)
384
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100385 def get_console_output(self, server_id, length):
Attila Fazekasd4299282013-02-22 13:25:23 +0100386 return self.action(server_id, 'os-getConsoleOutput', 'output',
387 length=length)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200388
389 def list_virtual_interfaces(self, server_id):
390 """
391 List the virtual interfaces used in an instance.
392 """
393 resp, body = self.get('/'.join(['servers', server_id,
394 'os-virtual-interfaces']))
395 return resp, json.loads(body)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000396
Yuiko Takada7835d502014-01-15 10:15:03 +0000397 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000398 """Rescue the provided server."""
Yuiko Takada7835d502014-01-15 10:15:03 +0000399 return self.action(server_id, 'rescue', None, **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000400
401 def unrescue_server(self, server_id):
402 """Unrescue the provided server."""
403 return self.action(server_id, 'unrescue', None)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900404
Zhu Zhuda070852013-09-25 08:07:57 -0500405 def get_server_diagnostics(self, server_id):
406 """Get the usage data for a server."""
407 resp, body = self.get("servers/%s/diagnostics" % str(server_id))
408 return resp, json.loads(body)
409
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900410 def list_instance_actions(self, server_id):
411 """List the provided server action."""
412 resp, body = self.get("servers/%s/os-instance-actions" %
413 str(server_id))
414 body = json.loads(body)
415 return resp, body['instanceActions']
416
417 def get_instance_action(self, server_id, request_id):
418 """Returns the action details of the provided server."""
419 resp, body = self.get("servers/%s/os-instance-actions/%s" %
420 (str(server_id), str(request_id)))
421 body = json.loads(body)
422 return resp, body['instanceAction']
Lingxian Kongaecc1092013-10-03 16:18:46 +0800423
424 def force_delete_server(self, server_id, **kwargs):
425 """Force delete a server."""
426 return self.action(server_id, 'forceDelete', None, **kwargs)
427
428 def restore_soft_deleted_server(self, server_id, **kwargs):
429 """Restore a soft-deleted server."""
430 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900431
432 def reset_network(self, server_id, **kwargs):
433 """Resets the Network of a server"""
434 return self.action(server_id, 'resetNetwork', None, **kwargs)
435
436 def inject_network_info(self, server_id, **kwargs):
437 """Inject the Network Info into server"""
438 return self.action(server_id, 'injectNetworkInfo', None, **kwargs)
Ghanshyam86d1a572014-03-05 10:19:25 +0900439
440 def get_vnc_console(self, server_id, console_type):
441 """Get URL of VNC console."""
442 return self.action(server_id, "os-getVNCConsole",
443 "console", type=console_type)