blob: aa9e6c695eddf8a4a649ae6346b39fa39d77d2d2 [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 Ohmichidb821052015-12-07 07:21:15 +000035 """Create server.
36
37 Available params: see http://developer.openstack.org/
38 api-ref-compute-v2.1.html#createServer
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +000039
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000040 Most parameters except the following are passed to the API without
41 any changes.
42 :param disk_config: The name is changed to OS-DCF:diskConfig
43 :param scheduler_hints: The name is changed to os:scheduler_hints and
44 the parameter is set in the same level as the parameter 'server'.
Daryl Wallecke5b83d42011-11-10 14:39:02 -060045 """
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000046 body = copy.deepcopy(kwargs)
47 if body.get('disk_config'):
48 body['OS-DCF:diskConfig'] = body.pop('disk_config')
Daryl Wallecke5b83d42011-11-10 14:39:02 -060049
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000050 hints = None
51 if body.get('scheduler_hints'):
52 hints = {'os:scheduler_hints': body.pop('scheduler_hints')}
Joseph Lanouxeef192f2014-08-01 14:32:53 +000053
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000054 post_body = {'server': body}
raiesmh08cbe21b02014-03-12 17:04:44 +053055
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000056 if hints:
raiesmh08cbe21b02014-03-12 17:04:44 +053057 post_body = dict(post_body.items() + hints.items())
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000058
raiesmh08cbe21b02014-03-12 17:04:44 +053059 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020060 resp, body = self.post('servers', post_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -050061
Daryl Wallecke5b83d42011-11-10 14:39:02 -060062 body = json.loads(body)
Mauro S. M. Rodriguesc5da4f42013-03-04 23:57:10 -050063 # NOTE(maurosr): this deals with the case of multiple server create
64 # with return reservation id set True
65 if 'reservation_id' in body:
David Kranz0fb14292015-02-11 15:55:20 -050066 return service_client.ResponseBody(resp, body)
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +090067 if self.enable_instance_password:
Ghanshyam7f989102014-07-29 14:23:26 +090068 create_schema = schema.create_server_with_admin_pass
69 else:
70 create_schema = schema.create_server
71 self.validate_response(create_schema, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090072 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060073
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000074 def update_server(self, server_id, **kwargs):
Ken'ichi Ohmichidb821052015-12-07 07:21:15 +000075 """Update server.
76
77 Available params: see http://developer.openstack.org/
78 api-ref-compute-v2.1.html#updateServer
Ken'ichi Ohmichidc4713c2015-10-15 08:35:36 +000079
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000080 Most parameters except the following are passed to the API without
81 any changes.
82 :param disk_config: The name is changed to OS-DCF:diskConfig
Daryl Wallecke5b83d42011-11-10 14:39:02 -060083 """
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000084 if kwargs.get('disk_config'):
85 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
Daryl Wallecke5b83d42011-11-10 14:39:02 -060086
Ken'ichi Ohmichib09fb272015-08-27 02:00:08 +000087 post_body = json.dumps({'server': kwargs})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000088 resp, body = self.put("servers/%s" % server_id, post_body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060089 body = json.loads(body)
Ken'ichi Ohmichi29cd5122014-04-28 11:04:52 +090090 self.validate_response(schema.update_server, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090091 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060092
Ken'ichi Ohmichi76800242015-07-03 05:12:31 +000093 def show_server(self, server_id):
Ken'ichi Ohmichidb821052015-12-07 07:21:15 +000094 """Get server details."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000095 resp, body = self.get("servers/%s" % server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060096 body = json.loads(body)
Ken'ichi Ohmichi21e4fc72014-05-08 16:46:23 +090097 self.validate_response(schema.get_server, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +090098 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060099
100 def delete_server(self, server_id):
Ken'ichi Ohmichidb821052015-12-07 07:21:15 +0000101 """Delete server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000102 resp, body = self.delete("servers/%s" % server_id)
ghanshyam274327a2015-03-23 10:29:45 +0900103 self.validate_response(schema.delete_server, resp, body)
David Kranz0fb14292015-02-11 15:55:20 -0500104 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600105
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000106 def list_servers(self, detail=False, **params):
Ken'ichi Ohmichidb821052015-12-07 07:21:15 +0000107 """List servers.
108
109 Available params: see http://developer.openstack.org/
110 api-ref-compute-v2.1.html#listServers
111 and http://developer.openstack.org/
112 api-ref-compute-v2.1.html#listDetailServers
113 """
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600114
115 url = 'servers'
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000116 _schema = schema.list_servers
117
118 if detail:
119 url += '/detail'
120 _schema = schema.list_servers_detail
Matthew Treinish26dd0fa2012-12-04 17:14:37 -0500121 if params:
122 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600123
chris fattarsi5098fa22012-04-17 13:27:00 -0700124 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600125 body = json.loads(body)
Ken'ichi Ohmichicbc26a82015-07-03 08:18:04 +0000126 self.validate_response(_schema, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500127 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600128
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600129 def list_addresses(self, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500130 """Lists all addresses for a server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000131 resp, body = self.get("servers/%s/ips" % server_id)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600132 body = json.loads(body)
Ghanshyamd847c582014-05-07 16:21:36 +0900133 self.validate_response(schema.list_addresses, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900134 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600135
136 def list_addresses_by_network(self, server_id, network_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500137 """Lists all addresses of a specific network type for a server."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700138 resp, body = self.get("servers/%s/ips/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000139 (server_id, network_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600140 body = json.loads(body)
Ghanshyam9541ad12014-05-07 16:38:43 +0900141 self.validate_response(schema.list_addresses_by_network, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500142 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600143
ghanshyam0f825252015-08-25 16:02:50 +0900144 def action(self, server_id, action_name,
ghanshyam274327a2015-03-23 10:29:45 +0900145 schema=schema.server_actions_common_schema,
ghanshyam0f825252015-08-25 16:02:50 +0900146 **kwargs):
Attila Fazekasd4299282013-02-22 13:25:23 +0100147 post_body = json.dumps({action_name: kwargs})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000148 resp, body = self.post('servers/%s/action' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200149 post_body)
ghanshyam0f825252015-08-25 16:02:50 +0900150 if body:
Ghanshyamd6d30402014-04-02 15:28:37 +0900151 body = json.loads(body)
ghanshyam0f825252015-08-25 16:02:50 +0900152 self.validate_response(schema, resp, body)
153 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600154
ivan-zhuccc89462013-10-31 16:53:12 +0800155 def create_backup(self, server_id, backup_type, rotation, name):
156 """Backup a server instance."""
ghanshyam0f825252015-08-25 16:02:50 +0900157 return self.action(server_id, "createBackup",
ivan-zhuccc89462013-10-31 16:53:12 +0800158 backup_type=backup_type,
159 rotation=rotation,
160 name=name)
161
Attila Fazekasd4299282013-02-22 13:25:23 +0100162 def change_password(self, server_id, adminPass):
163 """Changes the root password for the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900164 return self.action(server_id, 'changePassword',
Attila Fazekasd4299282013-02-22 13:25:23 +0100165 adminPass=adminPass)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600166
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000167 def show_password(self, server_id):
ivan-zhu6de5b042013-10-24 17:21:48 +0800168 resp, body = self.get("servers/%s/os-server-password" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000169 server_id)
ivan-zhu6de5b042013-10-24 17:21:48 +0800170 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000171 self.validate_response(schema.show_password, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500172 return service_client.ResponseBody(resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800173
174 def delete_password(self, server_id):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000175 """Removes the encrypted server password from the metadata server
176
ivan-zhu6de5b042013-10-24 17:21:48 +0800177 Note that this does not actually change the instance server
178 password.
179 """
Ghanshyam997c9092014-04-03 19:00:20 +0900180 resp, body = self.delete("servers/%s/os-server-password" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000181 server_id)
ghanshyam274327a2015-03-23 10:29:45 +0900182 self.validate_response(schema.server_actions_delete_password,
Ghanshyam997c9092014-04-03 19:00:20 +0900183 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500184 return service_client.ResponseBody(resp, body)
ivan-zhu6de5b042013-10-24 17:21:48 +0800185
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000186 def reboot_server(self, server_id, reboot_type):
Sean Daguef237ccb2013-01-04 15:19:14 -0500187 """Reboots a server."""
ghanshyam0f825252015-08-25 16:02:50 +0900188 return self.action(server_id, 'reboot', type=reboot_type)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600189
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000190 def rebuild_server(self, server_id, image_ref, **kwargs):
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000191 """Rebuilds a server with a new image.
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000192
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000193 Most parameters except the following are passed to the API without
194 any changes.
195 :param disk_config: The name is changed to OS-DCF:diskConfig
196 """
Attila Fazekasd4299282013-02-22 13:25:23 +0100197 kwargs['imageRef'] = image_ref
198 if 'disk_config' in kwargs:
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000199 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
Masayuki Igawa8f9c0c82015-03-03 09:38:08 +0900200 if self.enable_instance_password:
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900201 rebuild_schema = schema.rebuild_server_with_admin_pass
202 else:
203 rebuild_schema = schema.rebuild_server
ghanshyam0f825252015-08-25 16:02:50 +0900204 return self.action(server_id, 'rebuild',
Ghanshyam9c2e50d2014-07-22 21:32:05 +0900205 rebuild_schema, **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600206
Ken'ichi Ohmichi5271b0f2015-08-10 07:53:27 +0000207 def resize_server(self, server_id, flavor_ref, **kwargs):
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000208 """Changes the flavor of a server.
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000209
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000210 Most parameters except the following are passed to the API without
211 any changes.
212 :param disk_config: The name is changed to OS-DCF:diskConfig
213 """
Attila Fazekasd4299282013-02-22 13:25:23 +0100214 kwargs['flavorRef'] = flavor_ref
215 if 'disk_config' in kwargs:
Ken'ichi Ohmichi5a51f072015-08-13 03:15:39 +0000216 kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')
ghanshyam0f825252015-08-25 16:02:50 +0900217 return self.action(server_id, 'resize', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600218
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000219 def confirm_resize_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500220 """Confirms the flavor change for a server."""
Ghanshyam997c9092014-04-03 19:00:20 +0900221 return self.action(server_id, 'confirmResize',
ghanshyam0f825252015-08-25 16:02:50 +0900222 schema.server_actions_confirm_resize,
Ghanshyam997c9092014-04-03 19:00:20 +0900223 **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600224
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000225 def revert_resize_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500226 """Reverts a server back to its original flavor."""
ghanshyam0f825252015-08-25 16:02:50 +0900227 return self.action(server_id, 'revertResize', **kwargs)
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600228
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600229 def list_server_metadata(self, server_id):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000230 resp, body = self.get("servers/%s/metadata" % server_id)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600231 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900232 self.validate_response(schema.list_server_metadata, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900233 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600234
Chris Yeohd0b52e72013-09-17 22:38:59 +0930235 def set_server_metadata(self, server_id, meta, no_metadata_field=False):
236 if no_metadata_field:
237 post_body = ""
238 else:
239 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000240 resp, body = self.put('servers/%s/metadata' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200241 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600242 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900243 self.validate_response(schema.set_server_metadata, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900244 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600245
246 def update_server_metadata(self, server_id, meta):
247 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000248 resp, body = self.post('servers/%s/metadata' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200249 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600250 body = json.loads(body)
ghanshyam274327a2015-03-23 10:29:45 +0900251 self.validate_response(schema.update_server_metadata,
Ghanshyame8421062014-06-02 15:58:21 +0900252 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900253 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600254
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000255 def show_server_metadata_item(self, server_id, key):
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000256 resp, body = self.get("servers/%s/metadata/%s" % (server_id, key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600257 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000258 self.validate_response(schema.set_show_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900259 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900260 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600261
262 def set_server_metadata_item(self, server_id, key, meta):
263 post_body = json.dumps({'meta': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000264 resp, body = self.put('servers/%s/metadata/%s' % (server_id, key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200265 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600266 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000267 self.validate_response(schema.set_show_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900268 resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900269 return service_client.ResponseBody(resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600270
271 def delete_server_metadata_item(self, server_id, key):
chris fattarsi5098fa22012-04-17 13:27:00 -0700272 resp, body = self.delete("servers/%s/metadata/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000273 (server_id, key))
ghanshyam274327a2015-03-23 10:29:45 +0900274 self.validate_response(schema.delete_server_metadata_item,
Ghanshyameaaa6a42014-04-25 18:38:21 +0900275 resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500276 return service_client.ResponseBody(resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700277
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000278 def stop_server(self, server_id, **kwargs):
ghanshyam0f825252015-08-25 16:02:50 +0900279 return self.action(server_id, 'os-stop', **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700280
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000281 def start_server(self, server_id, **kwargs):
ghanshyam0f825252015-08-25 16:02:50 +0900282 return self.action(server_id, 'os-start', **kwargs)
Dan Smithc18d8c62012-07-02 08:09:26 -0700283
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000284 def attach_volume(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500285 """Attaches a volume to a server instance."""
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000286 post_body = json.dumps({'volumeAttachment': kwargs})
Dan Smithc18d8c62012-07-02 08:09:26 -0700287 resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200288 post_body)
Ghanshyam385c4e72014-03-27 11:42:25 +0900289 body = json.loads(body)
290 self.validate_response(schema.attach_volume, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900291 return service_client.ResponseBody(resp, body)
Dan Smithc18d8c62012-07-02 08:09:26 -0700292
Ken'ichi Ohmichib8461cb2015-11-20 08:10:51 +0000293 def detach_volume(self, server_id, volume_id): # noqa
Sean Daguef237ccb2013-01-04 15:19:14 -0500294 """Detaches a volume from a server instance."""
Dan Smithc18d8c62012-07-02 08:09:26 -0700295 resp, body = self.delete('servers/%s/os-volume_attachments/%s' %
296 (server_id, volume_id))
Ghanshyam385c4e72014-03-27 11:42:25 +0900297 self.validate_response(schema.detach_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -0500298 return service_client.ResponseBody(resp, body)
sapan-kona775cf632012-06-22 00:32:36 +0530299
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000300 def show_volume_attachment(self, server_id, attach_id):
Ghanshyam5c2a5582014-04-14 17:16:57 +0900301 """Return details about the given volume attachment."""
302 resp, body = self.get('servers/%s/os-volume_attachments/%s' % (
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000303 server_id, attach_id))
Ghanshyam5c2a5582014-04-14 17:16:57 +0900304 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000305 self.validate_response(schema.show_volume_attachment, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900306 return service_client.ResponseBody(resp, body)
Ghanshyam5c2a5582014-04-14 17:16:57 +0900307
308 def list_volume_attachments(self, server_id):
309 """Returns the list of volume attachments for a given instance."""
310 resp, body = self.get('servers/%s/os-volume_attachments' % (
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000311 server_id))
Ghanshyam5c2a5582014-04-14 17:16:57 +0900312 body = json.loads(body)
313 self.validate_response(schema.list_volume_attachments, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900314 return service_client.ResponseBody(resp, body)
Ghanshyam5c2a5582014-04-14 17:16:57 +0900315
Attila Fazekasd4299282013-02-22 13:25:23 +0100316 def add_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500317 """Adds a security group to the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900318 return self.action(server_id, 'addSecurityGroup', name=name)
sapan-kona775cf632012-06-22 00:32:36 +0530319
Attila Fazekasd4299282013-02-22 13:25:23 +0100320 def remove_security_group(self, server_id, name):
Sean Daguef237ccb2013-01-04 15:19:14 -0500321 """Removes a security group from the server."""
ghanshyam0f825252015-08-25 16:02:50 +0900322 return self.action(server_id, 'removeSecurityGroup', name=name)
Mate Lakat99ee9142012-09-14 12:34:46 +0100323
Ken'ichi Ohmichi86f58932015-08-18 04:16:15 +0000324 def live_migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500325 """This should be called with administrator privileges ."""
Ken'ichi Ohmichidf483232015-12-09 05:51:17 +0000326 return self.action(server_id, 'os-migrateLive', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600327
Attila Fazekasd4299282013-02-22 13:25:23 +0100328 def migrate_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500329 """Migrates a server to a new host."""
ghanshyam0f825252015-08-25 16:02:50 +0900330 return self.action(server_id, 'migrate', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600331
Attila Fazekasd4299282013-02-22 13:25:23 +0100332 def lock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500333 """Locks the given server."""
ghanshyam0f825252015-08-25 16:02:50 +0900334 return self.action(server_id, 'lock', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600335
Attila Fazekasd4299282013-02-22 13:25:23 +0100336 def unlock_server(self, server_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500337 """UNlocks the given server."""
ghanshyam0f825252015-08-25 16:02:50 +0900338 return self.action(server_id, 'unlock', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600339
Attila Fazekasd4299282013-02-22 13:25:23 +0100340 def suspend_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900341 """Suspends the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900342 return self.action(server_id, 'suspend', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600343
Attila Fazekasd4299282013-02-22 13:25:23 +0100344 def resume_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900345 """Un-suspends the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900346 return self.action(server_id, 'resume', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600347
Attila Fazekasd4299282013-02-22 13:25:23 +0100348 def pause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900349 """Pauses the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900350 return self.action(server_id, 'pause', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600351
Attila Fazekasd4299282013-02-22 13:25:23 +0100352 def unpause_server(self, server_id, **kwargs):
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900353 """Un-pauses the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900354 return self.action(server_id, 'unpause', **kwargs)
Daryl Walleck3d3f6102012-11-09 15:16:42 -0600355
Attila Fazekasd4299282013-02-22 13:25:23 +0100356 def reset_state(self, server_id, state='error'):
Sean Daguef237ccb2013-01-04 15:19:14 -0500357 """Resets the state of a server to active/error."""
ghanshyam0f825252015-08-25 16:02:50 +0900358 return self.action(server_id, 'os-resetState', state=state)
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100359
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900360 def shelve_server(self, server_id, **kwargs):
361 """Shelves the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900362 return self.action(server_id, 'shelve', **kwargs)
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900363
364 def unshelve_server(self, server_id, **kwargs):
365 """Un-shelves the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900366 return self.action(server_id, 'unshelve', **kwargs)
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +0900367
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900368 def shelve_offload_server(self, server_id, **kwargs):
369 """Shelve-offload the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900370 return self.action(server_id, 'shelveOffload', **kwargs)
Ken'ichi Ohmichi17b7f112014-02-20 06:33:49 +0900371
Attila Fazekase4cb04c2013-01-29 09:51:58 +0100372 def get_console_output(self, server_id, length):
Davanum Srinivas8a841c72014-06-19 18:33:14 -0400373 kwargs = {'length': length} if length else {}
ghanshyam0f825252015-08-25 16:02:50 +0900374 return self.action(server_id, 'os-getConsoleOutput',
ghanshyam274327a2015-03-23 10:29:45 +0900375 schema.get_console_output,
David Kranzae99b9a2015-02-16 13:37:01 -0500376 **kwargs)
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200377
378 def list_virtual_interfaces(self, server_id):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000379 """List the virtual interfaces used in an instance."""
Rami Vaknin76bc8bd2013-02-17 16:18:27 +0200380 resp, body = self.get('/'.join(['servers', server_id,
381 'os-virtual-interfaces']))
Ghanshyam08ce58d2014-04-04 14:51:14 +0900382 body = json.loads(body)
383 self.validate_response(schema.list_virtual_interfaces, resp, body)
David Kranzae99b9a2015-02-16 13:37:01 -0500384 return service_client.ResponseBody(resp, body)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000385
Yuiko Takada7835d502014-01-15 10:15:03 +0000386 def rescue_server(self, server_id, **kwargs):
nithya-ganesane6a36c82013-02-15 14:38:27 +0000387 """Rescue the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900388 return self.action(server_id, 'rescue',
David Kranzae99b9a2015-02-16 13:37:01 -0500389 schema.rescue_server,
David Kranzae99b9a2015-02-16 13:37:01 -0500390 **kwargs)
nithya-ganesane6a36c82013-02-15 14:38:27 +0000391
392 def unrescue_server(self, server_id):
393 """Unrescue the provided server."""
ghanshyam0f825252015-08-25 16:02:50 +0900394 return self.action(server_id, 'unrescue')
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900395
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000396 def show_server_diagnostics(self, server_id):
Zhu Zhuda070852013-09-25 08:07:57 -0500397 """Get the usage data for a server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000398 resp, body = self.get("servers/%s/diagnostics" % server_id)
David Kranzae99b9a2015-02-16 13:37:01 -0500399 return service_client.ResponseBody(resp, json.loads(body))
Zhu Zhuda070852013-09-25 08:07:57 -0500400
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900401 def list_instance_actions(self, server_id):
402 """List the provided server action."""
403 resp, body = self.get("servers/%s/os-instance-actions" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000404 server_id)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900405 body = json.loads(body)
Ghanshyam29966092014-04-07 17:27:41 +0900406 self.validate_response(schema.list_instance_actions, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900407 return service_client.ResponseBody(resp, body)
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900408
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000409 def show_instance_action(self, server_id, request_id):
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900410 """Returns the action details of the provided server."""
411 resp, body = self.get("servers/%s/os-instance-actions/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000412 (server_id, request_id))
Leo Toyodafaca6ff2013-04-22 16:52:53 +0900413 body = json.loads(body)
Ken'ichi Ohmichi277d1882015-11-20 00:44:06 +0000414 self.validate_response(schema.show_instance_action, resp, body)
ghanshyam0f825252015-08-25 16:02:50 +0900415 return service_client.ResponseBody(resp, body)
Lingxian Kongaecc1092013-10-03 16:18:46 +0800416
417 def force_delete_server(self, server_id, **kwargs):
418 """Force delete a server."""
ghanshyam0f825252015-08-25 16:02:50 +0900419 return self.action(server_id, 'forceDelete', **kwargs)
Lingxian Kongaecc1092013-10-03 16:18:46 +0800420
421 def restore_soft_deleted_server(self, server_id, **kwargs):
422 """Restore a soft-deleted server."""
ghanshyam0f825252015-08-25 16:02:50 +0900423 return self.action(server_id, 'restore', **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900424
425 def reset_network(self, server_id, **kwargs):
426 """Resets the Network of a server"""
ghanshyam0f825252015-08-25 16:02:50 +0900427 return self.action(server_id, 'resetNetwork', **kwargs)
Ghanshyam Mann79f4a092014-02-27 21:01:31 +0900428
429 def inject_network_info(self, server_id, **kwargs):
430 """Inject the Network Info into server"""
ghanshyam0f825252015-08-25 16:02:50 +0900431 return self.action(server_id, 'injectNetworkInfo', **kwargs)
Ghanshyam86d1a572014-03-05 10:19:25 +0900432
433 def get_vnc_console(self, server_id, console_type):
434 """Get URL of VNC console."""
435 return self.action(server_id, "os-getVNCConsole",
ghanshyam0f825252015-08-25 16:02:50 +0900436 schema.get_vnc_console,
Ghanshyamd6d30402014-04-02 15:28:37 +0900437 type=console_type)
ghanshyam4c7d2a02015-09-14 16:05:13 +0900438
439 def add_fixed_ip(self, server_id, **kwargs):
440 """Add a fixed IP to input server instance."""
441 return self.action(server_id, 'addFixedIp', **kwargs)
442
443 def remove_fixed_ip(self, server_id, **kwargs):
444 """Remove input fixed IP from input server instance."""
445 return self.action(server_id, 'removeFixedIp', **kwargs)