-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfiguration.py
More file actions
142 lines (120 loc) · 5.1 KB
/
configuration.py
File metadata and controls
142 lines (120 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# coding: utf-8
import sys
import os
"""
STACKIT Server Update Management API
API endpoints for Server Update Operations on STACKIT Servers.
The version of the OpenAPI document: 2.0
Contact: support@stackit.de
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
""" # noqa: E501 docstring might be too long
class HostConfiguration:
def __init__(
self,
region=None,
server_index=None,
server_variables=None,
server_operation_index=None,
server_operation_variables=None,
ignore_operation_servers=False,
) -> None:
print(
"WARNING: STACKIT will move to a new way of specifying regions, where the region is provided\n",
"as a function argument instead of being set in the client configuration.\n"
"Once all services have migrated, the methods to specify the region in the client configuration "
"will be removed.",
file=sys.stderr,
)
"""Constructor
"""
self._base_path = "https://server-update.api.stackit.cloud"
"""Default Base url
"""
self.server_index = 0 if server_index is None else server_index
self.server_operation_index = server_operation_index or {}
"""Default server index
"""
self.server_variables = server_variables or {}
if region:
self.server_variables["region"] = "{}.".format(region)
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.ignore_operation_servers = ignore_operation_servers
"""Ignore operation servers
"""
def get_host_settings(self):
"""Gets an array of host settings
:return: An array of host settings
"""
return [
{
"url": "https://server-update.api.stackit.cloud",
"description": "No description provided",
"variables": {
"region": {
"description": "No description provided",
"default_value": "global",
}
},
}
]
def get_host_from_settings(self, index, variables=None, servers=None):
"""Gets host URL based on the index and variables
:param index: array index of the host settings
:param variables: hash of variable and the corresponding value
:param servers: an array of host settings or None
:error: if a region is given for a global url
:return: URL based on host settings
"""
if index is None:
return self._base_path
variables = {} if variables is None else variables
servers = self.get_host_settings() if servers is None else servers
try:
server = servers[index]
except IndexError:
raise ValueError(
"Invalid index {0} when selecting the host settings. "
"Must be less than {1}".format(index, len(servers))
)
url = server["url"]
# check if environment variable was provided for region
# if nothing was set this is None
region_env = os.environ.get("STACKIT_REGION")
# go through variables and replace placeholders
for variable_name, variable in server.get("variables", {}).items():
# If a region is provided by the user for a global url
# return an error (except for providing via environment variable).
# The region is provided as a function argument instead of being set in the client configuration.
if (
variable_name == "region"
and (variable["default_value"] == "global" or variable["default_value"] == "")
and region_env is None
and variables.get(variable_name) is not None
):
raise ValueError(
"this API does not support setting a region in the the client configuration, "
"please check if the region can be specified as a function parameter"
)
used_value = variables.get(variable_name, variable["default_value"])
if "enum_values" in variable and used_value not in variable["enum_values"]:
given_value = variables[variable_name].replace(".", "")
valid_values = [v.replace(".", "") for v in variable["enum_values"]]
raise ValueError(
"The variable `{0}` in the host URL has invalid value '{1}'. Must be '{2}'.".format(
variable_name, given_value, valid_values
)
)
url = url.replace("{" + variable_name + "}", used_value)
return url
@property
def host(self):
"""Return generated host."""
return self.get_host_from_settings(self.server_index, variables=self.server_variables)
@host.setter
def host(self, value):
"""Fix base path."""
self._base_path = value
self.server_index = None