时间:2023-06-29 01:09:02 | 来源:网站运营
时间:2023-06-29 01:09:02 来源:网站运营
使用华为云API按需创建云服务器:今晚有时间,把去年的一个想法实现一下# coding: utf-8# 创建预付费云服务器,规格为1cpu,1G内存,1M EIP。from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": ak = "<you ak>" sk = "<you sk>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = CreatePostPaidServersRequest() listPostPaidServerTagServerTagsServer = [] listPostPaidServerSecurityGroupSecurityGroupsServer = [] # 云服务器对应系统盘相关配置。 rootVolumePostPaidServerRootVolume = PostPaidServerRootVolume( # 云服务器系统盘对应的磁盘类型,需要与系统所提供的磁盘类型相匹配。 volumetype="SAS" ) # 创建弹性IP的附加信息。 extendparamPostPaidServerEipExtendParam = PostPaidServerEipExtendParam( # 公网IP的计费模式。取值范围:prePaid - 预付费, 即包年包月;postPaid - 后付费, 即按需付费; charging_mode="postPaid" ) # 弹性IP地址带宽参数。 bandwidthPostPaidServerEipBandwidth = PostPaidServerEipBandwidth( # 功能说明:带宽大小 带宽(Mbit / s), 取值范围为[ 1, 2000 ]。 size=1, # 带宽的共享类型。 共享类型枚举:PER,表示独享。WHOLE,表示共享。 sharetype="PER" ) # 配置云服务器自动分配弹性IP时,创建弹性IP的配置参数。 eipPostPaidServerEip = PostPaidServerEip( # 弹性IP地址类型。 iptype="5_bgp", bandwidth=bandwidthPostPaidServerEipBandwidth, extendparam=extendparamPostPaidServerEipExtendParam ) # 配置云服务器的弹性IP信息,弹性IP有三种配置方式。 # 不使用(无该字段) # 自动分配,需要指定新创建弹性IP的信息 # 使用已有,需要指定已创建弹性IP的信息 publicipPostPaidServerPublicip = PostPaidServerPublicip( eip=eipPostPaidServerEip ) # 待创建云服务器的网卡信息。 listPostPaidServerNicNicsServer = [ # 待创建云服务器所在的子网信息,需要指定vpcid对应VPC下的子网ID,UUID格式。 PostPaidServerNic( subnet_id="0d106f5c-0b40-4864-9767-57e63b51cf99" ) ] # 创建云服务器附加信息。 extendparamPostPaidServerExtendParam = PostPaidServerExtendParam( #计费模式: 0:按需计费。 charging_mode=0, # 云服务器所在区域ID。 region_id="ap-southeast-1" ) # 云服务器对应数据盘相关配置。 listPostPaidServerDataVolumeDataVolumesServer = [] # 弹性云服务器信息 serverPostPaidServer = PostPaidServer( # 云服务器对应数据盘相关配置 data_volumes=listPostPaidServerDataVolumeDataVolumesServer, # 创建云服务器附加信息。 extendparam=extendparamPostPaidServerExtendParam, # 待创建云服务器的系统规格的ID。 flavor_ref="s6.small.1", # 待创建云服务器的系统镜像,需要指定已创建镜像的ID,ID格式为通用唯一识别码(Universally Unique Identifier,简称UUID)。 image_ref="5f524b35-2e88-46e1-9aa4-c6e59c2971bd", # 如果需要使用SSH密钥方式登录云服务器,请指定已创建密钥的名称。 key_name="KeyPair", # 云服务器名称。 name="strongswan", # 待创建云服务器的网卡信息。 nics=listPostPaidServerNicNicsServer, # 配置云服务器的弹性IP信息 publicip=publicipPostPaidServerPublicip, # 云服务器对应系统盘相关配置。 root_volume=rootVolumePostPaidServerRootVolume, # 云服务器对应安全组信息。 security_groups=listPostPaidServerSecurityGroupSecurityGroupsServer, # 弹性云服务器的标签。 server_tags=listPostPaidServerTagServerTagsServer, # 待创建云服务器所属虚拟私有云(简称VPC),需要指定已创建VPC的ID,UUID格式。 vpcid="45c02e54-3ec0-4efd-8c66-c7000984704a" ) request.body = CreatePostPaidServersRequestBody( server=serverPostPaidServer, # 是否只预检此次请求。 # true:发送检查请求,不会创建实例。检查项包括是否填写了必需参数、请求格式等。 # 如果检查不通过,则返回对应错误。 # 如果检查通过,则返回202状态码。 # false:发送正常请求,通过检查后并且执行创建云服务器请求。 dry_run=False ) response = client.create_post_paid_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)
执行后,会返回一个job_id和server_id{ "job_id":"ff8080817e498e160180040b84db1149", "serverIds":[ "68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" ]}
# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = ShowJobRequest() # 根据job_id,来查询云主机生命周期内的任何操作任务。 request.job_id = "ff8080817e498e160180040b84db1149" response = client.show_job(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
返回查询结果{ "begin_time":"2022-04-07T12:41:45.434Z", "end_time":"2022-04-07T12:42:33.109Z", "entities":{ "sub_jobs":[ { "status":"SUCCESS", "entities":{ "server_id":"68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" }, "job_id":"ff8080817e498e160180040b8536114d", "job_type":"createSingleServer", "begin_time":"2022-04-07T12:41:45.525Z", "end_time":"2022-04-07T12:42:21.462Z" } ], "sub_jobs_total":1 }, "job_id":"ff8080817e498e160180040b84db1149", "job_type":"createServer", "status":"SUCCESS"}
可以从web页面看到,已经创建了一个云服务器。# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: # 默认列出所有云服务器 request = ListServersDetailsRequest() response = client.list_servers_details(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
返回了云服务器的所有相关信息{ "count":1, "servers":[ { "status":"ACTIVE", "updated":"2022-04-07T12:42:14Z", "auto_terminate_time":"", "hostId":"1820bda7ebd23bfdc088f7242ab64eb18b492b8f9c23220059f28173", "OS-EXT-SRV-ATTR:host":"1820bda7ebd23bfdc088f7242ab64eb18b492b8f9c23220059f28173", "addresses":{ "45c02e54-3ec0-4efd-8c66-c7000984704a":[ { "version":"4", "addr":"192.168.2.97", "OS-EXT-IPS:type":"fixed", "OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:5b:a4:ff", "OS-EXT-IPS:port_id":"88fa7a14-0400-4065-a2ca-d1e788e30df2" }, { "version":"4", "addr":"159.138.140.179", "OS-EXT-IPS:type":"floating", "OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:5b:a4:ff", "OS-EXT-IPS:port_id":"88fa7a14-0400-4065-a2ca-d1e788e30df2" } ] }, "key_name":"KeyPair", "image":{ "id":"5f524b35-2e88-46e1-9aa4-c6e59c2971bd" }, "OS-EXT-STS:vm_state":"active", "OS-EXT-SRV-ATTR:instance_name":"instance-005b0627", "OS-EXT-SRV-ATTR:hypervisor_hostname":"b8ecd1578f1f22a6ec8d43489dd400a509da6890969e43f5f90e707f", "flavor":{ "id":"s6.small.1", "name":"s6.small.1", "disk":"0", "vcpus":"1", "ram":"1024" }, "id":"68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8", "security_groups":[ { "name":"default", "id":"b6fb502b-41b4-4153-9d05-59ff07417c8a" } ], "OS-EXT-AZ:availability_zone":"ap-southeast-1b", "user_id":"10157ed2b3824a30b1a47a17434dc3ab", "name":"strongswan", "created":"2022-04-07T12:41:46Z", "tenant_id":"42592e3253cc404e9a0f7b5a890ce536", "OS-DCF:diskConfig":"MANUAL", "accessIPv4":"", "accessIPv6":"", "progress":0, "OS-EXT-STS:power_state":1, "config_drive":"", "metadata":{ "cascaded.instance_extrainfo":"pcibridge:1", "charging_mode":"0", "image_name":"StrongSwan", "metering.image_id":"5f524b35-2e88-46e1-9aa4-c6e59c2971bd", "metering.imagetype":"private", "metering.resourcespeccode":"s6.small.1.linux", "metering.resourcetype":"1", "os_bit":"64", "os_type":"Linux", "vpc_id":"45c02e54-3ec0-4efd-8c66-c7000984704a" }, "OS-SRV-USG:launched_at":"2022-04-07T12:42:05.303833", "os-extended-volumes:volumes_attached":[ { "id":"e97eab60-c486-4efa-ab3f-090a2dc83055", "delete_on_termination":"true", "bootIndex":"0", "device":"/dev/vda" } ], "description":"", "host_status":"UP", "OS-EXT-SRV-ATTR:hostname":"strongswan", "OS-EXT-SRV-ATTR:reservation_id":"r-b2frc95i", "OS-EXT-SRV-ATTR:launch_index":0, "OS-EXT-SRV-ATTR:kernel_id":"", "OS-EXT-SRV-ATTR:ramdisk_id":"", "OS-EXT-SRV-ATTR:root_device_name":"/dev/vda", "locked":false, "tags":[ ], "os:scheduler_hints":{ }, "enterprise_project_id":"0", "sys_tags":[ { "key":"_sys_enterprise_project_id", "value":"0" } ], "cpu_options":{ } } ]}
# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = DeleteServersRequest() # 所需要删除的云服务器列表。 listServerIdServersbody = [ # 云服务器ID。 ServerId( id="68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" ) ] request.body = DeleteServersRequestBody( servers=listServerIdServersbody, # true:删除云服务器时会同时删除挂载在云服务器上的数据盘。 delete_volume=True, # true:删除云服务器时会同时删除绑定在云服务器上的弹性IP。 delete_publicip=True ) response = client.delete_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)
执行后,会返回一个job_id{"job_id": "ff8080817e498e1601800424e9cc181a"}
# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = ShowJobRequest() # 根据job_id,来查询云主机生命周期内的任何操作任务。 request.job_id = "ff8080817e498e1601800424e9cc181a" response = client.show_job(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
执行后,返回job执行情况{ "begin_time":"2022-04-07T13:09:29.675Z", "end_time":"2022-04-07T13:10:13.021Z", "entities":{ "sub_jobs":[ { "status":"SUCCESS", "entities":{ "server_id":"68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" }, "job_id":"ff8080817e498e1601800424ed3c181d", "job_type":"deleteVM", "begin_time":"2022-04-07T13:09:30.555Z", "end_time":"2022-04-07T13:10:03.983Z" } ], "sub_jobs_total":1 }, "job_id":"ff8080817e498e1601800424e9cc181a", "job_type":"deleteVMs", "status":"SUCCESS"}
关键词:创建,服务,使用