实例
创建实例
使用以下代码可以创建BCC实例,包括专属实例、普通型Ⅰ 型实例、普通型ⅠI 型实例、普通型ⅠII 型实例、GPU实例、计算优化I型实例、计算优化II型实例、存储优化I型实例:
def create_instance(self):
#如果用户未指定client_token参数,用uuid4生成一个随机字符串给client_token
client_token = str(uuid.uuid4())
#输入你要创建instance使用的镜像ID
image_id = 'your-choose-image-id'
#设置你要创建的实例类型
#各类型对应代码见下方『实例类型定义列表』
instance_type = 'your-choose-instance-type'
#选择付费方式:
#付费方式为后付费
post_paid_billing = bcc_model.Billing('Postpaid', 1)
#付费方式为包年包月计费的预付费
pre_paid_billing = bcc_model.Billing('Prepaid', 1)
#创建cds_list所需参数:
#云磁盘大小(普通云磁盘范围:5~32765,高性能云磁盘范围:5~32765,SSD云磁盘范围:50~32765
cds_size_in_gb = 5
#设置云磁盘类型
#具体类型对应见代码段下方的链接
storage_type = 'hp1'
#根据CDS数据盘快照创建CDS盘数据时,输入用户选择的快照策略id
snap_shot_id = 'your-choose-snap-shot-id'
#实例名称
instance_name = 'your-choose-instance-name' + client_token
#设置实例管理员密码(8-16位字符,英文,数字和符号必须同时存在,符号仅限!@#$%^*())
admin_pass = 'your-admin-pass'
#指定zone信息,默认为空,由系统自动选择,可通过调用查询可用区列表接口查询可用区列表
#格式为:国家-区域-可用区,如'中国-北京-可用区A'就是'cn-bj-a'
zone_name = 'country-region-zone'
#指定subnet信息,为空时将使用默认子网
subnet_id = 'your-choose-subnet-id'
#指定security_group_id信息,为空时将使用默认安全组
security_group_id = 'your-choose-security-group-id'
#输入你选择的gpu_card
#各gpu_card类型编码见下方『Gpu Type列表』
gpuCard='your-choose-Gpu-Type-id'
#输入你选择的fpga_card
#目前fpga_card类型只有KU115
fpgaCard='KU115'
#指定批量创建时的内网ip
internal_ips = ['192,168.113.110', '192,168.113.112']
#设置自动续费时间单位,可选"month"或者"year"
auto_renew_time_unit = "month"
#设置自动续费时间,按月是1-9 按年是 1-3
auto_renew_time = 1
#cds是否自动续费, 是: True 否: False
cds_auto_renew = True
#指定创建时的资源组id
res_group_id = 'your-choose-res-group-id'
self.assertEqual(
type(self.client.create_instance(1,# 设置创建BCC的核数
1,# 设置创建BCC的内存大小GB
image_id,# 设置创建BCC虚机使用的镜像
instance_type=instance_type,# 设置需要创建的BCC实例类型
billing=post_paid_billing,# 设置创建BCC的付费方式为后付费
root_disk_size_in_gb=40,# 设置BCC系统盘大小
root_disk_storage_type='hp1',# 设置BCC系统盘系统盘介质,默认使用hp1型云磁盘,系统盘磁盘类型可参见StorageType
create_cds_list=bcc_model.CreateCdsModel,(cds_size_in_gb, storage_type, snap_shot_id)# 设置需要创建的CDS数据盘
network_capacity_in_mbps=1,# 设置创建BCC使用的网络带宽大小(必须在1~200之间)
purchase_count=2,# 批量创建BCC实例时使用,必须为大于0的整数
cardCount=1,# 创建实例所要携带的GPU卡或FPGA卡数量,仅在gpuCard或fpgaCard字段不为空时有效
name=instance_name,# 设置创建BCC的名称
admin_pass=admin_pass,# 设置实例管理员密码
zone_name=zone_name,# 设置创建BCC所在的zone
subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网
security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组
gpuCard=gpuCard,#创建GPU实例时指定GPU类型
fpgaCard=fpgaCard,#创建FPGA实例时指定FPGA类型
auto_renew_time_unit=auto_renew_time_unit,#设置自动续费时间单位
auto_renew_time=auto_renew_time,#设置自动续费时间
cds_auto_renew=cds_auto_renew,#cds是否自动续费
client_token=client_token,
internal_ips=internal_ips,#指定批量创建时的内网IP
res_group_id=res_group_id#指定创建时的资源组id)),
baidubce.bce_response.BceResponse)
创建实例(通过规格参数)
使用以下代码可以通过规格参数创建BCC实例
def test_create_instance_by_spec(self):
"""
test case for create_instance_by_spec
"""
client_token = generate_client_token()
image_id = 'm-FBfg6s7W'
instance_name = 'Caesar_test_instance_' + client_token
resp = self.client.create_instance_by_spec("bcc.g4.c1m1",
image_id,
billing=post_paid_billing,# 设置创建BCC的付费方式为后付费
root_disk_size_in_gb=40,# 设置BCC系统盘大小
root_disk_storage_type='hp1',# 设置BCC系统盘系统盘介质,默认使用hp1型云磁盘,系统盘磁盘类型可参见StorageType
create_cds_list=bcc_model.CreateCdsModel,(cds_size_in_gb, storage_type, snap_shot_id)# 设置需要创建的CDS数据盘
network_capacity_in_mbps=1,# 设置创建BCC使用的网络带宽大小(必须在1~200之间)
purchase_count=2,# 批量创建BCC实例时使用,必须为大于0的整数
name=instance_name,# 设置创建BCC的名称
admin_pass=admin_pass,# 设置实例管理员密码
zone_name=zone_name,# 设置创建BCC所在的zone
subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网
security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组
auto_renew_time_unit=auto_renew_time_unit,#设置自动续费时间单位
auto_renew_time=auto_renew_time,#设置自动续费时间
cds_auto_renew=cds_auto_renew,#cds是否自动续费
client_token=client_token)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
-
必传参数:
- 规格名称(例如 bcc.g4.c1m1)
- 镜像ID
- 可选参数:参考普通创建实例接口
创建实例在专属服务器
使用以下代码可以在特定服务器中创建BCC实例:
def create_instance_from_dedicated_host(self):
#如果用户未指定client_token参数,用uuid4生成一个随机字符串给client_token
generate_client_token = str(uuid.uuid4())
client_token = generate_client_token()
#设置镜像id
image_id = 'your-choose-image-id'
#设置专属服务器id
dedicated_host_id = 'your-choose-dedicated-host-id'
#设置实例名
instance_name = 'your-choose-instance-name' + client_token
#设置实例管理员密码(8-16位字符,英文,数字和符号必须同时存在,符号仅限!@#$%^*())
admin_pass = 'your-admin-pass'
#指定subnet信息,为空时将使用默认子网
subnet_id = 'your-choose-subnet-id'
#指定security_group_id信息,为空时将使用默认安全组
security_group_id = 'your-choose-security-group-id'
#DCC专属服务器实例可以创建多块本地盘,需要指定磁盘类型及大小
#其它类型只能用一块本地盘,使用默认类型,只需指定大小
#设置创建本地磁盘大小
ephemeral_disk = EphemeralDisk(6144)
#设置本地盘类型为默认类型
ephemeral_disks = [ephemeral_disk.__dict__, ephemeral_disk.__dict__]
self.assertEqual(
type(self.client.create_instance_from_dedicated_host(1, #设置创建BCC的CPU核数
2, #设置创建BCC的内存大小GB
image_id, #设置创建BCC的镜像
dedicated_host_id, #设置创建BCC的专属服务器
purchase_count=1,# 批量创建BCC实例时使用,必须为大于0的整数
name=instance_name,# 设置创建BCC的名称
admin_pass=admin_pass,# 设置实例管理员密码
subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网
security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组
client_token=client_token
ephemeral_disks) #设置创建BCC的本地盘
baidubce.bce_response.BceResponse)
创建实例在专属服务器(支持对明文密码加密)
使用以下代码可以在特定服务器中创建BCC实例:
def create_instance_from_dedicated_host_with_encrypted_password(self):
#如果用户未指定client_token参数,用uuid4生成一个随机字符串给client_token
generate_client_token = str(uuid.uuid4())
client_token = generate_client_token()
#设置镜像id
image_id = 'your-choose-image-id'
#设置专属服务器id
dedicated_host_id = 'your-choose-dedicated-host-id'
#设置实例名
instance_name = 'your-choose-instance-name' + client_token
#设置实例管理员密码(8-16位字符,英文,数字和符号必须同时存在,符号仅限!@#$%^*())
admin_pass = 'your-admin-pass'
#指定subnet信息,为空时将使用默认子网
subnet_id = 'your-choose-subnet-id'
#指定security_group_id信息,为空时将使用默认安全组
security_group_id = 'your-choose-security-group-id'
#DCC专属服务器实例可以创建多块本地盘,需要指定磁盘类型及大小
#其它类型只能用一块本地盘,使用默认类型,只需指定大小
#设置创建本地磁盘大小
ephemeral_disk = EphemeralDisk(6144)
#设置本地盘类型为默认类型
ephemeral_disks = [ephemeral_disk.__dict__, ephemeral_disk.__dict__]
self.assertEqual(
type(self.client.create_instance_from_dedicated_host_with_encrypted_password(1, #设置创建BCC的CPU核数
2, #设置创建BCC的内存大小GB
image_id, #设置创建BCC的镜像
dedicated_host_id, #设置创建BCC的专属服务器
purchase_count=1,# 批量创建BCC实例时使用,必须为大于0的整数
name=instance_name,# 设置创建BCC的名称
admin_pass=admin_pass,# 设置实例管理员密码
subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网
security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组
client_token=client_token
ephemeral_disks) #设置创建BCC的本地盘
baidubce.bce_response.BceResponse)
查询实例列表
以下代码可以查询BCC虚机实例列表,支持通过内网ip、专属服务器id、可用区名称进行筛选:
def list_instances(self):
#批量获取列表的查询起始位置,是一个由系统产生的字符串
self.client.list_instances(marker='your-marker')
#设置返回数据大小
self.client.list_instances(max_keys=100)
#通过internal Ip过滤虚机BCC列表
self.client.list_instances(internal_ip='your-choose-internal-ip')
#通过专属服务器DCC id过滤虚机BCC列表
self.client.list_instances(dedicated_host_id='your-choose-dedicated-host-id')
#通过zone name过滤虚机BCC列表
self.client.list_instances(zone_name='country-region-zone')
#执行查询虚机列表操作
print self.client.list_instances()
查询实例详情
使用以下代码可以查询指定BCC虚机的详细信息:
def get_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
#设置是否返回创建失败信息,True表示返回创建失败信息,默认为False
contains_failed = True
self.assertEqual(
type(self.client.get_instance(instance_id, contains_failed)),
baidubce.bce_response.BceResponse)
启动实例
使用以下代码可以启动指定BCC虚机实例:
def start_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.start_instance(instance_id)),
baidubce.bce_response.BceResponse)
停止实例
使用以下代码可以停止指定BCC虚机实例:
def stop_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
#设置关机是否计费,关机不计费为True,关机计费为False,默认为False。注意:只有白名单用户才可以实行关机不计费
stop_with_no_charge = 'True'
self.assertEqual(
type(self.client.stop_instance(instance_id=instance_id,
stopWithNoCharge=stop_with_no_charge,
force_stop=force_stop)),
baidubce.bce_response.BceResponse)
重启实例
使用以下代码可以重启指定BCC虚机实例:
def reboot_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.reboot_instance(instance_id,
force_stop)),
baidubce.bce_response.BceResponse)
修改实例密码
使用以下代码可以修改指定BCC虚机实例的管理员密码:
def modify_instance_password(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
#设置新的密码
admin_pass = 'your-new-admin-pass'
self.assertEqual(
type(self.client.modify_instance_password(instance_id,admin_pass)),
baidubce.bce_response.BceResponse)
修改实例属性
使用以下代码可以修改指定BCC虚机实例的名称或网卡队列数:
def modify_instance_attributes(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
name = 'name_modify'
neteth_queuecount = 3 # 新的网卡队列数
self.assertEqual(
type(self.client.modify_instance_attributes(instance_id,
name=name,
neteth_queuecount= neteth_queuecount)),
baidubce.bce_response.BceResponse)
修改实例描述(新)
使用以下代码可以修改指定BCC虚机实例的描述:
def modify_instance_desc(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
desc = 'your_new_description'
self.assertEqual(
type(self.client.modify_instance_desc(instance_id,desc)),
baidubce.bce_response.BceResponse)
重装实例
使用以下代码可以使用镜像重建指定BCC虚机实例:
def rebuild_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
#设置使用的镜像id
image_id = 'your-choose-image-id'
#设置管理员密码
admin_pass = 'your-new-admin-pass'
self.assertEqual(
type(self.client.rebuild_instance(instance_id,image_id,admin_pass)),
baidubce.bce_response.BceResponse)
释放实例
对于后付费Postpaid以及预付费Prepaid过期的BCC虚机实例,可以使用以下代码将其释放:
def release_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.release_instance(instance_id)),
baidubce.bce_response.BceResponse)
实例扩容
支持对BCC虚机的CPU以及内存进行扩容操作,具体可参考以下代码:
def resize_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
client_token = generate_client_token()
self.assertEqual(
type(self.client.resize_instance(instance_id,
1,#新CPU核数
2,#新内存大小
client_token)),
baidubce.bce_response.BceResponse)
将实例加入安全组
以下代码可以将指定BCC虚机实例加入到指定安全组内:
def bind_instance_to_security_group(self):
#指定security_group_id信息
security_group_id = 'your-choose-security-group-id'
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.bind_instance_to_security_group(instance_id,security_group_id)),
baidubce.bce_response.BceResponse)
将实例移出安全组
以下代码可以将指定实例移出指定安全组中:
def unbind_instance_from_security_group(self):
#指定security_group_id信息
security_group_id = 'your-choose-security-group-id'
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.unbind_instance_from_security_group(instance_id,security_group_id)),
baidubce.bce_response.BceResponse)
绑定实例到标签(新)
以下代码可以将指定BCC虚机实例绑定指定标签:
def bind_instance_to_tags(self):
#输入标签信息,支持一次绑定多个标签
instance_tag1 = bcc_model.TagModel(tagKey='TestKey01',tagValue='TestValue01')
instance_tag2 = bcc_model.TagModel(tagKey='TestKey02',tagValue='TestValue02')
instance_tag_list = []
instance_tag_list.append(instance_tag1)
instance_tag_list.append(instance_tag2)
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.bind_instance_to_tags(instance_id,tags=instance_tag_list)),
baidubce.bce_response.BceResponse)
解绑实例标签(新)
以下代码可以将指定BCC虚机实例解绑指定标签:
def unbind_instance_to_tags(self):
#输入标签信息,支持一次解绑多个标签
instance_tag1 = bcc_model.TagModel(tagKey='TestKey01',tagValue='TestValue01')
instance_tag2 = bcc_model.TagModel(tagKey='TestKey02',tagValue='TestValue02')
instance_tag_list = []
instance_tag_list.append(instance_tag1)
instance_tag_list.append(instance_tag2)
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.unbind_instance_to_tags(instance_id,tags=instance_tag_list)),
baidubce.bce_response.BceResponse)
查询实例地址
使用以下代码可以查询指定BCC虚机实例的VNC URL:
def get_instance_vnc(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
self.assertEqual(
type(self.client.get_instance_vnc(instance_id)),
baidubce.bce_response.BceResponse)
实例续费
对BCC虚机的续费操作,可以延长过期时长,以下代码可以对磁盘进行续费:
def purchase_reserved_instance(self):
#设置你要操作的instance_id
instance_id = 'your-choose-instance-id'
#设置付费方式为预付费
billing = pre_paid_billing
client_token = generate_client_token()
self.assertEqual(
type(self.client.purchase_reserved_instance(instance_id,billing,client_token)),
baidubce.bce_response.BceResponse)
创建抢占实例
使用以下代码可以创建BCC抢占实例:
def create_instance_of_bid(self):
#如果用户未指定client_token参数,用uuid4生成一个随机字符串给client_token
client_token = str(uuid.uuid4())
#输入你要创建instance使用的镜像ID
image_id = 'your-choose-image-id'
#抢占实例出价模型, 市场价: "market" 自定义:"custom"
bid_model = 'market'
#抢占实例出价金额,若是自定义出价,且出价金额小于市场价,则不允许创建。当bidModel='custom'时才有效。
bid_price = 'you custom price'
#设置你要创建的实例类型
#各类型对应代码见下方『实例类型定义列表』
instance_type = 'your-choose-instance-type'
#创建cds_list所需参数:
#云磁盘大小(普通云磁盘范围:5~32765,高性能云磁盘范围:5~32765,SSD云磁盘范围:50~32765
cds_size_in_gb = 5
#设置云磁盘类型
#具体类型对应见代码段下方的链接
storage_type = 'hp1'
#根据CDS数据盘快照创建CDS盘数据时,输入用户选择的快照策略id
snap_shot_id = 'your-choose-snap-shot-id'
#实例名称
instance_name = 'your-choose-instance-name' + client_token
#设置实例管理员密码(8-16位字符,英文,数字和符号必须同时存在,符号仅限!@#$%^*())
admin_pass = 'your-admin-pass'
#指定zone信息,默认为空,由系统自动选择,可通过调用查询可用区列表接口查询可用区列表
#格式为:国家-区域-可用区,如'中国-北京-可用区A'就是'cn-bj-a'
zone_name = 'country-region-zone'
#指定subnet信息,为空时将使用默认子网
subnet_id = 'your-choose-subnet-id'
#指定security_group_id信息,为空时将使用默认安全组
security_group_id = 'your-choose-security-group-id'
#输入你选择的gpu_card
#各gpu_card类型编码
gpuCard='your-choose-Gpu-Type-id'
#输入你选择的fpga_card
#目前fpga_card类型只有KU115
fpgaCard='KU115'
#指定批量创建时的内网ip
internal_ips = ['192,168.113.110', '192,168.113.112']
#指定创建时的资源组id
res_group_id = 'your-choose-res-group-id'
self.assertEqual(
type(self.client.create_instance_of_bid(1,# 设置创建BCC的核数
1,# 设置创建BCC的内存大小GB
image_id,# 设置创建BCC虚机使用的镜像
instance_type=instance_type,# 设置需要创建的BCC实例类型
billing=post_paid_billing,# 设置创建BCC的付费方式为后付费
root_disk_size_in_gb=40,# 设置BCC系统盘大小
root_disk_storage_type='hp1',# 设置BCC系统盘系统盘介质,默认使用hp1型云磁盘,系统盘磁盘类型可参见StorageType
create_cds_list=bcc_model.CreateCdsModel,(cds_size_in_gb, storage_type, snap_shot_id)# 设置需要创建的CDS数据盘
network_capacity_in_mbps=1,# 设置创建BCC使用的网络带宽大小(必须在1~200之间)
purchase_count=2,# 批量创建BCC实例时使用,必须为大于0的整数
cardCount=1,# 创建实例所要携带的GPU卡或FPGA卡数量,仅在gpuCard或fpgaCard字段不为空时有效
name=instance_name,# 设置创建BCC的名称
admin_pass=admin_pass,# 设置实例管理员密码
zone_name=zone_name,# 设置创建BCC所在的zone
subnet_id=subnet_id,# 指定subnet信息,为空时将使用默认子网
security_group_id=security_group_id,# 指定securityGroup信息,为空时将使用默认安全组
gpuCard=gpuCard,#创建GPU实例时指定GPU类型
fpgaCard=fpgaCard,#创建FPGA实例时指定FPGA类型
bid_model=market,#创建抢占实例出价模型
client_token=client_token,
internal_ips=internal_ips,#指定批量创建时的内网IP
res_group_id=res_group_id#指定创建时的资源组id)),
baidubce.bce_response.BceResponse)
查询回收站实例列表
使用以下代码可以查询回收站实例列表
def test_list_recycled_instances(self):
"""
test case for list recycled instances
"""
resp = self.client.list_recycled_instances(payment_timing="prepay", recycle_begin='2023-03-11T00:00:00Z')
print(resp)
# print(json.loads(resp.content.decode('utf-8')))
定时释放 (限定后付费实例)
使用以下代码可以设置后付费实例定时释放,到达预设时间后自动释放bcc,自动释放时间可查询实例详情ReleaseTime。
def test_auto_release_instance(self):
instance_id = "i-XS7Db00e"
resp = self.client.auto_release_instance(instance_id=instance_id, release_time='2023-03-15T14:20:00Z')
# print(json.loads(resp.content.decode('utf-8')))
查询关联部署集的实例详情
使用以下代码可以查询关联了部署集的实例详情
def test_get_instance_with_deploy_set(self):
instance_id = "i-oUXBvdIx"
self.assertEqual(
type(self.client.get_instance_with_deploy_set(instance_id)),
baidubce.bce_response.BceResponse)
print(self.client.get_instance(instance_id))
查询关联部署集和失败相关字段实例详情
使用以下代码可以查询关联部署集和失败相关字段实例详情
def test_get_instance_with_deploy_set_and_failed(self):
"""
test case for get_instance
"""
instance_id = "i-oUXBvdIx"
self.assertEqual(
type(self.client.get_instance_with_deploy_set_and_failed(instance_id)),
baidubce.bce_response.BceResponse)
print(self.client.get_instance(instance_id))
修改实例主机名
使用以下代码可以修改实例主机名
def test_modify_instance_hostname(self):
"""
test case for modify_instance_hostname
"""
instance_id = "i-XS7Db00e"
resp = self.client.modify_instance_hostname(instance_id=instance_id, hostname="new.hostname20230315",
auto_reboot=True, is_open_hostname_domain=True)
# print(json.loads(resp.content.decode('utf-8')))
后付费资源从回收站恢复计费
使用以下代码可以将后付费资源从回收站恢复计费
def test_recovery_instances(self):
"""
test case for recovery_instances
"""
instance_ids = ["i-XS7Db00e", "i-FhvOuv4t"]
resp = self.client.recovery_instances(instance_ids)
# print(json.loads(resp.content.decode('utf-8')))
查询竞价实例市场价
使用以下代码可以查询竞价实例市场价
def test_get_bid_instance_price(self):
"""
test case for get_bid_instance_price
"""
resp = self.client.get_bid_instance_price(instance_type='N3', cpu_count=1, memory_cap_in_gb=1,
root_disk_size_in_gb=20)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
查询竞价实例套餐
使用以下代码可以查询竞价实例套餐
def test_list_bid_flavor(self):
"""
test case for list_bid_flavor
"""
resp = self.client.list_bid_flavor()
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
释放保护
使用以下代码可以为BCC实例设置释放保护,实例当前设置可查询实例详情DeletionProtection,默认0不保护,1释放保护中:
def test_modify_deletion_protection(self):
"""
test case for modify_deletion_protection
"""
resp = self.client.modify_deletion_protection("i-XS7Db00e", 1)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
查询实例套餐价格
通过以下代码可以查询实例资源套餐规格对应的价格
def test_get_price_by_spec(self):
"""
test case for get_price_by_spec
"""
resp = self.client.get_price_by_spec(spec_id="sl-fJDs8G9i", spec="bcc.g4.c1m1", payment_timing="prepay",
zone_name="szth", purchase_num=2, purchase_length=2)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
实例变更子网
如下代码可以变更实例的子网
def test_instance_change_subnet(self):
"""
test case for instance_change_subnet
"""
resp = self.client.instance_change_subnet(instance_id="i-oUXBvdIx", subnet_id="sbn-5k3wawcrtktz",
internal_ip="192.168.32.2", reboot=True)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
实例变更VPC
如下代码可以变更实例的VPC
def test_instance_change_vpc(self):
"""
test case for instance_change_vpc
"""
resp = self.client.instance_change_vpc(instance_id="i-oUXBvdIx", subnet_id="sbn-5k3wawcrtktz",
internal_ip="192.168.32.2", reboot=True)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
查询实例绑定的弹性网卡列表
使用以下代码可以查询实例绑定的弹性网卡列表
def test_list_instance_enis(self):
"""
test case for list_instance_enis
"""
resp = self.client.list_instance_enis(instance_id="i-oUXBvdIx")
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
查询实例套餐规格(新)
通过以下代码可以查询实例资源套餐规格列表信息
def test_list_flavor_spec(self):
"""
test case for list_flavor_spec
"""
resp = self.client.list_flavor_spec(zone_name='cn-bd-a')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
变配实例(通过实例套餐规格)
使用以下代码可以选择CPU,MemoryCapacityInGB,EphemeralDisks变配指定BCC实例:
def test_resize_instance_by_spec(self):
"""
test case for resize_instance_by_spec
"""
resp = self.client.resize_instance_by_spec(instance_id="i-oUXBvdIx", spec='bcc.ic1.c1m1')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
重装实例(批量)
使用以下代码重装实例:
def test_batch_rebuild_instances(self):
"""
test case for resize_instance_by_spec
"""
resp = self.client.batch_rebuild_instances(image_id="m-U4nNXY9T", admin_pass='123456', keypair_id="123",
instance_ids=["i-oUXBvdIx"])
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
计费变更-转预付费
使用以下代码对实例计费变更-转预付费:
def test_change_to_prepaid(self):
"""
test case for change_to_prepaid
"""
resp = self.client.change_to_prepaid(instance_id="i-45IP2Tn7", duration=3, relation_cds=True)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
查询可关机不计费的实例列表
查询可关机不计费的BCC实例列表:
def test_list_instance_no_charge(self):
"""
test case for list_instance_no_charge
"""
resp = self.client.list_instance_no_charge(keypair_id='k-Mk1c8QPE', zone_name='cn-bj-a')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
取消竞价实例订单
通过以下代码可以取消竞价实例订单
def test_cancel_bid_order(self):
"""
test case for cancel_bid_order
"""
resp = self.client.cancel_bid_order(order_id='test_id')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
开通自动续费(包含关联产品)
自动续费仅限预付费产品
def test_batch_create_auto_renew_rules(self):
"""
test case for batch_create_auto_renew_rules
"""
resp = self.client.batch_create_auto_renew_rules(instance_id='i-45IP2Tn7', renew_time=2, renew_time_unit='year')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
关闭自动续费(包含关联产品)
自动续费仅限预付费产品
def test_batch_delete_auto_renew_rules(self):
"""
test case for batch_delete_auto_renew_rules
"""
resp = self.client.batch_delete_auto_renew_rules(instance_id='i-45IP2Tn7')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
释放回收站实例
回收站实例7天后自动释放,清理回收站资源,可以使用以下代码将其释放:
def test_delete_recycled_instance(self):
"""
test case for delete_recycled_instance
"""
resp = self.client.delete_recycled_instance(instance_id='i-LKFVi0gI')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
根据实例ID批量查询实例列表
以下代码可以根据实例ID批量查询实例列表
def test_list_instance_by_instance_ids(self):
"""
test case for list_instance_by_instance_ids
"""
resp = self.client.list_instance_by_instance_ids(instance_ids=['i-45IP2Tn7', 'i-FhvOuv4t', 'i-oUXBvdIx'],
marker='123', max_keys=10000)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
批量删除实例
以下代码可以批量删除实例,同时也可以选择删除其关联的资源
def test_batch_delete_instance_with_related_resource(self):
"""
test case for batch_delete_instance_with_related_resource
"""
resp = self.client.batch_delete_instance_with_related_resource(instance_ids=['i-FhvOuv4t'],
related_release_flag=True, )
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
批量重启实例
使用以下代码可以批量重启实例
def test_batch_start_instance(self):
"""
test case for batch_start_instance
"""
resp = self.client.batch_start_instance(instance_ids=['i-FhvOuv4t'])
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
批量停止实例
使用以下代码可以批量停止实例
def test_batch_stop_instance(self):
"""
test case for batch_stop_instance
"""
resp = self.client.batch_stop_instance(instance_ids=['i-FhvOuv4t'], force_stop=True)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
批量变配实例
使用以下代码可以批量变配实例
def test_batch_resize_instance(self):
"""
test case for batch_resize_instance
"""
resp = self.client.batch_resize_instance(instance_ids=['i-FhvOuv4t'], spec='bcc.g4.c1m1',
subnet_id='subnet_id', logical_zone='zone_name', internal_ip_v4='ipv4')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
查询可以变配的实例规格
使用以下代码可以查询可以变配的实例规格
def test_list_available_resize_specs(self):
"""
test case for list_available_resize_specs
"""
resp = self.client.list_available_resize_specs(instance_ids=['i-FhvOuv4t'], spec='bcc.g4.c1m1',
spec_id='subnet_id', logical_zone='zone_name')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
批量转预付费
使用以下代码可以将实例批量转预付费,若需要对实例关联的cds变更,则必须在CdsList中传入所有需要转为预付费的cdsId,若所有cds均变更计费传入all。
def test_batch_change_instance_to_prepay(self):
"""
test case for batch_change_instance_to_prepay
"""
req1 = PayTimingChangeReqModel('i-FhvOuv4t', cdsList=['cds1'], autoPay=True, duration=1)
req2 = PayTimingChangeReqModel('i-45IP2Tn7', cdsList=['all'])
req = [req1, req2]
resp = self.client.batch_change_instance_to_prepay(req)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
批量转后付费
使用以下代码可以将实例批量转后付费,若需要对实例关联的cds变更,则必须在CdsList中传入所有需要转为预付费的cdsId,若所有cds均变更计费传入all。
def test_batch_change_instance_to_postpay(self):
"""
test case for batch_change_instance_to_postpay
"""
req1 = PayTimingChangeReqModel('i-FhvOuv4t', cdsList=['cds1'])
req2 = PayTimingChangeReqModel('i-45IP2Tn7', cdsList=['all'])
req = [req1, req2]
resp = self.client.batch_change_instance_to_postpay(req)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
获取实例角色列表
使用以下代码可以获取实例角色列表
def test_list_instance_roles(self):
"""
test case for list_instance_roles
"""
resp = self.client.list_instance_roles()
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
绑定角色
使用以下代码可以为BCC实例绑定角色
def test_bind_instance_role(self):
"""
test case for bind_instance_role
"""
resp = self.client.bind_instance_role(['i-FhvOuv4t', 'i-45IP2Tn7'], role_name='admin')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
解绑角色
使用以下代码可以为BCC实例解绑角色
def test_unbind_instance_role(self):
"""
test case for unbind_instance_role
"""
resp = self.client.unbind_instance_role(['i-FhvOuv4t', 'i-45IP2Tn7'], role_name='admin')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
添加Ipv6
使用以下代码可以为BCC实例添加Ipv6
def test_add_ipv6(self):
"""
test case for add_ipv6
"""
resp = self.client.add_ipv6(instance_id='i-FhvOuv4t', ipv6_address='new_addr', reboot=True)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)
释放Ipv6
使用以下代码可以为BCC实例释放Ipv6
def test_del_ipv6(self):
"""
test case for delete_ipv6
"""
resp = self.client.delete_ipv6(instance_id='i-FhvOuv4t', reboot=True)
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)