Skip to content

Commit fca65ad

Browse files
Generate authorization
1 parent 8e3722c commit fca65ad

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

services/authorization/src/stackit/authorization/configuration.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
Do not edit the class manually.
1212
""" # noqa: E501 docstring might be too long
1313

14+
import os
15+
1416

1517
class HostConfiguration:
1618
def __init__(
@@ -22,8 +24,15 @@ def __init__(
2224
server_operation_variables=None,
2325
ignore_operation_servers=False,
2426
) -> None:
25-
"""Constructor"""
26-
self._base_path = "https://authorization.api.stackit.cloud"
27+
print(
28+
"WARNING: STACKIT will move to a new way of specifying regions, where the region is provided\n",
29+
"as a function argument instead of being set in the client configuration.\n"
30+
"Once all services have migrated, the methods to specify the region in the client configuration "
31+
"will be removed.",
32+
)
33+
"""Constructor
34+
"""
35+
self._base_path = "https://authorization.api.eu01.stackit.cloud"
2736
"""Default Base url
2837
"""
2938
self.server_index = 0 if server_index is None else server_index
@@ -47,12 +56,13 @@ def get_host_settings(self):
4756
"""
4857
return [
4958
{
50-
"url": "https://authorization.api.stackit.cloud",
59+
"url": "https://authorization.api.{region}stackit.cloud",
5160
"description": "No description provided",
5261
"variables": {
5362
"region": {
5463
"description": "No description provided",
55-
"default_value": "global",
64+
"default_value": "eu01.",
65+
"enum_values": ["eu01.", "eu02."],
5666
}
5767
},
5868
}
@@ -63,6 +73,7 @@ def get_host_from_settings(self, index, variables=None, servers=None):
6373
:param index: array index of the host settings
6474
:param variables: hash of variable and the corresponding value
6575
:param servers: an array of host settings or None
76+
:error: if a region is given for a global url
6677
:return: URL based on host settings
6778
"""
6879
if index is None:
@@ -81,8 +92,25 @@ def get_host_from_settings(self, index, variables=None, servers=None):
8192

8293
url = server["url"]
8394

95+
# check if environment variable was provided for region
96+
# if nothing was set this is None
97+
region_env = os.environ.get("STACKIT_REGION")
98+
8499
# go through variables and replace placeholders
85100
for variable_name, variable in server.get("variables", {}).items():
101+
# If a region is provided by the user for a global url
102+
# return an error (except for providing via environment variable).
103+
# The region is provided as a function argument instead of being set in the client configuration.
104+
if (
105+
variable_name == "region"
106+
and (variable["default_value"] == "global" or variable["default_value"] == "")
107+
and region_env is None
108+
and variables.get(variable_name) is not None
109+
):
110+
raise ValueError(
111+
"this API does not support setting a region in the the client configuration, "
112+
"please check if the region can be specified as a function parameter"
113+
)
86114
used_value = variables.get(variable_name, variable["default_value"])
87115

88116
if "enum_values" in variable and used_value not in variable["enum_values"]:

0 commit comments

Comments
 (0)