Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
| 13 | from tempest.services.baremetal import base |
| 14 | |
| 15 | |
| 16 | class BaremetalClientV1(base.BaremetalClient): |
| 17 | """ |
| 18 | Base Tempest REST client for Ironic API v1. |
| 19 | |
| 20 | Specific implementations must implement serialize and deserialize |
| 21 | methods in order to send requests to Ironic. |
| 22 | |
| 23 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 24 | def __init__(self, auth_provider): |
| 25 | super(BaremetalClientV1, self).__init__(auth_provider) |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 26 | self.version = '1' |
| 27 | self.uri_prefix = 'v%s' % self.version |
| 28 | |
| 29 | @base.handle_errors |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 30 | def list_nodes(self, **kwargs): |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 31 | """List all existing nodes.""" |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 32 | return self._list_request('nodes', **kwargs) |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 33 | |
| 34 | @base.handle_errors |
| 35 | def list_chassis(self): |
| 36 | """List all existing chassis.""" |
| 37 | return self._list_request('chassis') |
| 38 | |
| 39 | @base.handle_errors |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 40 | def list_chassis_nodes(self, chassis_uuid): |
| 41 | """List all nodes associated with a chassis.""" |
| 42 | return self._list_request('/chassis/%s/nodes' % chassis_uuid) |
| 43 | |
| 44 | @base.handle_errors |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 45 | def list_ports(self, **kwargs): |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 46 | """List all existing ports.""" |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 47 | return self._list_request('ports', **kwargs) |
| 48 | |
| 49 | @base.handle_errors |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 50 | def list_node_ports(self, uuid): |
| 51 | """List all ports associated with the node.""" |
| 52 | return self._list_request('/nodes/%s/ports' % uuid) |
| 53 | |
| 54 | @base.handle_errors |
Mh Raies | fbe5451 | 2014-04-08 12:25:15 +0530 | [diff] [blame] | 55 | def list_nodestates(self, uuid): |
| 56 | """List all existing states.""" |
| 57 | return self._list_request('/nodes/%s/states' % uuid) |
| 58 | |
| 59 | @base.handle_errors |
Yuiko Takada | 3083fca | 2014-04-25 11:52:33 +0000 | [diff] [blame] | 60 | def list_ports_detail(self, **kwargs): |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 61 | """Details list all existing ports.""" |
Yuiko Takada | 3083fca | 2014-04-25 11:52:33 +0000 | [diff] [blame] | 62 | return self._list_request('/ports/detail', **kwargs) |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 63 | |
| 64 | @base.handle_errors |
Mh Raies | b71cb7f | 2014-03-28 10:51:31 +0530 | [diff] [blame] | 65 | def list_drivers(self): |
| 66 | """List all existing drivers.""" |
| 67 | return self._list_request('drivers') |
| 68 | |
| 69 | @base.handle_errors |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 70 | def show_node(self, uuid): |
| 71 | """ |
| 72 | Gets a specific node. |
| 73 | |
| 74 | :param uuid: Unique identifier of the node in UUID format. |
| 75 | :return: Serialized node as a dictionary. |
| 76 | |
| 77 | """ |
| 78 | return self._show_request('nodes', uuid) |
| 79 | |
| 80 | @base.handle_errors |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 81 | def show_node_by_instance_uuid(self, instance_uuid): |
| 82 | """ |
| 83 | Gets a node associated with given instance uuid. |
| 84 | |
| 85 | :param uuid: Unique identifier of the node in UUID format. |
| 86 | :return: Serialized node as a dictionary. |
| 87 | |
| 88 | """ |
| 89 | uri = '/nodes/detail?instance_uuid=%s' % instance_uuid |
| 90 | |
| 91 | return self._show_request('nodes', |
| 92 | uuid=None, |
| 93 | uri=uri) |
| 94 | |
| 95 | @base.handle_errors |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 96 | def show_chassis(self, uuid): |
| 97 | """ |
| 98 | Gets a specific chassis. |
| 99 | |
| 100 | :param uuid: Unique identifier of the chassis in UUID format. |
| 101 | :return: Serialized chassis as a dictionary. |
| 102 | |
| 103 | """ |
| 104 | return self._show_request('chassis', uuid) |
| 105 | |
| 106 | @base.handle_errors |
| 107 | def show_port(self, uuid): |
| 108 | """ |
| 109 | Gets a specific port. |
| 110 | |
| 111 | :param uuid: Unique identifier of the port in UUID format. |
| 112 | :return: Serialized port as a dictionary. |
| 113 | |
| 114 | """ |
| 115 | return self._show_request('ports', uuid) |
| 116 | |
Yuiko Takada | 8e2dfca | 2014-04-24 18:10:52 +0000 | [diff] [blame] | 117 | def show_driver(self, driver_name): |
| 118 | """ |
| 119 | Gets a specific driver. |
| 120 | |
| 121 | :param driver_name: Name of driver. |
| 122 | :return: Serialized driver as a dictionary. |
| 123 | """ |
| 124 | return self._show_request('drivers', driver_name) |
| 125 | |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 126 | @base.handle_errors |
| 127 | def create_node(self, chassis_id, **kwargs): |
| 128 | """ |
| 129 | Create a baremetal node with the specified parameters. |
| 130 | |
| 131 | :param cpu_arch: CPU architecture of the node. Default: x86_64. |
| 132 | :param cpu_num: Number of CPUs. Default: 8. |
| 133 | :param storage: Disk size. Default: 1024. |
| 134 | :param memory: Available RAM. Default: 4096. |
| 135 | :param driver: Driver name. Default: "fake" |
| 136 | :return: A tuple with the server response and the created node. |
| 137 | |
| 138 | """ |
| 139 | node = {'chassis_uuid': chassis_id, |
| 140 | 'properties': {'cpu_arch': kwargs.get('cpu_arch', 'x86_64'), |
| 141 | 'cpu_num': kwargs.get('cpu_num', 8), |
| 142 | 'storage': kwargs.get('storage', 1024), |
| 143 | 'memory': kwargs.get('memory', 4096)}, |
| 144 | 'driver': kwargs.get('driver', 'fake')} |
| 145 | |
| 146 | return self._create_request('nodes', 'node', node) |
| 147 | |
| 148 | @base.handle_errors |
| 149 | def create_chassis(self, **kwargs): |
| 150 | """ |
| 151 | Create a chassis with the specified parameters. |
| 152 | |
| 153 | :param description: The description of the chassis. |
| 154 | Default: test-chassis |
| 155 | :return: A tuple with the server response and the created chassis. |
| 156 | |
| 157 | """ |
| 158 | chassis = {'description': kwargs.get('description', 'test-chassis')} |
| 159 | |
| 160 | return self._create_request('chassis', 'chassis', chassis) |
| 161 | |
| 162 | @base.handle_errors |
| 163 | def create_port(self, node_id, **kwargs): |
| 164 | """ |
| 165 | Create a port with the specified parameters. |
| 166 | |
| 167 | :param node_id: The ID of the node which owns the port. |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 168 | :param address: MAC address of the port. |
| 169 | :param extra: Meta data of the port. Default: {'foo': 'bar'}. |
| 170 | :param uuid: UUID of the port. |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 171 | :return: A tuple with the server response and the created port. |
| 172 | |
| 173 | """ |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 174 | port = {'extra': kwargs.get('extra', {'foo': 'bar'}), |
| 175 | 'uuid': kwargs['uuid']} |
| 176 | |
| 177 | if node_id is not None: |
| 178 | port['node_uuid'] = node_id |
| 179 | |
| 180 | if kwargs['address'] is not None: |
| 181 | port['address'] = kwargs['address'] |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 182 | |
| 183 | return self._create_request('ports', 'port', port) |
| 184 | |
| 185 | @base.handle_errors |
| 186 | def delete_node(self, uuid): |
| 187 | """ |
| 188 | Deletes a node having the specified UUID. |
| 189 | |
| 190 | :param uuid: The unique identifier of the node. |
| 191 | :return: A tuple with the server response and the response body. |
| 192 | |
| 193 | """ |
| 194 | return self._delete_request('nodes', uuid) |
| 195 | |
| 196 | @base.handle_errors |
| 197 | def delete_chassis(self, uuid): |
| 198 | """ |
| 199 | Deletes a chassis having the specified UUID. |
| 200 | |
| 201 | :param uuid: The unique identifier of the chassis. |
| 202 | :return: A tuple with the server response and the response body. |
| 203 | |
| 204 | """ |
| 205 | return self._delete_request('chassis', uuid) |
| 206 | |
| 207 | @base.handle_errors |
| 208 | def delete_port(self, uuid): |
| 209 | """ |
| 210 | Deletes a port having the specified UUID. |
| 211 | |
| 212 | :param uuid: The unique identifier of the port. |
| 213 | :return: A tuple with the server response and the response body. |
| 214 | |
| 215 | """ |
| 216 | return self._delete_request('ports', uuid) |
| 217 | |
| 218 | @base.handle_errors |
| 219 | def update_node(self, uuid, **kwargs): |
| 220 | """ |
| 221 | Update the specified node. |
| 222 | |
| 223 | :param uuid: The unique identifier of the node. |
| 224 | :return: A tuple with the server response and the updated node. |
| 225 | |
| 226 | """ |
| 227 | node_attributes = ('properties/cpu_arch', |
| 228 | 'properties/cpu_num', |
| 229 | 'properties/storage', |
| 230 | 'properties/memory', |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 231 | 'driver', |
| 232 | 'instance_uuid') |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 233 | |
| 234 | patch = self._make_patch(node_attributes, **kwargs) |
| 235 | |
| 236 | return self._patch_request('nodes', uuid, patch) |
| 237 | |
| 238 | @base.handle_errors |
| 239 | def update_chassis(self, uuid, **kwargs): |
| 240 | """ |
| 241 | Update the specified chassis. |
| 242 | |
| 243 | :param uuid: The unique identifier of the chassis. |
| 244 | :return: A tuple with the server response and the updated chassis. |
| 245 | |
| 246 | """ |
| 247 | chassis_attributes = ('description',) |
| 248 | patch = self._make_patch(chassis_attributes, **kwargs) |
| 249 | |
| 250 | return self._patch_request('chassis', uuid, patch) |
| 251 | |
| 252 | @base.handle_errors |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 253 | def update_port(self, uuid, patch): |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 254 | """ |
| 255 | Update the specified port. |
| 256 | |
| 257 | :param uuid: The unique identifier of the port. |
Sergey Nikitin | 0d43eb5 | 2014-02-03 14:50:02 +0400 | [diff] [blame] | 258 | :param patch: List of dicts representing json patches. |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 259 | :return: A tuple with the server response and the updated port. |
| 260 | |
| 261 | """ |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 262 | |
| 263 | return self._patch_request('ports', uuid, patch) |
Mh Raies | f8ecf23 | 2014-04-17 12:43:55 +0530 | [diff] [blame] | 264 | |
| 265 | @base.handle_errors |
| 266 | def set_node_power_state(self, node_uuid, state): |
| 267 | """ |
| 268 | Set power state of the specified node. |
| 269 | |
| 270 | :param node_uuid: The unique identifier of the node. |
| 271 | :state: desired state to set (on/off/reboot). |
| 272 | |
| 273 | """ |
| 274 | target = {'target': state} |
| 275 | return self._put_request('nodes/%s/states/power' % node_uuid, |
| 276 | target) |
raiesmh08 | e5d8457 | 2014-06-23 09:49:03 +0530 | [diff] [blame] | 277 | |
| 278 | @base.handle_errors |
| 279 | def validate_driver_interface(self, node_uuid): |
| 280 | """ |
| 281 | Get all driver interfaces of a specific node. |
| 282 | |
| 283 | :param uuid: Unique identifier of the node in UUID format. |
| 284 | |
| 285 | """ |
| 286 | |
| 287 | uri = '{pref}/{res}/{uuid}/{postf}'.format(pref=self.uri_prefix, |
| 288 | res='nodes', |
| 289 | uuid=node_uuid, |
| 290 | postf='validate') |
| 291 | |
| 292 | return self._show_request('nodes', node_uuid, uri=uri) |
Lucas Alvares Gomes | 5d236cf | 2014-08-11 15:23:12 +0100 | [diff] [blame] | 293 | |
| 294 | @base.handle_errors |
| 295 | def set_node_boot_device(self, node_uuid, boot_device, persistent=False): |
| 296 | """ |
| 297 | Set the boot device of the specified node. |
| 298 | |
| 299 | :param node_uuid: The unique identifier of the node. |
| 300 | :param boot_device: The boot device name. |
| 301 | :param persistent: Boolean value. True if the boot device will |
| 302 | persist to all future boots, False if not. |
| 303 | Default: False. |
| 304 | |
| 305 | """ |
| 306 | request = {'boot_device': boot_device, 'persistent': persistent} |
| 307 | resp, body = self._put_request('nodes/%s/management/boot_device' % |
| 308 | node_uuid, request) |
| 309 | self.expected_success(204, resp.status) |
| 310 | return body |
| 311 | |
| 312 | @base.handle_errors |
| 313 | def get_node_boot_device(self, node_uuid): |
| 314 | """ |
| 315 | Get the current boot device of the specified node. |
| 316 | |
| 317 | :param node_uuid: The unique identifier of the node. |
| 318 | |
| 319 | """ |
| 320 | path = 'nodes/%s/management/boot_device' % node_uuid |
| 321 | resp, body = self._list_request(path) |
| 322 | self.expected_success(200, resp.status) |
| 323 | return body |
| 324 | |
| 325 | @base.handle_errors |
| 326 | def get_node_supported_boot_devices(self, node_uuid): |
| 327 | """ |
| 328 | Get the supported boot devices of the specified node. |
| 329 | |
| 330 | :param node_uuid: The unique identifier of the node. |
| 331 | |
| 332 | """ |
| 333 | path = 'nodes/%s/management/boot_device/supported' % node_uuid |
| 334 | resp, body = self._list_request(path) |
| 335 | self.expected_success(200, resp.status) |
| 336 | return body |
Yuiko Takada | bbf5cff | 2014-08-29 17:09:06 +0900 | [diff] [blame] | 337 | |
| 338 | @base.handle_errors |
| 339 | def get_console(self, node_uuid): |
| 340 | """ |
| 341 | Get connection information about the console. |
| 342 | |
| 343 | :param node_uuid: Unique identifier of the node in UUID format. |
| 344 | |
| 345 | """ |
| 346 | |
| 347 | resp, body = self._show_request('nodes/states/console', node_uuid) |
| 348 | self.expected_success(200, resp.status) |
| 349 | return resp, body |
| 350 | |
| 351 | @base.handle_errors |
| 352 | def set_console_mode(self, node_uuid, enabled): |
| 353 | """ |
| 354 | Start and stop the node console. |
| 355 | |
| 356 | :param node_uuid: Unique identifier of the node in UUID format. |
| 357 | :param enabled: Boolean value; whether to enable or disable the |
| 358 | console. |
| 359 | |
| 360 | """ |
| 361 | |
| 362 | enabled = {'enabled': enabled} |
| 363 | resp, body = self._put_request('nodes/%s/states/console' % node_uuid, |
| 364 | enabled) |
| 365 | self.expected_success(202, resp.status) |
| 366 | return resp, body |