ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
| 2 | # Copyright 2013 Hewlett-Packard Development Company, L.P. |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 3 | # Copyright 2013 IBM Corp |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import json |
| 19 | import time |
| 20 | import urllib |
| 21 | |
Ghanshyam | 7fa397c | 2014-04-01 19:32:38 +0900 | [diff] [blame] | 22 | from tempest.api_schema.compute import servers as common_schema |
Ken'ichi Ohmichi | 0260458 | 2014-03-14 16:23:41 +0900 | [diff] [blame] | 23 | from tempest.api_schema.compute.v3 import servers as schema |
Haiwei Xu | 16ee2c8 | 2014-03-05 04:59:18 +0900 | [diff] [blame] | 24 | from tempest.common import rest_client |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 25 | from tempest.common import waiters |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | from tempest import config |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 27 | from tempest import exceptions |
| 28 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 29 | CONF = config.CONF |
| 30 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 31 | |
Haiwei Xu | 16ee2c8 | 2014-03-05 04:59:18 +0900 | [diff] [blame] | 32 | class ServersV3ClientJSON(rest_client.RestClient): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 33 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 34 | def __init__(self, auth_provider): |
| 35 | super(ServersV3ClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 36 | self.service = CONF.compute.catalog_v3_type |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 37 | |
| 38 | def create_server(self, name, image_ref, flavor_ref, **kwargs): |
| 39 | """ |
| 40 | Creates an instance of a server. |
| 41 | name (Required): The name of the server. |
| 42 | image_ref (Required): Reference to the image used to build the server. |
| 43 | flavor_ref (Required): The flavor used to build the server. |
| 44 | Following optional keyword arguments are accepted: |
Ken'ichi Ohmichi | e985ca8 | 2013-11-12 15:10:35 +0900 | [diff] [blame] | 45 | admin_password: Sets the initial root password. |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 46 | key_name: Key name of keypair that was created earlier. |
| 47 | meta: A dictionary of values to be used as metadata. |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 48 | security_groups: A list of security group dicts. |
| 49 | networks: A list of network dicts with UUID and fixed_ip. |
| 50 | user_data: User data for instance. |
| 51 | availability_zone: Availability zone in which to launch instance. |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 52 | access_ip_v4: The IPv4 access address for the server. |
| 53 | access_ip_v6: The IPv6 access address for the server. |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 54 | min_count: Count of minimum number of instances to launch. |
| 55 | max_count: Count of maximum number of instances to launch. |
| 56 | disk_config: Determines if user or admin controls disk configuration. |
| 57 | return_reservation_id: Enable/Disable the return of reservation id |
Andrey Pavlov | 2a2fc41 | 2014-06-09 11:02:53 +0400 | [diff] [blame] | 58 | block_device_mapping: Block device mapping for the server. |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 59 | """ |
| 60 | post_body = { |
| 61 | 'name': name, |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 62 | 'image_ref': image_ref, |
| 63 | 'flavor_ref': flavor_ref |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 64 | } |
| 65 | |
ivan-zhu | 8209425 | 2013-11-21 10:16:11 +0800 | [diff] [blame] | 66 | for option in ['admin_password', 'key_name', 'networks', |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 67 | ('os-security-groups:security_groups', |
| 68 | 'security_groups'), |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 69 | ('os-user-data:user_data', 'user_data'), |
| 70 | ('os-availability-zone:availability_zone', |
| 71 | 'availability_zone'), |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 72 | ('os-access-ips:access_ip_v4', 'access_ip_v4'), |
| 73 | ('os-access-ips:access_ip_v6', 'access_ip_v6'), |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 74 | ('os-multiple-create:min_count', 'min_count'), |
| 75 | ('os-multiple-create:max_count', 'max_count'), |
| 76 | ('metadata', 'meta'), |
| 77 | ('os-disk-config:disk_config', 'disk_config'), |
| 78 | ('os-multiple-create:return_reservation_id', |
Andrey Pavlov | 2a2fc41 | 2014-06-09 11:02:53 +0400 | [diff] [blame] | 79 | 'return_reservation_id'), |
| 80 | ('os-block-device-mapping:block_device_mapping', |
| 81 | 'block_device_mapping')]: |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 82 | if isinstance(option, tuple): |
| 83 | post_param = option[0] |
| 84 | key = option[1] |
| 85 | else: |
| 86 | post_param = option |
| 87 | key = option |
| 88 | value = kwargs.get(key) |
| 89 | if value is not None: |
| 90 | post_body[post_param] = value |
| 91 | post_body = json.dumps({'server': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 92 | resp, body = self.post('servers', post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 93 | |
| 94 | body = json.loads(body) |
| 95 | # NOTE(maurosr): this deals with the case of multiple server create |
| 96 | # with return reservation id set True |
ivan-zhu | f0bf301 | 2013-11-25 16:03:42 +0800 | [diff] [blame] | 97 | if 'servers_reservation' in body: |
| 98 | return resp, body['servers_reservation'] |
Ken'ichi Ohmichi | 0260458 | 2014-03-14 16:23:41 +0900 | [diff] [blame] | 99 | self.validate_response(schema.create_server, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 100 | return resp, body['server'] |
| 101 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 102 | def update_server(self, server_id, name=None, meta=None, access_ip_v4=None, |
| 103 | access_ip_v6=None, disk_config=None): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 104 | """ |
| 105 | Updates the properties of an existing server. |
| 106 | server_id: The id of an existing server. |
| 107 | name: The name of the server. |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 108 | access_ip_v4: The IPv4 access address for the server. |
| 109 | access_ip_v6: The IPv6 access address for the server. |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 110 | """ |
| 111 | |
| 112 | post_body = {} |
| 113 | |
| 114 | if meta is not None: |
| 115 | post_body['metadata'] = meta |
| 116 | |
| 117 | if name is not None: |
| 118 | post_body['name'] = name |
| 119 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 120 | if access_ip_v4 is not None: |
ivan-zhu | f0bf301 | 2013-11-25 16:03:42 +0800 | [diff] [blame] | 121 | post_body['os-access-ips:access_ip_v4'] = access_ip_v4 |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 122 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 123 | if access_ip_v6 is not None: |
ivan-zhu | f0bf301 | 2013-11-25 16:03:42 +0800 | [diff] [blame] | 124 | post_body['os-access-ips:access_ip_v6'] = access_ip_v6 |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 125 | |
| 126 | if disk_config is not None: |
ivan-zhu | 5096952 | 2013-08-26 11:10:39 +0800 | [diff] [blame] | 127 | post_body['os-disk-config:disk_config'] = disk_config |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 128 | |
| 129 | post_body = json.dumps({'server': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 130 | resp, body = self.put("servers/%s" % str(server_id), post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 131 | body = json.loads(body) |
Ken'ichi Ohmichi | 29cd512 | 2014-04-28 11:04:52 +0900 | [diff] [blame] | 132 | self.validate_response(schema.update_server, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 133 | return resp, body['server'] |
| 134 | |
| 135 | def get_server(self, server_id): |
| 136 | """Returns the details of an existing server.""" |
| 137 | resp, body = self.get("servers/%s" % str(server_id)) |
| 138 | body = json.loads(body) |
Ken'ichi Ohmichi | 21e4fc7 | 2014-05-08 16:46:23 +0900 | [diff] [blame] | 139 | self.validate_response(schema.get_server, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 140 | return resp, body['server'] |
| 141 | |
| 142 | def delete_server(self, server_id): |
| 143 | """Deletes the given server.""" |
Ghanshyam | 4ac0274 | 2014-04-17 19:15:47 +0900 | [diff] [blame] | 144 | resp, body = self.delete("servers/%s" % str(server_id)) |
| 145 | self.validate_response(common_schema.delete_server, resp, body) |
| 146 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 147 | |
| 148 | def list_servers(self, params=None): |
| 149 | """Lists all servers for a user.""" |
| 150 | |
| 151 | url = 'servers' |
| 152 | if params: |
| 153 | url += '?%s' % urllib.urlencode(params) |
| 154 | |
| 155 | resp, body = self.get(url) |
| 156 | body = json.loads(body) |
Ghanshyam | 623c38f | 2014-04-21 17:16:21 +0900 | [diff] [blame] | 157 | self.validate_response(common_schema.list_servers, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 158 | return resp, body |
| 159 | |
| 160 | def list_servers_with_detail(self, params=None): |
| 161 | """Lists all servers in detail for a user.""" |
| 162 | |
| 163 | url = 'servers/detail' |
| 164 | if params: |
| 165 | url += '?%s' % urllib.urlencode(params) |
| 166 | |
| 167 | resp, body = self.get(url) |
| 168 | body = json.loads(body) |
Ghanshyam | 5174486 | 2014-06-13 12:56:24 +0900 | [diff] [blame] | 169 | self.validate_response(schema.list_servers_detail, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 170 | return resp, body |
| 171 | |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 172 | def wait_for_server_status(self, server_id, status, extra_timeout=0, |
| 173 | raise_on_error=True): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 174 | """Waits for a server to reach a given status.""" |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 175 | return waiters.wait_for_server_status(self, server_id, status, |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 176 | extra_timeout=extra_timeout, |
| 177 | raise_on_error=raise_on_error) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 178 | |
| 179 | def wait_for_server_termination(self, server_id, ignore_error=False): |
| 180 | """Waits for server to reach termination.""" |
| 181 | start_time = int(time.time()) |
| 182 | while True: |
| 183 | try: |
| 184 | resp, body = self.get_server(server_id) |
| 185 | except exceptions.NotFound: |
| 186 | return |
| 187 | |
| 188 | server_status = body['status'] |
| 189 | if server_status == 'ERROR' and not ignore_error: |
| 190 | raise exceptions.BuildErrorException(server_id=server_id) |
| 191 | |
| 192 | if int(time.time()) - start_time >= self.build_timeout: |
| 193 | raise exceptions.TimeoutException |
| 194 | |
| 195 | time.sleep(self.build_interval) |
| 196 | |
| 197 | def list_addresses(self, server_id): |
| 198 | """Lists all addresses for a server.""" |
| 199 | resp, body = self.get("servers/%s/ips" % str(server_id)) |
| 200 | body = json.loads(body) |
Ghanshyam | d847c58 | 2014-05-07 16:21:36 +0900 | [diff] [blame] | 201 | self.validate_response(schema.list_addresses, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 202 | return resp, body['addresses'] |
| 203 | |
| 204 | def list_addresses_by_network(self, server_id, network_id): |
| 205 | """Lists all addresses of a specific network type for a server.""" |
| 206 | resp, body = self.get("servers/%s/ips/%s" % |
| 207 | (str(server_id), network_id)) |
| 208 | body = json.loads(body) |
Ghanshyam | 9541ad1 | 2014-05-07 16:38:43 +0900 | [diff] [blame] | 209 | self.validate_response(schema.list_addresses_by_network, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 210 | return resp, body |
| 211 | |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 212 | def action(self, server_id, action_name, response_key, |
| 213 | schema=common_schema.server_actions_common_schema, **kwargs): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 214 | post_body = json.dumps({action_name: kwargs}) |
| 215 | resp, body = self.post('servers/%s/action' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 216 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 217 | if response_key is not None: |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 218 | body = json.loads(body) |
| 219 | # Check for Schema as 'None' because if we do not have any server |
| 220 | # action schema implemented yet then they can pass 'None' to skip |
| 221 | # the validation.Once all server action has their schema |
| 222 | # implemented then, this check can be removed if every actions are |
| 223 | # supposed to validate their response. |
| 224 | # TODO(GMann): Remove the below 'if' check once all server actions |
| 225 | # schema are implemented. |
| 226 | if schema is not None: |
| 227 | self.validate_response(schema, resp, body) |
| 228 | body = body[response_key] |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 229 | else: |
| 230 | self.validate_response(schema, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 231 | return resp, body |
| 232 | |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 233 | def create_backup(self, server_id, backup_type, rotation, name): |
| 234 | """Backup a server instance.""" |
| 235 | return self.action(server_id, "create_backup", None, |
| 236 | backup_type=backup_type, |
| 237 | rotation=rotation, |
| 238 | name=name) |
| 239 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 240 | def change_password(self, server_id, admin_password): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 241 | """Changes the root password for the server.""" |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 242 | return self.action(server_id, 'change_password', |
| 243 | None, schema.server_actions_change_password, |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 244 | admin_password=admin_password) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 245 | |
ivan-zhu | af8c4e6 | 2014-01-22 17:09:42 +0800 | [diff] [blame] | 246 | 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) |
Ghanshyam | 7fa397c | 2014-04-01 19:32:38 +0900 | [diff] [blame] | 250 | self.validate_response(common_schema.get_password, resp, body) |
ivan-zhu | af8c4e6 | 2014-01-22 17:09:42 +0800 | [diff] [blame] | 251 | 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 | """ |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 259 | 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-zhu | af8c4e6 | 2014-01-22 17:09:42 +0800 | [diff] [blame] | 264 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 265 | def reboot(self, server_id, reboot_type): |
| 266 | """Reboots a server.""" |
| 267 | return self.action(server_id, 'reboot', None, type=reboot_type) |
| 268 | |
| 269 | def rebuild(self, server_id, image_ref, **kwargs): |
| 270 | """Rebuilds a server with a new image.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 271 | kwargs['image_ref'] = image_ref |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 272 | if 'disk_config' in kwargs: |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 273 | kwargs['os-disk-config:disk_config'] = kwargs['disk_config'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 274 | del kwargs['disk_config'] |
Ghanshyam | 9c2e50d | 2014-07-22 21:32:05 +0900 | [diff] [blame] | 275 | 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) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 281 | |
| 282 | def resize(self, server_id, flavor_ref, **kwargs): |
| 283 | """Changes the flavor of a server.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 284 | kwargs['flavor_ref'] = flavor_ref |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 285 | if 'disk_config' in kwargs: |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 286 | kwargs['os-disk-config:disk_config'] = kwargs['disk_config'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 287 | del kwargs['disk_config'] |
| 288 | return self.action(server_id, 'resize', None, **kwargs) |
| 289 | |
| 290 | def confirm_resize(self, server_id, **kwargs): |
| 291 | """Confirms the flavor change for a server.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 292 | return self.action(server_id, 'confirm_resize', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 293 | |
| 294 | def revert_resize(self, server_id, **kwargs): |
| 295 | """Reverts a server back to its original flavor.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 296 | return self.action(server_id, 'revert_resize', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 297 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 298 | def create_image(self, server_id, name, meta=None): |
| 299 | """Creates an image of the original server.""" |
| 300 | |
| 301 | post_body = { |
| 302 | 'create_image': { |
| 303 | 'name': name, |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if meta is not None: |
| 308 | post_body['create_image']['metadata'] = meta |
| 309 | |
| 310 | post_body = json.dumps(post_body) |
| 311 | resp, body = self.post('servers/%s/action' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 312 | post_body) |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 313 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 314 | |
| 315 | def list_server_metadata(self, server_id): |
| 316 | resp, body = self.get("servers/%s/metadata" % str(server_id)) |
| 317 | body = json.loads(body) |
Ghanshyam | a8f6653 | 2014-04-23 17:18:14 +0900 | [diff] [blame] | 318 | self.validate_response(common_schema.list_server_metadata, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 319 | return resp, body['metadata'] |
| 320 | |
| 321 | def set_server_metadata(self, server_id, meta, no_metadata_field=False): |
| 322 | if no_metadata_field: |
| 323 | post_body = "" |
| 324 | else: |
| 325 | post_body = json.dumps({'metadata': meta}) |
| 326 | resp, body = self.put('servers/%s/metadata' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 327 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 328 | body = json.loads(body) |
Haiwei Xu | 3d2b7af | 2014-04-12 02:50:26 +0900 | [diff] [blame] | 329 | self.validate_response(common_schema.set_server_metadata, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 330 | return resp, body['metadata'] |
| 331 | |
| 332 | def update_server_metadata(self, server_id, meta): |
| 333 | post_body = json.dumps({'metadata': meta}) |
| 334 | resp, body = self.post('servers/%s/metadata' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 335 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 336 | body = json.loads(body) |
Ghanshyam | e842106 | 2014-06-02 15:58:21 +0900 | [diff] [blame] | 337 | self.validate_response(schema.update_server_metadata, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 338 | return resp, body['metadata'] |
| 339 | |
| 340 | def get_server_metadata_item(self, server_id, key): |
| 341 | resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key)) |
| 342 | body = json.loads(body) |
Ghanshyam | eaaa6a4 | 2014-04-25 18:38:21 +0900 | [diff] [blame] | 343 | self.validate_response(schema.set_get_server_metadata_item, |
| 344 | resp, body) |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 345 | return resp, body['metadata'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 346 | |
| 347 | def set_server_metadata_item(self, server_id, key, meta): |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 348 | post_body = json.dumps({'metadata': meta}) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 349 | resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 350 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 351 | body = json.loads(body) |
Ghanshyam | eaaa6a4 | 2014-04-25 18:38:21 +0900 | [diff] [blame] | 352 | self.validate_response(schema.set_get_server_metadata_item, |
| 353 | resp, body) |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 354 | return resp, body['metadata'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 355 | |
| 356 | def delete_server_metadata_item(self, server_id, key): |
| 357 | resp, body = self.delete("servers/%s/metadata/%s" % |
| 358 | (str(server_id), key)) |
Ghanshyam | eaaa6a4 | 2014-04-25 18:38:21 +0900 | [diff] [blame] | 359 | self.validate_response(common_schema.delete_server_metadata_item, |
| 360 | resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 361 | return resp, body |
| 362 | |
| 363 | def stop(self, server_id, **kwargs): |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 364 | return self.action(server_id, 'stop', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 365 | |
| 366 | def start(self, server_id, **kwargs): |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 367 | return self.action(server_id, 'start', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 368 | |
| 369 | def attach_volume(self, server_id, volume_id, device='/dev/vdz'): |
| 370 | """Attaches a volume to a server instance.""" |
Ghanshyam | 385c4e7 | 2014-03-27 11:42:25 +0900 | [diff] [blame] | 371 | resp, body = self.action(server_id, 'attach', None, |
| 372 | volume_id=volume_id, device=device) |
| 373 | self.validate_response(schema.attach_detach_volume, resp, body) |
| 374 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 375 | |
| 376 | def detach_volume(self, server_id, volume_id): |
| 377 | """Detaches a volume from a server instance.""" |
Ghanshyam | 385c4e7 | 2014-03-27 11:42:25 +0900 | [diff] [blame] | 378 | resp, body = self.action(server_id, 'detach', None, |
| 379 | volume_id=volume_id) |
| 380 | self.validate_response(schema.attach_detach_volume, resp, body) |
| 381 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 382 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 383 | def live_migrate_server(self, server_id, dest_host, use_block_migration): |
| 384 | """This should be called with administrator privileges .""" |
| 385 | |
| 386 | migrate_params = { |
| 387 | "disk_over_commit": False, |
| 388 | "block_migration": use_block_migration, |
| 389 | "host": dest_host |
| 390 | } |
| 391 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 392 | req_body = json.dumps({'migrate_live': migrate_params}) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 393 | |
| 394 | resp, body = self.post("servers/%s/action" % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 395 | req_body) |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 396 | self.validate_response(common_schema.server_actions_common_schema, |
| 397 | resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 398 | return resp, body |
| 399 | |
| 400 | def migrate_server(self, server_id, **kwargs): |
| 401 | """Migrates a server to a new host.""" |
| 402 | return self.action(server_id, 'migrate', None, **kwargs) |
| 403 | |
| 404 | def lock_server(self, server_id, **kwargs): |
| 405 | """Locks the given server.""" |
| 406 | return self.action(server_id, 'lock', None, **kwargs) |
| 407 | |
| 408 | def unlock_server(self, server_id, **kwargs): |
| 409 | """UNlocks the given server.""" |
| 410 | return self.action(server_id, 'unlock', None, **kwargs) |
| 411 | |
| 412 | def suspend_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 413 | """Suspends the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 414 | return self.action(server_id, 'suspend', None, **kwargs) |
| 415 | |
| 416 | def resume_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 417 | """Un-suspends the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 418 | return self.action(server_id, 'resume', None, **kwargs) |
| 419 | |
| 420 | def pause_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 421 | """Pauses the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 422 | return self.action(server_id, 'pause', None, **kwargs) |
| 423 | |
| 424 | def unpause_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 425 | """Un-pauses the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 426 | return self.action(server_id, 'unpause', None, **kwargs) |
| 427 | |
| 428 | def reset_state(self, server_id, state='error'): |
| 429 | """Resets the state of a server to active/error.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 430 | return self.action(server_id, 'reset_state', None, state=state) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 431 | |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 432 | def shelve_server(self, server_id, **kwargs): |
| 433 | """Shelves the provided server.""" |
| 434 | return self.action(server_id, 'shelve', None, **kwargs) |
| 435 | |
| 436 | def unshelve_server(self, server_id, **kwargs): |
| 437 | """Un-shelves the provided server.""" |
| 438 | return self.action(server_id, 'unshelve', None, **kwargs) |
| 439 | |
Ken'ichi Ohmichi | 17b7f11 | 2014-02-20 06:33:49 +0900 | [diff] [blame] | 440 | def shelve_offload_server(self, server_id, **kwargs): |
| 441 | """Shelve-offload the provided server.""" |
| 442 | return self.action(server_id, 'shelve_offload', None, **kwargs) |
| 443 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 444 | def get_console_output(self, server_id, length): |
Matt Riedemann | 20e18dd | 2014-06-10 09:13:23 -0700 | [diff] [blame] | 445 | if length is None: |
| 446 | # NOTE(mriedem): -1 means optional/unlimited in the nova v3 API. |
| 447 | length = -1 |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 448 | return self.action(server_id, 'get_console_output', 'output', |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 449 | common_schema.get_console_output, length=length) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 450 | |
Yuiko Takada | 7835d50 | 2014-01-15 10:15:03 +0000 | [diff] [blame] | 451 | def rescue_server(self, server_id, **kwargs): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 452 | """Rescue the provided server.""" |
Ghanshyam | 8845791 | 2014-07-25 16:02:12 +0900 | [diff] [blame^] | 453 | post_body = json.dumps({'rescue': kwargs}) |
| 454 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 455 | post_body) |
| 456 | if CONF.compute_feature_enabled.enable_instance_password: |
| 457 | rescue_schema = schema.rescue_server_with_admin_pass |
| 458 | else: |
| 459 | rescue_schema = schema.rescue_server |
| 460 | body = json.loads(body) |
| 461 | self.validate_response(rescue_schema, resp, body) |
| 462 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 463 | |
| 464 | def unrescue_server(self, server_id): |
| 465 | """Unrescue the provided server.""" |
| 466 | return self.action(server_id, 'unrescue', None) |
| 467 | |
| 468 | def get_server_diagnostics(self, server_id): |
| 469 | """Get the usage data for a server.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 470 | resp, body = self.get("servers/%s/os-server-diagnostics" % |
| 471 | str(server_id)) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 472 | return resp, json.loads(body) |
| 473 | |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 474 | def list_server_actions(self, server_id): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 475 | """List the provided server action.""" |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 476 | resp, body = self.get("servers/%s/os-server-actions" % |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 477 | str(server_id)) |
| 478 | body = json.loads(body) |
Ghanshyam | 2996609 | 2014-04-07 17:27:41 +0900 | [diff] [blame] | 479 | self.validate_response(schema.list_server_actions, resp, body) |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 480 | return resp, body['server_actions'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 481 | |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 482 | def get_server_action(self, server_id, request_id): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 483 | """Returns the action details of the provided server.""" |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 484 | resp, body = self.get("servers/%s/os-server-actions/%s" % |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 485 | (str(server_id), str(request_id))) |
| 486 | body = json.loads(body) |
Ghanshyam | 4b41dfd | 2014-07-17 13:40:38 +0900 | [diff] [blame] | 487 | self.validate_response(schema.get_server_action, resp, body) |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 488 | return resp, body['server_action'] |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 489 | |
| 490 | def force_delete_server(self, server_id, **kwargs): |
| 491 | """Force delete a server.""" |
| 492 | return self.action(server_id, 'force_delete', None, **kwargs) |
| 493 | |
| 494 | def restore_soft_deleted_server(self, server_id, **kwargs): |
| 495 | """Restore a soft-deleted server.""" |
| 496 | return self.action(server_id, 'restore', None, **kwargs) |
Ghanshyam Mann | 41c1757 | 2014-02-27 18:52:56 +0900 | [diff] [blame] | 497 | |
| 498 | def get_vnc_console(self, server_id, type): |
| 499 | """Get URL of VNC console.""" |
| 500 | post_body = json.dumps({ |
| 501 | "get_vnc_console": { |
| 502 | "type": type |
| 503 | } |
| 504 | }) |
| 505 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 506 | post_body) |
| 507 | body = json.loads(body) |
Ghanshyam | d6d3040 | 2014-04-02 15:28:37 +0900 | [diff] [blame] | 508 | self.validate_response(common_schema.get_vnc_console, resp, body) |
Ghanshyam Mann | 41c1757 | 2014-02-27 18:52:56 +0900 | [diff] [blame] | 509 | return resp, body['console'] |
Ghanshyam | 0549f7b | 2014-03-04 19:07:52 +0900 | [diff] [blame] | 510 | |
| 511 | def reset_network(self, server_id, **kwargs): |
| 512 | """Resets the Network of a server""" |
| 513 | return self.action(server_id, 'reset_network', None, **kwargs) |
| 514 | |
| 515 | def inject_network_info(self, server_id, **kwargs): |
| 516 | """Inject the Network Info into server""" |
| 517 | return self.action(server_id, 'inject_network_info', None, **kwargs) |
Ghanshyam | 70876d0 | 2014-03-11 11:40:18 +0900 | [diff] [blame] | 518 | |
| 519 | def get_spice_console(self, server_id, console_type): |
| 520 | """Get URL of Spice console.""" |
| 521 | return self.action(server_id, "get_spice_console" |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 522 | "console", None, type=console_type) |
Ghanshyam | 70876d0 | 2014-03-11 11:40:18 +0900 | [diff] [blame] | 523 | |
| 524 | def get_rdp_console(self, server_id, console_type): |
| 525 | """Get URL of RDP console.""" |
| 526 | return self.action(server_id, "get_rdp_console" |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 527 | "console", None, type=console_type) |