| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | ## |
| 5 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 19 | ## |
| 20 | |
| 21 | """ |
| 22 | asyncio RO python client to interact with RO-server |
| 23 | """ |
| 24 | |
| 25 | import asyncio |
| 26 | import aiohttp |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 27 | import json |
| 28 | import yaml |
| 29 | import logging |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 30 | from urllib.parse import quote |
| 31 | from uuid import UUID |
| 32 | from copy import deepcopy |
| 33 | |
| tierno | 275411e | 2018-05-16 14:33:32 +0200 | [diff] [blame] | 34 | __author__ = "Alfonso Tierno" |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 35 | __date__ = "$09-Jan-2018 09:09:48$" |
| tierno | 275411e | 2018-05-16 14:33:32 +0200 | [diff] [blame] | 36 | __version__ = "0.1.2" |
| 37 | version_date = "2018-05-16" |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 38 | requests = None |
| 39 | |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 40 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 41 | class ROClientException(Exception): |
| 42 | def __init__(self, message, http_code=400): |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 43 | """Common Exception for all RO client exceptions""" |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 44 | self.http_code = http_code |
| 45 | Exception.__init__(self, message) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def remove_envelop(item, indata=None): |
| 49 | """ |
| 50 | Obtain the useful data removing the envelop. It goes through the vnfd or nsd catalog and returns the |
| 51 | vnfd or nsd content |
| 52 | :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns' |
| 53 | :param indata: Content to be inspected |
| 54 | :return: the useful part of indata (a reference, not a new dictionay) |
| 55 | """ |
| 56 | clean_indata = indata |
| 57 | if not indata: |
| 58 | return {} |
| 59 | if item == "vnfd": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 60 | if clean_indata.get("vnfd:vnfd-catalog"): |
| 61 | clean_indata = clean_indata["vnfd:vnfd-catalog"] |
| 62 | elif clean_indata.get("vnfd-catalog"): |
| 63 | clean_indata = clean_indata["vnfd-catalog"] |
| 64 | if clean_indata.get("vnfd"): |
| 65 | if ( |
| 66 | not isinstance(clean_indata["vnfd"], list) |
| 67 | or len(clean_indata["vnfd"]) != 1 |
| 68 | ): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 69 | raise ROClientException("'vnfd' must be a list only one element") |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 70 | clean_indata = clean_indata["vnfd"][0] |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 71 | elif item == "nsd": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 72 | if clean_indata.get("nsd:nsd-catalog"): |
| 73 | clean_indata = clean_indata["nsd:nsd-catalog"] |
| 74 | elif clean_indata.get("nsd-catalog"): |
| 75 | clean_indata = clean_indata["nsd-catalog"] |
| 76 | if clean_indata.get("nsd"): |
| 77 | if ( |
| 78 | not isinstance(clean_indata["nsd"], list) |
| 79 | or len(clean_indata["nsd"]) != 1 |
| 80 | ): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 81 | raise ROClientException("'nsd' must be a list only one element") |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 82 | clean_indata = clean_indata["nsd"][0] |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 83 | elif item == "sdn": |
| 84 | if len(indata) == 1 and "sdn_controller" in indata: |
| 85 | clean_indata = indata["sdn_controller"] |
| 86 | elif item == "tenant": |
| 87 | if len(indata) == 1 and "tenant" in indata: |
| 88 | clean_indata = indata["tenant"] |
| 89 | elif item in ("vim", "vim_account", "datacenters"): |
| 90 | if len(indata) == 1 and "datacenter" in indata: |
| 91 | clean_indata = indata["datacenter"] |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 92 | elif item == "wim": |
| 93 | if len(indata) == 1 and "wim" in indata: |
| 94 | clean_indata = indata["wim"] |
| 95 | elif item == "wim_account": |
| 96 | if len(indata) == 1 and "wim_account" in indata: |
| 97 | clean_indata = indata["wim_account"] |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 98 | elif item == "ns" or item == "instances": |
| 99 | if len(indata) == 1 and "instance" in indata: |
| 100 | clean_indata = indata["instance"] |
| 101 | else: |
| Gabriel Cuba | 4c0e680 | 2023-10-09 13:22:38 -0500 | [diff] [blame] | 102 | raise ROClientException("remove_envelop with unknown item {}".format(item)) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 103 | |
| 104 | return clean_indata |
| 105 | |
| 106 | |
| 107 | class ROClient: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 108 | headers_req = {"Accept": "application/yaml", "content-type": "application/yaml"} |
| 109 | client_to_RO = { |
| 110 | "tenant": "tenants", |
| 111 | "vim": "datacenters", |
| 112 | "vim_account": "datacenters", |
| 113 | "sdn": "sdn_controllers", |
| 114 | "vnfd": "vnfs", |
| 115 | "nsd": "scenarios", |
| 116 | "wim": "wims", |
| 117 | "wim_account": "wims", |
| 118 | "ns": "instances", |
| 119 | } |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 120 | mandatory_for_create = { |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 121 | "tenant": ("name",), |
| 122 | "vnfd": ("name", "id"), |
| 123 | "nsd": ("name", "id"), |
| 124 | "ns": ("name", "scenario", "datacenter"), |
| 125 | "vim": ("name", "vim_url"), |
| 126 | "wim": ("name", "wim_url"), |
| 127 | "vim_account": (), |
| 128 | "wim_account": (), |
| 129 | "sdn": ("name", "type"), |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 130 | } |
| 131 | timeout_large = 120 |
| 132 | timeout_short = 30 |
| 133 | |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 134 | def __init__(self, uri, **kwargs): |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 135 | self.uri = uri |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 136 | |
| 137 | self.username = kwargs.get("username") |
| 138 | self.password = kwargs.get("password") |
| 139 | self.tenant_id_name = kwargs.get("tenant") |
| 140 | self.tenant = None |
| 141 | self.datacenter_id_name = kwargs.get("datacenter") |
| 142 | self.datacenter = None |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 143 | logger_name = kwargs.get("logger_name", "lcm.ro") |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 144 | self.logger = logging.getLogger(logger_name) |
| 145 | if kwargs.get("loglevel"): |
| 146 | self.logger.setLevel(kwargs["loglevel"]) |
| 147 | global requests |
| 148 | requests = kwargs.get("TODO remove") |
| 149 | |
| 150 | def __getitem__(self, index): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 151 | if index == "tenant": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 152 | return self.tenant_id_name |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 153 | elif index == "datacenter": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 154 | return self.datacenter_id_name |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 155 | elif index == "username": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 156 | return self.username |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 157 | elif index == "password": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 158 | return self.password |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 159 | elif index == "uri": |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 160 | return self.uri |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 161 | else: |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 162 | raise KeyError("Invalid key '{}'".format(index)) |
| endika | c295040 | 2020-09-14 11:20:00 +0200 | [diff] [blame] | 163 | |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 164 | def __setitem__(self, index, value): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 165 | if index == "tenant": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 166 | self.tenant_id_name = value |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 167 | elif index == "datacenter" or index == "vim": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 168 | self.datacenter_id_name = value |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 169 | elif index == "username": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 170 | self.username = value |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 171 | elif index == "password": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 172 | self.password = value |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 173 | elif index == "uri": |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 174 | self.uri = value |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 175 | else: |
| 176 | raise KeyError("Invalid key '{}'".format(index)) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 177 | self.tenant = None # force to reload tenant with different credentials |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 178 | self.datacenter = None # force to reload datacenter with different credentials |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 179 | |
| tierno | b520391 | 2020-08-11 11:20:13 +0000 | [diff] [blame] | 180 | @staticmethod |
| 181 | def _parse(descriptor, descriptor_format, response=False): |
| Gabriel Cuba | d089a16 | 2024-03-19 18:01:13 -0500 | [diff] [blame] | 182 | error_text = "" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 183 | if ( |
| 184 | descriptor_format |
| 185 | and descriptor_format != "json" |
| 186 | and descriptor_format != "yaml" |
| 187 | ): |
| 188 | raise ROClientException( |
| 189 | "'descriptor_format' must be a 'json' or 'yaml' text" |
| 190 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 191 | if descriptor_format != "json": |
| 192 | try: |
| Luis | ccdc216 | 2022-07-01 14:35:49 +0000 | [diff] [blame] | 193 | return yaml.safe_load(descriptor) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 194 | except yaml.YAMLError as exc: |
| 195 | error_pos = "" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 196 | if hasattr(exc, "problem_mark"): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 197 | mark = exc.problem_mark |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 198 | error_pos = " at line:{} column:{}s".format( |
| 199 | mark.line + 1, mark.column + 1 |
| 200 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 201 | error_text = "yaml format error" + error_pos |
| 202 | elif descriptor_format != "yaml": |
| 203 | try: |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 204 | return json.loads(descriptor) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 205 | except Exception as e: |
| 206 | if response: |
| 207 | error_text = "json format error" + str(e) |
| 208 | |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 209 | raise ROClientException(error_text) |
| tierno | b520391 | 2020-08-11 11:20:13 +0000 | [diff] [blame] | 210 | |
| 211 | @staticmethod |
| 212 | def _parse_error_yaml(descriptor): |
| 213 | json_error = None |
| 214 | try: |
| Luis | ccdc216 | 2022-07-01 14:35:49 +0000 | [diff] [blame] | 215 | json_error = yaml.safe_load(descriptor) |
| tierno | b520391 | 2020-08-11 11:20:13 +0000 | [diff] [blame] | 216 | return json_error["error"]["description"] |
| 217 | except Exception: |
| 218 | return str(json_error or descriptor) |
| 219 | |
| 220 | @staticmethod |
| 221 | def _parse_yaml(descriptor, response=False): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 222 | try: |
| Luis | ccdc216 | 2022-07-01 14:35:49 +0000 | [diff] [blame] | 223 | return yaml.safe_load(descriptor) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 224 | except yaml.YAMLError as exc: |
| 225 | error_pos = "" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 226 | if hasattr(exc, "problem_mark"): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 227 | mark = exc.problem_mark |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 228 | error_pos = " at line:{} column:{}s".format( |
| 229 | mark.line + 1, mark.column + 1 |
| 230 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 231 | error_text = "yaml format error" + error_pos |
| 232 | if response: |
| 233 | raise ROClientException(error_text) |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 234 | raise ROClientException(error_text) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 235 | |
| 236 | @staticmethod |
| 237 | def check_if_uuid(uuid_text): |
| 238 | """ |
| 239 | Check if text correspond to an uuid foramt |
| 240 | :param uuid_text: |
| 241 | :return: True if it is an uuid False if not |
| 242 | """ |
| 243 | try: |
| 244 | UUID(uuid_text) |
| 245 | return True |
| tierno | 9876813 | 2018-09-11 12:07:21 +0200 | [diff] [blame] | 246 | except Exception: |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 247 | return False |
| 248 | |
| 249 | @staticmethod |
| 250 | def _create_envelop(item, indata=None): |
| 251 | """ |
| 252 | Returns a new dict that incledes indata with the expected envelop |
| 253 | :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns' |
| 254 | :param indata: Content to be enveloped |
| 255 | :return: a new dic with {<envelop>: {indata} } where envelop can be e.g. tenant, datacenter, ... |
| 256 | """ |
| 257 | if item == "vnfd": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 258 | return {"vnfd-catalog": {"vnfd": [indata]}} |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 259 | elif item == "nsd": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 260 | return {"nsd-catalog": {"nsd": [indata]}} |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 261 | elif item == "tenant": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 262 | return {"tenant": indata} |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 263 | elif item in ("vim", "vim_account", "datacenter"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 264 | return {"datacenter": indata} |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 265 | elif item == "wim": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 266 | return {"wim": indata} |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 267 | elif item == "wim_account": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 268 | return {"wim_account": indata} |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 269 | elif item == "ns" or item == "instances": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 270 | return {"instance": indata} |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 271 | elif item == "sdn": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 272 | return {"sdn_controller": indata} |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 273 | else: |
| Gabriel Cuba | 4c0e680 | 2023-10-09 13:22:38 -0500 | [diff] [blame] | 274 | raise ROClientException("remove_envelop with unknown item {}".format(item)) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 275 | |
| 276 | @staticmethod |
| 277 | def update_descriptor(desc, kwargs): |
| 278 | desc = deepcopy(desc) # do not modify original descriptor |
| 279 | try: |
| 280 | for k, v in kwargs.items(): |
| 281 | update_content = desc |
| 282 | kitem_old = None |
| 283 | klist = k.split(".") |
| 284 | for kitem in klist: |
| 285 | if kitem_old is not None: |
| 286 | update_content = update_content[kitem_old] |
| 287 | if isinstance(update_content, dict): |
| 288 | kitem_old = kitem |
| 289 | elif isinstance(update_content, list): |
| 290 | kitem_old = int(kitem) |
| 291 | else: |
| 292 | raise ROClientException( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 293 | "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format( |
| 294 | k, kitem |
| 295 | ) |
| 296 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 297 | if v == "__DELETE__": |
| 298 | del update_content[kitem_old] |
| 299 | else: |
| 300 | update_content[kitem_old] = v |
| 301 | return desc |
| 302 | except KeyError: |
| 303 | raise ROClientException( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 304 | "Invalid query string '{}'. Descriptor does not contain '{}'".format( |
| 305 | k, kitem_old |
| 306 | ) |
| 307 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 308 | except ValueError: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 309 | raise ROClientException( |
| 310 | "Invalid query string '{}'. Expected integer index list instead of '{}'".format( |
| 311 | k, kitem |
| 312 | ) |
| 313 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 314 | except IndexError: |
| 315 | raise ROClientException( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 316 | "Invalid query string '{}'. Index '{}' out of range".format( |
| 317 | k, kitem_old |
| 318 | ) |
| 319 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 320 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 321 | async def _get_item_uuid(self, session, item, item_id_name, all_tenants=False): |
| 322 | if all_tenants: |
| 323 | tenant_text = "/any" |
| 324 | elif all_tenants is None: |
| 325 | tenant_text = "" |
| 326 | else: |
| 327 | if not self.tenant: |
| 328 | await self._get_tenant(session) |
| 329 | tenant_text = "/" + self.tenant |
| 330 | |
| 331 | item_id = 0 |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 332 | url = "{}{}/{}".format(self.uri, tenant_text, item) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 333 | if self.check_if_uuid(item_id_name): |
| 334 | item_id = item_id_name |
| 335 | url += "/" + item_id_name |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 336 | elif ( |
| 337 | item_id_name and item_id_name.startswith("'") and item_id_name.endswith("'") |
| 338 | ): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 339 | item_id_name = item_id_name[1:-1] |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 340 | self.logger.debug("RO GET %s", url) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 341 | # timeout = aiohttp.ClientTimeout(total=self.timeout_short) |
| 342 | async with session.get(url, headers=self.headers_req) as response: |
| 343 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 344 | self.logger.debug( |
| 345 | "GET {} [{}] {}".format(url, response.status, response_text[:100]) |
| 346 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 347 | if response.status == 404: # NOT_FOUND |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 348 | raise ROClientException( |
| 349 | "No {} found with id '{}'".format(item[:-1], item_id_name), |
| 350 | http_code=404, |
| 351 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 352 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 353 | raise ROClientException( |
| 354 | self._parse_error_yaml(response_text), http_code=response.status |
| 355 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 356 | content = self._parse_yaml(response_text, response=True) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 357 | |
| 358 | if item_id: |
| 359 | return item_id |
| 360 | desc = content[item] |
| Gabriel Cuba | 4c0e680 | 2023-10-09 13:22:38 -0500 | [diff] [blame] | 361 | if not isinstance(desc, list): |
| 362 | raise ROClientException( |
| 363 | "_get_item_uuid get a non dict with a list inside {}".format(type(desc)) |
| 364 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 365 | uuid = None |
| 366 | for i in desc: |
| 367 | if item_id_name and i["name"] != item_id_name: |
| 368 | continue |
| 369 | if uuid: # found more than one |
| 370 | raise ROClientException( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 371 | "Found more than one {} with name '{}'. uuid must be used".format( |
| 372 | item, item_id_name |
| 373 | ), |
| 374 | http_code=404, |
| 375 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 376 | uuid = i["uuid"] |
| 377 | if not uuid: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 378 | raise ROClientException( |
| 379 | "No {} found with name '{}'".format(item[:-1], item_id_name), |
| 380 | http_code=404, |
| 381 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 382 | return uuid |
| 383 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 384 | async def _get_tenant(self, session): |
| 385 | if not self.tenant: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 386 | self.tenant = await self._get_item_uuid( |
| 387 | session, "tenants", self.tenant_id_name, None |
| 388 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 389 | return self.tenant |
| endika | c295040 | 2020-09-14 11:20:00 +0200 | [diff] [blame] | 390 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 391 | async def _get_datacenter(self, session): |
| 392 | if not self.tenant: |
| 393 | await self._get_tenant(session) |
| 394 | if not self.datacenter: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 395 | self.datacenter = await self._get_item_uuid( |
| 396 | session, "datacenters", self.datacenter_id_name, True |
| 397 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 398 | return self.datacenter |
| 399 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 400 | async def _create_item( |
| 401 | self, |
| 402 | session, |
| 403 | item, |
| 404 | descriptor, |
| 405 | item_id_name=None, |
| 406 | action=None, |
| 407 | all_tenants=False, |
| 408 | ): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 409 | if all_tenants: |
| 410 | tenant_text = "/any" |
| 411 | elif all_tenants is None: |
| 412 | tenant_text = "" |
| 413 | else: |
| 414 | if not self.tenant: |
| 415 | await self._get_tenant(session) |
| 416 | tenant_text = "/" + self.tenant |
| 417 | payload_req = yaml.safe_dump(descriptor) |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 418 | # print payload_req |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 419 | |
| 420 | api_version_text = "" |
| 421 | if item == "vnfs": |
| 422 | # assumes version v3 only |
| 423 | api_version_text = "/v3" |
| 424 | item = "vnfd" |
| 425 | elif item == "scenarios": |
| 426 | # assumes version v3 only |
| 427 | api_version_text = "/v3" |
| 428 | item = "nsd" |
| 429 | |
| 430 | if not item_id_name: |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 431 | uuid = "" |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 432 | elif self.check_if_uuid(item_id_name): |
| 433 | uuid = "/{}".format(item_id_name) |
| 434 | else: |
| 435 | # check that exist |
| 436 | uuid = await self._get_item_uuid(session, item, item_id_name, all_tenants) |
| 437 | uuid = "/{}".format(uuid) |
| 438 | if not action: |
| 439 | action = "" |
| 440 | else: |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 441 | action = "/{}".format(action) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 442 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 443 | url = "{}{apiver}{tenant}/{item}{id}{action}".format( |
| 444 | self.uri, |
| 445 | apiver=api_version_text, |
| 446 | tenant=tenant_text, |
| 447 | item=item, |
| 448 | id=uuid, |
| 449 | action=action, |
| 450 | ) |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 451 | self.logger.debug("RO POST %s %s", url, payload_req) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 452 | # timeout = aiohttp.ClientTimeout(total=self.timeout_large) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 453 | async with session.post( |
| 454 | url, headers=self.headers_req, data=payload_req |
| 455 | ) as response: |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 456 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 457 | self.logger.debug( |
| 458 | "POST {} [{}] {}".format(url, response.status, response_text[:100]) |
| 459 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 460 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 461 | raise ROClientException( |
| 462 | self._parse_error_yaml(response_text), http_code=response.status |
| 463 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 464 | |
| 465 | return self._parse_yaml(response_text, response=True) |
| 466 | |
| 467 | async def _del_item(self, session, item, item_id_name, all_tenants=False): |
| 468 | if all_tenants: |
| 469 | tenant_text = "/any" |
| 470 | elif all_tenants is None: |
| 471 | tenant_text = "" |
| 472 | else: |
| 473 | if not self.tenant: |
| 474 | await self._get_tenant(session) |
| 475 | tenant_text = "/" + self.tenant |
| 476 | if not self.check_if_uuid(item_id_name): |
| 477 | # check that exist |
| 478 | _all_tenants = all_tenants |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 479 | if item in ("datacenters", "wims"): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 480 | _all_tenants = True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 481 | uuid = await self._get_item_uuid( |
| 482 | session, item, item_id_name, all_tenants=_all_tenants |
| 483 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 484 | else: |
| 485 | uuid = item_id_name |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 486 | |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 487 | url = "{}{}/{}/{}".format(self.uri, tenant_text, item, uuid) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 488 | self.logger.debug("DELETE %s", url) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 489 | # timeout = aiohttp.ClientTimeout(total=self.timeout_short) |
| 490 | async with session.delete(url, headers=self.headers_req) as response: |
| 491 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 492 | self.logger.debug( |
| 493 | "DELETE {} [{}] {}".format(url, response.status, response_text[:100]) |
| 494 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 495 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 496 | raise ROClientException( |
| 497 | self._parse_error_yaml(response_text), http_code=response.status |
| 498 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 499 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 500 | return self._parse_yaml(response_text, response=True) |
| 501 | |
| 502 | async def _list_item(self, session, item, all_tenants=False, filter_dict=None): |
| 503 | if all_tenants: |
| 504 | tenant_text = "/any" |
| 505 | elif all_tenants is None: |
| 506 | tenant_text = "" |
| 507 | else: |
| 508 | if not self.tenant: |
| 509 | await self._get_tenant(session) |
| 510 | tenant_text = "/" + self.tenant |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 511 | |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 512 | url = "{}{}/{}".format(self.uri, tenant_text, item) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 513 | separator = "?" |
| 514 | if filter_dict: |
| 515 | for k in filter_dict: |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 516 | url += separator + quote(str(k)) + "=" + quote(str(filter_dict[k])) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 517 | separator = "&" |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 518 | self.logger.debug("RO GET %s", url) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 519 | # timeout = aiohttp.ClientTimeout(total=self.timeout_short) |
| 520 | async with session.get(url, headers=self.headers_req) as response: |
| 521 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 522 | self.logger.debug( |
| 523 | "GET {} [{}] {}".format(url, response.status, response_text[:100]) |
| 524 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 525 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 526 | raise ROClientException( |
| 527 | self._parse_error_yaml(response_text), http_code=response.status |
| 528 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 529 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 530 | return self._parse_yaml(response_text, response=True) |
| 531 | |
| 532 | async def _edit_item(self, session, item, item_id, descriptor, all_tenants=False): |
| 533 | if all_tenants: |
| 534 | tenant_text = "/any" |
| 535 | elif all_tenants is None: |
| 536 | tenant_text = "" |
| 537 | else: |
| 538 | if not self.tenant: |
| 539 | await self._get_tenant(session) |
| 540 | tenant_text = "/" + self.tenant |
| 541 | |
| 542 | payload_req = yaml.safe_dump(descriptor) |
| endika | c295040 | 2020-09-14 11:20:00 +0200 | [diff] [blame] | 543 | |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 544 | # print payload_req |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 545 | url = "{}{}/{}/{}".format(self.uri, tenant_text, item, item_id) |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 546 | self.logger.debug("RO PUT %s %s", url, payload_req) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 547 | # timeout = aiohttp.ClientTimeout(total=self.timeout_large) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 548 | async with session.put( |
| 549 | url, headers=self.headers_req, data=payload_req |
| 550 | ) as response: |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 551 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 552 | self.logger.debug( |
| 553 | "PUT {} [{}] {}".format(url, response.status, response_text[:100]) |
| 554 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 555 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 556 | raise ROClientException( |
| 557 | self._parse_error_yaml(response_text), http_code=response.status |
| 558 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 559 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 560 | return self._parse_yaml(response_text, response=True) |
| 561 | |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 562 | async def get_version(self): |
| 563 | """ |
| 564 | Obtain RO server version. |
| 565 | :return: a list with integers ["major", "minor", "release"]. Raises ROClientException on Error, |
| 566 | """ |
| 567 | try: |
| tierno | c231a87 | 2020-01-21 08:49:05 +0000 | [diff] [blame] | 568 | response_text = "" |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 569 | async with aiohttp.ClientSession() as session: |
| tierno | 69f0d38 | 2020-05-07 13:08:09 +0000 | [diff] [blame] | 570 | url = "{}/version".format(self.uri) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 571 | self.logger.debug("RO GET %s", url) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 572 | # timeout = aiohttp.ClientTimeout(total=self.timeout_short) |
| 573 | async with session.get(url, headers=self.headers_req) as response: |
| 574 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 575 | self.logger.debug( |
| 576 | "GET {} [{}] {}".format( |
| 577 | url, response.status, response_text[:100] |
| 578 | ) |
| 579 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 580 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 581 | raise ROClientException( |
| 582 | self._parse_error_yaml(response_text), |
| 583 | http_code=response.status, |
| 584 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 585 | |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 586 | for word in str(response_text).split(" "): |
| 587 | if "." in word: |
| 588 | version_text, _, _ = word.partition("-") |
| tierno | 8069ce5 | 2019-08-28 15:34:33 +0000 | [diff] [blame] | 589 | return version_text |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 590 | raise ROClientException( |
| 591 | "Got invalid version text: '{}'".format(response_text), |
| 592 | http_code=500, |
| 593 | ) |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 594 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 595 | raise ROClientException(e, http_code=504) |
| 596 | except asyncio.TimeoutError: |
| 597 | raise ROClientException("Timeout", http_code=504) |
| 598 | except Exception as e: |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 599 | self.logger.critical( |
| 600 | "Got invalid version text: '{}'; causing exception {}".format( |
| 601 | response_text, str(e) |
| 602 | ) |
| 603 | ) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 604 | raise ROClientException( |
| 605 | "Got invalid version text: '{}'; causing exception {}".format( |
| 606 | response_text, e |
| 607 | ), |
| 608 | http_code=500, |
| 609 | ) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 610 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 611 | async def get_list(self, item, all_tenants=False, filter_by=None): |
| 612 | """ |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 613 | List of items filtered by the contents in the dictionary "filter_by". |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 614 | :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns' |
| 615 | :param all_tenants: True if not filtering by tenant. Only allowed for admin |
| 616 | :param filter_by: dictionary with filtering |
| 617 | :return: a list of dict. It can be empty. Raises ROClientException on Error, |
| 618 | """ |
| 619 | try: |
| 620 | if item not in self.client_to_RO: |
| 621 | raise ROClientException("Invalid item {}".format(item)) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 622 | if item == "tenant": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 623 | all_tenants = None |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 624 | async with aiohttp.ClientSession() as session: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 625 | content = await self._list_item( |
| 626 | session, |
| 627 | self.client_to_RO[item], |
| 628 | all_tenants=all_tenants, |
| 629 | filter_dict=filter_by, |
| 630 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 631 | if isinstance(content, dict): |
| 632 | if len(content) == 1: |
| 633 | for _, v in content.items(): |
| 634 | return v |
| 635 | return content.values()[0] |
| 636 | else: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 637 | raise ROClientException( |
| 638 | "Output not a list neither dict with len equal 1", http_code=500 |
| 639 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 640 | return content |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 641 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 642 | raise ROClientException(e, http_code=504) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 643 | except asyncio.TimeoutError: |
| 644 | raise ROClientException("Timeout", http_code=504) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 645 | |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 646 | async def delete(self, item, item_id_name=None, all_tenants=False): |
| 647 | """ |
| 648 | Delete the information of an item from its id or name |
| 649 | :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns' |
| 650 | :param item_id_name: RO id or name of the item. Raise and exception if more than one found |
| 651 | :param all_tenants: True if not filtering by tenant. Only allowed for admin |
| 652 | :return: dictionary with the information or raises ROClientException on Error, NotFound, found several |
| 653 | """ |
| 654 | try: |
| 655 | if item not in self.client_to_RO: |
| 656 | raise ROClientException("Invalid item {}".format(item)) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 657 | if item in ("tenant", "vim", "wim"): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 658 | all_tenants = None |
| 659 | |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 660 | async with aiohttp.ClientSession() as session: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 661 | result = await self._del_item( |
| 662 | session, |
| 663 | self.client_to_RO[item], |
| 664 | item_id_name, |
| 665 | all_tenants=all_tenants, |
| 666 | ) |
| tierno | fa66d15 | 2018-08-28 10:13:45 +0000 | [diff] [blame] | 667 | # in case of ns delete, get the action_id embeded in text |
| 668 | if item == "ns" and result.get("result"): |
| 669 | _, _, action_id = result["result"].partition("action_id=") |
| 670 | action_id, _, _ = action_id.partition(" ") |
| 671 | if action_id: |
| 672 | result["action_id"] = action_id |
| 673 | return result |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 674 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 675 | raise ROClientException(e, http_code=504) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 676 | except asyncio.TimeoutError: |
| 677 | raise ROClientException("Timeout", http_code=504) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 678 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 679 | async def edit( |
| 680 | self, item, item_id_name, descriptor=None, descriptor_format=None, **kwargs |
| 681 | ): |
| 682 | """Edit an item |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 683 | :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns', 'vim' |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 684 | :param item_id_name: RO id or name of the item. Raise and exception if more than one found |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 685 | :param descriptor: can be a dict, or a yaml/json text. Autodetect unless descriptor_format is provided |
| 686 | :param descriptor_format: Can be 'json' or 'yaml' |
| 687 | :param kwargs: Overrides descriptor with values as name, description, vim_url, vim_url_admin, vim_type |
| 688 | keys can be a dot separated list to specify elements inside dict |
| 689 | :return: dictionary with the information or raises ROClientException on Error |
| 690 | """ |
| 691 | try: |
| 692 | if isinstance(descriptor, str): |
| 693 | descriptor = self._parse(descriptor, descriptor_format) |
| 694 | elif descriptor: |
| 695 | pass |
| 696 | else: |
| 697 | descriptor = {} |
| 698 | |
| 699 | if item not in self.client_to_RO: |
| 700 | raise ROClientException("Invalid item {}".format(item)) |
| 701 | desc = remove_envelop(item, descriptor) |
| 702 | |
| 703 | # Override descriptor with kwargs |
| 704 | if kwargs: |
| 705 | desc = self.update_descriptor(desc, kwargs) |
| 706 | all_tenants = False |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 707 | if item in ("tenant", "vim"): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 708 | all_tenants = None |
| 709 | |
| 710 | create_desc = self._create_envelop(item, desc) |
| 711 | |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 712 | async with aiohttp.ClientSession() as session: |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 713 | _all_tenants = all_tenants |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 714 | if item == "vim": |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 715 | _all_tenants = True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 716 | item_id = await self._get_item_uuid( |
| 717 | session, |
| 718 | self.client_to_RO[item], |
| 719 | item_id_name, |
| 720 | all_tenants=_all_tenants, |
| 721 | ) |
| 722 | if item == "vim": |
| tierno | fe1c37f | 2018-05-17 22:58:04 +0200 | [diff] [blame] | 723 | _all_tenants = None |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 724 | # await self._get_tenant(session) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 725 | outdata = await self._edit_item( |
| 726 | session, |
| 727 | self.client_to_RO[item], |
| 728 | item_id, |
| 729 | create_desc, |
| 730 | all_tenants=_all_tenants, |
| 731 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 732 | return remove_envelop(item, outdata) |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 733 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 734 | raise ROClientException(e, http_code=504) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 735 | except asyncio.TimeoutError: |
| 736 | raise ROClientException("Timeout", http_code=504) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 737 | |
| 738 | async def create(self, item, descriptor=None, descriptor_format=None, **kwargs): |
| 739 | """ |
| 740 | Creates an item from its descriptor |
| 741 | :param item: can be 'tenant', 'vnfd', 'nsd', 'ns', 'vim', 'vim_account', 'sdn' |
| 742 | :param descriptor: can be a dict, or a yaml/json text. Autodetect unless descriptor_format is provided |
| 743 | :param descriptor_format: Can be 'json' or 'yaml' |
| 744 | :param kwargs: Overrides descriptor with values as name, description, vim_url, vim_url_admin, vim_type |
| 745 | keys can be a dot separated list to specify elements inside dict |
| 746 | :return: dictionary with the information or raises ROClientException on Error |
| 747 | """ |
| 748 | try: |
| 749 | if isinstance(descriptor, str): |
| 750 | descriptor = self._parse(descriptor, descriptor_format) |
| 751 | elif descriptor: |
| 752 | pass |
| 753 | else: |
| 754 | descriptor = {} |
| 755 | |
| 756 | if item not in self.client_to_RO: |
| 757 | raise ROClientException("Invalid item {}".format(item)) |
| 758 | desc = remove_envelop(item, descriptor) |
| 759 | |
| 760 | # Override descriptor with kwargs |
| 761 | if kwargs: |
| 762 | desc = self.update_descriptor(desc, kwargs) |
| 763 | |
| 764 | for mandatory in self.mandatory_for_create[item]: |
| 765 | if mandatory not in desc: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 766 | raise ROClientException( |
| 767 | "'{}' is mandatory parameter for {}".format(mandatory, item) |
| 768 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 769 | |
| 770 | all_tenants = False |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 771 | if item in ("tenant", "vim", "wim"): |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 772 | all_tenants = None |
| 773 | |
| 774 | create_desc = self._create_envelop(item, desc) |
| 775 | |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 776 | async with aiohttp.ClientSession() as session: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 777 | outdata = await self._create_item( |
| 778 | session, |
| 779 | self.client_to_RO[item], |
| 780 | create_desc, |
| 781 | all_tenants=all_tenants, |
| 782 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 783 | return remove_envelop(item, outdata) |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 784 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 785 | raise ROClientException(e, http_code=504) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 786 | except asyncio.TimeoutError: |
| 787 | raise ROClientException("Timeout", http_code=504) |
| 788 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 789 | async def attach( |
| 790 | self, item, item_id_name=None, descriptor=None, descriptor_format=None, **kwargs |
| 791 | ): |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 792 | """ |
| 793 | Attach a datacenter or wim to a tenant, creating a vim_account, wim_account |
| 794 | :param item: can be vim_account or wim_account |
| 795 | :param item_id_name: id or name of the datacenter, wim |
| 796 | :param descriptor: |
| 797 | :param descriptor_format: |
| 798 | :param kwargs: |
| 799 | :return: |
| 800 | """ |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 801 | try: |
| 802 | if isinstance(descriptor, str): |
| 803 | descriptor = self._parse(descriptor, descriptor_format) |
| 804 | elif descriptor: |
| 805 | pass |
| 806 | else: |
| 807 | descriptor = {} |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 808 | |
| 809 | desc = remove_envelop(item, descriptor) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 810 | |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 811 | # # check that exist |
| 812 | # uuid = self._get_item_uuid(session, "datacenters", uuid_name, all_tenants=True) |
| 813 | # tenant_text = "/" + self._get_tenant() |
| 814 | if kwargs: |
| 815 | desc = self.update_descriptor(desc, kwargs) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 816 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 817 | if item == "vim_account": |
| 818 | if not desc.get("vim_tenant_name") and not desc.get("vim_tenant_id"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 819 | raise ROClientException( |
| 820 | "Wrong descriptor. At least vim_tenant_name or vim_tenant_id must be " |
| 821 | "provided" |
| 822 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 823 | elif item != "wim_account": |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 824 | raise ROClientException( |
| 825 | "Attach with unknown item {}. Must be 'vim_account' or 'wim_account'".format( |
| 826 | item |
| 827 | ) |
| 828 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 829 | create_desc = self._create_envelop(item, desc) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 830 | payload_req = yaml.safe_dump(create_desc) |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 831 | async with aiohttp.ClientSession() as session: |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 832 | # check that exist |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 833 | item_id = await self._get_item_uuid( |
| 834 | session, self.client_to_RO[item], item_id_name, all_tenants=True |
| 835 | ) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 836 | await self._get_tenant(session) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 837 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 838 | url = "{}/{tenant}/{item}/{item_id}".format( |
| 839 | self.uri, |
| 840 | tenant=self.tenant, |
| 841 | item=self.client_to_RO[item], |
| 842 | item_id=item_id, |
| 843 | ) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 844 | self.logger.debug("RO POST %s %s", url, payload_req) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 845 | # timeout = aiohttp.ClientTimeout(total=self.timeout_large) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 846 | async with session.post( |
| 847 | url, headers=self.headers_req, data=payload_req |
| 848 | ) as response: |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 849 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 850 | self.logger.debug( |
| 851 | "POST {} [{}] {}".format( |
| 852 | url, response.status, response_text[:100] |
| 853 | ) |
| 854 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 855 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 856 | raise ROClientException( |
| 857 | self._parse_error_yaml(response_text), |
| 858 | http_code=response.status, |
| 859 | ) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 860 | |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 861 | response_desc = self._parse_yaml(response_text, response=True) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 862 | desc = remove_envelop(item, response_desc) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 863 | return desc |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 864 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 865 | raise ROClientException(e, http_code=504) |
| 866 | except asyncio.TimeoutError: |
| 867 | raise ROClientException("Timeout", http_code=504) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 868 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 869 | async def detach(self, item, item_id_name=None): |
| tierno | 750b245 | 2018-05-17 16:39:29 +0200 | [diff] [blame] | 870 | # TODO replace the code with delete_item(vim_account,...) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 871 | try: |
| Gabriel Cuba | e789898 | 2023-05-11 01:57:21 -0500 | [diff] [blame] | 872 | async with aiohttp.ClientSession() as session: |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 873 | # check that exist |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 874 | item_id = await self._get_item_uuid( |
| 875 | session, self.client_to_RO[item], item_id_name, all_tenants=False |
| 876 | ) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 877 | tenant = await self._get_tenant(session) |
| tierno | c0e42e2 | 2018-05-11 11:36:10 +0200 | [diff] [blame] | 878 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 879 | url = "{}/{tenant}/{item}/{datacenter}".format( |
| 880 | self.uri, |
| 881 | tenant=tenant, |
| 882 | item=self.client_to_RO[item], |
| 883 | datacenter=item_id, |
| 884 | ) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 885 | self.logger.debug("RO DELETE %s", url) |
| endika | c295040 | 2020-09-14 11:20:00 +0200 | [diff] [blame] | 886 | |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 887 | # timeout = aiohttp.ClientTimeout(total=self.timeout_large) |
| 888 | async with session.delete(url, headers=self.headers_req) as response: |
| 889 | response_text = await response.read() |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 890 | self.logger.debug( |
| 891 | "DELETE {} [{}] {}".format( |
| 892 | url, response.status, response_text[:100] |
| 893 | ) |
| 894 | ) |
| calvinosanch | d5916fd | 2020-01-09 17:19:53 +0100 | [diff] [blame] | 895 | if response.status >= 300: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 896 | raise ROClientException( |
| 897 | self._parse_error_yaml(response_text), |
| 898 | http_code=response.status, |
| 899 | ) |
| endika | c295040 | 2020-09-14 11:20:00 +0200 | [diff] [blame] | 900 | |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 901 | response_desc = self._parse_yaml(response_text, response=True) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 902 | desc = remove_envelop(item, response_desc) |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 903 | return desc |
| calvinosanch | 30ccee3 | 2020-01-13 12:01:36 +0100 | [diff] [blame] | 904 | except (aiohttp.ClientOSError, aiohttp.ClientError) as e: |
| tierno | 22f4f9c | 2018-06-11 18:53:39 +0200 | [diff] [blame] | 905 | raise ROClientException(e, http_code=504) |
| 906 | except asyncio.TimeoutError: |
| 907 | raise ROClientException("Timeout", http_code=504) |