镜像
更新时间:2023-03-31
通过实例创建自定义镜像
- 用于创建自定义镜像,默认每个账号配额20个,创建后的镜像可用于创建实例
- 只有 Running 或 Stopped 状态的实例才可以执行成功
-
仅限通过系统盘快照创建自定义镜像
def create_image_from_instance_id(self): client_token = generate_client_token() #设置创建镜像名称 image_name = 'your-image-name' + client_token #用于创建镜像的实例ID instance_id='your-choose-instance-id' self.assertEqual( type(self.client.create_image_from_instance_id(image_name,instance_id=instance_id,client_token=client_token)), baidubce.bce_response.BceResponse)
通过快照创建自定义镜像
通过快照创建时,只有 Available 状态的快照才可以执行成功:
def create_image_from_snapshot_id(self):
client_token = generate_client_token()
#设置创建镜像名称
image_name = 'your-image-name' + client_token
#用于创建镜像的快照ID
snapshot_id='your-choose-snapshot-id'
self.assertEqual(
type(self.client.create_image_from_snapshot_id(image_name,snapshot_id=snapshot_id,client_token=client_token)),
baidubce.bce_response.BceResponse)
查询镜像列表
用于查询用户所有的镜像信息
查询的镜像信息中包括系统镜像、自定义镜像和服务集成镜像
支持按 imageType 来过滤查询,此参数非必需,未设置时默认为 All,即查询所有类型的镜像:
def list_images(self):
#指定要查询何种类型的镜像
#All(所有)
#System(系统镜像/公共镜像)
#Custom(自定义镜像)
#Integration(服务集成镜像)
#Sharing(共享镜像)
#GpuBccSystem(GPU专用公共镜像)
#GpuBccCustom(GPU专用自定义镜像)
#FpgaBccSystem(FPGA专用公共镜像)
#FpgaBccCustom(FPGA专用自定义镜像)
image_type = 'All'
#批量获取列表的查询的起始位置
marker = 'your-marker'
#每页包含的最大数量
max_keys = 100
self.assertEqual(
type(self.client.list_images(image_type=image_type,marker=marker,max_keys=max_keys)),
baidubce.bce_response.BceResponse)
查询镜像详情
用于根据指定镜像ID查询单个镜像的详细信息:
def get_image(self):
#待查询镜像ID
image_id='your-choose-image-id'
self.assertEqual(
type(self.client.get_image(image_id)),
baidubce.bce_response.BceResponse)
删除自定义镜像
用于删除用户自己的指定的自定义镜像,仅限自定义镜像,系统镜像和服务集成镜像不能删除
镜像删除后无法恢复,不能再用于创建、重置实例:
def delete_image(self):
#待删除镜像ID
image_id='your-choose-image-id'
self.assertEqual(
type(self.client.delete_image(image_id)),
baidubce.bce_response.BceResponse)
跨区域复制镜像(新)
用于用户跨区域复制自定义镜像,仅限自定义镜像,系统镜像和服务集成镜像不能复制:
def remote_copy_image(self):
#设置被复制镜像id
image_id = 'your-choose-image-id'
#设置目的region id,可多个region
destRegions = ['bd', 'su']
#设置复制镜像名称
remote_image_name = 'new-remote-image'
self.assertEqual(
type(self.client.remote_copy_image(image_id=image_id,
name=remote_image_name,
destRegions=destRegions)),
baidubce.bce_response.BceResponse)
取消跨区域复制镜像(新)
用于取消跨区域复制自定义镜像:
def cancle_remote_copy_image(self):
#设置镜像id
image_id = 'your-choose-image-id'
self.assertEqual(
type(self.client.cancle_remote_copy_image(image_id=image_id)),
baidubce.bce_response.BceResponse)
共享自定义镜像(新)
用于共享用户自己的指定的自定义镜像,仅限自定义镜像,系统镜像和服务集成镜像不能共享:
def share_image(self):
#待共享镜像id
image_id = 'your-choose-image-id'
#被共享用户id
account_id = 'your-choose-account-id'
self.assertEqual(
type(self.client.share_image(image_id=image_id,account_id=account_id)),
baidubce.bce_response.BceResponse)
取消共享自定义镜像(新)
用于取消共享用户自己的指定的自定义镜像:
def unshare_image(self):
#待取消共享镜像id
image_id = 'your-choose-image-id'
#待取消被共享用户id
account_id = 'your-choose-account-id'
self.assertEqual(
type(self.client.unshare_image(image_id=image_id,account_id=account_id)),
baidubce.bce_response.BceResponse)
查询已共享用户列表(新)
用于查询镜像已共享的用户列表:
def list_shared_user(self):
#待查询镜像id
image_id = 'your-choose-image-id'
self.assertEqual(
type(self.client.list_shared_user(image_id=image_id)),
baidubce.bce_response.BceResponse)
根据实例短id批量查询os信息(新)
用于根据实例短id批量查询OS信息:
def list_os(self):
#待查询实例id
instance_id1 = 'your-choose-instance-id1'
instance_id2 = 'your-choose-instance-id2'
instance_ids = []
instance_ids.append(instance_id1, instance_id2)
self.assertEqual(
type(self.client.list_os(instance_ids=instance_ids)),
baidubce.bce_response.BceResponse)
镜像链绑定标签
使用以下代码可以给指定镜像绑定标签
def test_bind_image_to_tags(self):
"""
test case for bind_image_to_tags
"""
tag1 = bcc_model.TagModel(tagKey='TestKey02',
tagValue='TestValue02')
tag2 = bcc_model.TagModel(tagKey='TestKey03',
tagValue='TestValue03')
tag_list = []
tag_list.append(tag1)
tag_list.append(tag2)
resp = self.client.bind_image_to_tags(image_id='i-FhvOuv4t', tags=tag_list)
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_unbind_image_to_tags(self):
"""
test case for unbind_image_to_tags
"""
tag1 = bcc_model.TagModel(tagKey='TestKey02',
tagValue='TestValue02')
tag2 = bcc_model.TagModel(tagKey='TestKey03',
tagValue='TestValue03')
tag_list = []
tag_list.append(tag1)
tag_list.append(tag2)
resp = self.client.unbind_image_to_tags(image_id='i-FhvOuv4t', tags=tag_list)
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_import_custom_image(self):
"""
test case for import_custom_image
"""
resp = self.client.import_custom_image(os_name='os-name', os_arch='os-arch', os_type='os-type',
os_version='os_version', name='name', bos_url='url')
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)