Skip to content

Commit 1654059

Browse files
author
v_suiyufei
committed
aihc-dev-12971 [Task] 【python sdk】服务扩缩容和配置公网访问接口
Change-Id: I962984eb3b725e04447d876be13b7225646f1405
1 parent 51ea40b commit 1654059

3 files changed

Lines changed: 117 additions & 85 deletions

File tree

baidubce/services/aihc/modules/service/service_client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,31 +245,31 @@ def ModifyServiceReplicas(
245245
instanceCount: int
246246
):
247247
"""
248-
扩缩容在线服务
248+
服务扩缩容
249249
250-
参考文档:https://cloud.baidu.com/doc/AIHC/s/Omb4vbjhh
250+
参考文档:https://cloud.baidu.com/doc/AIHC/s/4mb4vaipm
251251
252252
Args:
253-
serviceId: 服务ID(必填)
254-
instanceCount: 实例数量(必填)
253+
serviceId: 服务ID(必填,Query参数
254+
instanceCount: 实例数量(必填,Query参数
255255
256256
Returns:
257257
baidubce.bce_response.BceResponse: 扩缩容结果
258+
259+
Raises:
260+
BceHttpClientError: 当API调用失败时抛出
261+
BceServerError: 当服务器返回错误时抛出
258262
"""
259263
path = b'/'
260264
params = {
261265
'action': 'ModifyServiceReplicas',
262266
'serviceId': serviceId,
263-
}
264-
265-
body = {
266-
'instanceCount': instanceCount,
267+
'instanceCount': instanceCount
267268
}
268269

269270
return self._send_request(
270271
http_methods.POST,
271272
path,
272-
body=json.dumps(body),
273273
params=params
274274
)
275275

