-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstandard_provider.py
More file actions
333 lines (303 loc) · 11.8 KB
/
standard_provider.py
File metadata and controls
333 lines (303 loc) · 11.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# coding=utf-8
from __future__ import unicode_literals, print_function
import re
from volcenginesdkcore.endpoint.endpoint_provider import EndpointProvider, ResolvedEndpoint
# -----------------------------
# Errors (模仿 volcengineerr.New)
# -----------------------------
class StandProviderError(Exception):
def __init__(self, code, message):
Exception.__init__(self, "%s: %s" % (code, message))
self.code = code
self.message = message
# -----------------------------
# 常量
# -----------------------------
DEFAULT_FORMAT = "{Service}{Region}.{SiteStack}.com"
# 站点栈
SITE_STACK_VOLC_IPV4 = "volcengineapi"
SITE_STACK_VOLC_DUAL_STACK = "volcengine-api"
# -----------------------------
# 数据模型(兼容 2.7/3.x 的朴素类)
# -----------------------------
class StandardEndpointResolverVariable(object):
def __init__(self):
self.service = ""
self.region = ""
self.site_stack = ""
self.extension = {}
def to_dict(self):
return {
"Service": self.service,
"Region": self.region,
"SiteStack": self.site_stack,
"Extension": self.extension,
}
class ServiceInfo(object):
def __init__(self, service, is_global):
self.service = service
self.IsGlobal = is_global
ServiceInfos = {
"vpc": ServiceInfo("vpc", False),
"ecs": ServiceInfo("ecs", False),
"billing": ServiceInfo("billing", True),
"ark": ServiceInfo("ark", False),
"iam": ServiceInfo("iam", True),
"mcs": ServiceInfo("mcs", False),
"rocketmq": ServiceInfo("rocketmq", False),
"bytehouse": ServiceInfo("bytehouse", False),
"dns": ServiceInfo("dns", True),
"autoscaling": ServiceInfo("autoscaling", False),
"spark": ServiceInfo("spark", False),
"cloud_detect": ServiceInfo("cloud_detect", False),
"filenas": ServiceInfo("filenas", False),
"escloud": ServiceInfo("escloud", False),
"flink": ServiceInfo("flink", False),
"cp": ServiceInfo("cp", False),
"vefaas": ServiceInfo("vefaas", False),
"ml_platform": ServiceInfo("ml_platform", False),
"edx": ServiceInfo("edx", True),
"dcdn": ServiceInfo("dcdn", True),
"cdn": ServiceInfo("cdn", True),
"kafka": ServiceInfo("kafka", False),
"certificate_service": ServiceInfo("certificate_service", True),
"waf": ServiceInfo("waf", True),
"rds_mssql": ServiceInfo("rds_mssql", False),
"cloudtrail": ServiceInfo("cloudtrail", False),
"vei_api": ServiceInfo("vei_api", True),
"cen": ServiceInfo("cen", True),
"rabbitmq": ServiceInfo("rabbitmq", False),
"vmp": ServiceInfo("vmp", False),
"volc_observe": ServiceInfo("volc_observe", False),
"dataleap": ServiceInfo("dataleap", False),
"fw_center": ServiceInfo("fw_center", True),
"redis": ServiceInfo("redis", False),
"mcdn": ServiceInfo("mcdn", True),
"cloudidentity": ServiceInfo("cloudidentity", False),
"vedbm": ServiceInfo("vedbm", False),
"cv": ServiceInfo("cv", True),
"translate": ServiceInfo("translate", True),
"cloud_trail": ServiceInfo("cloud_trail", False),
"bio": ServiceInfo("bio", False),
"nta": ServiceInfo("nta", True),
"elasticmapreduce": ServiceInfo("elasticmapreduce", False),
"vepfs": ServiceInfo("vepfs", False),
"seccenter": ServiceInfo("seccenter", True),
"advdefence": ServiceInfo("advdefence", True),
"tis": ServiceInfo("tis", True),
"organization": ServiceInfo("organization", True),
"vke": ServiceInfo("vke", False),
"Redis": ServiceInfo("Redis", False),
"privatelink": ServiceInfo("privatelink", False),
"RocketMQ": ServiceInfo("RocketMQ", False),
"Kafka": ServiceInfo("Kafka", False),
"rds_mysql": ServiceInfo("rds_mysql", False),
"rds_postgresql": ServiceInfo("rds_postgresql", False),
"storage_ebs": ServiceInfo("storage_ebs", False),
"clb": ServiceInfo("clb", False),
"alb": ServiceInfo("alb", False),
"FileNAS": ServiceInfo("FileNAS", False),
"configcenter": ServiceInfo("configcenter", False),
"cr": ServiceInfo("cr", False),
"sts": ServiceInfo("sts", False),
"mongodb": ServiceInfo("mongodb", False),
"transitrouter": ServiceInfo("transitrouter", False),
"Volc_Observe": ServiceInfo("Volc_Observe", False),
"dms": ServiceInfo("dms", False),
"auto_scaling": ServiceInfo("auto_scaling", False),
"directconnect": ServiceInfo("directconnect", False),
"kms": ServiceInfo("kms", False),
"dbw": ServiceInfo("dbw", False),
"dts": ServiceInfo("dts", False),
"natgateway": ServiceInfo("natgateway", False),
"tos": ServiceInfo("tos", False),
"TLS": ServiceInfo("TLS", False),
"vpn": ServiceInfo("vpn", False),
"vod": ServiceInfo("vod", False),
"quota": ServiceInfo("quota", True),
"ecs_ops": ServiceInfo("ecs_ops", True),
"as_ops": ServiceInfo("as_ops", True),
"account_management": ServiceInfo("account_management", True),
"account_management_byteplus": ServiceInfo("account_management_byteplus", True),
"bandwidthquota": ServiceInfo("bandwidthquota", True),
"psa_manager": ServiceInfo("psa_manager", True),
"dc_controller": ServiceInfo("dc_controller", False),
"eps_platform_trade": ServiceInfo("eps_platform_trade", False),
"eps_platform_fund": ServiceInfo("eps_platform_fund", False),
"commercialization": ServiceInfo("commercialization", True),
"veecp_openapi": ServiceInfo("veecp_openapi", False),
"orgnization": ServiceInfo("orgnization", True),
"coze": ServiceInfo("coze", True),
"sec_agent": ServiceInfo("sec_agent", True),
"sec_intelligent_dev": ServiceInfo("sec_intelligent_dev", True),
"vegame": ServiceInfo("vegame", False),
"acep": ServiceInfo("acep", True),
"private_zone": ServiceInfo("private_zone", True),
"sqs": ServiceInfo("sqs", False),
"resourcecenter": ServiceInfo("resourcecenter", True),
"aiotvideo": ServiceInfo("aiotvideo", True),
"apig": ServiceInfo("apig", False),
"bmq": ServiceInfo("bmq", False),
"bytehouse_ce": ServiceInfo("bytehouse_ce", False),
"cloudmonitor": ServiceInfo("cloudmonitor", False),
"emr": ServiceInfo("emr", False),
"ga": ServiceInfo("ga", True),
"graph": ServiceInfo("graph", False),
"gtm": ServiceInfo("gtm", True),
"hbase": ServiceInfo("hbase", False),
"metakms": ServiceInfo("metakms", False),
"na": ServiceInfo("na", True),
"resource_share": ServiceInfo("resource_share", True),
"speech_saas_prod": ServiceInfo("speech_saas_prod", True),
"tag": ServiceInfo("tag", True),
"vefaas_dev": ServiceInfo("vefaas_dev", False),
"vms": ServiceInfo("vms", False),
"eco_partner": ServiceInfo("eco_partner", True),
"smc": ServiceInfo("smc", True),
"livesaas": ServiceInfo("livesaas", True),
}
# -----------------------------
# Region 校验
# -----------------------------
class RegionChecker(object):
def __init__(self, white_regions, pattern):
self.white_regions = white_regions or {}
self.pattern = pattern
def validate(self, region):
if self.white_regions and region in self.white_regions:
return True
if self.pattern:
return bool(self.pattern.match(region))
return False
region_matcher = RegionChecker(
{
"ap-singapore-1": {},
"ap-southeast-1": {},
"ap-southeast-2": {},
"ap-southeast-3": {},
"byteplus-global": {},
"cn-beijing": {},
"cn-beijing-autodriving": {},
"cn-beijing-selfdrive": {},
"cn-beijing2": {},
"cn-beijing300": {},
"cn-changsha-sdv": {},
"cn-chengdu": {},
"cn-chengdu-sdv": {},
"cn-chongqing-sdv": {},
"cn-datong": {},
"cn-east-1-dedicated": {},
"cn-gaofang-bj": {},
"cn-gaofang-gz1": {},
"cn-gaofang-nt1": {},
"cn-gaofang-nt2": {},
"cn-gaofang-nt3": {},
"cn-gaofang-nt4": {},
"cn-gaofang-nt5": {},
"cn-guangzhou": {},
"cn-guilin-boe": {},
"cn-hangzhou": {},
"cn-hjxj": {},
"cn-hjzg": {},
"cn-hlbx": {},
"cn-hlxj": {},
"cn-hlzg": {},
"cn-hongkong": {},
"cn-hongkong-pop": {},
"cn-lfbx": {},
"cn-lfxj": {},
"cn-lfzg": {},
"cn-macau-pop-sdv": {},
"cn-mainland": {},
"cn-nanjing-bbit": {},
"cn-ningbo-sdv": {},
"cn-north-1": {},
"cn-north-1-dedicated": {},
"cn-north-boe": {},
"cn-shanghai": {},
"cn-shanghai-autodriving": {},
"cn-taiwan-boe": {},
"cn-wuhan": {},
"cn-wulanchabu": {},
"cn-xian-boe-sdv": {},
"overseas-1": {},
"rec-cn": {},
"rec-sg": {},
},
re.compile(
r"^(?:[a-z]{2}-[a-z]+(?:-[a-z]+)?|(?:cn|ap|eu|na|sa|me|af)-[a-z]+-\d+(?:-(?:finance|exclusive|local|inner))?)$"
),
)
# -----------------------------
# 工具函数
# -----------------------------
def standardize_domain_service_code(service_code):
"""lower + '_' -> '-'"""
return service_code.lower().replace("_", "-")
# -----------------------------
# 解析器
# -----------------------------
class StandardEndpointResolver(EndpointProvider):
def __init__(self, fmt=None, site_stack=None, extension=None, custom_services=None):
self.fmt = fmt or DEFAULT_FORMAT
self.variables = StandardEndpointResolverVariable()
self.variables.site_stack = site_stack or SITE_STACK_VOLC_IPV4
self.variables.extension = extension or {}
self.custom_services = custom_services or {}
def endpoint_for(self, service, region, custom_bootstrap_region=None, use_dual_stack=None, **kwargs):
# 1) Region 校验
if not region_matcher.validate(region):
raise StandProviderError(
"InvalidRegion",
"invalid region %s for standard endpoint resolver, "
"please upgrade the sdk endpoint resolver to the latest version" % region
)
# 2) 默认模板
fmt = self.fmt or DEFAULT_FORMAT
# 3) 变量准备
self.variables.service = standardize_domain_service_code(service)
svc_info = ServiceInfos.get(service)
if not svc_info:
svc_info = self.custom_services.get(service)
if not svc_info:
raise StandProviderError(
"ServiceNotFound",
"service %s not found in ServiceInfos or customServices, "
"please upgrade the sdk endpoint resolver to the latest version" % service
)
# 非全局服务拼接 ".{region}",全局则为空
if not svc_info.IsGlobal:
self.variables.region = "." + region
else:
self.variables.region = ""
# 4) IP 版本 -> SiteStack
if use_dual_stack:
self.variables.site_stack = SITE_STACK_VOLC_DUAL_STACK
else:
self.variables.site_stack = SITE_STACK_VOLC_IPV4
# 5) 渲染
try:
rendered = fmt.format(**self.variables.to_dict())
except KeyError as err:
raise StandProviderError(
"TemplateExecuteError",
"failed to execute template for format %s, missing variable %s" % (fmt, err)
)
except Exception as err:
raise StandProviderError(
"TemplateExecuteError",
"failed to execute template for format %s, variable %r: %s" % (fmt, self.variables.to_dict(), err)
)
return ResolvedEndpoint(rendered)
# -----------------------------
if __name__ == "__main__":
r = StandardEndpointResolver()
try:
ep = r.endpoint_for(
service="vpc",
region="ap-southeast-1",
use_dual_stack=False,
)
print(ep.host) # 例如:vpc.ap-southeast-1.volcengine-api.com
except Exception as e:
e.print()