前置条件
开通服务
申请开通云办公服务的具体操作请参考开通云办公服务API
获取AK/SK
开发者在使用前需先获取账号的ak、sk、projectId、endpoint。
您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。 endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。 projectId 云服务所在项目 ID ,根据你想操作的项目所属区域选择对应的项目 ID 。
运行环境
Python3 及其以上版本。
SDK获取和安装
您可以通过加入相应的依赖项获取和安装SDK。
Copied!
pip install huaweicloudsdkworkspace
示例代码
调用前请替换示例中的变量,请根据需要执行的方法注释掉无关方法,并执行Run Application。
Copied!
import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions.exceptions import ConnectionException
from huaweicloudsdkcore.exceptions.exceptions import RequestTimeoutException
from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkworkspace.v2.workspace_client import WorkspaceClient
from huaweicloudsdkworkspace.v2.model.batch_action_desktops_req import BatchActionDesktopsReq
from huaweicloudsdkworkspace.v2.model.batch_run_desktops_request import BatchRunDesktopsRequest
from huaweicloudsdkworkspace.v2.model.create_desktop_req import CreateDesktopReq
from huaweicloudsdkworkspace.v2.model.create_desktop_request import CreateDesktopRequest
from huaweicloudsdkworkspace.v2.model.delete_desktop_request import DeleteDesktopRequest
from huaweicloudsdkworkspace.v2.model.desktop import Desktop
from huaweicloudsdkworkspace.v2.model.list_desktops_request import ListDesktopsRequest
from huaweicloudsdkworkspace.v2.model.volume import Volume
from huaweicloudsdkworkspace.v2.region.workspace_region import WorkspaceRegion
class WorkspaceDesktopsManageDemo:
def __init__(self):
pass
@staticmethod
def main(args):
ak = os.environ["HUAWEICLOUD_SDK_AK"]
sk = os.environ["HUAWEICLOUD_SDK_SK"]
project_id = "<YOUR PROJECTID>"
auth = BasicCredentials(
ak=ak,
sk=sk,
project_id=project_id
)
config = HttpConfig.get_default_config()
config.ignore_ssl_verification = True
config.proxy_protocol = "http"
client = WorkspaceClient.new_builder() \
.with_credentials(credentials=auth) \
.with_region(region=WorkspaceRegion.value_of(region_id="cn-north-4")) \
.with_http_config(config=config) \
.build()
WorkspaceDesktopsManageDemo.__list_desktops(client)
@staticmethod
def __list_desktops(workspace_client):
try:
list_desktops_response = workspace_client.list_desktops(ListDesktopsRequest())
print(list_desktops_response)
except (ConnectionException, RequestTimeoutException) as e:
print(e.err_message)
except ServiceResponseException as e:
print(e.status_code)
print(e.error_code)
print(e.error_msg)
@staticmethod
def __create_desktop(workspace_client):
user_name = "{your userName string}"
user_email = "{your userEmail string}"
user_group = "{your userGroup string}"
product_id = "{your productId string}"
image_id = "{your imageId string}"
image_type = "{your imageType string}"
volume_type = "{your volumeType string}"
volume_size = 80
try:
desktops = []
desktops.append(
Desktop(
user_name=user_name,
user_email=user_email,
user_group=user_group
)
)
create_desktop_req = CreateDesktopReq()
create_desktop_req.product_id = product_id
create_desktop_req.image_id = image_id
create_desktop_req.image_type = image_type
create_desktop_req.desktop_type = "dedicated"
create_desktop_req.desktops = desktops
create_desktop_req.root_volume = Volume(
type=volume_type,
size=volume_size
)
create_desktop_response = workspace_client.create_desktop(
CreateDesktopRequest(
body=create_desktop_req
)
)
print(create_desktop_response)
except (ConnectionException, RequestTimeoutException) as e:
print(e.err_message)
except ServiceResponseException as e:
print(e.status_code)
print(e.error_code)
print(e.error_msg)
@staticmethod
def __batch_run_desktops(workspace_client):
desktop_id = "{your desktopId string}"
op_type = "{your opType string}"
try:
desktop_ids = []
desktop_ids.append(desktop_id)
batch_action_desktops_req = BatchActionDesktopsReq()
batch_action_desktops_req.desktop_ids = desktop_ids
batch_action_desktops_req.op_type = op_type
batch_run_desktops_response = workspace_client.batch_run_desktops(
BatchRunDesktopsRequest(
body=batch_action_desktops_req
)
)
print(batch_run_desktops_response)
except (ConnectionException, RequestTimeoutException) as e:
print(e.err_message)
except ServiceResponseException as e:
print(e.status_code)
print(e.error_code)
print(e.error_msg)
@staticmethod
def __delete_desktop(workspace_client):
desktop_id = "{your desktopId string}"
try:
delete_desktop_response = workspace_client.delete_desktop(
DeleteDesktopRequest(
desktop_id=desktop_id
)
)
print(delete_desktop_response)
except (ConnectionException, RequestTimeoutException) as e:
print(e.err_message)
except ServiceResponseException as e:
print(e.status_code)
print(e.error_code)
print(e.error_msg)
if __name__ == "__main__":
WorkspaceDesktopsManageDemo().main(any)
版本说明
本示例基于华为云SDK开发
功能介绍
基于华为云 Python SDK对云办公桌面进行操作的代码示例。
其中包括:对云办公桌面进行操作的增(创建桌面)、删(删除单个桌面)、改(操作桌面)、查(查询桌面列表)。
本示例将以上操作整合,完成了从云办公桌面的创建到查询、修改,再到删除的整个生命周期的覆盖。
前置条件
开通服务
申请开通云办公服务的具体操作请参考开通云办公服务API
获取AK/SK
开发者在使用前需先获取账号的ak、sk、projectId、endpoint。
您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。 endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。 projectId 云服务所在项目 ID ,根据你想操作的项目所属区域选择对应的项目 ID 。
运行环境
Python3 及其以上版本。
SDK获取和安装
您可以通过加入相应的依赖项获取和安装SDK。
示例代码
调用前请替换示例中的变量,请根据需要执行的方法注释掉无关方法,并执行Run Application。
import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions.exceptions import ConnectionException from huaweicloudsdkcore.exceptions.exceptions import RequestTimeoutException from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException from huaweicloudsdkcore.http.http_config import HttpConfig from huaweicloudsdkworkspace.v2.workspace_client import WorkspaceClient from huaweicloudsdkworkspace.v2.model.batch_action_desktops_req import BatchActionDesktopsReq from huaweicloudsdkworkspace.v2.model.batch_run_desktops_request import BatchRunDesktopsRequest from huaweicloudsdkworkspace.v2.model.create_desktop_req import CreateDesktopReq from huaweicloudsdkworkspace.v2.model.create_desktop_request import CreateDesktopRequest from huaweicloudsdkworkspace.v2.model.delete_desktop_request import DeleteDesktopRequest from huaweicloudsdkworkspace.v2.model.desktop import Desktop from huaweicloudsdkworkspace.v2.model.list_desktops_request import ListDesktopsRequest from huaweicloudsdkworkspace.v2.model.volume import Volume from huaweicloudsdkworkspace.v2.region.workspace_region import WorkspaceRegion class WorkspaceDesktopsManageDemo: def __init__(self): pass @staticmethod def main(args): # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; # 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak = os.environ["HUAWEICLOUD_SDK_AK"] sk = os.environ["HUAWEICLOUD_SDK_SK"] project_id = "<YOUR PROJECTID>" auth = BasicCredentials( ak=ak, sk=sk, project_id=project_id ) config = HttpConfig.get_default_config() config.ignore_ssl_verification = True config.proxy_protocol = "http" client = WorkspaceClient.new_builder() \ .with_credentials(credentials=auth) \ .with_region(region=WorkspaceRegion.value_of(region_id="cn-north-4")) \ .with_http_config(config=config) \ .build() # 查询桌面列表 WorkspaceDesktopsManageDemo.__list_desktops(client) # 创建桌面 # WorkspaceDesktopsManageDemo.__create_desktop(client) # 操作桌面 # WorkspaceDesktopsManageDemo.__batch_run_desktops(client) # 删除单个桌面 # WorkspaceDesktopsManageDemo.__delete_desktop(client) @staticmethod def __list_desktops(workspace_client): try: list_desktops_response = workspace_client.list_desktops(ListDesktopsRequest()) print(list_desktops_response) except (ConnectionException, RequestTimeoutException) as e: print(e.err_message) except ServiceResponseException as e: print(e.status_code) print(e.error_code) print(e.error_msg) @staticmethod def __create_desktop(workspace_client): user_name = "{your userName string}" user_email = "{your userEmail string}" user_group = "{your userGroup string}" # 套餐ID product_id = "{your productId string}" # 镜像ID image_id = "{your imageId string}" # 镜像类型。private:私有镜像。gold:公共镜像。 image_type = "{your imageType string}" # 系统盘类型。SAS:高IO。 SSD:超高IO。 volume_type = "{your volumeType string}" # 磁盘容量,单位GB。系统盘大小范围[80-32760],数据盘范围[10-32760],大小为10的倍数。 volume_size = 80 try: desktops = [] desktops.append( Desktop( user_name=user_name, user_email=user_email, user_group=user_group ) ) create_desktop_req = CreateDesktopReq() create_desktop_req.product_id = product_id create_desktop_req.image_id = image_id create_desktop_req.image_type = image_type create_desktop_req.desktop_type = "dedicated" create_desktop_req.desktops = desktops create_desktop_req.root_volume = Volume( type=volume_type, size=volume_size ) create_desktop_response = workspace_client.create_desktop( CreateDesktopRequest( body=create_desktop_req ) ) print(create_desktop_response) except (ConnectionException, RequestTimeoutException) as e: print(e.err_message) except ServiceResponseException as e: print(e.status_code) print(e.error_code) print(e.error_msg) @staticmethod def __batch_run_desktops(workspace_client): desktop_id = "{your desktopId string}" # 操作类型。 os-start 启动。 reboot 重启。 os-stop 关机。 op_type = "{your opType string}" try: desktop_ids = [] desktop_ids.append(desktop_id) batch_action_desktops_req = BatchActionDesktopsReq() batch_action_desktops_req.desktop_ids = desktop_ids batch_action_desktops_req.op_type = op_type batch_run_desktops_response = workspace_client.batch_run_desktops( BatchRunDesktopsRequest( body=batch_action_desktops_req ) ) print(batch_run_desktops_response) except (ConnectionException, RequestTimeoutException) as e: print(e.err_message) except ServiceResponseException as e: print(e.status_code) print(e.error_code) print(e.error_msg) @staticmethod def __delete_desktop(workspace_client): desktop_id = "{your desktopId string}" try: delete_desktop_response = workspace_client.delete_desktop( DeleteDesktopRequest( desktop_id=desktop_id ) ) print(delete_desktop_response) except (ConnectionException, RequestTimeoutException) as e: print(e.err_message) except ServiceResponseException as e: print(e.status_code) print(e.error_code) print(e.error_msg) if __name__ == "__main__": WorkspaceDesktopsManageDemo().main(any)
参考
更多信息请参考: 查询桌面列表API
创建桌面API
删除单个桌面API
操作桌面API
修订记录