在线服务部署相关
更新时间:2025-11-26
拉取服务列表
使用以下代码可以拉取服务列表。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('--------------------------------DescribeServices start--------------------------------')
36 page_number = 1
37 page_size = 10
38 order_by = "createdAt"
39 order = "asc"
40
41 response = aihc_client.service.DescribeServices(
42 pageNumber=page_number,
43 pageSize=page_size,
44 orderBy=order_by,
45 order=order
46 )
47 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
48except BceHttpClientError as e:
49 if isinstance(e.last_error, BceServerError):
50 __logger.error('send request failed. Response %s, code: %s, msg: %s'
51 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
52 else:
53 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为拉取服务列表
查询服务详情
使用以下代码可以查询服务详情。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('--------------------------------DescribeService start--------------------------------')
36 service_id = "s-xxxxxxxx" # 替换为实际的服务 ID
37 response = aihc_client.service.DescribeService(serviceId=service_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为查询服务详情
获取服务状态
使用以下代码可以获取服务状态。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('-------------------------DescribeServiceStatus start--------------------------------')
36 service_id = "s-xxxxxxxx" # 替换为实际的服务 ID
37 response = aihc_client.service.DescribeServiceStatus(serviceId=service_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为获取服务状态
创建在线服务
使用以下代码可以创建在线服务。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11from baidubce.services.aihc.modules.service.service_model import ResourcePoolConf, ImageConf, ContainerConf
12
13# 加载环境变量(可选,推荐使用)
14dotenv.load_dotenv()
15
16# 配置日志
17logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
18logging.getLogger().setLevel(logging.INFO)
19logging.getLogger("baidubce").setLevel(logging.INFO)
20__logger = logging.getLogger(__name__)
21__logger.setLevel(logging.INFO)
22
23# 配置认证信息和服务端点
24HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
25AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
26SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
27
28# 创建BCE客户端配置
29config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
30
31# 创建 AihcClient 实例
32aihc_client = AihcClient(config)
33
34# 调用接口
35try:
36 __logger.info('--------------------------------CreateService start--------------------------------')
37 # 构造资源池配置
38 resource_pool_conf = ResourcePoolConf(
39 resourcePoolId="cce-xxx",
40 queueName='default',
41 resourcePoolType="",
42 resourcePoolName="pool-xxx"
43 )
44
45 # 构造镜像配置
46 image_conf = ImageConf(
47 imageType=1,
48 imageUrl="registry.baidubce.com/csm-online/sglang-router:0.2.0",
49 )
50
51 # 构造容器配置
52 containers = [ContainerConf(
53 name="container-test",
54 cpus=16,
55 memory=64,
56 acceleratorCount=1,
57 image=image_conf,
58 command=[
59 "python", "-m", "sglang_router.launch_router",
60 "--host", "0.0.0.0", "--port", "30000"
61 ],
62 )]
63
64 # 创建服务
65 response = aihc_client.service.CreateService(
66 serviceConf={
67 "name": "pythonsdk-aihc-service-test",
68 "acceleratorType": "NVIDIA H20-3e",
69 "workloadType": "",
70 "instanceCount": 1,
71 "resourcePool": resource_pool_conf,
72 "containers": containers
73 }
74 )
75 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
76except BceHttpClientError as e:
77 if isinstance(e.last_error, BceServerError):
78 __logger.error('send request failed. Response %s, code: %s, msg: %s'
79 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
80 else:
81 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为创建在线服务
删除在线服务
使用以下代码可以删除在线服务。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('----------------DeleteService start-----------------------------')
36 service_id = "s-xxxxxxxx"
37 response = aihc_client.service.DeleteService(serviceId=service_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为删除在线服务
升级在线服务
使用以下代码可以升级在线服务。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11from baidubce.services.aihc.modules.service.service_model import ResourcePoolConf, ImageConf, ContainerConf, ModifyServiceConf
12
13# 加载环境变量(可选,推荐使用)
14dotenv.load_dotenv()
15
16# 配置日志
17logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
18logging.getLogger().setLevel(logging.INFO)
19logging.getLogger("baidubce").setLevel(logging.INFO)
20__logger = logging.getLogger(__name__)
21__logger.setLevel(logging.INFO)
22
23# 配置认证信息和服务端点
24HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
25AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
26SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
27
28# 创建BCE客户端配置
29config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
30
31# 创建 AihcClient 实例
32aihc_client = AihcClient(config)
33
34# 调用接口
35try:
36 __logger.info('--------------------------------ModifyService start--------------------------------')
37 service_id = "s-xxxxxxxx"
38
39 # 构造更新后的配置
40 resource_pool_conf = ResourcePoolConf(
41 resourcePoolId="cce-xxx",
42 queueName='default',
43 resourcePoolType="",
44 resourcePoolName="pool-xxx"
45 )
46
47 service_conf = ModifyServiceConf(
48 name="service-test-updated",
49 acceleratorType="NVIDIA H20-3e",
50 instanceCount=3,
51 resourcePool=resource_pool_conf,
52 containers=[ContainerConf(
53 name="custom-container",
54 cpus=16,
55 memory=64,
56 acceleratorCount=1,
57 image=ImageConf(
58 imageType=1,
59 imageUrl="registry.baidubce.com/csm-online/sglang-router:0.2.0",
60 ),
61 command=["python", "launch.py"]
62 )]
63 )
64
65 response = aihc_client.service.ModifyService(serviceId=service_id, serviceConf=service_conf)
66 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
67except BceHttpClientError as e:
68 if isinstance(e.last_error, BceServerError):
69 __logger.error('send request failed. Response %s, code: %s, msg: %s'
70 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
71 else:
72 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为升级在线服务
服务扩缩容
使用以下代码可以服务扩缩容。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('-----------------------ModifyServiceReplicas start-----------------------------')
36 service_id = "s-xxxxxxxx"
37 instance_count = 2 # 目标实例数
38
39 response = aihc_client.service.ModifyServiceReplicas(
40 serviceId=service_id,
41 instanceCount=instance_count
42 )
43 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
44except BceHttpClientError as e:
45 if isinstance(e.last_error, BceServerError):
46 __logger.error('send request failed. Response %s, code: %s, msg: %s'
47 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
48 else:
49 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为服务扩缩容
拉取服务pod列表
使用以下代码可以拉取服务pod列表。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('--------------------------DescribeServicePods start--------------------------------')
36 service_id = "s-xxxxxxxx"
37 response = aihc_client.service.DescribeServicePods(serviceId=service_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为拉取服务pod列表
删除Pod并重建
使用以下代码可以删除Pod并重建。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('--------------------------------DeleteServicePod start--------------------------------')
36 service_id = "s-xxxxxxxx"
37 instance_id = "pod-xxxxxxxx" # Pod 实例 ID
38
39 response = aihc_client.service.DeleteServicePod(
40 serviceId=service_id,
41 instanceId=instance_id
42 )
43 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
44except BceHttpClientError as e:
45 if isinstance(e.last_error, BceServerError):
46 __logger.error('send request failed. Response %s, code: %s, msg: %s'
47 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
48 else:
49 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为删除Pod并重建
摘除Pod流量
使用以下代码可以摘除Pod流量。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('--------------------------------DisableServicePod start--------------------------------')
36 service_id = "s-xxxxxxxx"
37 instance_id = "pod-xxxxxxxx"
38 block = False # 是否阻塞等待
39
40 response = aihc_client.service.DisableServicePod(
41 serviceId=service_id,
42 instanceId=instance_id,
43 block=block
44 )
45 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
46except BceHttpClientError as e:
47 if isinstance(e.last_error, BceServerError):
48 __logger.error('send request failed. Response %s, code: %s, msg: %s'
49 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
50 else:
51 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为摘除Pod流量
配置公网访问
使用以下代码可以配置公网访问。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('------------------------ModifyServiceNetConfig start-----------------------------')
36 service_id = "s-xxxxxxxx"
37 public_access = False # False 关闭,True 开启
38
39 response = aihc_client.service.ModifyServiceNetConfig(
40 serviceId=service_id,
41 publicAccess=public_access
42 )
43 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
44except BceHttpClientError as e:
45 if isinstance(e.last_error, BceServerError):
46 __logger.error('send request failed. Response %s, code: %s, msg: %s'
47 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
48 else:
49 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为配置公网访问
获取实例组列表
使用以下代码可以获取实例组列表。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('-------------------DescribeServicePodGroups start--------------------------------')
36 service_id = "s-xxxxxxxx"
37 response = aihc_client.service.DescribeServicePodGroups(serviceId=service_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为获取实例组列表
拉取服务变更记录
使用以下代码可以拉取服务变更记录。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('----------------------DescribeServiceChangelogs start--------------------------------')
36 service_id = "s-xxxxxxxx"
37 response = aihc_client.service.DescribeServiceChangelogs(serviceId=service_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为拉取服务变更记录
查询服务变更详情
使用以下代码可以查询服务变更详情。
Python
1import logging
2import os
3import json
4
5import dotenv
6
7from baidubce.bce_client_configuration import BceClientConfiguration
8from baidubce.auth.bce_credentials import BceCredentials
9from baidubce.services.aihc.aihc_client import AihcClient
10from baidubce.exception import BceHttpClientError, BceServerError
11
12# 加载环境变量(可选,推荐使用)
13dotenv.load_dotenv()
14
15# 配置日志
16logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
17logging.getLogger().setLevel(logging.INFO)
18logging.getLogger("baidubce").setLevel(logging.INFO)
19__logger = logging.getLogger(__name__)
20__logger.setLevel(logging.INFO)
21
22# 配置认证信息和服务端点
23HOST = os.getenv('HOST') or 'https://aihc.bj.baidubce.com' # 百舸AIHC服务地址
24AK = os.getenv('AK') or 'your-access-key-id' # 您的Access Key ID
25SK = os.getenv('SK') or 'your-secret-access-key' # 您的Secret Access Key
26
27# 创建BCE客户端配置
28config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=HOST)
29
30# 创建 AihcClient 实例
31aihc_client = AihcClient(config)
32
33# 调用接口
34try:
35 __logger.info('--------------------------DescribeServiceChangelog start-----------------------------')
36 change_id = "ch-xxxxxxxx" # 变更 ID
37 response = aihc_client.service.DescribeServiceChangelog(changeId=change_id)
38 print(json.dumps(response.__dict__, default=lambda o: o.__dict__, ensure_ascii=False))
39except BceHttpClientError as e:
40 if isinstance(e.last_error, BceServerError):
41 __logger.error('send request failed. Response %s, code: %s, msg: %s'
42 % (e.last_error.status_code, e.last_error.code, str(e.last_error)))
43 else:
44 __logger.error('send request failed. Unknown exception: %s' % e)
注意: 根据接口文档去填写具体的访问参数,接口链接为查询服务变更详情
