時間:2023-06-29 01:09:02 | 來源:網(wǎng)站運營
時間:2023-06-29 01:09:02 來源:網(wǎng)站運營
使用華為云API按需創(chuàng)建云服務器:今晚有時間,把去年的一個想法實現(xiàn)一下# coding: utf-8# 創(chuàng)建預付費云服務器,規(guī)格為1cpu,1G內(nèi)存,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 = [] # 云服務器對應系統(tǒng)盤相關(guān)配置。 rootVolumePostPaidServerRootVolume = PostPaidServerRootVolume( # 云服務器系統(tǒng)盤對應的磁盤類型,需要與系統(tǒng)所提供的磁盤類型相匹配。 volumetype="SAS" ) # 創(chuàng)建彈性IP的附加信息。 extendparamPostPaidServerEipExtendParam = PostPaidServerEipExtendParam( # 公網(wǎng)IP的計費模式。取值范圍:prePaid - 預付費, 即包年包月;postPaid - 后付費, 即按需付費; charging_mode="postPaid" ) # 彈性IP地址帶寬參數(shù)。 bandwidthPostPaidServerEipBandwidth = PostPaidServerEipBandwidth( # 功能說明:帶寬大小 帶寬(Mbit / s), 取值范圍為[ 1, 2000 ]。 size=1, # 帶寬的共享類型。 共享類型枚舉:PER,表示獨享。WHOLE,表示共享。 sharetype="PER" ) # 配置云服務器自動分配彈性IP時,創(chuàng)建彈性IP的配置參數(shù)。 eipPostPaidServerEip = PostPaidServerEip( # 彈性IP地址類型。 iptype="5_bgp", bandwidth=bandwidthPostPaidServerEipBandwidth, extendparam=extendparamPostPaidServerEipExtendParam ) # 配置云服務器的彈性IP信息,彈性IP有三種配置方式。 # 不使用(無該字段) # 自動分配,需要指定新創(chuàng)建彈性IP的信息 # 使用已有,需要指定已創(chuàng)建彈性IP的信息 publicipPostPaidServerPublicip = PostPaidServerPublicip( eip=eipPostPaidServerEip ) # 待創(chuàng)建云服務器的網(wǎng)卡信息。 listPostPaidServerNicNicsServer = [ # 待創(chuàng)建云服務器所在的子網(wǎng)信息,需要指定vpcid對應VPC下的子網(wǎng)ID,UUID格式。 PostPaidServerNic( subnet_id="0d106f5c-0b40-4864-9767-57e63b51cf99" ) ] # 創(chuàng)建云服務器附加信息。 extendparamPostPaidServerExtendParam = PostPaidServerExtendParam( #計費模式: 0:按需計費。 charging_mode=0, # 云服務器所在區(qū)域ID。 region_id="ap-southeast-1" ) # 云服務器對應數(shù)據(jù)盤相關(guān)配置。 listPostPaidServerDataVolumeDataVolumesServer = [] # 彈性云服務器信息 serverPostPaidServer = PostPaidServer( # 云服務器對應數(shù)據(jù)盤相關(guān)配置 data_volumes=listPostPaidServerDataVolumeDataVolumesServer, # 創(chuàng)建云服務器附加信息。 extendparam=extendparamPostPaidServerExtendParam, # 待創(chuàng)建云服務器的系統(tǒng)規(guī)格的ID。 flavor_ref="s6.small.1", # 待創(chuàng)建云服務器的系統(tǒng)鏡像,需要指定已創(chuàng)建鏡像的ID,ID格式為通用唯一識別碼(Universally Unique Identifier,簡稱UUID)。 image_ref="5f524b35-2e88-46e1-9aa4-c6e59c2971bd", # 如果需要使用SSH密鑰方式登錄云服務器,請指定已創(chuàng)建密鑰的名稱。 key_name="KeyPair", # 云服務器名稱。 name="strongswan", # 待創(chuàng)建云服務器的網(wǎng)卡信息。 nics=listPostPaidServerNicNicsServer, # 配置云服務器的彈性IP信息 publicip=publicipPostPaidServerPublicip, # 云服務器對應系統(tǒng)盤相關(guān)配置。 root_volume=rootVolumePostPaidServerRootVolume, # 云服務器對應安全組信息。 security_groups=listPostPaidServerSecurityGroupSecurityGroupsServer, # 彈性云服務器的標簽。 server_tags=listPostPaidServerTagServerTagsServer, # 待創(chuàng)建云服務器所屬虛擬私有云(簡稱VPC),需要指定已創(chuàng)建VPC的ID,UUID格式。 vpcid="45c02e54-3ec0-4efd-8c66-c7000984704a" ) request.body = CreatePostPaidServersRequestBody( server=serverPostPaidServer, # 是否只預檢此次請求。 # true:發(fā)送檢查請求,不會創(chuàng)建實例。檢查項包括是否填寫了必需參數(shù)、請求格式等。 # 如果檢查不通過,則返回對應錯誤。 # 如果檢查通過,則返回202狀態(tài)碼。 # false:發(fā)送正常請求,通過檢查后并且執(zhí)行創(chuàng)建云服務器請求。 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)
執(zhí)行后,會返回一個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__": # 輸入準備基本數(shù)據(jù)中的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() # 根據(jù)job_id,來查詢云主機生命周期內(nèi)的任何操作任務。 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)
返回查詢結(jié)果{ "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頁面看到,已經(jīng)創(chuàng)建了一個云服務器。# 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__": # 輸入準備基本數(shù)據(jù)中的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)
返回了云服務器的所有相關(guān)信息{ "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__": # 輸入準備基本數(shù)據(jù)中的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:刪除云服務器時會同時刪除掛載在云服務器上的數(shù)據(jù)盤。 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)
執(zhí)行后,會返回一個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__": # 輸入準備基本數(shù)據(jù)中的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() # 根據(jù)job_id,來查詢云主機生命周期內(nèi)的任何操作任務。 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)
執(zhí)行后,返回job執(zhí)行情況{ "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"}
關(guān)鍵詞:創(chuàng)建,服務,使用
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。