ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
nithya-ganesan | e6a36c8 | 2013-02-15 14:38:27 +0000 | [diff] [blame] | 2 | # Copyright 2013 Hewlett-Packard Development Company, L.P. |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 3 | # All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 17 | import json |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 18 | import time |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 20 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | from tempest.common.rest_client import RestClient |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 22 | from tempest.common import waiters |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 23 | from tempest import config |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | from tempest import exceptions |
| 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 26 | CONF = config.CONF |
| 27 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 28 | |
Dan Smith | cf8fab6 | 2012-08-14 08:03:48 -0700 | [diff] [blame] | 29 | class ServersClientJSON(RestClient): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 30 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 31 | def __init__(self, username, password, auth_url, tenant_name=None, |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 32 | auth_version='v2'): |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 33 | super(ServersClientJSON, self).__init__(username, password, |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 34 | auth_url, tenant_name, |
| 35 | auth_version=auth_version) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 36 | self.service = CONF.compute.catalog_type |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 37 | |
Rohit Karajgi | 95446a2 | 2011-12-12 06:21:43 -0800 | [diff] [blame] | 38 | def create_server(self, name, image_ref, flavor_ref, **kwargs): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 39 | """ |
| 40 | Creates an instance of a server. |
Rohit Karajgi | 95446a2 | 2011-12-12 06:21:43 -0800 | [diff] [blame] | 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: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 45 | adminPass: Sets the initial root password. |
chris fattarsi | a6c2a73 | 2012-05-02 17:00:19 -0700 | [diff] [blame] | 46 | key_name: Key name of keypair that was created earlier. |
David Kranz | 28e79de | 2012-04-12 15:49:41 -0400 | [diff] [blame] | 47 | meta: A dictionary of values to be used as metadata. |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 48 | personality: A list of dictionaries for files to be injected into |
| 49 | the server. |
Rohit Karajgi | 95446a2 | 2011-12-12 06:21:43 -0800 | [diff] [blame] | 50 | security_groups: A list of security group dicts. |
| 51 | networks: A list of network dicts with UUID and fixed_ip. |
| 52 | user_data: User data for instance. |
| 53 | availability_zone: Availability zone in which to launch instance. |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 54 | accessIPv4: The IPv4 access address for the server. |
| 55 | accessIPv6: The IPv6 access address for the server. |
Rohit Karajgi | 95446a2 | 2011-12-12 06:21:43 -0800 | [diff] [blame] | 56 | min_count: Count of minimum number of instances to launch. |
| 57 | max_count: Count of maximum number of instances to launch. |
Daryl Walleck | e36d500 | 2012-03-28 09:56:10 -0500 | [diff] [blame] | 58 | disk_config: Determines if user or admin controls disk configuration. |
Mauro S. M. Rodrigues | c5da4f4 | 2013-03-04 23:57:10 -0500 | [diff] [blame] | 59 | return_reservation_id: Enable/Disable the return of reservation id |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 60 | """ |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 61 | post_body = { |
| 62 | 'name': name, |
| 63 | 'imageRef': image_ref, |
David Kranz | 28e79de | 2012-04-12 15:49:41 -0400 | [diff] [blame] | 64 | 'flavorRef': flavor_ref |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 65 | } |
| 66 | |
chris fattarsi | a6c2a73 | 2012-05-02 17:00:19 -0700 | [diff] [blame] | 67 | for option in ['personality', 'adminPass', 'key_name', |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 68 | 'security_groups', 'networks', 'user_data', |
| 69 | 'availability_zone', 'accessIPv4', 'accessIPv6', |
| 70 | 'min_count', 'max_count', ('metadata', 'meta'), |
Mauro S. M. Rodrigues | c5da4f4 | 2013-03-04 23:57:10 -0500 | [diff] [blame] | 71 | ('OS-DCF:diskConfig', 'disk_config'), |
| 72 | 'return_reservation_id']: |
David Kranz | 28e79de | 2012-04-12 15:49:41 -0400 | [diff] [blame] | 73 | if isinstance(option, tuple): |
| 74 | post_param = option[0] |
| 75 | key = option[1] |
| 76 | else: |
| 77 | post_param = option |
| 78 | key = option |
| 79 | value = kwargs.get(key) |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 80 | if value is not None: |
David Kranz | 28e79de | 2012-04-12 15:49:41 -0400 | [diff] [blame] | 81 | post_body[post_param] = value |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 82 | post_body = json.dumps({'server': post_body}) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 83 | resp, body = self.post('servers', post_body, self.headers) |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 84 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 85 | body = json.loads(body) |
Mauro S. M. Rodrigues | c5da4f4 | 2013-03-04 23:57:10 -0500 | [diff] [blame] | 86 | # NOTE(maurosr): this deals with the case of multiple server create |
| 87 | # with return reservation id set True |
| 88 | if 'reservation_id' in body: |
| 89 | return resp, body |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 90 | return resp, body['server'] |
| 91 | |
| 92 | def update_server(self, server_id, name=None, meta=None, accessIPv4=None, |
ivan-zhu | 72d7d5b | 2013-10-16 17:30:58 +0800 | [diff] [blame] | 93 | accessIPv6=None, disk_config=None): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 94 | """ |
| 95 | Updates the properties of an existing server. |
| 96 | server_id: The id of an existing server. |
| 97 | name: The name of the server. |
| 98 | personality: A list of files to be injected into the server. |
| 99 | accessIPv4: The IPv4 access address for the server. |
| 100 | accessIPv6: The IPv6 access address for the server. |
| 101 | """ |
| 102 | |
| 103 | post_body = {} |
| 104 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 105 | if meta is not None: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 106 | post_body['metadata'] = meta |
| 107 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 108 | if name is not None: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 109 | post_body['name'] = name |
| 110 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 111 | if accessIPv4 is not None: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 112 | post_body['accessIPv4'] = accessIPv4 |
| 113 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 114 | if accessIPv6 is not None: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 115 | post_body['accessIPv6'] = accessIPv6 |
| 116 | |
ivan-zhu | 72d7d5b | 2013-10-16 17:30:58 +0800 | [diff] [blame] | 117 | if disk_config is not None: |
| 118 | post_body['OS-DCF:diskConfig'] = disk_config |
| 119 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 120 | post_body = json.dumps({'server': post_body}) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 121 | resp, body = self.put("servers/%s" % str(server_id), |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 122 | post_body, self.headers) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 123 | body = json.loads(body) |
| 124 | return resp, body['server'] |
| 125 | |
| 126 | def get_server(self, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 127 | """Returns the details of an existing server.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 128 | resp, body = self.get("servers/%s" % str(server_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 129 | body = json.loads(body) |
| 130 | return resp, body['server'] |
| 131 | |
| 132 | def delete_server(self, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 133 | """Deletes the given server.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 134 | return self.delete("servers/%s" % str(server_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 135 | |
| 136 | def list_servers(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 137 | """Lists all servers for a user.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 138 | |
| 139 | url = 'servers' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 140 | if params: |
| 141 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 142 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 143 | resp, body = self.get(url) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 144 | body = json.loads(body) |
| 145 | return resp, body |
| 146 | |
| 147 | def list_servers_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 148 | """Lists all servers in detail for a user.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 149 | |
| 150 | url = 'servers/detail' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 151 | if params: |
| 152 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 153 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 154 | resp, body = self.get(url) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 155 | body = json.loads(body) |
| 156 | return resp, body |
| 157 | |
Zhi Kun Liu | e540176 | 2013-09-11 20:45:48 +0800 | [diff] [blame] | 158 | def wait_for_server_status(self, server_id, status, extra_timeout=0, |
| 159 | raise_on_error=True): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 160 | """Waits for a server to reach a given status.""" |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 161 | return waiters.wait_for_server_status(self, server_id, status, |
Zhi Kun Liu | e540176 | 2013-09-11 20:45:48 +0800 | [diff] [blame] | 162 | extra_timeout=extra_timeout, |
| 163 | raise_on_error=raise_on_error) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 164 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 165 | def wait_for_server_termination(self, server_id, ignore_error=False): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 166 | """Waits for server to reach termination.""" |
Rohit Karajgi | 4a64d23 | 2012-05-17 07:44:37 -0700 | [diff] [blame] | 167 | start_time = int(time.time()) |
| 168 | while True: |
| 169 | try: |
| 170 | resp, body = self.get_server(server_id) |
| 171 | except exceptions.NotFound: |
| 172 | return |
| 173 | |
| 174 | server_status = body['status'] |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 175 | if server_status == 'ERROR' and not ignore_error: |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 176 | raise exceptions.BuildErrorException(server_id=server_id) |
Rohit Karajgi | 4a64d23 | 2012-05-17 07:44:37 -0700 | [diff] [blame] | 177 | |
| 178 | if int(time.time()) - start_time >= self.build_timeout: |
| 179 | raise exceptions.TimeoutException |
| 180 | |
| 181 | time.sleep(self.build_interval) |
| 182 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 183 | def list_addresses(self, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 184 | """Lists all addresses for a server.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 185 | resp, body = self.get("servers/%s/ips" % str(server_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 186 | body = json.loads(body) |
| 187 | return resp, body['addresses'] |
| 188 | |
| 189 | def list_addresses_by_network(self, server_id, network_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 190 | """Lists all addresses of a specific network type for a server.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 191 | resp, body = self.get("servers/%s/ips/%s" % |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 192 | (str(server_id), network_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 193 | body = json.loads(body) |
| 194 | return resp, body |
| 195 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 196 | def action(self, server_id, action_name, response_key, **kwargs): |
| 197 | post_body = json.dumps({action_name: kwargs}) |
| 198 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 199 | post_body, self.headers) |
| 200 | if response_key is not None: |
| 201 | body = json.loads(body)[response_key] |
| 202 | return resp, body |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 203 | |
ivan-zhu | ccc8946 | 2013-10-31 16:53:12 +0800 | [diff] [blame] | 204 | def create_backup(self, server_id, backup_type, rotation, name): |
| 205 | """Backup a server instance.""" |
| 206 | return self.action(server_id, "createBackup", None, |
| 207 | backup_type=backup_type, |
| 208 | rotation=rotation, |
| 209 | name=name) |
| 210 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 211 | def change_password(self, server_id, adminPass): |
| 212 | """Changes the root password for the server.""" |
| 213 | return self.action(server_id, 'changePassword', None, |
| 214 | adminPass=adminPass) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 215 | |
ivan-zhu | 6de5b04 | 2013-10-24 17:21:48 +0800 | [diff] [blame] | 216 | def get_password(self, server_id): |
| 217 | resp, body = self.get("servers/%s/os-server-password" % |
| 218 | str(server_id)) |
| 219 | body = json.loads(body) |
| 220 | return resp, body |
| 221 | |
| 222 | def delete_password(self, server_id): |
| 223 | """ |
| 224 | Removes the encrypted server password from the metadata server |
| 225 | Note that this does not actually change the instance server |
| 226 | password. |
| 227 | """ |
| 228 | return self.delete("servers/%s/os-server-password" % |
| 229 | str(server_id)) |
| 230 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 231 | def reboot(self, server_id, reboot_type): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 232 | """Reboots a server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 233 | return self.action(server_id, 'reboot', None, type=reboot_type) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 234 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 235 | def rebuild(self, server_id, image_ref, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 236 | """Rebuilds a server with a new image.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 237 | kwargs['imageRef'] = image_ref |
| 238 | if 'disk_config' in kwargs: |
| 239 | kwargs['OS-DCF:diskConfig'] = kwargs['disk_config'] |
| 240 | del kwargs['disk_config'] |
| 241 | return self.action(server_id, 'rebuild', 'server', **kwargs) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 242 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 243 | def resize(self, server_id, flavor_ref, **kwargs): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 244 | """Changes the flavor of a server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 245 | kwargs['flavorRef'] = flavor_ref |
| 246 | if 'disk_config' in kwargs: |
| 247 | kwargs['OS-DCF:diskConfig'] = kwargs['disk_config'] |
| 248 | del kwargs['disk_config'] |
| 249 | return self.action(server_id, 'resize', None, **kwargs) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 250 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 251 | def confirm_resize(self, server_id, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 252 | """Confirms the flavor change for a server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 253 | return self.action(server_id, 'confirmResize', None, **kwargs) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 254 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 255 | def revert_resize(self, server_id, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 256 | """Reverts a server back to its original flavor.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 257 | return self.action(server_id, 'revertResize', None, **kwargs) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 258 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 259 | def create_image(self, server_id, name): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 260 | """Creates an image of the given server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 261 | return self.action(server_id, 'createImage', None, name=name) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 262 | |
| 263 | def list_server_metadata(self, server_id): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 264 | resp, body = self.get("servers/%s/metadata" % str(server_id)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 265 | body = json.loads(body) |
| 266 | return resp, body['metadata'] |
| 267 | |
Chris Yeoh | d0b52e7 | 2013-09-17 22:38:59 +0930 | [diff] [blame] | 268 | def set_server_metadata(self, server_id, meta, no_metadata_field=False): |
| 269 | if no_metadata_field: |
| 270 | post_body = "" |
| 271 | else: |
| 272 | post_body = json.dumps({'metadata': meta}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 273 | resp, body = self.put('servers/%s/metadata' % str(server_id), |
| 274 | post_body, self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 275 | body = json.loads(body) |
| 276 | return resp, body['metadata'] |
| 277 | |
| 278 | def update_server_metadata(self, server_id, meta): |
| 279 | post_body = json.dumps({'metadata': meta}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 280 | resp, body = self.post('servers/%s/metadata' % str(server_id), |
| 281 | post_body, self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 282 | body = json.loads(body) |
| 283 | return resp, body['metadata'] |
| 284 | |
| 285 | def get_server_metadata_item(self, server_id, key): |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 286 | resp, body = self.get("servers/%s/metadata/%s" % (str(server_id), key)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 287 | body = json.loads(body) |
| 288 | return resp, body['meta'] |
| 289 | |
| 290 | def set_server_metadata_item(self, server_id, key, meta): |
| 291 | post_body = json.dumps({'meta': meta}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 292 | resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key), |
| 293 | post_body, self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 294 | body = json.loads(body) |
| 295 | return resp, body['meta'] |
| 296 | |
| 297 | def delete_server_metadata_item(self, server_id, key): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 298 | resp, body = self.delete("servers/%s/metadata/%s" % |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 299 | (str(server_id), key)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 300 | return resp, body |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 301 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 302 | def stop(self, server_id, **kwargs): |
| 303 | return self.action(server_id, 'os-stop', None, **kwargs) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 304 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 305 | def start(self, server_id, **kwargs): |
| 306 | return self.action(server_id, 'os-start', None, **kwargs) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 307 | |
| 308 | def attach_volume(self, server_id, volume_id, device='/dev/vdz'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 309 | """Attaches a volume to a server instance.""" |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 310 | post_body = json.dumps({ |
| 311 | 'volumeAttachment': { |
| 312 | 'volumeId': volume_id, |
| 313 | 'device': device, |
| 314 | } |
| 315 | }) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 316 | resp, body = self.post('servers/%s/os-volume_attachments' % server_id, |
| 317 | post_body, self.headers) |
| 318 | return resp, body |
| 319 | |
| 320 | def detach_volume(self, server_id, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 321 | """Detaches a volume from a server instance.""" |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 322 | resp, body = self.delete('servers/%s/os-volume_attachments/%s' % |
| 323 | (server_id, volume_id)) |
| 324 | return resp, body |
sapan-kona | 775cf63 | 2012-06-22 00:32:36 +0530 | [diff] [blame] | 325 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 326 | def add_security_group(self, server_id, name): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 327 | """Adds a security group to the server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 328 | return self.action(server_id, 'addSecurityGroup', None, name=name) |
sapan-kona | 775cf63 | 2012-06-22 00:32:36 +0530 | [diff] [blame] | 329 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 330 | def remove_security_group(self, server_id, name): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 331 | """Removes a security group from the server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 332 | return self.action(server_id, 'removeSecurityGroup', None, name=name) |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 333 | |
| 334 | def live_migrate_server(self, server_id, dest_host, use_block_migration): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 335 | """This should be called with administrator privileges .""" |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 336 | |
| 337 | migrate_params = { |
| 338 | "disk_over_commit": False, |
| 339 | "block_migration": use_block_migration, |
| 340 | "host": dest_host |
| 341 | } |
| 342 | |
| 343 | req_body = json.dumps({'os-migrateLive': migrate_params}) |
| 344 | |
| 345 | resp, body = self.post("servers/%s/action" % str(server_id), |
| 346 | req_body, self.headers) |
| 347 | return resp, body |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 348 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 349 | def migrate_server(self, server_id, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 350 | """Migrates a server to a new host.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 351 | return self.action(server_id, 'migrate', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 352 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 353 | def lock_server(self, server_id, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 354 | """Locks the given server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 355 | return self.action(server_id, 'lock', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 356 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 357 | def unlock_server(self, server_id, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 358 | """UNlocks the given server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 359 | return self.action(server_id, 'unlock', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 360 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 361 | def suspend_server(self, server_id, **kwargs): |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 362 | """Suspends the provided server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 363 | return self.action(server_id, 'suspend', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 364 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 365 | def resume_server(self, server_id, **kwargs): |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 366 | """Un-suspends the provided server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 367 | return self.action(server_id, 'resume', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 368 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 369 | def pause_server(self, server_id, **kwargs): |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 370 | """Pauses the provided server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 371 | return self.action(server_id, 'pause', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 372 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 373 | def unpause_server(self, server_id, **kwargs): |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 374 | """Un-pauses the provided server.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 375 | return self.action(server_id, 'unpause', None, **kwargs) |
Daryl Walleck | 3d3f610 | 2012-11-09 15:16:42 -0600 | [diff] [blame] | 376 | |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 377 | def reset_state(self, server_id, state='error'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 378 | """Resets the state of a server to active/error.""" |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 379 | return self.action(server_id, 'os-resetState', None, state=state) |
Attila Fazekas | e4cb04c | 2013-01-29 09:51:58 +0100 | [diff] [blame] | 380 | |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 381 | def shelve_server(self, server_id, **kwargs): |
| 382 | """Shelves the provided server.""" |
| 383 | return self.action(server_id, 'shelve', None, **kwargs) |
| 384 | |
| 385 | def unshelve_server(self, server_id, **kwargs): |
| 386 | """Un-shelves the provided server.""" |
| 387 | return self.action(server_id, 'unshelve', None, **kwargs) |
| 388 | |
Attila Fazekas | e4cb04c | 2013-01-29 09:51:58 +0100 | [diff] [blame] | 389 | def get_console_output(self, server_id, length): |
Attila Fazekas | d429928 | 2013-02-22 13:25:23 +0100 | [diff] [blame] | 390 | return self.action(server_id, 'os-getConsoleOutput', 'output', |
| 391 | length=length) |
Rami Vaknin | 76bc8bd | 2013-02-17 16:18:27 +0200 | [diff] [blame] | 392 | |
| 393 | def list_virtual_interfaces(self, server_id): |
| 394 | """ |
| 395 | List the virtual interfaces used in an instance. |
| 396 | """ |
| 397 | resp, body = self.get('/'.join(['servers', server_id, |
| 398 | 'os-virtual-interfaces'])) |
| 399 | return resp, json.loads(body) |
nithya-ganesan | e6a36c8 | 2013-02-15 14:38:27 +0000 | [diff] [blame] | 400 | |
Yuiko Takada | 7835d50 | 2014-01-15 10:15:03 +0000 | [diff] [blame] | 401 | def rescue_server(self, server_id, **kwargs): |
nithya-ganesan | e6a36c8 | 2013-02-15 14:38:27 +0000 | [diff] [blame] | 402 | """Rescue the provided server.""" |
Yuiko Takada | 7835d50 | 2014-01-15 10:15:03 +0000 | [diff] [blame] | 403 | return self.action(server_id, 'rescue', None, **kwargs) |
nithya-ganesan | e6a36c8 | 2013-02-15 14:38:27 +0000 | [diff] [blame] | 404 | |
| 405 | def unrescue_server(self, server_id): |
| 406 | """Unrescue the provided server.""" |
| 407 | return self.action(server_id, 'unrescue', None) |
Leo Toyoda | faca6ff | 2013-04-22 16:52:53 +0900 | [diff] [blame] | 408 | |
Zhu Zhu | da07085 | 2013-09-25 08:07:57 -0500 | [diff] [blame] | 409 | def get_server_diagnostics(self, server_id): |
| 410 | """Get the usage data for a server.""" |
| 411 | resp, body = self.get("servers/%s/diagnostics" % str(server_id)) |
| 412 | return resp, json.loads(body) |
| 413 | |
Leo Toyoda | faca6ff | 2013-04-22 16:52:53 +0900 | [diff] [blame] | 414 | def list_instance_actions(self, server_id): |
| 415 | """List the provided server action.""" |
| 416 | resp, body = self.get("servers/%s/os-instance-actions" % |
| 417 | str(server_id)) |
| 418 | body = json.loads(body) |
| 419 | return resp, body['instanceActions'] |
| 420 | |
| 421 | def get_instance_action(self, server_id, request_id): |
| 422 | """Returns the action details of the provided server.""" |
| 423 | resp, body = self.get("servers/%s/os-instance-actions/%s" % |
| 424 | (str(server_id), str(request_id))) |
| 425 | body = json.loads(body) |
| 426 | return resp, body['instanceAction'] |
Lingxian Kong | aecc109 | 2013-10-03 16:18:46 +0800 | [diff] [blame] | 427 | |
| 428 | def force_delete_server(self, server_id, **kwargs): |
| 429 | """Force delete a server.""" |
| 430 | return self.action(server_id, 'forceDelete', None, **kwargs) |
| 431 | |
| 432 | def restore_soft_deleted_server(self, server_id, **kwargs): |
| 433 | """Restore a soft-deleted server.""" |
| 434 | return self.action(server_id, 'restore', None, **kwargs) |