blob: 0987f0527cc137be327f7edbeb886479acf22822 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.common.rest_client import RestClient
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
Dan Smithcf8fab62012-08-14 08:03:48 -070029class ServersClientJSON(RestClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060030
Matthew Treinish684d8992014-01-30 16:27:40 +000031 def __init__(self, username, password, auth_url, tenant_name=None,
Brant Knudsonc7ca3342013-03-28 21:08:50 -050032 auth_version='v2'):
Matthew Treinish684d8992014-01-30 16:27:40 +000033 super(ServersClientJSON, self).__init__(username, password,
Brant Knudsonc7ca3342013-03-28 21:08:50 -050034 auth_url, tenant_name,
35 auth_version=auth_version)
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
Daryl Wallecke5b83d42011-11-10 14:39:02 -060082 post_body = json.dumps({'server': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -070083 resp, body = self.post('servers', post_body, self.headers)
Jay Pipes5135bfc2012-01-05 15:46:49 -050084
Daryl Wallecke5b83d42011-11-10 14:39:02 -060085 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050086 # NOTE(maurosr): this deals with the case of multiple server create
87 # with return reservation id set True
88 if 'reservation_id' in body:
89 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -060090 return resp, body['server']
91
92 def update_server(self, server_id, name=None, meta=None, accessIPv4=None,
ivan-zhu72d7d5b2013-10-16 17:30:58 +080093 accessIPv6=None, disk_config=None):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060094 """
95 Updates the properties of an existing server.
96 server_id: The id of an existing server.
97 name: The name of the server.
98 personality: A list of files to be injected into the server.
99 accessIPv4: The IPv4 access address for the server.
100 accessIPv6: The IPv6 access address for the server.
101 """
102
103 post_body = {}
104
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800105 if meta is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600106 post_body['metadata'] = meta
107
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800108 if name is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600109 post_body['name'] = name
110
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800111 if accessIPv4 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600112 post_body['accessIPv4'] = accessIPv4
113
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800114 if accessIPv6 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600115 post_body['accessIPv6'] = accessIPv6
116
ivan-zhu72d7d5b2013-10-16 17:30:58 +0800117 if disk_config is not None:
118 post_body['OS-DCF:diskConfig'] = disk_config
119
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600120 post_body = json.dumps({'server': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -0700121 resp, body = self.put("servers/%s" % str(server_id),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800122 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600123 body = json.loads(body)
124 return resp, body['server']
125
126 def get_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500127 """Returns the details of an existing server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700128 resp, body = self.get("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600129 body = json.loads(body)
130 return resp, body['server']
131
132 def delete_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500133 """Deletes the given server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700134 return self.delete("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600135
136 def list_servers(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500137 """Lists all servers for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600138
139 url = 'servers'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500140 if params:
141 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600142
chris fattarsi5098fa22012-04-17 13:27:00 -0700143 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600144 body = json.loads(body)
145 return resp, body
146
147 def list_servers_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500148 """Lists all servers in detail for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600149
150 url = 'servers/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500151 if params:
152 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600153
chris fattarsi5098fa22012-04-17 13:27:00 -0700154 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600155 body = json.loads(body)
156 return resp, body
157
Zhi Kun Liue5401762013-09-11 20:45:48 +0800158 def wait_for_server_status(self, server_id, status, extra_timeout=0,
159 raise_on_error=True):
Sean Daguef237ccb2013-01-04 15:19:14 -0500160 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900161 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liue5401762013-09-11 20:45:48 +0800162 extra_timeout=extra_timeout,
163 raise_on_error=raise_on_error)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600164
Jay Pipes051075a2012-04-28 17:39:37 -0400165 def wait_for_server_termination(self, server_id, ignore_error=False):
Sean Daguef237ccb2013-01-04 15:19:14 -0500166 """Waits for server to reach termination."""
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700167 start_time = int(time.time())
168 while True:
169 try:
170 resp, body = self.get_server(server_id)
171 except exceptions.NotFound:
172 return
173
174 server_status = body['status']
Jay Pipes051075a2012-04-28 17:39:37 -0400175 if server_status == 'ERROR' and not ignore_error:
Jay Pipes444c3e62012-10-04 19:26:35 -0400176 raise exceptions.BuildErrorException(server_id=server_id)
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700177
178 if int(time.time()) - start_time >= self.build_timeout:
179 raise exceptions.TimeoutException
180
181 time.sleep(self.build_interval)
182
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600183 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500184 """Lists all addresses for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700185 resp, body = self.get("servers/%s/ips" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600186 body = json.loads(body)
187 return resp, body['addresses']
188
189 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500190 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700191 resp, body = self.get("servers/%s/ips/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800192 (str(server_id), network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600193 body = json.loads(body)
194 return resp, body
195
Attila Fazekasd4299282013-02-22 13:25:23 +0100196 def action(self, server_id, action_name, response_key, **kwargs):
197 post_body = json.dumps({action_name: kwargs})
198 resp, body = self.post('servers/%s/action' % str(server_id),
199 post_body, self.headers)
200 if response_key is not None:
201 body = json.loads(body)[response_key]
202 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600203
ivan-zhuccc89462013-10-31 16:53:12 +0800204 def create_backup(self, server_id, backup_type, rotation, name):
205 """Backup a server instance."""
206 return self.action(server_id, "createBackup", None,
207 backup_type=backup_type,
208 rotation=rotation,
209 name=name)
210
Attila Fazekasd4299282013-02-22 13:25:23 +0100211 def change_password(self, server_id, adminPass):
212 """Changes the root password for the server."""
213 return self.action(server_id, 'changePassword', None,
214 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600215
ivan-zhu6de5b042013-10-24 17:21:48 +0800216 def get_password(self, server_id):
217 resp, body = self.get("servers/%s/os-server-password" %
218 str(server_id))
219 body = json.loads(body)
220 return resp, body
221
222 def delete_password(self, server_id):
223 """
224 Removes the encrypted server password from the metadata server
225 Note that this does not actually change the instance server
226 password.
227 """
228 return self.delete("servers/%s/os-server-password" %
229 str(server_id))
230
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600231 def reboot(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500232 """Reboots a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100233 return self.action(server_id, 'reboot', None, type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600234
Attila Fazekasd4299282013-02-22 13:25:23 +0100235 def rebuild(self, server_id, image_ref, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500236 """Rebuilds a server with a new image."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100237 kwargs['imageRef'] = image_ref
238 if 'disk_config' in kwargs:
239 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
240 del kwargs['disk_config']
241 return self.action(server_id, 'rebuild', 'server', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600242
Attila Fazekasd4299282013-02-22 13:25:23 +0100243 def resize(self, server_id, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600244 """Changes the flavor of a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100245 kwargs['flavorRef'] = flavor_ref
246 if 'disk_config' in kwargs:
247 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
248 del kwargs['disk_config']
249 return self.action(server_id, 'resize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600250
Attila Fazekasd4299282013-02-22 13:25:23 +0100251 def confirm_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500252 """Confirms the flavor change for a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100253 return self.action(server_id, 'confirmResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600254
Attila Fazekasd4299282013-02-22 13:25:23 +0100255 def revert_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500256 """Reverts a server back to its original flavor."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100257 return self.action(server_id, 'revertResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600258
Attila Fazekasd4299282013-02-22 13:25:23 +0100259 def create_image(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500260 """Creates an image of the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100261 return self.action(server_id, 'createImage', None, name=name)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600262
263 def list_server_metadata(self, server_id):
chris fattarsi5098fa22012-04-17 13:27:00 -0700264 resp, body = self.get("servers/%s/metadata" % str(server_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600265 body = json.loads(body)
266 return resp, body['metadata']
267
Chris Yeohd0b52e72013-09-17 22:38:59 +0930268 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
269 if no_metadata_field:
270 post_body = ""
271 else:
272 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800273 resp, body = self.put('servers/%s/metadata' % str(server_id),
274 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600275 body = json.loads(body)
276 return resp, body['metadata']
277
278 def update_server_metadata(self, server_id, meta):
279 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800280 resp, body = self.post('servers/%s/metadata' % str(server_id),
281 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600282 body = json.loads(body)
283 return resp, body['metadata']
284
285 def get_server_metadata_item(self, server_id, key):
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800286 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600287 body = json.loads(body)
288 return resp, body['meta']
289
290 def set_server_metadata_item(self, server_id, key, meta):
291 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800292 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
293 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600294 body = json.loads(body)
295 return resp, body['meta']
296
297 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700298 resp, body = self.delete("servers/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800299 (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600300 return resp, body
Dan Smithc18d8c62012-07-02 08:09:26 -0700301
Attila Fazekasd4299282013-02-22 13:25:23 +0100302 def stop(self, server_id, **kwargs):
303 return self.action(server_id, 'os-stop', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700304
Attila Fazekasd4299282013-02-22 13:25:23 +0100305 def start(self, server_id, **kwargs):
306 return self.action(server_id, 'os-start', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700307
308 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500309 """Attaches a volume to a server instance."""
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900310 post_body = json.dumps({
311 'volumeAttachment': {
312 'volumeId': volume_id,
313 'device': device,
314 }
315 })
Dan Smithc18d8c62012-07-02 08:09:26 -0700316 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
317 post_body, self.headers)
318 return resp, body
319
320 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500321 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700322 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
323 (server_id, volume_id))
324 return resp, body
sapan-kona775cf632012-06-22 00:32:36 +0530325
Attila Fazekasd4299282013-02-22 13:25:23 +0100326 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500327 """Adds a security group to the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100328 return self.action(server_id, 'addSecurityGroup', None, name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530329
Attila Fazekasd4299282013-02-22 13:25:23 +0100330 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500331 """Removes a security group from the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100332 return self.action(server_id, 'removeSecurityGroup', None, name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100333
334 def live_migrate_server(self, server_id, dest_host, use_block_migration):
Sean Daguef237ccb2013-01-04 15:19:14 -0500335 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100336
337 migrate_params = {
338 "disk_over_commit": False,
339 "block_migration": use_block_migration,
340 "host": dest_host
341 }
342
343 req_body = json.dumps({'os-migrateLive': migrate_params})
344
345 resp, body = self.post("servers/%s/action" % str(server_id),
346 req_body, self.headers)
347 return resp, body
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600348
Attila Fazekasd4299282013-02-22 13:25:23 +0100349 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500350 """Migrates a server to a new host."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100351 return self.action(server_id, 'migrate', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600352
Attila Fazekasd4299282013-02-22 13:25:23 +0100353 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500354 """Locks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100355 return self.action(server_id, 'lock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600356
Attila Fazekasd4299282013-02-22 13:25:23 +0100357 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500358 """UNlocks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100359 return self.action(server_id, 'unlock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600360
Attila Fazekasd4299282013-02-22 13:25:23 +0100361 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900362 """Suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100363 return self.action(server_id, 'suspend', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600364
Attila Fazekasd4299282013-02-22 13:25:23 +0100365 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900366 """Un-suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100367 return self.action(server_id, 'resume', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600368
Attila Fazekasd4299282013-02-22 13:25:23 +0100369 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900370 """Pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100371 return self.action(server_id, 'pause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600372
Attila Fazekasd4299282013-02-22 13:25:23 +0100373 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900374 """Un-pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100375 return self.action(server_id, 'unpause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600376
Attila Fazekasd4299282013-02-22 13:25:23 +0100377 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500378 """Resets the state of a server to active/error."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100379 return self.action(server_id, 'os-resetState', None, state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100380
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900381 def shelve_server(self, server_id, **kwargs):
382 """Shelves the provided server."""
383 return self.action(server_id, 'shelve', None, **kwargs)
384
385 def unshelve_server(self, server_id, **kwargs):
386 """Un-shelves the provided server."""
387 return self.action(server_id, 'unshelve', None, **kwargs)
388
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100389 def get_console_output(self, server_id, length):
Attila Fazekasd4299282013-02-22 13:25:23 +0100390 return self.action(server_id, 'os-getConsoleOutput', 'output',
391 length=length)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200392
393 def list_virtual_interfaces(self, server_id):
394 """
395 List the virtual interfaces used in an instance.
396 """
397 resp, body = self.get('/'.join(['servers', server_id,
398 'os-virtual-interfaces']))
399 return resp, json.loads(body)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000400
Yuiko Takada7835d502014-01-15 10:15:03 +0000401 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000402 """Rescue the provided server."""
Yuiko Takada7835d502014-01-15 10:15:03 +0000403 return self.action(server_id, 'rescue', None, **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000404
405 def unrescue_server(self, server_id):
406 """Unrescue the provided server."""
407 return self.action(server_id, 'unrescue', None)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900408
Zhu Zhuda070852013-09-25 08:07:57 -0500409 def get_server_diagnostics(self, server_id):
410 """Get the usage data for a server."""
411 resp, body = self.get("servers/%s/diagnostics" % str(server_id))
412 return resp, json.loads(body)
413
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900414 def list_instance_actions(self, server_id):
415 """List the provided server action."""
416 resp, body = self.get("servers/%s/os-instance-actions" %
417 str(server_id))
418 body = json.loads(body)
419 return resp, body['instanceActions']
420
421 def get_instance_action(self, server_id, request_id):
422 """Returns the action details of the provided server."""
423 resp, body = self.get("servers/%s/os-instance-actions/%s" %
424 (str(server_id), str(request_id)))
425 body = json.loads(body)
426 return resp, body['instanceAction']
Lingxian Kongaecc1092013-10-03 16:18:46 +0800427
428 def force_delete_server(self, server_id, **kwargs):
429 """Force delete a server."""
430 return self.action(server_id, 'forceDelete', None, **kwargs)
431
432 def restore_soft_deleted_server(self, server_id, **kwargs):
433 """Restore a soft-deleted server."""
434 return self.action(server_id, 'restore', None, **kwargs)