4. 开始使用
4.1 导入依赖模块
Copied!
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions.exceptions import ClientRequestException
from huaweicloudsdkcore.exceptions.exceptions import ServerResponseException
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkevs.v2.evs_client import EvsClient
from huaweicloudsdkevs.v2.model.bss_param_for_create_volume import BssParamForCreateVolume
from huaweicloudsdkevs.v2.model.bss_param_for_resize_volume import BssParamForResizeVolume
from huaweicloudsdkevs.v2.model.create_volume_option import CreateVolumeOption
from huaweicloudsdkevs.v2.model.create_volume_request import CreateVolumeRequest
from huaweicloudsdkevs.v2.model.create_volume_request_body import CreateVolumeRequestBody
from huaweicloudsdkevs.v2.model.create_volume_scheduler_hints import CreateVolumeSchedulerHints
from huaweicloudsdkevs.v2.model.delete_volume_request import DeleteVolumeRequest
from huaweicloudsdkevs.v2.model.list_volumes_request import ListVolumesRequest
from huaweicloudsdkevs.v2.model.os_extend import OsExtend
from huaweicloudsdkevs.v2.model.resize_volume_request import ResizeVolumeRequest
from huaweicloudsdkevs.v2.model.resize_volume_request_body import ResizeVolumeRequestBody
from huaweicloudsdkevs.v2.model.show_volume_request import ShowVolumeRequest
4.2 初始化认证信息
Copied!
credentials = BasicCredentials(
ak="${ak}",
sk="${sk}",
project_id="${project_id}"
)
相关参数说明如下所示:
- ${ak}:华为云账号Access Key。
- ${sk}:华为云账号Secret Access Key。
- ${project_id}:EVS服务对应区域的项目ID。
4.3 初始化EVS服务的客户端
Copied!
config = HttpConfig.get_default_config()
config.ignore_ssl_verification = True
config.proxy_protocol = "http"
evs_client = EvsClient.new_builder() \
.with_credentials(credentials=credentials) \
.with_endpoint(endpoint="${evs_endpoint}") \
.with_http_config(config=config) \
.build()
相关参数说明如下所示:
Credential: 用户认证信息,见4.2的初始化认证信息
Endpoint: EVS服务域名,当前EVS服务支持的region信息见 地区和终端节点信息
HttpConfig: 客户端属性
4.4 配置请求参数
以创建包周期云硬盘为例:
Copied!
request_body = CreateVolumeRequestBody(
volume=self.generate_volume_option(),
bss_param=self.generate_bss_param()
)
request = CreateVolumeRequest(
body=request_body
)
相关demo见 5. SDK demo代码解析
4.5 发送请求
以创建包周期云硬盘为例:
Copied!
try:
response = evs_client.create_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5. SDK demo代码解析
5.1 创建按需云硬盘接口
demo请见Application.createDemandVolume()方法
5.1.1 请求参数说明
Copied!
request_body = CreateVolumeRequestBody(
volume=self.generate_volume_option()
)
request = CreateVolumeRequest(
body=request_body
)
5.1.2 发送请求
Copied!
try:
response = evs_client.create_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.1.3 接口及参数说明
请见 创建云硬盘接口
5.2 创建包周期云硬盘
创建包周期云硬盘,除了需要传云硬盘相关的参数,还需要传计费相关的参数,具体demo见Application.createVolume()方法
5.2.1 构造请求参数
Copied!
request_body = CreateVolumeRequestBody(
volume=self.generate_volume_option(),
bss_param=self.generate_bss_param()
)
request = CreateVolumeRequest(
body=request_body
)
5.2.2 发送请求
Copied!
try:
response = evs_client.create_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.2.3 接口及参数说明
请见 创建云硬盘接口
5.3 通过数据源创建云硬盘
EVS服务除了提供创建空白云硬盘的能力,也提供了通过数据源创建云硬盘的能力。EVS服务支持通过备份,镜像和快照三种数据源来创建云硬盘。具体方法见Application.createVolumeByDataSource()方法
注意使用快照创建云硬盘,云硬盘类型需与快照源云硬盘保持一致,云硬盘大小也需要大于数据源的云硬盘大小。
5.3.1 构造请求参数
Copied!
volume_option = self.generate_volume_option()
create_volume_scheduler_hints = CreateVolumeSchedulerHints()
create_volume_scheduler_hints.dedicated_storage_id = "${dss_id}"
request_body = CreateVolumeRequestBody(
volume=volume_option
)
request = CreateVolumeRequest(
body=request_body
)
5.3.2 发送请求
Copied!
try:
response = evs_client.create_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.3.3 接口及参数说明
请见 创建云硬盘接口
5.4 扩容云硬盘
具体demo见Application.extendVolume()方法
5.4.1 构造请求参数
Copied!
extend = OsExtend(
new_size=50
)
bss_param = BssParamForResizeVolume(
is_auto_pay="false"
)
request_body = ResizeVolumeRequestBody(
os_extend=extend,
bss_param=bss_param
)
request = ResizeVolumeRequest(
body=request_body,
volume_id="${volume_id}"
)
5.4.2 发送请求
Copied!
try:
response = evs_client.resize_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.4.3 接口及参数说明
请见 扩容云硬盘接口
5.5 查询单个云硬盘详情
具体demo见Application.getVolume()方法
5.5.1 构造请求参数
Copied!
request = ShowVolumeRequest(
volume_id="${volume_id}"
)
5.5.2 发送请求
Copied!
try:
response = evs_client.show_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.5.3 接口及参数说明
请见 查询单个云硬盘详情接口
5.6 查询所有云硬盘详情
具体demo见Application.listVolume()方法
5.6.1 构造请求参数
Copied!
request = ListVolumesRequest()
.withMarker("${volume_id}")
.withLimit(5);
.withOffset(0);
.withSortKey("")
.withAvailabilityZone("${availabilityZone}");
.withDedicatedStorageId("${dss_id}")
.withDedicatedStorageName("${dss_name}")
.withEnterpriseProjectId("${enterprise_project_id}")
.withId("${volume_id}")
.withIds("${volume_ids}")
.withMetadata("${metadata}")
.withMultiattach(true);
.withName("${name}");
.withServerId("${server_id}");
.withServiceType("${service_type}")
.withStatus("${status}")
.withVolumeTypeId(CreateVolumeOption.VolumeTypeEnum.SSD);
5.6.2 发送请求
Copied!
try:
response = evs_client.list_volumes(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.6.3 接口及参数说明
请见 查询所有云硬盘详情接口
5.7 删除云硬盘
具体demo见Application.deleteVolume()方法
5.7.1 构造请求参数
Copied!
request = DeleteVolumeRequest(
volume_id="${volume_id}"
)
5.7.2 发送请求
Copied!
try:
response = evs_client.delete_volume(request)
print(response)
except ClientRequestException as e:
print(str(e.status_code))
print(e)
except ServerResponseException as e:
print(str(e.status_code))
print(e.error_msg)
5.7.3 接口及参数说明
请见 删除云硬盘接口
EVS服务云硬盘管理示例
1. 简介
华为云提供了EVS服务端SDK,您可以直接集成服务端SDK来调用EVS的相关API,从而实现对EVS服务的快速操作。该示例展示了如何通过python版SDK对云硬盘进行管理,其中包括增(创建云硬盘),删(删除云硬盘),改(扩容云硬盘),查(查询云硬盘)。
2. 开发前准备
3. 安装SDK
您可以通过加入相应的依赖项获取和安装SDK。
4. 开始使用
4.1 导入依赖模块
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions.exceptions import ClientRequestException from huaweicloudsdkcore.exceptions.exceptions import ServerResponseException from huaweicloudsdkcore.http.http_config import HttpConfig from huaweicloudsdkevs.v2.evs_client import EvsClient from huaweicloudsdkevs.v2.model.bss_param_for_create_volume import BssParamForCreateVolume from huaweicloudsdkevs.v2.model.bss_param_for_resize_volume import BssParamForResizeVolume from huaweicloudsdkevs.v2.model.create_volume_option import CreateVolumeOption from huaweicloudsdkevs.v2.model.create_volume_request import CreateVolumeRequest from huaweicloudsdkevs.v2.model.create_volume_request_body import CreateVolumeRequestBody from huaweicloudsdkevs.v2.model.create_volume_scheduler_hints import CreateVolumeSchedulerHints from huaweicloudsdkevs.v2.model.delete_volume_request import DeleteVolumeRequest from huaweicloudsdkevs.v2.model.list_volumes_request import ListVolumesRequest from huaweicloudsdkevs.v2.model.os_extend import OsExtend from huaweicloudsdkevs.v2.model.resize_volume_request import ResizeVolumeRequest from huaweicloudsdkevs.v2.model.resize_volume_request_body import ResizeVolumeRequestBody from huaweicloudsdkevs.v2.model.show_volume_request import ShowVolumeRequest
4.2 初始化认证信息
# 初始化认证信息 credentials = BasicCredentials( ak="${ak}", sk="${sk}", project_id="${project_id}" )
相关参数说明如下所示:
4.3 初始化EVS服务的客户端
config = HttpConfig.get_default_config() config.ignore_ssl_verification = True config.proxy_protocol = "http" # 初始化客户端 evs_client = EvsClient.new_builder() \ .with_credentials(credentials=credentials) \ .with_endpoint(endpoint="${evs_endpoint}") \ .with_http_config(config=config) \ .build()
相关参数说明如下所示:
Credential: 用户认证信息,见4.2的初始化认证信息
Endpoint: EVS服务域名,当前EVS服务支持的region信息见 地区和终端节点信息
HttpConfig: 客户端属性
4.4 配置请求参数
以创建包周期云硬盘为例:
# 配置请求参数,创建包周期云硬盘需要传计费策略参数BssParamForCreateVolume request_body = CreateVolumeRequestBody( volume=self.generate_volume_option(), bss_param=self.generate_bss_param() ) # 计费策略参数,包周期云硬盘必填 # 以下参数为创建云硬盘可选参数,可以根据实际需要进行添加 # .withServerId("${server_id}") //虚拟机id,选填参数,指定了该id,会将创建好的云硬盘挂载到该目标主机上 # .withOsSCHHNTSchedulerHints(createVolumeSchedulerHints); request = CreateVolumeRequest( body=request_body )
相关demo见 5. SDK demo代码解析
4.5 发送请求
以创建包周期云硬盘为例:
try: response = evs_client.create_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5. SDK demo代码解析
5.1 创建按需云硬盘接口
demo请见Application.createDemandVolume()方法
5.1.1 请求参数说明
# 配置请求参数,创建按需云硬盘不需要传计费策略参数BssParamForCreateVolume request_body = CreateVolumeRequestBody( volume=self.generate_volume_option() ) # 以下参数为创建云硬盘可选参数,可以根据实际需要进行添加 # .withServerId("${server_id}") ; //虚拟机id,选填参数,指定了该id,会将创建好的云硬盘挂载到该目标主机上 # .withOsSCHHNTSchedulerHints(createVolumeSchedulerHints); request = CreateVolumeRequest( body=request_body )
5.1.2 发送请求
try: response = evs_client.create_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.1.3 接口及参数说明
请见 创建云硬盘接口
5.2 创建包周期云硬盘
创建包周期云硬盘,除了需要传云硬盘相关的参数,还需要传计费相关的参数,具体demo见Application.createVolume()方法
5.2.1 构造请求参数
# 配置请求参数,创建包周期云硬盘需要传计费策略参数BssParamForCreateVolume request_body = CreateVolumeRequestBody( volume=self.generate_volume_option(), bss_param=self.generate_bss_param() ) # 计费策略参数,包周期云硬盘必填 # 以下参数为创建云硬盘可选参数,可以根据实际需要进行添加 # .withServerId("${server_id}") //虚拟机id,选填参数,指定了该id,会将创建好的云硬盘挂载到该目标主机上 # .withOsSCHHNTSchedulerHints(createVolumeSchedulerHints); request = CreateVolumeRequest( body=request_body )
5.2.2 发送请求
try: response = evs_client.create_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.2.3 接口及参数说明
请见 创建云硬盘接口
5.3 通过数据源创建云硬盘
EVS服务除了提供创建空白云硬盘的能力,也提供了通过数据源创建云硬盘的能力。EVS服务支持通过备份,镜像和快照三种数据源来创建云硬盘。具体方法见Application.createVolumeByDataSource()方法 注意使用快照创建云硬盘,云硬盘类型需与快照源云硬盘保持一致,云硬盘大小也需要大于数据源的云硬盘大小。
5.3.1 构造请求参数
# 配置数据源信息, EVS服务支持备份,镜像和快照三种数据源创建云硬盘,只支持使用一种数据源创建云硬盘 volume_option = self.generate_volume_option() # 配置备份数据源 # volumeOption.setBackupId("${backup_id}"); # 配置镜像数据源 # volumeOption.setImageRef("${image_id}"); # 配置快照数据源 # volumeOption.setSnapshotId("${snapshot_id}"); # 存储池id,选填参数,指定了该id,会将创建在指定的存储池 create_volume_scheduler_hints = CreateVolumeSchedulerHints() create_volume_scheduler_hints.dedicated_storage_id = "${dss_id}" request_body = CreateVolumeRequestBody( volume=volume_option ) # 以下参数为创建云硬盘可选参数,可以根据实际需要进行添加 # .withBssParam(generateBssParam()); //计费策略参数,包周期云硬盘必填 # .withServerId("${server_id}") //虚拟机id,选填参数,指定了该id,会将创建好的云硬盘挂载到该目标主机上 # .withOsSCHHNTSchedulerHints(createVolumeSchedulerHints); request = CreateVolumeRequest( body=request_body )
5.3.2 发送请求
try: response = evs_client.create_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.3.3 接口及参数说明
请见 创建云硬盘接口
5.4 扩容云硬盘
具体demo见Application.extendVolume()方法
5.4.1 构造请求参数
# 请求参数配置 # 扩容参数 extend = OsExtend( new_size=50 ) # 扩容后云硬盘大小 # 计费相关参数 bss_param = BssParamForResizeVolume( is_auto_pay="false" ) # 创建请求体 request_body = ResizeVolumeRequestBody( os_extend=extend, bss_param=bss_param ) # 选填参数,该参数只有在云硬盘为包周期的情况下有意义。默认值为false # 创建请求 request = ResizeVolumeRequest( body=request_body, volume_id="${volume_id}" ) # 进行扩容的云硬盘id
5.4.2 发送请求
try: response = evs_client.resize_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.4.3 接口及参数说明
请见 扩容云硬盘接口
5.5 查询单个云硬盘详情
具体demo见Application.getVolume()方法
5.5.1 构造请求参数
# 创建请求 request = ShowVolumeRequest( volume_id="${volume_id}" )
5.5.2 发送请求
try: response = evs_client.show_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.5.3 接口及参数说明
请见 查询单个云硬盘详情接口
5.6 查询所有云硬盘详情
具体demo见Application.listVolume()方法
5.6.1 构造请求参数
# 创建请求 request = ListVolumesRequest() .withMarker("${volume_id}") # 分页参数,通过云硬盘ID进行分页查询 .withLimit(5); # 通过云硬盘ID进行分页查询 .withOffset(0); # 偏移量 .withSortKey("") # 返回结果按该关键字排序,默认为"created_at" # 下面为过滤参数,根据需要进行填写 .withAvailabilityZone("${availabilityZone}"); .withDedicatedStorageId("${dss_id}") .withDedicatedStorageName("${dss_name}") .withEnterpriseProjectId("${enterprise_project_id}") .withId("${volume_id}") .withIds("${volume_ids}") .withMetadata("${metadata}") .withMultiattach(true); .withName("${name}"); .withServerId("${server_id}"); .withServiceType("${service_type}") .withStatus("${status}") .withVolumeTypeId(CreateVolumeOption.VolumeTypeEnum.SSD);
5.6.2 发送请求
try: response = evs_client.list_volumes(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.6.3 接口及参数说明
请见 查询所有云硬盘详情接口
5.7 删除云硬盘
具体demo见Application.deleteVolume()方法
5.7.1 构造请求参数
# 创建请求 request = DeleteVolumeRequest( volume_id="${volume_id}" )
5.7.2 发送请求
try: response = evs_client.delete_volume(request) print(response) except ClientRequestException as e: print(str(e.status_code)) print(e) except ServerResponseException as e: print(str(e.status_code)) print(e.error_msg)
5.7.3 接口及参数说明
请见 删除云硬盘接口
6. FAQ
暂无
7. 参考
更多信息请参考 EVS服务文档