blob: 49c6da925c6fd9a437046b0ff3ce13e1a3ab378e [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
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000017import copy
18
Matthew Treinish21905512015-07-13 10:33:35 -040019from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040020from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090021
ghanshyamaa93b4b2015-03-20 11:03:44 +090022from tempest.api_schema.response.compute.v2_1 import servers as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000023from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024
Daryl Wallecke5b83d42011-11-10 14:39:02 -060025
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000026class ServersClient(service_client.ServiceClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060027
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090028 def __init__(self, auth_provider, service, region,
29 enable_instance_password=True, **kwargs):
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000030 super(ServersClient, self).__init__(
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090031 auth_provider, service, region, **kwargs)
32 self.enable_instance_password = enable_instance_password
33
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000034 def create_server(self, **kwargs):
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +000035 """Create server
36
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000037 Most parameters except the following are passed to the API without
38 any changes.
39 :param disk_config: The name is changed to OS-DCF:diskConfig
40 :param scheduler_hints: The name is changed to os:scheduler_hints and
41 the parameter is set in the same level as the parameter 'server'.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060042 """
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000043 body = copy.deepcopy(kwargs)
44 if body.get('disk_config'):
45 body['OS-DCF:diskConfig'] = body.pop('disk_config')
Daryl Wallecke5b83d42011-11-10 14:39:02 -060046
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000047 hints = None
48 if body.get('scheduler_hints'):
49 hints = {'os:scheduler_hints': body.pop('scheduler_hints')}
Joseph Lanouxeef192f2014-08-01 14:32:53 +000050
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000051 post_body = {'server': body}
raiesmh08cbe21b02014-03-12 17:04:44 +053052
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000053 if hints:
raiesmh08cbe21b02014-03-12 17:04:44 +053054 post_body = dict(post_body.items() + hints.items())
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000055
raiesmh08cbe21b02014-03-12 17:04:44 +053056 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020057 resp, body = self.post('servers', post_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -050058
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050060 # NOTE(maurosr): this deals with the case of multiple server create
61 # with return reservation id set True
62 if 'reservation_id' in body:
David Kranz0fb14292015-02-11 15:55:20 -050063 return service_client.ResponseBody(resp, body)
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090064 if self.enable_instance_password:
Ghanshyam7f989102014-07-29 14:23:26 +090065 create_schema = schema.create_server_with_admin_pass
66 else:
67 create_schema = schema.create_server
68 self.validate_response(create_schema, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090069 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060070
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000071 def update_server(self, server_id, **kwargs):
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +000072 """Update server
73
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000074 Most parameters except the following are passed to the API without
75 any changes.
76 :param disk_config: The name is changed to OS-DCF:diskConfig
Daryl Wallecke5b83d42011-11-10 14:39:02 -060077 """
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000078 if kwargs.get('disk_config'):
79 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
Daryl Wallecke5b83d42011-11-10 14:39:02 -060080
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000081 post_body = json.dumps({'server': kwargs})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000082 resp, body = self.put("servers/%s" % server_id, post_body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060083 body = json.loads(body)
Ken'ichi Ohmichi29cd5122014-04-28 11:04:52 +090084 self.validate_response(schema.update_server, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090085 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060086
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +000087 def show_server(self, server_id):
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +000088 """Get server details"""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000089 resp, body = self.get("servers/%s" % server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060090 body = json.loads(body)
Ken'ichi Ohmichi21e4fc72014-05-08 16:46:23 +090091 self.validate_response(schema.get_server, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090092 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060093
94 def delete_server(self, server_id):
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +000095 """Delete server"""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000096 resp, body = self.delete("servers/%s" % server_id)
ghanshyam274327a2015-03-23 10:29:45 +090097 self.validate_response(schema.delete_server, resp, body)
David Kranz0fb14292015-02-11 15:55:20 -050098 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060099
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000100 def list_servers(self, detail=False, **params):
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +0000101 """List servers"""
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600102
103 url = 'servers'
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000104 _schema = schema.list_servers
105
106 if detail:
107 url += '/detail'
108 _schema = schema.list_servers_detail
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500109 if params:
110 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600111
chris fattarsi5098fa22012-04-17 13:27:00 -0700112 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600113 body = json.loads(body)
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000114 self.validate_response(_schema, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500115 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600116
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600117 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500118 """Lists all addresses for a server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000119 resp, body = self.get("servers/%s/ips" % server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600120 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900121 self.validate_response(schema.list_addresses, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900122 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600123
124 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500125 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700126 resp, body = self.get("servers/%s/ips/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000127 (server_id, network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600128 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900129 self.validate_response(schema.list_addresses_by_network, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500130 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600131
ghanshyam0f825252015-08-25 16:02:50 +0900132 def action(self, server_id, action_name,
ghanshyam274327a2015-03-23 10:29:45 +0900133 schema=schema.server_actions_common_schema,
ghanshyam0f825252015-08-25 16:02:50 +0900134 **kwargs):
Attila Fazekasd4299282013-02-22 13:25:23 +0100135 post_body = json.dumps({action_name: kwargs})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000136 resp, body = self.post('servers/%s/action' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200137 post_body)
ghanshyam0f825252015-08-25 16:02:50 +0900138 if body:
Ghanshyamd6d30402014-04-02 15:28:37 +0900139 body = json.loads(body)
ghanshyam0f825252015-08-25 16:02:50 +0900140 self.validate_response(schema, resp, body)
141 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600142
ivan-zhuccc89462013-10-31 16:53:12 +0800143 def create_backup(self, server_id, backup_type, rotation, name):
144 """Backup a server instance."""
ghanshyam0f825252015-08-25 16:02:50 +0900145 return self.action(server_id, "createBackup",
ivan-zhuccc89462013-10-31 16:53:12 +0800146 backup_type=backup_type,
147 rotation=rotation,
148 name=name)
149
Attila Fazekasd4299282013-02-22 13:25:23 +0100150 def change_password(self, server_id, adminPass):
151 """Changes the root password for the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900152 return self.action(server_id, 'changePassword',
Attila Fazekasd4299282013-02-22 13:25:23 +0100153 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600154
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000155 def show_password(self, server_id):
ivan-zhu6de5b042013-10-24 17:21:48 +0800156 resp, body = self.get("servers/%s/os-server-password" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000157 server_id)
ivan-zhu6de5b042013-10-24 17:21:48 +0800158 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000159 self.validate_response(schema.show_password, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500160 return service_client.ResponseBody(resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800161
162 def delete_password(self, server_id):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000163 """Removes the encrypted server password from the metadata server
164
ivan-zhu6de5b042013-10-24 17:21:48 +0800165 Note that this does not actually change the instance server
166 password.
167 """
Ghanshyam997c9092014-04-03 19:00:20 +0900168 resp, body = self.delete("servers/%s/os-server-password" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000169 server_id)
ghanshyam274327a2015-03-23 10:29:45 +0900170 self.validate_response(schema.server_actions_delete_password,
Ghanshyam997c9092014-04-03 19:00:20 +0900171 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500172 return service_client.ResponseBody(resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800173
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000174 def reboot_server(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500175 """Reboots a server."""
ghanshyam0f825252015-08-25 16:02:50 +0900176 return self.action(server_id, 'reboot', type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600177
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000178 def rebuild_server(self, server_id, image_ref, **kwargs):
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000179 """Rebuilds a server with a new image.
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000180
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000181 Most parameters except the following are passed to the API without
182 any changes.
183 :param disk_config: The name is changed to OS-DCF:diskConfig
184 """
Attila Fazekasd4299282013-02-22 13:25:23 +0100185 kwargs['imageRef'] = image_ref
186 if 'disk_config' in kwargs:
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000187 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +0900188 if self.enable_instance_password:
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900189 rebuild_schema = schema.rebuild_server_with_admin_pass
190 else:
191 rebuild_schema = schema.rebuild_server
ghanshyam0f825252015-08-25 16:02:50 +0900192 return self.action(server_id, 'rebuild',
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900193 rebuild_schema, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600194
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000195 def resize_server(self, server_id, flavor_ref, **kwargs):
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000196 """Changes the flavor of a server.
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000197
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000198 Most parameters except the following are passed to the API without
199 any changes.
200 :param disk_config: The name is changed to OS-DCF:diskConfig
201 """
Attila Fazekasd4299282013-02-22 13:25:23 +0100202 kwargs['flavorRef'] = flavor_ref
203 if 'disk_config' in kwargs:
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000204 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
ghanshyam0f825252015-08-25 16:02:50 +0900205 return self.action(server_id, 'resize', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600206
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000207 def confirm_resize_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500208 """Confirms the flavor change for a server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900209 return self.action(server_id, 'confirmResize',
ghanshyam0f825252015-08-25 16:02:50 +0900210 schema.server_actions_confirm_resize,
Ghanshyam997c9092014-04-03 19:00:20 +0900211 **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600212
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000213 def revert_resize_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500214 """Reverts a server back to its original flavor."""
ghanshyam0f825252015-08-25 16:02:50 +0900215 return self.action(server_id, 'revertResize', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600216
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600217 def list_server_metadata(self, server_id):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000218 resp, body = self.get("servers/%s/metadata" % server_id)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600219 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900220 self.validate_response(schema.list_server_metadata, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900221 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600222
Chris Yeohd0b52e72013-09-17 22:38:59 +0930223 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
224 if no_metadata_field:
225 post_body = ""
226 else:
227 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000228 resp, body = self.put('servers/%s/metadata' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200229 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600230 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900231 self.validate_response(schema.set_server_metadata, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900232 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600233
234 def update_server_metadata(self, server_id, meta):
235 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000236 resp, body = self.post('servers/%s/metadata' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200237 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600238 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900239 self.validate_response(schema.update_server_metadata,
Ghanshyame8421062014-06-02 15:58:21 +0900240 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900241 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600242
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000243 def show_server_metadata_item(self, server_id, key):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000244 resp, body = self.get("servers/%s/metadata/%s" % (server_id, key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600245 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000246 self.validate_response(schema.set_show_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900247 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900248 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600249
250 def set_server_metadata_item(self, server_id, key, meta):
251 post_body = json.dumps({'meta': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000252 resp, body = self.put('servers/%s/metadata/%s' % (server_id, key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200253 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600254 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000255 self.validate_response(schema.set_show_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900256 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900257 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600258
259 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700260 resp, body = self.delete("servers/%s/metadata/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000261 (server_id, key))
ghanshyam274327a2015-03-23 10:29:45 +0900262 self.validate_response(schema.delete_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900263 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500264 return service_client.ResponseBody(resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700265
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000266 def stop_server(self, server_id, **kwargs):
ghanshyam0f825252015-08-25 16:02:50 +0900267 return self.action(server_id, 'os-stop', **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700268
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000269 def start_server(self, server_id, **kwargs):
ghanshyam0f825252015-08-25 16:02:50 +0900270 return self.action(server_id, 'os-start', **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700271
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000272 def attach_volume(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500273 """Attaches a volume to a server instance."""
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000274 post_body = json.dumps({'volumeAttachment': kwargs})
Dan Smithc18d8c62012-07-02 08:09:26 -0700275 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200276 post_body)
Ghanshyam385c4e72014-03-27 11:42:25 +0900277 body = json.loads(body)
278 self.validate_response(schema.attach_volume, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900279 return service_client.ResponseBody(resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700280
281 def detach_volume(self, server_id, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500282 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700283 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
284 (server_id, volume_id))
Ghanshyam385c4e72014-03-27 11:42:25 +0900285 self.validate_response(schema.detach_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500286 return service_client.ResponseBody(resp, body)
sapan-kona775cf632012-06-22 00:32:36 +0530287
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000288 def show_volume_attachment(self, server_id, attach_id):
Ghanshyam5c2a5582014-04-14 17:16:57 +0900289 """Return details about the given volume attachment."""
290 resp, body = self.get('servers/%s/os-volume_attachments/%s' % (
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000291 server_id, attach_id))
Ghanshyam5c2a5582014-04-14 17:16:57 +0900292 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000293 self.validate_response(schema.show_volume_attachment, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900294 return service_client.ResponseBody(resp, body)
Ghanshyam5c2a5582014-04-14 17:16:57 +0900295
296 def list_volume_attachments(self, server_id):
297 """Returns the list of volume attachments for a given instance."""
298 resp, body = self.get('servers/%s/os-volume_attachments' % (
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000299 server_id))
Ghanshyam5c2a5582014-04-14 17:16:57 +0900300 body = json.loads(body)
301 self.validate_response(schema.list_volume_attachments, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900302 return service_client.ResponseBody(resp, body)
Ghanshyam5c2a5582014-04-14 17:16:57 +0900303
Attila Fazekasd4299282013-02-22 13:25:23 +0100304 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500305 """Adds a security group to the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900306 return self.action(server_id, 'addSecurityGroup', name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530307
Attila Fazekasd4299282013-02-22 13:25:23 +0100308 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500309 """Removes a security group from the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900310 return self.action(server_id, 'removeSecurityGroup', name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100311
Ken'ichi Ohmichi86f58932015-08-18 04:16:15 +0000312 def live_migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500313 """This should be called with administrator privileges ."""
Mate Lakat99ee9142012-09-14 12:34:46 +0100314
Ken'ichi Ohmichi86f58932015-08-18 04:16:15 +0000315 req_body = json.dumps({'os-migrateLive': kwargs})
Mate Lakat99ee9142012-09-14 12:34:46 +0100316
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000317 resp, body = self.post("servers/%s/action" % server_id, req_body)
ghanshyam274327a2015-03-23 10:29:45 +0900318 self.validate_response(schema.server_actions_common_schema,
Ghanshyam997c9092014-04-03 19:00:20 +0900319 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500320 return service_client.ResponseBody(resp, body)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600321
Attila Fazekasd4299282013-02-22 13:25:23 +0100322 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500323 """Migrates a server to a new host."""
ghanshyam0f825252015-08-25 16:02:50 +0900324 return self.action(server_id, 'migrate', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600325
Attila Fazekasd4299282013-02-22 13:25:23 +0100326 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500327 """Locks the given server."""
ghanshyam0f825252015-08-25 16:02:50 +0900328 return self.action(server_id, 'lock', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600329
Attila Fazekasd4299282013-02-22 13:25:23 +0100330 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500331 """UNlocks the given server."""
ghanshyam0f825252015-08-25 16:02:50 +0900332 return self.action(server_id, 'unlock', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600333
Attila Fazekasd4299282013-02-22 13:25:23 +0100334 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900335 """Suspends the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900336 return self.action(server_id, 'suspend', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600337
Attila Fazekasd4299282013-02-22 13:25:23 +0100338 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900339 """Un-suspends the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900340 return self.action(server_id, 'resume', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600341
Attila Fazekasd4299282013-02-22 13:25:23 +0100342 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900343 """Pauses the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900344 return self.action(server_id, 'pause', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600345
Attila Fazekasd4299282013-02-22 13:25:23 +0100346 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900347 """Un-pauses the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900348 return self.action(server_id, 'unpause', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600349
Attila Fazekasd4299282013-02-22 13:25:23 +0100350 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500351 """Resets the state of a server to active/error."""
ghanshyam0f825252015-08-25 16:02:50 +0900352 return self.action(server_id, 'os-resetState', state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100353
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900354 def shelve_server(self, server_id, **kwargs):
355 """Shelves the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900356 return self.action(server_id, 'shelve', **kwargs)
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900357
358 def unshelve_server(self, server_id, **kwargs):
359 """Un-shelves the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900360 return self.action(server_id, 'unshelve', **kwargs)
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900361
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900362 def shelve_offload_server(self, server_id, **kwargs):
363 """Shelve-offload the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900364 return self.action(server_id, 'shelveOffload', **kwargs)
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900365
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100366 def get_console_output(self, server_id, length):
Davanum Srinivas8a841c72014-06-19 18:33:14 -0400367 kwargs = {'length': length} if length else {}
ghanshyam0f825252015-08-25 16:02:50 +0900368 return self.action(server_id, 'os-getConsoleOutput',
ghanshyam274327a2015-03-23 10:29:45 +0900369 schema.get_console_output,
David Kranzae99b9a2015-02-16 13:37:01 -0500370 **kwargs)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200371
372 def list_virtual_interfaces(self, server_id):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000373 """List the virtual interfaces used in an instance."""
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200374 resp, body = self.get('/'.join(['servers', server_id,
375 'os-virtual-interfaces']))
Ghanshyam08ce58d2014-04-04 14:51:14 +0900376 body = json.loads(body)
377 self.validate_response(schema.list_virtual_interfaces, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500378 return service_client.ResponseBody(resp, body)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000379
Yuiko Takada7835d502014-01-15 10:15:03 +0000380 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000381 """Rescue the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900382 return self.action(server_id, 'rescue',
David Kranzae99b9a2015-02-16 13:37:01 -0500383 schema.rescue_server,
David Kranzae99b9a2015-02-16 13:37:01 -0500384 **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000385
386 def unrescue_server(self, server_id):
387 """Unrescue the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900388 return self.action(server_id, 'unrescue')
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900389
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000390 def show_server_diagnostics(self, server_id):
Zhu Zhuda070852013-09-25 08:07:57 -0500391 """Get the usage data for a server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000392 resp, body = self.get("servers/%s/diagnostics" % server_id)
David Kranzae99b9a2015-02-16 13:37:01 -0500393 return service_client.ResponseBody(resp, json.loads(body))
Zhu Zhuda070852013-09-25 08:07:57 -0500394
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900395 def list_instance_actions(self, server_id):
396 """List the provided server action."""
397 resp, body = self.get("servers/%s/os-instance-actions" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000398 server_id)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900399 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900400 self.validate_response(schema.list_instance_actions, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900401 return service_client.ResponseBody(resp, body)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900402
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000403 def show_instance_action(self, server_id, request_id):
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900404 """Returns the action details of the provided server."""
405 resp, body = self.get("servers/%s/os-instance-actions/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000406 (server_id, request_id))
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900407 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000408 self.validate_response(schema.show_instance_action, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900409 return service_client.ResponseBody(resp, body)
Lingxian Kongaecc1092013-10-03 16:18:46 +0800410
411 def force_delete_server(self, server_id, **kwargs):
412 """Force delete a server."""
ghanshyam0f825252015-08-25 16:02:50 +0900413 return self.action(server_id, 'forceDelete', **kwargs)
Lingxian Kongaecc1092013-10-03 16:18:46 +0800414
415 def restore_soft_deleted_server(self, server_id, **kwargs):
416 """Restore a soft-deleted server."""
ghanshyam0f825252015-08-25 16:02:50 +0900417 return self.action(server_id, 'restore', **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900418
419 def reset_network(self, server_id, **kwargs):
420 """Resets the Network of a server"""
ghanshyam0f825252015-08-25 16:02:50 +0900421 return self.action(server_id, 'resetNetwork', **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900422
423 def inject_network_info(self, server_id, **kwargs):
424 """Inject the Network Info into server"""
ghanshyam0f825252015-08-25 16:02:50 +0900425 return self.action(server_id, 'injectNetworkInfo', **kwargs)
Ghanshyam86d1a572014-03-05 10:19:25 +0900426
427 def get_vnc_console(self, server_id, console_type):
428 """Get URL of VNC console."""
429 return self.action(server_id, "os-getVNCConsole",
ghanshyam0f825252015-08-25 16:02:50 +0900430 schema.get_vnc_console,
Ghanshyamd6d30402014-04-02 15:28:37 +0900431 type=console_type)
ghanshyam4c7d2a02015-09-14 16:05:13 +0900432
433 def add_fixed_ip(self, server_id, **kwargs):
434 """Add a fixed IP to input server instance."""
435 return self.action(server_id, 'addFixedIp', **kwargs)
436
437 def remove_fixed_ip(self, server_id, **kwargs):
438 """Remove input fixed IP from input server instance."""
439 return self.action(server_id, 'removeFixedIp', **kwargs)