blob: be23b7861079ecdb263d82fb457457f5853e6de0 [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020021from tempest.api_schema.response.compute import servers as common_schema
22from tempest.api_schema.response.compute.v2 import servers as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000023from tempest.common import service_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
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000031class ServersClientJSON(service_client.ServiceClient):
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.
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050054 return_reservation_id: Enable/Disable the return of reservation id
Andrey Pavlov2a2fc412014-06-09 11:02:53 +040055 block_device_mapping: Block device mapping for the server.
Joseph Lanouxeef192f2014-08-01 14:32:53 +000056 block_device_mapping_v2: Block device mapping V2 for the server.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060057 """
Daryl Wallecke5b83d42011-11-10 14:39:02 -060058 post_body = {
59 'name': name,
60 'imageRef': image_ref,
David Kranz28e79de2012-04-12 15:49:41 -040061 'flavorRef': flavor_ref
Daryl Wallecke5b83d42011-11-10 14:39:02 -060062 }
63
chris fattarsia6c2a732012-05-02 17:00:19 -070064 for option in ['personality', 'adminPass', 'key_name',
Zhongyue Luoe0884a32012-09-25 17:24:17 +080065 'security_groups', 'networks', 'user_data',
66 'availability_zone', 'accessIPv4', 'accessIPv6',
67 'min_count', 'max_count', ('metadata', 'meta'),
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050068 ('OS-DCF:diskConfig', 'disk_config'),
Joseph Lanouxeef192f2014-08-01 14:32:53 +000069 'return_reservation_id', 'block_device_mapping',
70 'block_device_mapping_v2']:
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
Joseph Lanouxeef192f2014-08-01 14:32:53 +000080
raiesmh08cbe21b02014-03-12 17:04:44 +053081 post_body = {'server': post_body}
82
83 if 'sched_hints' in kwargs:
84 hints = {'os:scheduler_hints': kwargs.get('sched_hints')}
85 post_body = dict(post_body.items() + hints.items())
86 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020087 resp, body = self.post('servers', post_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -050088
Daryl Wallecke5b83d42011-11-10 14:39:02 -060089 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050090 # NOTE(maurosr): this deals with the case of multiple server create
91 # with return reservation id set True
92 if 'reservation_id' in body:
93 return resp, body
Ghanshyam7f989102014-07-29 14:23:26 +090094 if CONF.compute_feature_enabled.enable_instance_password:
95 create_schema = schema.create_server_with_admin_pass
96 else:
97 create_schema = schema.create_server
98 self.validate_response(create_schema, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060099 return resp, body['server']
100
101 def update_server(self, server_id, name=None, meta=None, accessIPv4=None,
ivan-zhu72d7d5b2013-10-16 17:30:58 +0800102 accessIPv6=None, disk_config=None):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600103 """
104 Updates the properties of an existing server.
105 server_id: The id of an existing server.
106 name: The name of the server.
107 personality: A list of files to be injected into the server.
108 accessIPv4: The IPv4 access address for the server.
109 accessIPv6: The IPv6 access address for the server.
110 """
111
112 post_body = {}
113
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800114 if meta is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600115 post_body['metadata'] = meta
116
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800117 if name is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600118 post_body['name'] = name
119
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800120 if accessIPv4 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600121 post_body['accessIPv4'] = accessIPv4
122
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800123 if accessIPv6 is not None:
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600124 post_body['accessIPv6'] = accessIPv6
125
ivan-zhu72d7d5b2013-10-16 17:30:58 +0800126 if disk_config is not None:
127 post_body['OS-DCF:diskConfig'] = disk_config
128
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600129 post_body = json.dumps({'server': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +0200130 resp, body = self.put("servers/%s" % str(server_id), post_body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600131 body = json.loads(body)
Ken'ichi Ohmichi29cd5122014-04-28 11:04:52 +0900132 self.validate_response(schema.update_server, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600133 return resp, body['server']
134
135 def get_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500136 """Returns the details of an existing server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700137 resp, body = self.get("servers/%s" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600138 body = json.loads(body)
Ken'ichi Ohmichi21e4fc72014-05-08 16:46:23 +0900139 self.validate_response(schema.get_server, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600140 return resp, body['server']
141
142 def delete_server(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500143 """Deletes the given server."""
Ghanshyam4ac02742014-04-17 19:15:47 +0900144 resp, body = self.delete("servers/%s" % str(server_id))
145 self.validate_response(common_schema.delete_server, resp, body)
146 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600147
148 def list_servers(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500149 """Lists all servers for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600150
151 url = 'servers'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500152 if params:
153 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600154
chris fattarsi5098fa22012-04-17 13:27:00 -0700155 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600156 body = json.loads(body)
Ghanshyam623c38f2014-04-21 17:16:21 +0900157 self.validate_response(common_schema.list_servers, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600158 return resp, body
159
160 def list_servers_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500161 """Lists all servers in detail for a user."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600162
163 url = 'servers/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500164 if params:
165 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600166
chris fattarsi5098fa22012-04-17 13:27:00 -0700167 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600168 body = json.loads(body)
Ghanshyam51744862014-06-13 12:56:24 +0900169 self.validate_response(schema.list_servers_detail, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600170 return resp, body
171
Zhi Kun Liue5401762013-09-11 20:45:48 +0800172 def wait_for_server_status(self, server_id, status, extra_timeout=0,
Adam Gandelmanc78c7572014-08-28 18:38:55 -0700173 raise_on_error=True, ready_wait=True):
Sean Daguef237ccb2013-01-04 15:19:14 -0500174 """Waits for a server to reach a given status."""
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900175 return waiters.wait_for_server_status(self, server_id, status,
Zhi Kun Liue5401762013-09-11 20:45:48 +0800176 extra_timeout=extra_timeout,
Adam Gandelmanc78c7572014-08-28 18:38:55 -0700177 raise_on_error=raise_on_error,
178 ready_wait=ready_wait)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600179
Jay Pipes051075a2012-04-28 17:39:37 -0400180 def wait_for_server_termination(self, server_id, ignore_error=False):
Sean Daguef237ccb2013-01-04 15:19:14 -0500181 """Waits for server to reach termination."""
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700182 start_time = int(time.time())
183 while True:
184 try:
185 resp, body = self.get_server(server_id)
186 except exceptions.NotFound:
187 return
188
189 server_status = body['status']
Jay Pipes051075a2012-04-28 17:39:37 -0400190 if server_status == 'ERROR' and not ignore_error:
Jay Pipes444c3e62012-10-04 19:26:35 -0400191 raise exceptions.BuildErrorException(server_id=server_id)
Rohit Karajgi4a64d232012-05-17 07:44:37 -0700192
193 if int(time.time()) - start_time >= self.build_timeout:
194 raise exceptions.TimeoutException
195
196 time.sleep(self.build_interval)
197
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600198 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500199 """Lists all addresses for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700200 resp, body = self.get("servers/%s/ips" % str(server_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600201 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900202 self.validate_response(schema.list_addresses, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600203 return resp, body['addresses']
204
205 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500206 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700207 resp, body = self.get("servers/%s/ips/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800208 (str(server_id), network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600209 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900210 self.validate_response(schema.list_addresses_by_network, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600211 return resp, body
212
Ghanshyamd6d30402014-04-02 15:28:37 +0900213 def action(self, server_id, action_name, response_key,
Ghanshyam997c9092014-04-03 19:00:20 +0900214 schema=common_schema.server_actions_common_schema, **kwargs):
Attila Fazekasd4299282013-02-22 13:25:23 +0100215 post_body = json.dumps({action_name: kwargs})
216 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200217 post_body)
Attila Fazekasd4299282013-02-22 13:25:23 +0100218 if response_key is not None:
Ghanshyamd6d30402014-04-02 15:28:37 +0900219 body = json.loads(body)
Ghanshyam7ee264a2014-04-02 16:37:57 +0900220 # Check for Schema as 'None' because if we do not have any server
Ghanshyamd6d30402014-04-02 15:28:37 +0900221 # action schema implemented yet then they can pass 'None' to skip
222 # the validation.Once all server action has their schema
223 # implemented then, this check can be removed if every actions are
224 # supposed to validate their response.
Ghanshyam7ee264a2014-04-02 16:37:57 +0900225 # TODO(GMann): Remove the below 'if' check once all server actions
226 # schema are implemented.
Ghanshyamd6d30402014-04-02 15:28:37 +0900227 if schema is not None:
228 self.validate_response(schema, resp, body)
229 body = body[response_key]
Ghanshyam997c9092014-04-03 19:00:20 +0900230 else:
231 self.validate_response(schema, resp, body)
Attila Fazekasd4299282013-02-22 13:25:23 +0100232 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600233
ivan-zhuccc89462013-10-31 16:53:12 +0800234 def create_backup(self, server_id, backup_type, rotation, name):
235 """Backup a server instance."""
236 return self.action(server_id, "createBackup", None,
237 backup_type=backup_type,
238 rotation=rotation,
239 name=name)
240
Attila Fazekasd4299282013-02-22 13:25:23 +0100241 def change_password(self, server_id, adminPass):
242 """Changes the root password for the server."""
243 return self.action(server_id, 'changePassword', None,
244 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600245
ivan-zhu6de5b042013-10-24 17:21:48 +0800246 def get_password(self, server_id):
247 resp, body = self.get("servers/%s/os-server-password" %
248 str(server_id))
249 body = json.loads(body)
Ghanshyam7fa397c2014-04-01 19:32:38 +0900250 self.validate_response(common_schema.get_password, resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800251 return resp, body
252
253 def delete_password(self, server_id):
254 """
255 Removes the encrypted server password from the metadata server
256 Note that this does not actually change the instance server
257 password.
258 """
Ghanshyam997c9092014-04-03 19:00:20 +0900259 resp, body = self.delete("servers/%s/os-server-password" %
260 str(server_id))
261 self.validate_response(common_schema.server_actions_delete_password,
262 resp, body)
263 return resp, body
ivan-zhu6de5b042013-10-24 17:21:48 +0800264
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600265 def reboot(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500266 """Reboots a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100267 return self.action(server_id, 'reboot', None, type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600268
Attila Fazekasd4299282013-02-22 13:25:23 +0100269 def rebuild(self, server_id, image_ref, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500270 """Rebuilds a server with a new image."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100271 kwargs['imageRef'] = image_ref
272 if 'disk_config' in kwargs:
273 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
274 del kwargs['disk_config']
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900275 if CONF.compute_feature_enabled.enable_instance_password:
276 rebuild_schema = schema.rebuild_server_with_admin_pass
277 else:
278 rebuild_schema = schema.rebuild_server
279 return self.action(server_id, 'rebuild', 'server',
280 rebuild_schema, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600281
Attila Fazekasd4299282013-02-22 13:25:23 +0100282 def resize(self, server_id, flavor_ref, **kwargs):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600283 """Changes the flavor of a server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100284 kwargs['flavorRef'] = flavor_ref
285 if 'disk_config' in kwargs:
286 kwargs['OS-DCF:diskConfig'] = kwargs['disk_config']
287 del kwargs['disk_config']
288 return self.action(server_id, 'resize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600289
Attila Fazekasd4299282013-02-22 13:25:23 +0100290 def confirm_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500291 """Confirms the flavor change for a server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900292 return self.action(server_id, 'confirmResize',
293 None, schema.server_actions_confirm_resize,
294 **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600295
Attila Fazekasd4299282013-02-22 13:25:23 +0100296 def revert_resize(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500297 """Reverts a server back to its original flavor."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100298 return self.action(server_id, 'revertResize', None, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600299
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600300 def list_server_metadata(self, server_id):
chris fattarsi5098fa22012-04-17 13:27:00 -0700301 resp, body = self.get("servers/%s/metadata" % str(server_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600302 body = json.loads(body)
Ghanshyama8f66532014-04-23 17:18:14 +0900303 self.validate_response(common_schema.list_server_metadata, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600304 return resp, body['metadata']
305
Chris Yeohd0b52e72013-09-17 22:38:59 +0930306 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
307 if no_metadata_field:
308 post_body = ""
309 else:
310 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800311 resp, body = self.put('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200312 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600313 body = json.loads(body)
Haiwei Xu3d2b7af2014-04-12 02:50:26 +0900314 self.validate_response(common_schema.set_server_metadata, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600315 return resp, body['metadata']
316
317 def update_server_metadata(self, server_id, meta):
318 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800319 resp, body = self.post('servers/%s/metadata' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200320 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600321 body = json.loads(body)
Ghanshyame8421062014-06-02 15:58:21 +0900322 self.validate_response(common_schema.update_server_metadata,
323 resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600324 return resp, body['metadata']
325
326 def get_server_metadata_item(self, server_id, key):
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800327 resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600328 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900329 self.validate_response(schema.set_get_server_metadata_item,
330 resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600331 return resp, body['meta']
332
333 def set_server_metadata_item(self, server_id, key, meta):
334 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800335 resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200336 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600337 body = json.loads(body)
Ghanshyameaaa6a42014-04-25 18:38:21 +0900338 self.validate_response(schema.set_get_server_metadata_item,
339 resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600340 return resp, body['meta']
341
342 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700343 resp, body = self.delete("servers/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800344 (str(server_id), key))
Ghanshyameaaa6a42014-04-25 18:38:21 +0900345 self.validate_response(common_schema.delete_server_metadata_item,
346 resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600347 return resp, body
Dan Smithc18d8c62012-07-02 08:09:26 -0700348
Attila Fazekasd4299282013-02-22 13:25:23 +0100349 def stop(self, server_id, **kwargs):
350 return self.action(server_id, 'os-stop', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700351
Attila Fazekasd4299282013-02-22 13:25:23 +0100352 def start(self, server_id, **kwargs):
353 return self.action(server_id, 'os-start', None, **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700354
355 def attach_volume(self, server_id, volume_id, device='/dev/vdz'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500356 """Attaches a volume to a server instance."""
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900357 post_body = json.dumps({
358 'volumeAttachment': {
359 'volumeId': volume_id,
360 'device': device,
361 }
362 })
Dan Smithc18d8c62012-07-02 08:09:26 -0700363 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200364 post_body)
Ghanshyam385c4e72014-03-27 11:42:25 +0900365 body = json.loads(body)
366 self.validate_response(schema.attach_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500367 return service_client.ResponseBody(resp, body['volumeAttachment'])
Dan Smithc18d8c62012-07-02 08:09:26 -0700368
369 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500370 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700371 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
372 (server_id, volume_id))
Ghanshyam385c4e72014-03-27 11:42:25 +0900373 self.validate_response(schema.detach_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500374 return service_client.ResponseBody(resp, body)
sapan-kona775cf632012-06-22 00:32:36 +0530375
Ghanshyam5c2a5582014-04-14 17:16:57 +0900376 def get_volume_attachment(self, server_id, attach_id):
377 """Return details about the given volume attachment."""
378 resp, body = self.get('servers/%s/os-volume_attachments/%s' % (
379 str(server_id), attach_id))
380 body = json.loads(body)
381 self.validate_response(schema.get_volume_attachment, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500382 return service_client.ResponseBody(resp, body['volumeAttachment'])
Ghanshyam5c2a5582014-04-14 17:16:57 +0900383
384 def list_volume_attachments(self, server_id):
385 """Returns the list of volume attachments for a given instance."""
386 resp, body = self.get('servers/%s/os-volume_attachments' % (
387 str(server_id)))
388 body = json.loads(body)
389 self.validate_response(schema.list_volume_attachments, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500390 return service_client.ResponseBodyList(resp, body['volumeAttachments'])
Ghanshyam5c2a5582014-04-14 17:16:57 +0900391
Attila Fazekasd4299282013-02-22 13:25:23 +0100392 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500393 """Adds a security group to the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100394 return self.action(server_id, 'addSecurityGroup', None, name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530395
Attila Fazekasd4299282013-02-22 13:25:23 +0100396 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500397 """Removes a security group from the server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100398 return self.action(server_id, 'removeSecurityGroup', None, name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100399
400 def live_migrate_server(self, server_id, dest_host, use_block_migration):
Sean Daguef237ccb2013-01-04 15:19:14 -0500401 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100402
403 migrate_params = {
404 "disk_over_commit": False,
405 "block_migration": use_block_migration,
406 "host": dest_host
407 }
408
409 req_body = json.dumps({'os-migrateLive': migrate_params})
410
vponomaryovf4c27f92014-02-18 10:56:42 +0200411 resp, body = self.post("servers/%s/action" % str(server_id), req_body)
Ghanshyam997c9092014-04-03 19:00:20 +0900412 self.validate_response(common_schema.server_actions_common_schema,
413 resp, body)
Mate Lakat99ee9142012-09-14 12:34:46 +0100414 return resp, body
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600415
Attila Fazekasd4299282013-02-22 13:25:23 +0100416 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500417 """Migrates a server to a new host."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100418 return self.action(server_id, 'migrate', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600419
Attila Fazekasd4299282013-02-22 13:25:23 +0100420 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500421 """Locks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100422 return self.action(server_id, 'lock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600423
Attila Fazekasd4299282013-02-22 13:25:23 +0100424 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500425 """UNlocks the given server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100426 return self.action(server_id, 'unlock', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600427
Attila Fazekasd4299282013-02-22 13:25:23 +0100428 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900429 """Suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100430 return self.action(server_id, 'suspend', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600431
Attila Fazekasd4299282013-02-22 13:25:23 +0100432 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900433 """Un-suspends the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100434 return self.action(server_id, 'resume', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600435
Attila Fazekasd4299282013-02-22 13:25:23 +0100436 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900437 """Pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100438 return self.action(server_id, 'pause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600439
Attila Fazekasd4299282013-02-22 13:25:23 +0100440 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900441 """Un-pauses the provided server."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100442 return self.action(server_id, 'unpause', None, **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600443
Attila Fazekasd4299282013-02-22 13:25:23 +0100444 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500445 """Resets the state of a server to active/error."""
Attila Fazekasd4299282013-02-22 13:25:23 +0100446 return self.action(server_id, 'os-resetState', None, state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100447
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900448 def shelve_server(self, server_id, **kwargs):
449 """Shelves the provided server."""
450 return self.action(server_id, 'shelve', None, **kwargs)
451
452 def unshelve_server(self, server_id, **kwargs):
453 """Un-shelves the provided server."""
454 return self.action(server_id, 'unshelve', None, **kwargs)
455
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900456 def shelve_offload_server(self, server_id, **kwargs):
457 """Shelve-offload the provided server."""
458 return self.action(server_id, 'shelveOffload', None, **kwargs)
459
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100460 def get_console_output(self, server_id, length):
Davanum Srinivas8a841c72014-06-19 18:33:14 -0400461 kwargs = {'length': length} if length else {}
Attila Fazekasd4299282013-02-22 13:25:23 +0100462 return self.action(server_id, 'os-getConsoleOutput', 'output',
Davanum Srinivas8a841c72014-06-19 18:33:14 -0400463 common_schema.get_console_output, **kwargs)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200464
465 def list_virtual_interfaces(self, server_id):
466 """
467 List the virtual interfaces used in an instance.
468 """
469 resp, body = self.get('/'.join(['servers', server_id,
470 'os-virtual-interfaces']))
Ghanshyam08ce58d2014-04-04 14:51:14 +0900471 body = json.loads(body)
472 self.validate_response(schema.list_virtual_interfaces, resp, body)
473 return resp, body
nithya-ganesane6a36c82013-02-15 14:38:27 +0000474
Yuiko Takada7835d502014-01-15 10:15:03 +0000475 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000476 """Rescue the provided server."""
Ghanshyam88457912014-07-25 16:02:12 +0900477 return self.action(server_id, 'rescue', 'adminPass',
478 schema.rescue_server, **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000479
480 def unrescue_server(self, server_id):
481 """Unrescue the provided server."""
482 return self.action(server_id, 'unrescue', None)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900483
Zhu Zhuda070852013-09-25 08:07:57 -0500484 def get_server_diagnostics(self, server_id):
485 """Get the usage data for a server."""
486 resp, body = self.get("servers/%s/diagnostics" % str(server_id))
487 return resp, json.loads(body)
488
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900489 def list_instance_actions(self, server_id):
490 """List the provided server action."""
491 resp, body = self.get("servers/%s/os-instance-actions" %
492 str(server_id))
493 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900494 self.validate_response(schema.list_instance_actions, resp, body)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900495 return resp, body['instanceActions']
496
497 def get_instance_action(self, server_id, request_id):
498 """Returns the action details of the provided server."""
499 resp, body = self.get("servers/%s/os-instance-actions/%s" %
500 (str(server_id), str(request_id)))
501 body = json.loads(body)
Ghanshyam4b41dfd2014-07-17 13:40:38 +0900502 self.validate_response(schema.get_instance_action, resp, body)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900503 return resp, body['instanceAction']
Lingxian Kongaecc1092013-10-03 16:18:46 +0800504
505 def force_delete_server(self, server_id, **kwargs):
506 """Force delete a server."""
507 return self.action(server_id, 'forceDelete', None, **kwargs)
508
509 def restore_soft_deleted_server(self, server_id, **kwargs):
510 """Restore a soft-deleted server."""
511 return self.action(server_id, 'restore', None, **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900512
513 def reset_network(self, server_id, **kwargs):
514 """Resets the Network of a server"""
515 return self.action(server_id, 'resetNetwork', None, **kwargs)
516
517 def inject_network_info(self, server_id, **kwargs):
518 """Inject the Network Info into server"""
519 return self.action(server_id, 'injectNetworkInfo', None, **kwargs)
Ghanshyam86d1a572014-03-05 10:19:25 +0900520
521 def get_vnc_console(self, server_id, console_type):
522 """Get URL of VNC console."""
523 return self.action(server_id, "os-getVNCConsole",
Ghanshyamd6d30402014-04-02 15:28:37 +0900524 "console", common_schema.get_vnc_console,
525 type=console_type)
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530526
527 def create_server_group(self, name, policies):
528 """
529 Create the server group
530 name : Name of the server-group
531 policies : List of the policies - affinity/anti-affinity)
532 """
533 post_body = {
534 'name': name,
535 'policies': policies,
536 }
537
538 post_body = json.dumps({'server_group': post_body})
539 resp, body = self.post('os-server-groups', post_body)
540
541 body = json.loads(body)
Ghanshyamf81f9fa2014-05-23 13:38:56 +0900542 self.validate_response(schema.create_get_server_group, resp, body)
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530543 return resp, body['server_group']
544
545 def delete_server_group(self, server_group_id):
546 """Delete the given server-group."""
Ghanshyamf81f9fa2014-05-23 13:38:56 +0900547 resp, body = self.delete("os-server-groups/%s" % str(server_group_id))
548 self.validate_response(schema.delete_server_group, resp, body)
549 return resp, body
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530550
551 def list_server_groups(self):
552 """List the server-groups."""
553 resp, body = self.get("os-server-groups")
554 body = json.loads(body)
Ghanshyam438ed3b2014-06-18 17:25:41 +0900555 self.validate_response(schema.list_server_groups, resp, body)
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530556 return resp, body['server_groups']
557
558 def get_server_group(self, server_group_id):
559 """Get the details of given server_group."""
560 resp, body = self.get("os-server-groups/%s" % str(server_group_id))
561 body = json.loads(body)
Ghanshyamf81f9fa2014-05-23 13:38:56 +0900562 self.validate_response(schema.create_get_server_group, resp, body)
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530563 return resp, body['server_group']