blob: b832af03decaea8a882706e992b2be0c68a8de45 [file] [log] [blame]
dwallecke62b9f02012-10-10 23:34:42 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Daryl Wallecke5b83d42011-11-10 14:39:02 -060018import json
Daryl Wallecke5b83d42011-11-10 14:39:02 -060019import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050020import urllib
Daryl Wallecke5b83d42011-11-10 14:39:02 -060021
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest.common.rest_client import RestClient
23from tempest import exceptions
24
Daryl Wallecke5b83d42011-11-10 14:39:02 -060025
Dan Smithcf8fab62012-08-14 08:03:48 -070026class ServersClientJSON(RestClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060027
Daryl Walleck587385b2012-03-03 13:00:26 -060028 def __init__(self, config, username, password, auth_url, tenant_name=None):
Dan Smithcf8fab62012-08-14 08:03:48 -070029 super(ServersClientJSON, self).__init__(config, username, password,
30 auth_url, tenant_name)
chris fattarsi5098fa22012-04-17 13:27:00 -070031 self.service = self.config.compute.catalog_type
Daryl Wallecke5b83d42011-11-10 14:39:02 -060032
Rohit Karajgi95446a22011-12-12 06:21:43 -080033 def create_server(self, name, image_ref, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060034 """
35 Creates an instance of a server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080036 name (Required): The name of the server.
37 image_ref (Required): Reference to the image used to build the server.
38 flavor_ref (Required): The flavor used to build the server.
39 Following optional keyword arguments are accepted:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060040 adminPass: Sets the initial root password.
chris fattarsia6c2a732012-05-02 17:00:19 -070041 key_name: Key name of keypair that was created earlier.
David Kranz28e79de2012-04-12 15:49:41 -040042 meta: A dictionary of values to be used as metadata.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060043 personality: A list of dictionaries for files to be injected into
44 the server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080045 security_groups: A list of security group dicts.
46 networks: A list of network dicts with UUID and fixed_ip.
47 user_data: User data for instance.
48 availability_zone: Availability zone in which to launch instance.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060049 accessIPv4: The IPv4 access address for the server.
50 accessIPv6: The IPv6 access address for the server.
Rohit Karajgi95446a22011-12-12 06:21:43 -080051 min_count: Count of minimum number of instances to launch.
52 max_count: Count of maximum number of instances to launch.
Daryl Wallecke36d5002012-03-28 09:56:10 -050053 disk_config: Determines if user or admin controls disk configuration.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060054 """
Daryl Wallecke5b83d42011-11-10 14:39:02 -060055 post_body = {
56 'name': name,
57 'imageRef': image_ref,
David Kranz28e79de2012-04-12 15:49:41 -040058 'flavorRef': flavor_ref
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059 }
60
chris fattarsia6c2a732012-05-02 17:00:19 -070061 for option in ['personality', 'adminPass', 'key_name',
Zhongyue Luoe0884a32012-09-25 17:24:17 +080062 'security_groups', 'networks', 'user_data',
63 'availability_zone', 'accessIPv4', 'accessIPv6',
64 'min_count', 'max_count', ('metadata', 'meta'),
65 ('OS-DCF:diskConfig', 'disk_config')]:
David Kranz28e79de2012-04-12 15:49:41 -040066 if isinstance(option, tuple):
67 post_param = option[0]
68 key = option[1]
69 else:
70 post_param = option
71 key = option
72 value = kwargs.get(key)
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080073 if value is not None:
David Kranz28e79de2012-04-12 15:49:41 -040074 post_body[post_param] = value
Daryl Wallecke5b83d42011-11-10 14:39:02 -060075 post_body = json.dumps({'server': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -070076 resp, body = self.post('servers', post_body, self.headers)
Jay Pipes5135bfc2012-01-05 15:46:49 -050077
Daryl Wallecke5b83d42011-11-10 14:39:02 -060078 body = json.loads(body)
79 return resp, body['server']
80
81 def update_server(self, server_id, name=None, meta=None, accessIPv4=None,
82 accessIPv6=None):
83 """
84 Updates the properties of an existing server.
85 server_id: The id of an existing server.
86 name: The name of the server.
87 personality: A list of files to be injected into the server.
88 accessIPv4: The IPv4 access address for the server.
89 accessIPv6: The IPv6 access address for the server.
90 """
91
92 post_body = {}
93
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080094 if meta is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060095 post_body['metadata'] = meta
96
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080097 if name is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060098 post_body['name'] = name
99
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800100 if accessIPv4 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600101 post_body['accessIPv4'] = accessIPv4
102
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800103 if accessIPv6 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600104 post_body['accessIPv6'] = accessIPv6
105
106 post_body = json.dumps({'server': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -0700107 resp, body = self.put("servers/%s" % str(server_id),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800108 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600109 body = json.loads(body)
110 return resp, body['server']
111
112 def get_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500113 """Returns the details of an existing server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700114 resp, body = self.get("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600115 body = json.loads(body)
116 return resp, body['server']
117
118 def delete_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500119 """Deletes the given server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700120 return self.delete("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600121
122 def list_servers(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500123 """Lists all servers for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600124
125 url = 'servers'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500126 if params:
127 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600128
chris fattarsi5098fa22012-04-17 13:27:00 -0700129 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600130 body = json.loads(body)
131 return resp, body
132
133 def list_servers_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500134 """Lists all servers in detail for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600135
136 url = 'servers/detail'
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 wait_for_server_status(self, server_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500145 """Waits for a server to reach a given status."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600146 resp, body = self.get_server(server_id)
147 server_status = body['status']
148 start = int(time.time())
149
150 while(server_status != status):
151 time.sleep(self.build_interval)
152 resp, body = self.get_server(server_id)
153 server_status = body['status']
154
Jay Pipes5135bfc2012-01-05 15:46:49 -0500155 if server_status == 'ERROR':
156 raise exceptions.BuildErrorException(server_id=server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600157
Eoghan Glynnf72969c2012-03-05 12:33:10 +0000158 timed_out = int(time.time()) - start >= self.build_timeout
159
160 if server_status != status and timed_out:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800161 message = ('Server %s failed to reach %s status within the '
162 'required time (%s s).' %
163 (server_id, status, self.build_timeout))
Eoghan Glynnf72969c2012-03-05 12:33:10 +0000164 message += ' Current status: %s.' % server_status
Daryl Walleckf0087032011-12-18 13:37:05 -0600165 raise exceptions.TimeoutException(message)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600166
Jay Pipes051075a2012-04-28 17:39:37 -0400167 def wait_for_server_termination(self, server_id, ignore_error=False):
Sean Daguef237ccb2013-01-04 15:19:14 -0500168 """Waits for server to reach termination."""
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700169 start_time = int(time.time())
170 while True:
171 try:
172 resp, body = self.get_server(server_id)
173 except exceptions.NotFound:
174 return
175
176 server_status = body['status']
Jay Pipes051075a2012-04-28 17:39:37 -0400177 if server_status == 'ERROR' and not ignore_error:
Jay Pipes444c3e62012-10-04 19:26:35 -0400178 raise exceptions.BuildErrorException(server_id=server_id)
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700179
180 if int(time.time()) - start_time >= self.build_timeout:
181 raise exceptions.TimeoutException
182
183 time.sleep(self.build_interval)
184
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600185 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500186 """Lists all addresses for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700187 resp, body = self.get("servers/%s/ips" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600188 body = json.loads(body)
189 return resp, body['addresses']
190
191 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500192 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700193 resp, body = self.get("servers/%s/ips/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800194 (str(server_id), network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600195 body = json.loads(body)
196 return resp, body
197
198 def change_password(self, server_id, password):
Sean Daguef237ccb2013-01-04 15:19:14 -0500199 """Changes the root password for the server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600200 post_body = {
201 'changePassword': {
202 'adminPass': password,
203 }
204 }
205
206 post_body = json.dumps(post_body)
chris fattarsi5098fa22012-04-17 13:27:00 -0700207 return self.post('servers/%s/action' % str(server_id),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800208 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600209
210 def reboot(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500211 """Reboots a server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600212 post_body = {
213 'reboot': {
214 'type': reboot_type,
215 }
216 }
217
218 post_body = json.dumps(post_body)
chris fattarsi5098fa22012-04-17 13:27:00 -0700219 return self.post('servers/%s/action' % str(server_id),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800220 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600221
222 def rebuild(self, server_id, image_ref, name=None, meta=None,
Daryl Wallecke36d5002012-03-28 09:56:10 -0500223 personality=None, adminPass=None, disk_config=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500224 """Rebuilds a server with a new image."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600225 post_body = {
226 'imageRef': image_ref,
227 }
228
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800229 if name is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600230 post_body['name'] = name
231
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800232 if adminPass is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600233 post_body['adminPass'] = adminPass
234
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800235 if meta is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600236 post_body['metadata'] = meta
237
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800238 if personality is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600239 post_body['personality'] = personality
240
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800241 if disk_config is not None:
Daryl Wallecke36d5002012-03-28 09:56:10 -0500242 post_body['OS-DCF:diskConfig'] = disk_config
243
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600244 post_body = json.dumps({'rebuild': post_body})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800245 resp, body = self.post('servers/%s/action' % str(server_id),
246 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600247 body = json.loads(body)
Brian Waldon738cd632011-12-12 18:45:09 -0500248 return resp, body['server']
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600249
Daryl Wallecke36d5002012-03-28 09:56:10 -0500250 def resize(self, server_id, flavor_ref, disk_config=None):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600251 """Changes the flavor of a server."""
252 post_body = {
253 'resize': {
254 'flavorRef': flavor_ref,
255 }
256 }
257
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800258 if disk_config is not None:
Daryl Wallecke36d5002012-03-28 09:56:10 -0500259 post_body['resize']['OS-DCF:diskConfig'] = disk_config
260
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600261 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800262 resp, body = self.post('servers/%s/action' % str(server_id),
263 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600264 return resp, body
265
266 def confirm_resize(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500267 """Confirms the flavor change for a server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600268 post_body = {
Brian Waldon3bde07f2011-12-13 15:11:22 -0500269 'confirmResize': None,
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600270 }
271
272 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800273 resp, body = self.post('servers/%s/action' % str(server_id),
274 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600275 return resp, body
276
277 def revert_resize(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500278 """Reverts a server back to its original flavor."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600279 post_body = {
Brian Waldon3bde07f2011-12-13 15:11:22 -0500280 'revertResize': None,
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600281 }
282
283 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800284 resp, body = self.post('servers/%s/action' % str(server_id),
285 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600286 return resp, body
287
288 def create_image(self, server_id, image_name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500289 """Creates an image of the given server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600290 post_body = {
291 'createImage': {
292 'name': image_name,
293 }
294 }
295
296 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800297 resp, body = self.post('servers/%s/action' % str(server_id),
298 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600299 return resp, body
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600300
301 def list_server_metadata(self, server_id):
chris fattarsi5098fa22012-04-17 13:27:00 -0700302 resp, body = self.get("servers/%s/metadata" % str(server_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600303 body = json.loads(body)
304 return resp, body['metadata']
305
306 def set_server_metadata(self, server_id, meta):
307 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800308 resp, body = self.put('servers/%s/metadata' % str(server_id),
309 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600310 body = json.loads(body)
311 return resp, body['metadata']
312
313 def update_server_metadata(self, server_id, meta):
314 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800315 resp, body = self.post('servers/%s/metadata' % str(server_id),
316 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600317 body = json.loads(body)
318 return resp, body['metadata']
319
320 def get_server_metadata_item(self, server_id, key):
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800321 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600322 body = json.loads(body)
323 return resp, body['meta']
324
325 def set_server_metadata_item(self, server_id, key, meta):
326 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800327 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
328 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600329 body = json.loads(body)
330 return resp, body['meta']
331
332 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700333 resp, body = self.delete("servers/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800334 (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600335 return resp, body
Dan Smithc18d8c62012-07-02 08:09:26 -0700336
337 def stop(self, server_id):
338 post_body = json.dumps({'os-stop': None})
339 resp, body = self.post('servers/%s/action' % server_id,
340 post_body, self.headers)
341
342 def start(self, server_id):
343 post_body = json.dumps({'os-start': None})
344 resp, body = self.post('servers/%s/action' % server_id,
345 post_body, self.headers)
346
347 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500348 """Attaches a volume to a server instance."""
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900349 post_body = json.dumps({
350 'volumeAttachment': {
351 'volumeId': volume_id,
352 'device': device,
353 }
354 })
Dan Smithc18d8c62012-07-02 08:09:26 -0700355 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
356 post_body, self.headers)
357 return resp, body
358
359 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500360 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700361 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
362 (server_id, volume_id))
363 return resp, body
sapan-kona775cf632012-06-22 00:32:36 +0530364
365 def add_security_group(self, server_id, security_group_name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500366 """Adds a security group to the server."""
sapan-kona775cf632012-06-22 00:32:36 +0530367 post_body = {
368 'addSecurityGroup': {
369 'name': security_group_name
370 }
371 }
372 post_body = json.dumps(post_body)
373 return self.post('servers/%s/action' % server_id,
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800374 post_body, self.headers)
sapan-kona775cf632012-06-22 00:32:36 +0530375
376 def remove_security_group(self, server_id, security_group_name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500377 """Removes a security group from the server."""
sapan-kona775cf632012-06-22 00:32:36 +0530378 post_body = {
379 'removeSecurityGroup': {
380 'name': security_group_name
381 }
382 }
383 post_body = json.dumps(post_body)
384 return self.post('servers/%s/action' % server_id,
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800385 post_body, self.headers)
Mate Lakat99ee9142012-09-14 12:34:46 +0100386
387 def live_migrate_server(self, server_id, dest_host, use_block_migration):
Sean Daguef237ccb2013-01-04 15:19:14 -0500388 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100389
390 migrate_params = {
391 "disk_over_commit": False,
392 "block_migration": use_block_migration,
393 "host": dest_host
394 }
395
396 req_body = json.dumps({'os-migrateLive': migrate_params})
397
398 resp, body = self.post("servers/%s/action" % str(server_id),
399 req_body, self.headers)
400 return resp, body
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600401
402 def list_servers_for_all_tenants(self):
403
404 url = self.base_url + '/servers?all_tenants=1'
405 resp = self.requests.get(url)
406 resp, body = self.post('servers', post_body, self.headers)
407
408 body = json.loads(body)
409 return resp, body['servers']
410
411 def migrate_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500412 """Migrates a server to a new host."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600413 post_body = {'migrate': 'null'}
414 post_body = json.dumps(post_body)
415 resp, body = self.post('servers/%s/action' % server_id,
416 post_body, self.headers)
417 return resp, body
418
419 def confirm_migration(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500420 """Confirms the migration of a server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600421 post_body = {'confirmResize': 'null'}
422 post_body = json.dumps(post_body)
423 resp, body = self.post('servers/%s/action' % server_id,
424 post_body, self.headers)
425 return resp, body
426
427 def lock_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500428 """Locks the given server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600429 post_body = {'lock': 'null'}
430 post_body = json.dumps(post_body)
431 resp, body = self.post('servers/%s/action' % server_id,
432 post_body, self.headers)
433
434 def unlock_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500435 """UNlocks the given server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600436 post_body = {'unlock': 'null'}
437 post_body = json.dumps(post_body)
438 resp, body = self.post('servers/%s/action' % server_id,
439 post_body, self.headers)
440
441 def start_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500442 """Starts the given server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600443 post_body = {'os-start': 'null'}
444 post_body = json.dumps(post_body)
445 resp, body = self.post('servers/%s/action' % server_id,
446 post_body, self.headers)
447
448 def stop_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500449 """Stops the given server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600450 post_body = {'os-stop': 'null'}
451 post_body = json.dumps(post_body)
452 resp, body = self.post('servers/%s/action' % server_id,
453 post_body, self.headers)
454
455 def suspend_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500456 """Suspends the provded server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600457 post_body = {'suspend': 'null'}
458 post_body = json.dumps(post_body)
459 resp, body = self.post('servers/%s/action' % server_id,
460 post_body, self.headers)
461
462 def resume_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500463 """Un-suspends the provded server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600464 post_body = {'resume': 'null'}
465 post_body = json.dumps(post_body)
466 resp, body = self.post('servers/%s/action' % server_id,
467 post_body, self.headers)
468
469 def pause_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500470 """Pauses the provded server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600471 post_body = {'pause': 'null'}
472 post_body = json.dumps(post_body)
473 resp, body = self.post('servers/%s/action' % server_id,
474 post_body, self.headers)
475
476 def unpause_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500477 """Un-pauses the provded server."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600478 post_body = {'unpause': 'null'}
479 post_body = json.dumps(post_body)
480 resp, body = self.post('servers/%s/action' % server_id,
481 post_body, self.headers)
482
483 def reset_state(self, server_id, new_state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500484 """Resets the state of a server to active/error."""
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600485 post_body = {
486 'os-resetState': {
487 'state': new_state
488 }
489 }
490 resp, body = self.post('servers/%s/action' % server_id,
491 post_body, self.headers)
492 return resp, body