使用API快捷创建ECS
【摘要】 有时候会有不断重复的创建某个特定配置的ECS的需求,在第一次控制台页面上创建时,在快结束时,可以将调用代码保存下来,这样以后就可以直接调用快速创建。比如,保存的创建某个8C32G、镜像CentOS7.9、设置好VPC,ROOT密码的ECS,它的python代码如下,这个是调用 huaweicloudsdkcore 和 huaweicloudsdkecs 提供的SDK/API来实现ECS创建。...
有时候会有不断重复的创建某个特定配置的ECS的需求,在第一次控制台页面上创建时,在快结束时,可以将调用代码保存下来,这样以后就可以直接调用快速创建。
比如,保存的创建某个8C32G、镜像CentOS7.9、设置好VPC,ROOT密码的ECS,它的python代码如下,这个是调用 huaweicloudsdkcore
和 huaweicloudsdkecs
提供的SDK/API来实现ECS创建。
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkecs.v2 import *
if __name__ == "__main__":
ak = "??RYLI??"
sk = "??s7gcg3??"
credentials = BasicCredentials(ak, sk) \
client = EcsClient.new_builder() \
.with_credentials(credentials) \
.with_region(EcsRegion.value_of("cn-south-1")) \
.build()
try:
request = CreateServersRequest()
listPrePaidServerTagServerTagsServer = []
listPrePaidServerMetadataServer = {
"op_svc_userid": "???7ad4a00f2d41f5bc00a59???",
"admin_pass": "",
"__support_agent_list": "hss,ces"
}
extendparamPrePaidServerExtendParam = PrePaidServerExtendParam(
charging_mode="0",
region_id="cn-south-1"
)
listPrePaidServerSecurityGroupSecurityGroupsServer = [
PrePaidServerSecurityGroup(
id="???-4396-4e74-a148-???"
)
]
listPrePaidServerDataVolumeDataVolumesServer = []
extendparamPrePaidServerRootVolumeExtendParam = PrePaidServerRootVolumeExtendParam(
resource_spec_code="GPSSD",
resource_type="3"
)
rootVolumePrePaidServerRootVolume = PrePaidServerRootVolume(
volumetype="GPSSD",
size=40,
extendparam=extendparamPrePaidServerRootVolumeExtendParam,
hwpassthrough=False
)
listPrePaidServerNicNicsServer = [
PrePaidServerNic(
subnet_id="???-a0ca-4df7-b865-5ca84ad2a7ee",
ip_address=""
)
]
serverPrePaidServer = PrePaidServer(
image_ref="???-ab5e-4b0c-bcc0-52949077090c",
flavor_ref="t6.2xlarge.4",
name="tas",
user_data="???KZWNobyAncm9vdDokNiREcEJabFpMTSRnMGY2OU9qSm9aYWE5blBXLnRMejNQMlRGaTZvcXlmclNSOFk0amJsV3JjQnNJeDM3aDlWZFhSczAwTDc4VGNZcnFpZmdhSWNIaDdSNG94M3FUQi5XMScgfCBjaHBhc3N3ZCAtZTs=",
vpcid="???-825b-4ce5-874f-f302e08257bc",
nics=listPrePaidServerNicNicsServer,
count=1,
is_auto_rename=False,
root_volume=rootVolumePrePaidServerRootVolume,
data_volumes=listPrePaidServerDataVolumeDataVolumesServer,
security_groups=listPrePaidServerSecurityGroupSecurityGroupsServer,
availability_zone="cn-south-1c",
extendparam=extendparamPrePaidServerExtendParam,
metadata=listPrePaidServerMetadataServer,
server_tags=listPrePaidServerTagServerTagsServer,
description=""
)
request.body = CreateServersRequestBody(
server=serverPrePaidServer
)
response = client.create_servers(request)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code)
print(e.request_id)
print(e.error_code)
print(e.error_msg)
然后在运行这个python3脚本前,先安装一下SDK:
pip3 install huaweicloudsdkcore huaweicloudsdkecs
然后运行就可以了,很快可以在页面上看到一台ECS在创建中,一会就创建好了。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)