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 | |
Marc Koderer | 6fbd74f | 2014-08-04 09:38:19 +0200 | [diff] [blame] | 22 | from tempest.api_schema.response.compute import servers as common_schema |
| 23 | from tempest.api_schema.response.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'] |
Ghanshyam | 7f98910 | 2014-07-29 14:23:26 +0900 | [diff] [blame] | 99 | if CONF.compute_feature_enabled.enable_instance_password: |
| 100 | create_schema = schema.create_server_with_admin_pass |
| 101 | else: |
| 102 | create_schema = schema.create_server |
| 103 | self.validate_response(create_schema, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 104 | return resp, body['server'] |
| 105 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 106 | def update_server(self, server_id, name=None, meta=None, access_ip_v4=None, |
| 107 | access_ip_v6=None, disk_config=None): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 108 | """ |
| 109 | Updates the properties of an existing server. |
| 110 | server_id: The id of an existing server. |
| 111 | name: The name of the server. |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 112 | access_ip_v4: The IPv4 access address for the server. |
| 113 | access_ip_v6: The IPv6 access address for the server. |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 114 | """ |
| 115 | |
| 116 | post_body = {} |
| 117 | |
| 118 | if meta is not None: |
| 119 | post_body['metadata'] = meta |
| 120 | |
| 121 | if name is not None: |
| 122 | post_body['name'] = name |
| 123 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 124 | if access_ip_v4 is not None: |
ivan-zhu | f0bf301 | 2013-11-25 16:03:42 +0800 | [diff] [blame] | 125 | post_body['os-access-ips:access_ip_v4'] = access_ip_v4 |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 126 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 127 | if access_ip_v6 is not None: |
ivan-zhu | f0bf301 | 2013-11-25 16:03:42 +0800 | [diff] [blame] | 128 | post_body['os-access-ips:access_ip_v6'] = access_ip_v6 |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 129 | |
| 130 | if disk_config is not None: |
ivan-zhu | 5096952 | 2013-08-26 11:10:39 +0800 | [diff] [blame] | 131 | post_body['os-disk-config:disk_config'] = disk_config |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 132 | |
| 133 | post_body = json.dumps({'server': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 134 | resp, body = self.put("servers/%s" % str(server_id), post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 135 | body = json.loads(body) |
Ken'ichi Ohmichi | 29cd512 | 2014-04-28 11:04:52 +0900 | [diff] [blame] | 136 | self.validate_response(schema.update_server, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 137 | return resp, body['server'] |
| 138 | |
| 139 | def get_server(self, server_id): |
| 140 | """Returns the details of an existing server.""" |
| 141 | resp, body = self.get("servers/%s" % str(server_id)) |
| 142 | body = json.loads(body) |
Ken'ichi Ohmichi | 21e4fc7 | 2014-05-08 16:46:23 +0900 | [diff] [blame] | 143 | self.validate_response(schema.get_server, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 144 | return resp, body['server'] |
| 145 | |
| 146 | def delete_server(self, server_id): |
| 147 | """Deletes the given server.""" |
Ghanshyam | 4ac0274 | 2014-04-17 19:15:47 +0900 | [diff] [blame] | 148 | resp, body = self.delete("servers/%s" % str(server_id)) |
| 149 | self.validate_response(common_schema.delete_server, resp, body) |
| 150 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 151 | |
| 152 | def list_servers(self, params=None): |
| 153 | """Lists all servers for a user.""" |
| 154 | |
| 155 | url = 'servers' |
| 156 | if params: |
| 157 | url += '?%s' % urllib.urlencode(params) |
| 158 | |
| 159 | resp, body = self.get(url) |
| 160 | body = json.loads(body) |
Ghanshyam | 623c38f | 2014-04-21 17:16:21 +0900 | [diff] [blame] | 161 | self.validate_response(common_schema.list_servers, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 162 | return resp, body |
| 163 | |
| 164 | def list_servers_with_detail(self, params=None): |
| 165 | """Lists all servers in detail for a user.""" |
| 166 | |
| 167 | url = 'servers/detail' |
| 168 | if params: |
| 169 | url += '?%s' % urllib.urlencode(params) |
| 170 | |
| 171 | resp, body = self.get(url) |
| 172 | body = json.loads(body) |
Ghanshyam | 5174486 | 2014-06-13 12:56:24 +0900 | [diff] [blame] | 173 | self.validate_response(schema.list_servers_detail, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 174 | return resp, body |
| 175 | |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 176 | def wait_for_server_status(self, server_id, status, extra_timeout=0, |
| 177 | raise_on_error=True): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 178 | """Waits for a server to reach a given status.""" |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 179 | return waiters.wait_for_server_status(self, server_id, status, |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 180 | extra_timeout=extra_timeout, |
| 181 | raise_on_error=raise_on_error) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 182 | |
| 183 | def wait_for_server_termination(self, server_id, ignore_error=False): |
| 184 | """Waits for server to reach termination.""" |
| 185 | start_time = int(time.time()) |
| 186 | while True: |
| 187 | try: |
| 188 | resp, body = self.get_server(server_id) |
| 189 | except exceptions.NotFound: |
| 190 | return |
| 191 | |
| 192 | server_status = body['status'] |
| 193 | if server_status == 'ERROR' and not ignore_error: |
| 194 | raise exceptions.BuildErrorException(server_id=server_id) |
| 195 | |
| 196 | if int(time.time()) - start_time >= self.build_timeout: |
| 197 | raise exceptions.TimeoutException |
| 198 | |
| 199 | time.sleep(self.build_interval) |
| 200 | |
| 201 | def list_addresses(self, server_id): |
| 202 | """Lists all addresses for a server.""" |
| 203 | resp, body = self.get("servers/%s/ips" % str(server_id)) |
| 204 | body = json.loads(body) |
Ghanshyam | d847c58 | 2014-05-07 16:21:36 +0900 | [diff] [blame] | 205 | self.validate_response(schema.list_addresses, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 206 | return resp, body['addresses'] |
| 207 | |
| 208 | def list_addresses_by_network(self, server_id, network_id): |
| 209 | """Lists all addresses of a specific network type for a server.""" |
| 210 | resp, body = self.get("servers/%s/ips/%s" % |
| 211 | (str(server_id), network_id)) |
| 212 | body = json.loads(body) |
Ghanshyam | 9541ad1 | 2014-05-07 16:38:43 +0900 | [diff] [blame] | 213 | self.validate_response(schema.list_addresses_by_network, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 214 | return resp, body |
| 215 | |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 216 | def action(self, server_id, action_name, response_key, |
| 217 | schema=common_schema.server_actions_common_schema, **kwargs): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 218 | post_body = json.dumps({action_name: kwargs}) |
| 219 | resp, body = self.post('servers/%s/action' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 220 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 221 | if response_key is not None: |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 222 | body = json.loads(body) |
| 223 | # Check for Schema as 'None' because if we do not have any server |
| 224 | # action schema implemented yet then they can pass 'None' to skip |
| 225 | # the validation.Once all server action has their schema |
| 226 | # implemented then, this check can be removed if every actions are |
| 227 | # supposed to validate their response. |
| 228 | # TODO(GMann): Remove the below 'if' check once all server actions |
| 229 | # schema are implemented. |
| 230 | if schema is not None: |
| 231 | self.validate_response(schema, resp, body) |
| 232 | body = body[response_key] |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 233 | else: |
| 234 | self.validate_response(schema, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 235 | return resp, body |
| 236 | |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 237 | def create_backup(self, server_id, backup_type, rotation, name): |
| 238 | """Backup a server instance.""" |
| 239 | return self.action(server_id, "create_backup", None, |
| 240 | backup_type=backup_type, |
| 241 | rotation=rotation, |
| 242 | name=name) |
| 243 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 244 | def change_password(self, server_id, admin_password): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 245 | """Changes the root password for the server.""" |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 246 | return self.action(server_id, 'change_password', |
| 247 | None, schema.server_actions_change_password, |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 248 | admin_password=admin_password) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 249 | |
ivan-zhu | af8c4e6 | 2014-01-22 17:09:42 +0800 | [diff] [blame] | 250 | def get_password(self, server_id): |
| 251 | resp, body = self.get("servers/%s/os-server-password" % |
| 252 | str(server_id)) |
| 253 | body = json.loads(body) |
Ghanshyam | 7fa397c | 2014-04-01 19:32:38 +0900 | [diff] [blame] | 254 | self.validate_response(common_schema.get_password, resp, body) |
ivan-zhu | af8c4e6 | 2014-01-22 17:09:42 +0800 | [diff] [blame] | 255 | return resp, body |
| 256 | |
| 257 | def delete_password(self, server_id): |
| 258 | """ |
| 259 | Removes the encrypted server password from the metadata server |
| 260 | Note that this does not actually change the instance server |
| 261 | password. |
| 262 | """ |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 263 | resp, body = self.delete("servers/%s/os-server-password" % |
| 264 | str(server_id)) |
| 265 | self.validate_response(common_schema.server_actions_delete_password, |
| 266 | resp, body) |
| 267 | return resp, body |
ivan-zhu | af8c4e6 | 2014-01-22 17:09:42 +0800 | [diff] [blame] | 268 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 269 | def reboot(self, server_id, reboot_type): |
| 270 | """Reboots a server.""" |
| 271 | return self.action(server_id, 'reboot', None, type=reboot_type) |
| 272 | |
| 273 | def rebuild(self, server_id, image_ref, **kwargs): |
| 274 | """Rebuilds a server with a new image.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 275 | kwargs['image_ref'] = image_ref |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 276 | if 'disk_config' in kwargs: |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 277 | kwargs['os-disk-config:disk_config'] = kwargs['disk_config'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 278 | del kwargs['disk_config'] |
Ghanshyam | 9c2e50d | 2014-07-22 21:32:05 +0900 | [diff] [blame] | 279 | if CONF.compute_feature_enabled.enable_instance_password: |
| 280 | rebuild_schema = schema.rebuild_server_with_admin_pass |
| 281 | else: |
| 282 | rebuild_schema = schema.rebuild_server |
| 283 | return self.action(server_id, 'rebuild', 'server', |
| 284 | rebuild_schema, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 285 | |
| 286 | def resize(self, server_id, flavor_ref, **kwargs): |
| 287 | """Changes the flavor of a server.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 288 | kwargs['flavor_ref'] = flavor_ref |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 289 | if 'disk_config' in kwargs: |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 290 | kwargs['os-disk-config:disk_config'] = kwargs['disk_config'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 291 | del kwargs['disk_config'] |
| 292 | return self.action(server_id, 'resize', None, **kwargs) |
| 293 | |
| 294 | def confirm_resize(self, server_id, **kwargs): |
| 295 | """Confirms the flavor change for a server.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 296 | return self.action(server_id, 'confirm_resize', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 297 | |
| 298 | def revert_resize(self, server_id, **kwargs): |
| 299 | """Reverts a server back to its original flavor.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 300 | return self.action(server_id, 'revert_resize', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 301 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 302 | def create_image(self, server_id, name, meta=None): |
| 303 | """Creates an image of the original server.""" |
| 304 | |
| 305 | post_body = { |
| 306 | 'create_image': { |
| 307 | 'name': name, |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if meta is not None: |
| 312 | post_body['create_image']['metadata'] = meta |
| 313 | |
| 314 | post_body = json.dumps(post_body) |
| 315 | resp, body = self.post('servers/%s/action' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 316 | post_body) |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 317 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 318 | |
| 319 | def list_server_metadata(self, server_id): |
| 320 | resp, body = self.get("servers/%s/metadata" % str(server_id)) |
| 321 | body = json.loads(body) |
Ghanshyam | a8f6653 | 2014-04-23 17:18:14 +0900 | [diff] [blame] | 322 | self.validate_response(common_schema.list_server_metadata, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 323 | return resp, body['metadata'] |
| 324 | |
| 325 | def set_server_metadata(self, server_id, meta, no_metadata_field=False): |
| 326 | if no_metadata_field: |
| 327 | post_body = "" |
| 328 | else: |
| 329 | post_body = json.dumps({'metadata': meta}) |
| 330 | resp, body = self.put('servers/%s/metadata' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 331 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 332 | body = json.loads(body) |
Haiwei Xu | 3d2b7af | 2014-04-12 02:50:26 +0900 | [diff] [blame] | 333 | self.validate_response(common_schema.set_server_metadata, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 334 | return resp, body['metadata'] |
| 335 | |
| 336 | def update_server_metadata(self, server_id, meta): |
| 337 | post_body = json.dumps({'metadata': meta}) |
| 338 | resp, body = self.post('servers/%s/metadata' % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 339 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 340 | body = json.loads(body) |
Ghanshyam | e842106 | 2014-06-02 15:58:21 +0900 | [diff] [blame] | 341 | self.validate_response(schema.update_server_metadata, resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 342 | return resp, body['metadata'] |
| 343 | |
| 344 | def get_server_metadata_item(self, server_id, key): |
| 345 | resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key)) |
| 346 | body = json.loads(body) |
Ghanshyam | eaaa6a4 | 2014-04-25 18:38:21 +0900 | [diff] [blame] | 347 | self.validate_response(schema.set_get_server_metadata_item, |
| 348 | resp, body) |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 349 | return resp, body['metadata'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 350 | |
| 351 | def set_server_metadata_item(self, server_id, key, meta): |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 352 | post_body = json.dumps({'metadata': meta}) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 353 | resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 354 | post_body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 355 | body = json.loads(body) |
Ghanshyam | eaaa6a4 | 2014-04-25 18:38:21 +0900 | [diff] [blame] | 356 | self.validate_response(schema.set_get_server_metadata_item, |
| 357 | resp, body) |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 358 | return resp, body['metadata'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 359 | |
| 360 | def delete_server_metadata_item(self, server_id, key): |
| 361 | resp, body = self.delete("servers/%s/metadata/%s" % |
| 362 | (str(server_id), key)) |
Ghanshyam | eaaa6a4 | 2014-04-25 18:38:21 +0900 | [diff] [blame] | 363 | self.validate_response(common_schema.delete_server_metadata_item, |
| 364 | resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 365 | return resp, body |
| 366 | |
| 367 | def stop(self, server_id, **kwargs): |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 368 | return self.action(server_id, 'stop', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 369 | |
| 370 | def start(self, server_id, **kwargs): |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 371 | return self.action(server_id, 'start', None, **kwargs) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 372 | |
| 373 | def attach_volume(self, server_id, volume_id, device='/dev/vdz'): |
| 374 | """Attaches a volume to a server instance.""" |
Ghanshyam | 385c4e7 | 2014-03-27 11:42:25 +0900 | [diff] [blame] | 375 | resp, body = self.action(server_id, 'attach', None, |
| 376 | volume_id=volume_id, device=device) |
| 377 | self.validate_response(schema.attach_detach_volume, resp, body) |
| 378 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 379 | |
| 380 | def detach_volume(self, server_id, volume_id): |
| 381 | """Detaches a volume from a server instance.""" |
Ghanshyam | 385c4e7 | 2014-03-27 11:42:25 +0900 | [diff] [blame] | 382 | resp, body = self.action(server_id, 'detach', None, |
| 383 | volume_id=volume_id) |
| 384 | self.validate_response(schema.attach_detach_volume, resp, body) |
| 385 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 386 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 387 | def live_migrate_server(self, server_id, dest_host, use_block_migration): |
| 388 | """This should be called with administrator privileges .""" |
| 389 | |
| 390 | migrate_params = { |
| 391 | "disk_over_commit": False, |
| 392 | "block_migration": use_block_migration, |
| 393 | "host": dest_host |
| 394 | } |
| 395 | |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 396 | req_body = json.dumps({'migrate_live': migrate_params}) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 397 | |
| 398 | resp, body = self.post("servers/%s/action" % str(server_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 399 | req_body) |
Ghanshyam | 997c909 | 2014-04-03 19:00:20 +0900 | [diff] [blame] | 400 | self.validate_response(common_schema.server_actions_common_schema, |
| 401 | resp, body) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 402 | return resp, body |
| 403 | |
| 404 | def migrate_server(self, server_id, **kwargs): |
| 405 | """Migrates a server to a new host.""" |
| 406 | return self.action(server_id, 'migrate', None, **kwargs) |
| 407 | |
| 408 | def lock_server(self, server_id, **kwargs): |
| 409 | """Locks the given server.""" |
| 410 | return self.action(server_id, 'lock', None, **kwargs) |
| 411 | |
| 412 | def unlock_server(self, server_id, **kwargs): |
| 413 | """UNlocks the given server.""" |
| 414 | return self.action(server_id, 'unlock', None, **kwargs) |
| 415 | |
| 416 | def suspend_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 417 | """Suspends the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 418 | return self.action(server_id, 'suspend', None, **kwargs) |
| 419 | |
| 420 | def resume_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 421 | """Un-suspends the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 422 | return self.action(server_id, 'resume', None, **kwargs) |
| 423 | |
| 424 | def pause_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 425 | """Pauses the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 426 | return self.action(server_id, 'pause', None, **kwargs) |
| 427 | |
| 428 | def unpause_server(self, server_id, **kwargs): |
Zhi Kun Liu | de89272 | 2013-12-30 15:27:52 +0800 | [diff] [blame] | 429 | """Un-pauses the provided server.""" |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 430 | return self.action(server_id, 'unpause', None, **kwargs) |
| 431 | |
| 432 | def reset_state(self, server_id, state='error'): |
| 433 | """Resets the state of a server to active/error.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 434 | return self.action(server_id, 'reset_state', None, state=state) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 435 | |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 436 | def shelve_server(self, server_id, **kwargs): |
| 437 | """Shelves the provided server.""" |
| 438 | return self.action(server_id, 'shelve', None, **kwargs) |
| 439 | |
| 440 | def unshelve_server(self, server_id, **kwargs): |
| 441 | """Un-shelves the provided server.""" |
| 442 | return self.action(server_id, 'unshelve', None, **kwargs) |
| 443 | |
Ken'ichi Ohmichi | 17b7f11 | 2014-02-20 06:33:49 +0900 | [diff] [blame] | 444 | def shelve_offload_server(self, server_id, **kwargs): |
| 445 | """Shelve-offload the provided server.""" |
| 446 | return self.action(server_id, 'shelve_offload', None, **kwargs) |
| 447 | |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 448 | def get_console_output(self, server_id, length): |
Matt Riedemann | 20e18dd | 2014-06-10 09:13:23 -0700 | [diff] [blame] | 449 | if length is None: |
| 450 | # NOTE(mriedem): -1 means optional/unlimited in the nova v3 API. |
| 451 | length = -1 |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 452 | return self.action(server_id, 'get_console_output', 'output', |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 453 | common_schema.get_console_output, length=length) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 454 | |
Yuiko Takada | 7835d50 | 2014-01-15 10:15:03 +0000 | [diff] [blame] | 455 | def rescue_server(self, server_id, **kwargs): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 456 | """Rescue the provided server.""" |
Ghanshyam | 8845791 | 2014-07-25 16:02:12 +0900 | [diff] [blame] | 457 | post_body = json.dumps({'rescue': kwargs}) |
| 458 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 459 | post_body) |
| 460 | if CONF.compute_feature_enabled.enable_instance_password: |
| 461 | rescue_schema = schema.rescue_server_with_admin_pass |
| 462 | else: |
| 463 | rescue_schema = schema.rescue_server |
| 464 | body = json.loads(body) |
| 465 | self.validate_response(rescue_schema, resp, body) |
| 466 | return resp, body |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 467 | |
| 468 | def unrescue_server(self, server_id): |
| 469 | """Unrescue the provided server.""" |
| 470 | return self.action(server_id, 'unrescue', None) |
| 471 | |
| 472 | def get_server_diagnostics(self, server_id): |
| 473 | """Get the usage data for a server.""" |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 474 | resp, body = self.get("servers/%s/os-server-diagnostics" % |
| 475 | str(server_id)) |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 476 | return resp, json.loads(body) |
| 477 | |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 478 | def list_server_actions(self, server_id): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 479 | """List the provided server action.""" |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 480 | resp, body = self.get("servers/%s/os-server-actions" % |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 481 | str(server_id)) |
| 482 | body = json.loads(body) |
Ghanshyam | 2996609 | 2014-04-07 17:27:41 +0900 | [diff] [blame] | 483 | self.validate_response(schema.list_server_actions, resp, body) |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 484 | return resp, body['server_actions'] |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 485 | |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 486 | def get_server_action(self, server_id, request_id): |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 487 | """Returns the action details of the provided server.""" |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 488 | resp, body = self.get("servers/%s/os-server-actions/%s" % |
ivan-zhu | 0911194 | 2013-08-01 08:09:16 +0800 | [diff] [blame] | 489 | (str(server_id), str(request_id))) |
| 490 | body = json.loads(body) |
Ghanshyam | 4b41dfd | 2014-07-17 13:40:38 +0900 | [diff] [blame] | 491 | self.validate_response(schema.get_server_action, resp, body) |
Rohan Kanade | 8ac8c97 | 2014-04-09 07:35:56 +0200 | [diff] [blame] | 492 | return resp, body['server_action'] |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 493 | |
| 494 | def force_delete_server(self, server_id, **kwargs): |
| 495 | """Force delete a server.""" |
| 496 | return self.action(server_id, 'force_delete', None, **kwargs) |
| 497 | |
| 498 | def restore_soft_deleted_server(self, server_id, **kwargs): |
| 499 | """Restore a soft-deleted server.""" |
| 500 | return self.action(server_id, 'restore', None, **kwargs) |
Ghanshyam Mann | 41c1757 | 2014-02-27 18:52:56 +0900 | [diff] [blame] | 501 | |
| 502 | def get_vnc_console(self, server_id, type): |
| 503 | """Get URL of VNC console.""" |
| 504 | post_body = json.dumps({ |
| 505 | "get_vnc_console": { |
| 506 | "type": type |
| 507 | } |
| 508 | }) |
| 509 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 510 | post_body) |
| 511 | body = json.loads(body) |
Ghanshyam | d6d3040 | 2014-04-02 15:28:37 +0900 | [diff] [blame] | 512 | self.validate_response(common_schema.get_vnc_console, resp, body) |
Ghanshyam Mann | 41c1757 | 2014-02-27 18:52:56 +0900 | [diff] [blame] | 513 | return resp, body['console'] |
Ghanshyam | 0549f7b | 2014-03-04 19:07:52 +0900 | [diff] [blame] | 514 | |
| 515 | def reset_network(self, server_id, **kwargs): |
| 516 | """Resets the Network of a server""" |
| 517 | return self.action(server_id, 'reset_network', None, **kwargs) |
| 518 | |
| 519 | def inject_network_info(self, server_id, **kwargs): |
| 520 | """Inject the Network Info into server""" |
| 521 | return self.action(server_id, 'inject_network_info', None, **kwargs) |
Ghanshyam | 70876d0 | 2014-03-11 11:40:18 +0900 | [diff] [blame] | 522 | |
| 523 | def get_spice_console(self, server_id, console_type): |
| 524 | """Get URL of Spice console.""" |
| 525 | return self.action(server_id, "get_spice_console" |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 526 | "console", None, type=console_type) |
Ghanshyam | 70876d0 | 2014-03-11 11:40:18 +0900 | [diff] [blame] | 527 | |
| 528 | def get_rdp_console(self, server_id, console_type): |
| 529 | """Get URL of RDP console.""" |
| 530 | return self.action(server_id, "get_rdp_console" |
Ghanshyam | 7ee264a | 2014-04-02 16:37:57 +0900 | [diff] [blame] | 531 | "console", None, type=console_type) |