| 1 |
# Copyright 2017 Red Hat, Inc |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
import copy |
| 17 |
from typing import TYPE_CHECKING, Optional, overload |
| 18 |
|
| 19 |
import os_service_types.data |
| 20 |
from os_service_types import exc |
| 21 |
from os_service_types import types |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from requests import sessions |
| 25 |
|
| 26 |
__all__ = ['ServiceTypes'] |
| 27 |
|
| 28 |
BUILTIN_DATA = os_service_types.data.read_data('service-types.json') |
| 29 |
SERVICE_TYPES_URL = "https://service-types.openstack.org/service-types.json" |
| 30 |
|
| 31 |
|
| 32 |
@overload |
| 33 |
def _normalize_type(service_type: str) -> str: ... |
| 34 |
|
| 35 |
|
| 36 |
@overload |
| 37 |
def _normalize_type(service_type: None) -> None: ... |
| 38 |
|
| 39 |
|
| 40 |
def _normalize_type(service_type: str | None) -> str | None: |
| 41 |
if not service_type: |
| 42 |
# TODO(stephenfin): This should be an error |
| 43 |
return None |
| 44 |
return service_type.replace('_', '-') |
| 45 |
|
| 46 |
|
| 47 |
class ServiceTypes: |
| 48 |
"""Encapsulation of the OpenStack Service Types Authority data. |
| 49 |
|
| 50 |
The Service Types Authority data will be either pulled from its remote |
| 51 |
location or from local files as is appropriate. |
| 52 |
|
| 53 |
If the user passes a Session, remote data will be fetched. If the user |
| 54 |
does not do that, local builtin data will be used. |
| 55 |
|
| 56 |
:param session: An object that behaves like a `requests.sessions.Session` |
| 57 |
or a `keystoneauth1.session.Session` that provides a get method |
| 58 |
and returns an object that behaves like a `requests.models.Response`. |
| 59 |
Optional. If session is omitted, no remote actions will be performed. |
| 60 |
:param bool only_remote: By default if there is a problem fetching data |
| 61 |
remotely the builtin data will be returned as a fallback. only_remote |
| 62 |
will cause remote failures to raise an error instead of falling back. |
| 63 |
Optional, defaults to False. |
| 64 |
:param bool warn: Emit warnings when a non-official service_type is |
| 65 |
provided. This provides an easy way for consuming applications to |
| 66 |
warn users when they are using old types. |
| 67 |
:raises ValueError: If session is None and only_remote is True |
| 68 |
:raises IOError: If session is given and only_remote is True and there is |
| 69 |
an error fetching remote data. |
| 70 |
""" |
| 71 |
|
| 72 |
def __init__( |
| 73 |
self, |
| 74 |
session: Optional['sessions.Session'] = None, |
| 75 |
only_remote: bool = False, |
| 76 |
warn: bool = False, |
| 77 |
) -> None: |
| 78 |
if not session and only_remote: |
| 79 |
raise ValueError( |
| 80 |
"only_remote was requested but no Session was provided." |
| 81 |
) |
| 82 |
self._service_types_data: types.ServiceTypes = BUILTIN_DATA |
| 83 |
self._warn = warn |
| 84 |
if session: |
| 85 |
try: |
| 86 |
response = session.get(SERVICE_TYPES_URL) |
| 87 |
response.raise_for_status() |
| 88 |
self._service_types_data = response.json() |
| 89 |
except OSError: |
| 90 |
# If we can't fetch, fall backto BUILTIN |
| 91 |
if only_remote: |
| 92 |
raise |
| 93 |
|
| 94 |
def _canonical_project_name(self, name: str | None) -> str: |
| 95 |
"Convert repo name to project name." |
| 96 |
if name is None: |
| 97 |
raise ValueError("Empty project name is not allowed") |
| 98 |
# Handle openstack/ prefix going away from STA data |
| 99 |
return name.rpartition('/')[-1] |
| 100 |
|
| 101 |
@property |
| 102 |
def url(self) -> str: |
| 103 |
"The URL from which the data was retrieved." |
| 104 |
return SERVICE_TYPES_URL |
| 105 |
|
| 106 |
@property |
| 107 |
def version(self) -> str: |
| 108 |
"The version of the data." |
| 109 |
return self._service_types_data['version'] |
| 110 |
|
| 111 |
@property |
| 112 |
def forward(self) -> dict[str, list[str]]: |
| 113 |
"Mapping service-type names to their aliases." |
| 114 |
return copy.deepcopy(self._service_types_data['forward']) |
| 115 |
|
| 116 |
@property |
| 117 |
def reverse(self) -> dict[str, str]: |
| 118 |
"Mapping aliases to their service-type names." |
| 119 |
return copy.deepcopy(self._service_types_data['reverse']) |
| 120 |
|
| 121 |
@property |
| 122 |
def services(self) -> list[types.Service]: |
| 123 |
"Full service-type data listing." |
| 124 |
return copy.deepcopy(self._service_types_data['services']) |
| 125 |
|
| 126 |
@property |
| 127 |
def all_types_by_service_type(self) -> dict[str, list[str]]: |
| 128 |
"Mapping of official service type to official type and aliases." |
| 129 |
return copy.deepcopy( |
| 130 |
self._service_types_data['all_types_by_service_type'] |
| 131 |
) |
| 132 |
|
| 133 |
@property |
| 134 |
def primary_service_by_project(self) -> dict[str, types.Service]: |
| 135 |
"Mapping of project name to the primary associated service." |
| 136 |
return copy.deepcopy( |
| 137 |
self._service_types_data['primary_service_by_project'] |
| 138 |
) |
| 139 |
|
| 140 |
@property |
| 141 |
def service_types_by_project(self) -> dict[str, list[str]]: |
| 142 |
"Mapping of project name to a list of all associated service-types." |
| 143 |
return copy.deepcopy( |
| 144 |
self._service_types_data['service_types_by_project'] |
| 145 |
) |
| 146 |
|
| 147 |
def get_official_service_data( |
| 148 |
self, service_type: str |
| 149 |
) -> types.Service | None: |
| 150 |
"""Get the service data for an official service_type. |
| 151 |
|
| 152 |
:param str service_type: The official service-type to get data for. |
| 153 |
:returns dict: Service data for the service or None if not found. |
| 154 |
""" |
| 155 |
service_type = _normalize_type(service_type) |
| 156 |
for service in self._service_types_data['services']: |
| 157 |
if service_type == service['service_type']: |
| 158 |
return service |
| 159 |
return None |
| 160 |
|
| 161 |
def get_service_data(self, service_type: str) -> types.Service | None: |
| 162 |
"""Get the service data for a given service_type. |
| 163 |
|
| 164 |
:param str service_type: The service-type or alias to get data for. |
| 165 |
:returns dict: Service data for the service or None if not found. |
| 166 |
""" |
| 167 |
official_service_type = self.get_service_type(service_type) |
| 168 |
if not official_service_type: |
| 169 |
return None |
| 170 |
return self.get_official_service_data(official_service_type) |
| 171 |
|
| 172 |
def is_official(self, service_type: str) -> bool: |
| 173 |
"""Is the given service-type an official service-type? |
| 174 |
|
| 175 |
:param str service_type: The service-type to test. |
| 176 |
:returns bool: True if it's an official type, False otherwise. |
| 177 |
""" |
| 178 |
return self.get_official_service_data(service_type) is not None |
| 179 |
|
| 180 |
def is_alias(self, service_type: str) -> bool: |
| 181 |
"""Is the given service-type an alias? |
| 182 |
|
| 183 |
:param str service_type: The service-type to test. |
| 184 |
:returns bool: True if it's an alias type, False otherwise. |
| 185 |
""" |
| 186 |
service_type = _normalize_type(service_type) |
| 187 |
return service_type in self._service_types_data['reverse'] |
| 188 |
|
| 189 |
def is_known(self, service_type: str) -> bool: |
| 190 |
"""Is the given service-type an official type or an alias? |
| 191 |
|
| 192 |
:param str service_type: The service-type to test. |
| 193 |
:returns bool: True if it's a known type, False otherwise. |
| 194 |
""" |
| 195 |
return self.is_official(service_type) or self.is_alias(service_type) |
| 196 |
|
| 197 |
def is_match(self, requested: str, found: str) -> bool: |
| 198 |
"""Does a requested service-type match one found in the catalog? |
| 199 |
|
| 200 |
A requested service-type matches a service-type in the catalog if |
| 201 |
it is either a direct match, if the service-type in the catalog is |
| 202 |
an official type and the requested type is one of its aliases, or |
| 203 |
if the requested type is an official type and the type in the catalog |
| 204 |
is one of its aliases. |
| 205 |
|
| 206 |
A requested alias cannot match a different alias because there are |
| 207 |
historical implications related to versioning to some historical |
| 208 |
aliases that cannot be safely reasoned about in an automatic fashion. |
| 209 |
|
| 210 |
:param str requested: A service-type that someone is looking for. |
| 211 |
:param str found: A service-type found in a catalog |
| 212 |
|
| 213 |
:returns bool: True if the service-type being requested matches the |
| 214 |
entry in the catalog. False if it does not. |
| 215 |
""" |
| 216 |
# Exact match |
| 217 |
if requested == found: |
| 218 |
return True |
| 219 |
|
| 220 |
# Found is official type, requested is one of its aliases |
| 221 |
if requested in self.get_aliases(found): |
| 222 |
return True |
| 223 |
|
| 224 |
# Found is an alias, requested is an official type |
| 225 |
if requested == self.get_service_type(found): |
| 226 |
return True |
| 227 |
|
| 228 |
return False |
| 229 |
|
| 230 |
def get_aliases(self, service_type: str) -> list[str]: |
| 231 |
"""Returns the list of aliases for a given official service-type. |
| 232 |
|
| 233 |
:param service_type: An official service-type. |
| 234 |
:returns: List of aliases, or empty list if there are none. |
| 235 |
""" |
| 236 |
service_type = _normalize_type(service_type) |
| 237 |
return self._service_types_data['forward'].get(service_type, []) |
| 238 |
|
| 239 |
def get_service_type( |
| 240 |
self, service_type: str, permissive: bool = False |
| 241 |
) -> str | None: |
| 242 |
"""Given a possible service_type, return the official type. |
| 243 |
|
| 244 |
:param service_type: A potential service-type. |
| 245 |
:param permissive: |
| 246 |
Return the original type if the given service_type is not found. |
| 247 |
:returns: The official service-type, or None if there is no match. |
| 248 |
""" |
| 249 |
service_type = _normalize_type(service_type) |
| 250 |
if self.is_official(service_type): |
| 251 |
return service_type |
| 252 |
official = self._service_types_data['reverse'].get(service_type) |
| 253 |
if permissive and official is None: |
| 254 |
if self._warn: |
| 255 |
exc.warn(exc.UnofficialUsageWarning, given=service_type) |
| 256 |
return service_type |
| 257 |
if self._warn: |
| 258 |
exc.warn( |
| 259 |
exc.AliasUsageWarning, given=service_type, official=official |
| 260 |
) |
| 261 |
return official |
| 262 |
|
| 263 |
def get_all_types(self, service_type: str) -> list[str]: |
| 264 |
"""Get a list of official types and all known aliases. |
| 265 |
|
| 266 |
:param str service_type: The service-type or alias to get data for. |
| 267 |
:returns dict: Service data for the service or None if not found. |
| 268 |
""" |
| 269 |
service_type = _normalize_type(service_type) |
| 270 |
if not self.is_known(service_type): |
| 271 |
return [service_type] |
| 272 |
official_service_type = self.get_service_type(service_type) |
| 273 |
assert official_service_type is not None |
| 274 |
return self.all_types_by_service_type[official_service_type] |
| 275 |
|
| 276 |
def get_project_name(self, service_type: str) -> str | None: |
| 277 |
"""Return the OpenStack project name for a given service_type. |
| 278 |
|
| 279 |
:param str service_type: An official service-type or alias. |
| 280 |
:returns: The OpenStack project name or None if there is no match. |
| 281 |
""" |
| 282 |
service_type = _normalize_type(service_type) |
| 283 |
service = self.get_service_data(service_type) |
| 284 |
if service: |
| 285 |
return service['project'] |
| 286 |
return None |
| 287 |
|
| 288 |
def get_service_data_for_project( |
| 289 |
self, project_name: str |
| 290 |
) -> types.Service | None: |
| 291 |
"""Return the service information associated with a project. |
| 292 |
|
| 293 |
:param name: A repository or project name in the form |
| 294 |
``'openstack/{project}'`` or just ``'{project}'``. |
| 295 |
:type name: str |
| 296 |
:raises ValueError: If project_name is None |
| 297 |
:returns: dict or None if not found |
| 298 |
""" |
| 299 |
project_name = self._canonical_project_name(project_name) |
| 300 |
return self.primary_service_by_project.get(project_name) |
| 301 |
|
| 302 |
def get_all_service_data_for_project( |
| 303 |
self, project_name: str |
| 304 |
) -> list[types.Service]: |
| 305 |
"""Return the information for every service associated with a project. |
| 306 |
|
| 307 |
:param name: A repository or project name in the form |
| 308 |
``'openstack/{project}'`` or just ``'{project}'``. |
| 309 |
:type name: str |
| 310 |
:raises ValueError: If project_name is None |
| 311 |
:returns: list of dicts |
| 312 |
""" |
| 313 |
data = [] |
| 314 |
for service_type in self.service_types_by_project.get( |
| 315 |
project_name, [] |
| 316 |
): |
| 317 |
service = self.get_service_data(service_type) |
| 318 |
if service: |
| 319 |
data.append(service) |
| 320 |
return data |