@@ -425,12 +425,16 @@ def ModifyServiceNetConfig(
425425
参考文档:https://cloud.baidu.com/doc/AIHC/s/Gmb4vgurj
426426
427427
Args:
428-
serviceId: 服务ID(必填)
429-
publicAccess: 是否开启公网访问(必填)
430-
eip: 弹性公网IP(可选)
428+
serviceId: 服务ID(必填,Query参数
429+
publicAccess: 是否开启公网访问(必填,Query参数
430+
eip: 弹性公网IP(可选,Query参数
431431
432432
Returns:
433433
baidubce.bce_response.BceResponse: 公网访问配置结果
434+
435+
Raises:
436+
BceHttpClientError: 当API调用失败时抛出
437+
BceServerError: 当服务器返回错误时抛出
434438
"""
435439
path = b'/'
436440
params = {

baidubce/services/aihc/modules/service/service_model.py

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class ResourcePoolConf(dict):
3232

3333
def __init__(
3434
self,
35-
resourcePoolId: str,
36-
queueName: str,
37-
resourcePoolType: str,
38-
resourcePoolName: str
35+
resourcePoolId, # type: str
36+
queueName, # type: str
37+
resourcePoolType, # type: str
38+
resourcePoolName # type: str
3939
):
4040
super().__init__()
4141
self["resourcePoolId"] = resourcePoolId
@@ -52,7 +52,7 @@ class PFSConfig(dict):
5252
InstanceId (str): PFS实例ID(如"pfs-12345")
5353
"""
5454

55-
def __init__(self, sourcePath: str, InstanceId: str):
55+
def __init__(self, sourcePath, InstanceId):
5656
super().__init__()
5757
self["sourcePath"] = sourcePath
5858
self["InstanceId"] = InstanceId
@@ -72,9 +72,9 @@ class VolumnConf(dict):
7272

7373
def __init__(
7474
self,
75-
volumeType: str,
76-
volumnName: str,
77-
pfs: Optional[dict] = None
75+
volumeType, # type: str
76+
volumnName, # type: str
77+
pfs, # type: Optional[dict] = None
7878
):
7979
super().__init__()
8080
self["volumeType"] = volumeType
@@ -92,8 +92,8 @@ class StorageConf(dict):
9292

9393
def __init__(
9494
self,
95-
shmSize: Optional[int] = None,
96-
volumns: Optional[List[dict]] = None
95+
shmSize, # type: Optional[int] = None,
96+
volumns # type: Optional[List[dict]] = None
9797
):
9898
super().__init__()
9999
if shmSize is not None:
@@ -116,10 +116,10 @@ class ImageConf(dict):
116116

117117
def __init__(
118118
self,
119-
imageType: int,
120-
imageUrl: str,
121-
username: Optional[str] = None,
122-
password: Optional[str] = None
119+
imageType, # type: int
120+
imageUrl, # type: str
121+
username, # type: Optional[str]
122+
password # type: Optional[str]
123123
):
124124
super().__init__()
125125
self["imageType"] = imageType
@@ -136,7 +136,7 @@ class ExecAction(dict):
136136
command (list): 要执行的命令数组(如["/bin/sh", "-c", "echo ready"])
137137
"""
138138

139-
def __init__(self, command: list):
139+
def __init__(self, command): # type: list
140140
super().__init__()
141141
self["command"] = command
142142

@@ -148,7 +148,7 @@ class HTTPGetAction(dict):
148148
port (int): 请求端口号(如8080)
149149
"""
150150

151-
def __init__(self, path: str, port: int):
151+
def __init__(self, path, port): # type: (str, int)
152152
super().__init__()
153153
self["path"] = path
154154
self["port"] = port
@@ -160,7 +160,7 @@ class TCPSocketAction(dict):
160160
port (int): 要检查的端口号
161161
"""
162162

163-
def __init__(self, port: int):
163+
def __init__(self, port): # type: int
164164
super().__init__()
165165
self["port"] = port
166166

@@ -177,9 +177,9 @@ class ProbeHandlerConf(dict):
177177

178178
def __init__(
179179
self,
180-
exec: Optional[dict] = None,
181-
httpGet: Optional[dict] = None,
182-
tcpSocketAction: Optional[dict] = None
180+
exec, # type: Optional[dict]
181+
httpGet, # type: Optional[dict]
182+
tcpSocketAction # type: Optional[dict]
183183
):
184184
super().__init__()
185185
if exec is not None:
@@ -203,12 +203,12 @@ class ProbeConf(dict):
203203

204204
def __init__(
205205
self,
206-
initialDelaySeconds: int,
207-
timeoutSeconds: int,
208-
periodSeconds: int,
209-
successThreshold: int,
210-
failureThreshold: int,
211-
handler: dict
206+
initialDelaySeconds, # type: int
207+
timeoutSeconds, # type: int
208+
periodSeconds, # type: int
209+
successThreshold, # type: int
210+
failureThreshold, # type: int
211+
handler # type: dict
212212
):
213213
super().__init__()
214214
self["initialDelaySeconds"] = initialDelaySeconds
@@ -240,19 +240,19 @@ class ContainerConf(dict):
240240

241241
def __init__(
242242
self,
243-
name: str,
244-
cpus: int,
245-
memory: int,
246-
acceleratorCount: int,
247-
image: dict,
248-
command: Optional[list] = None,
249-
runArgs: Optional[list] = None,
250-
ports: Optional[list] = None,
251-
envs: Optional[dict] = None,
252-
volumeMounts: Optional[list] = None,
253-
readinessProbe: Optional[dict] = None,
254-
startupsProbe: Optional[dict] = None,
255-
livenessProbe: Optional[dict] = None
243+
name, # type: str
244+
cpus, # type: int
245+
memory, # type: int
246+
acceleratorCount, # type: int
247+
image, # type: dict
248+
command, # type: Optional[list]
249+
runArgs, # type: Optional[list]
250+
ports, # type: Optional[list]
251+
envs, # type: Optional[dict]
252+
volumeMounts, # type: Optional[list]
253+
readinessProbe, # type: Optional[dict]
254+
startupsProbe, # type: Optional[dict]
255+
livenessProbe # type: Optional[dict]
256256
):
257257
super().__init__()
258258
self["name"] = name
@@ -286,7 +286,7 @@ class AiGatewayConf(dict):
286286
enableAuth (bool): 是否开启鉴权(True/False)
287287
"""
288288

289-
def __init__(self, enableAuth: bool):
289+
def __init__(self, enableAuth): # type: bool
290290
super().__init__()
291291
self["enableAuth"] = enableAuth
292292

@@ -303,10 +303,10 @@ class AccessConf(dict):
303303

304304
def __init__(
305305
self,
306-
publicAccess: Optional[bool] = None,
307-
eip: Optional[str] = None,
308-
aiGateway: Optional[dict] = None,
309-
networkType: Optional[str] = None
306+
publicAccess, # type: Optional[bool]
307+
eip, # type: Optional[str]
308+
aiGateway, # type: Optional[dict]
309+
networkType # type: Optional[str]
310310
):
311311
super().__init__()
312312
if publicAccess is not None:
@@ -326,7 +326,7 @@ class LogConf(dict):
326326
persistent (Optional[bool]): 是否持久化容器标准输出日志(默认False)
327327
"""
328328

329-
def __init__(self, persistent: Optional[bool] = None):
329+
def __init__(self, persistent=None): # type: Optional[bool]
330330
super().__init__()
331331
if persistent is not None: # 日志持久化开关
332332
self["persistent"] = persistent
@@ -344,8 +344,8 @@ class CanaryStrategyConf(dict):
344344

345345
def __init__(
346346
self,
347-
maxSurge: Optional[int] = None,
348-
maxUnavailable: Optional[int] = None
347+
maxSurge, # type: Optional[int]
348+
maxUnavailable # type: Optional[int]
349349
):
350350
super().__init__()
351351
if maxSurge is not None:
@@ -361,7 +361,7 @@ class ScheduleConf(dict):
361361
priority (str): 优先级("high"/"normal"/"low")
362362
"""
363363

364-
def __init__(self, priority: str):
364+
def __init__(self, priority): # type: str
365365
super().__init__()
366366
self["priority"] = priority
367367

@@ -399,11 +399,11 @@ class Misc(dict):
399399

400400
def __init__(
401401
self,
402-
podLabels: Optional[dict] = None,
403-
podAnnotations: Optional[dict] = None,
404-
gracePeriodSec: Optional[int] = None,
405-
fedPodsPerIns: Optional[int] = None,
406-
enableRDMA: Optional[bool] = None
402+
podLabels, # type: Optional[dict]
403+
podAnnotations, # type: Optional[dict]
404+
gracePeriodSec, # type: Optional[int]
405+
fedPodsPerIns, # type: Optional[int]
406+
enableRDMA # type: Optional[bool]
407407
):
408408
super().__init__()
409409
if podLabels is not None:
@@ -437,17 +437,17 @@ class ServiceConf(dict):
437437

438438
def __init__(
439439
self,
440-
name: str,
441-
acceleratorType: str,
442-
workloadType: str,
443-
instanceCount: int,
444-
resourcePool: dict,
445-
containers: List[dict],
446-
storage: Optional[dict] = None,
447-
access: Optional[dict] = None,
448-
log: Optional[dict] = None,
449-
deploy: Optional[dict] = None,
450-
misc: Optional[dict] = None
440+
name, # type: str
441+
acceleratorType, # type: str
442+
workloadType, # type: str
443+
instanceCount, # type: int
444+
resourcePool, # type: dict
445+
containers, # type: List[dict]
446+
storage, # type: Optional[dict]
447+
access, # type: Optional[dict]
448+
log, # type: Optional[dict]
449+
deploy, # type: Optional[dict]
450+
misc # type: Optional[dict]
451451
):
452452
super().__init__()
453453
self["name"] = name
@@ -485,15 +485,15 @@ class ModifyServiceConf(dict):
485485

486486
def __init__(
487487
self,
488-
name: str,
489-
acceleratorType: str,
490-
instanceCount: int,
491-
resourcePool: dict,
492-
containers: List[dict],
493-
storage: Optional[dict] = None,
494-
log: Optional[dict] = None,
495-
deploy: Optional[dict] = None,
496-
misc: Optional[dict] = None
488+
name, # type: str
489+
acceleratorType, # type: str
490+
instanceCount, # type: int
491+
resourcePool, # type: dict
492+
containers, # type: List[dict]
493+
storage, # type: Optional[dict]
494+
log, # type: Optional[dict]
495+
deploy, # type: Optional[dict]
496+
misc # type: Optional[dict]
497497
):
498498
super().__init__()
499499
self["name"] = name

sample/aihc/aihc_service_sample.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,34 @@ def main():
371371
# else:
372372
# __logger.error('send request failed. Unknown exception: %s' % e)
373373

374+
# # 服务扩缩容
375+
# try:
376+
# __logger.info('-----------------------ModifyServiceReplicas start...-----------------------------')
377+
# service_id = "s-xxx"
378+
# response = aihc_client.service.ModifyServiceReplicas(serviceId=service_id, instanceCount=2)
379+
# print(json.dumps(to_dict(response), ensure_ascii=False))
380+
# __logger.info('ModifyServiceReplicas: %s', response.__dict__.keys())
381+
# except BceHttpClientError as e:
382+
# if isinstance(e.last_error, BceServerError):
383+
# __logger.error('send request failed. Response %s, code: %s, msg: %s'
384+
# % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
385+
# else:
386+
# __logger.error('send request failed. Unknown exception: %s' % e)
387+
388+
# # 配置公网访问
389+
# try:
390+
# __logger.info('------------------------ModifyServiceNetConfig start...-----------------------------')
391+
# service_id = "s-xxx"
392+
# response = aihc_client.service.ModifyServiceNetConfig(serviceId=service_id, publicAccess=False)
393+
# print(json.dumps(to_dict(response), ensure_ascii=False))
394+
# __logger.info('ModifyServiceNetConfig: %s', response.__dict__.keys())
395+
# except BceHttpClientError as e:
396+
# if isinstance(e.last_error, BceServerError):
397+
# __logger.error('send request failed. Response %s, code: %s, msg: %s'
398+
# % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
399+
# else:
400+
# __logger.error('send request failed. Unknown exception: %s' % e)
401+
374402
# # 删除服务
375403
# try:
376404
# __logger.info('----------------DeleteService start...-----------------------------')

0 commit comments

Comments
 (0)