华为云SDK调用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了华为云SDK调用相关的知识,希望对你有一定的参考价值。
华为云SDK下载:https://support.huaweicloud.com/devg-sdk/zh-cn_topic_0070637158.html方式一:下载GitHub源码安装方式,如下:
将代码下载到您项目合适的位置,以pythonsdk作为您选择的位置举例,执行下面的命令完成源码下载和安装:
git clone https://github.com/huaweicloud/huaweicloud-sdk-python pythonsdk
cd pythonsdk
pip install -r requirements.txt
python setup.py install
方式二:使用pip安装,请执行以下命令:
pip install huaweicloud-sdk-python
"""
华为云服务器创建sdk接 入
"""
from openstack import connection
import re
import time
zone_name=input("输入地区:")
region_list={
"广州": "cn-south-1", "上海": "cn-east-2"
}
region_name = region_list.get(zone_name)
projectId = "xxxxxxx" #项目id
cloud = "myhuaweicloud.com"
region= region_name
AK = "xxxxxxx"
SK = "xxxxxxxx"
conn = connection.Connection(
project_id=projectId,
cloud=cloud,
region=region,
ak=AK,
sk=SK)
name = input("输入vpc名称:")
#查询/创建vpc,返回vpcid
def test_create_vpc(_conn):
global vpc_id
query = {
"limit": 2
}
objs = _conn.vpcv1.vpcs(**query)
for obj in objs:
s = str(obj)
vpc_name1 = re.findall(r‘.*name=(.*?),.*‘, s)
if name == vpc_name1[0]:
id = re.findall(r‘id=(.*?),.*?‘, s)
vpc_id = id[0]
#print("VPCID:"+vpc_id)
break
else:
data = {
"name": name,
}
vpc_inf = str(_conn.vpcv1.create_vpc(**data))
id = re.findall(r‘.*id=(.*?),.*‘, vpc_inf)
vpc_id = id[0]
#print("VPCID:"+vpc_id)
#查询/创建子网,返回子网网络id
def test_subnets(_conn):
global subnet_id
query = {
"limit": 2
}
objs = _conn.vpcv1.subnets(**query)
for obj in objs:
sub = str(obj)
subnet_name1 = re.findall(r‘.*name=(.*?),.*‘, sub)
if name == subnet_name1[0]:
id = re.findall(r‘id=(.*?),.*‘, sub)
subnet_id=id[0]
#print("子网ID:" + subnet_id)
break
else:
data = {
"name": name,
"cidr": "192.168.0.0/24",
"gateway_ip": "192.168.0.1",
"vpc_id": vpc_id,
}
x = str(_conn.vpcv1.create_subnet(**data))
id = re.findall(r‘.*id=(.*?),.*‘, x)
subnet_id = id[0]
#print("子网ID:" + subnet_id)
#查询/创建安全组,返回安全组id
def security_groups(_conn):
global sg_id
query = {
"limit": 2
}
objs = _conn.vpcv1.security_groups(**query)
for obj in objs:
s = str(obj)
sg_name1 = re.findall(r‘.*name=(.*?),.*‘, s)
if name == sg_name1[0] :
id = re.findall(r‘id=(.*?),.*‘, s)
sg_id = id[0]
#print("安全组ID:"+sg_id)
break
else:
data = {
"name": name
}
sg_inf = str(_conn.vpcv1.create_security_group(**data))
id = re.findall(r‘id=(.*?),.*‘, sg_inf)
sg_id = id[0]
#print("安全组ID:" + sg_id)
server_name = input("输入服务器名称:")
root_wd = input("输入服务器密码:")
root_vo = input("系统盘:")
data_vo = input("数据盘:")
zone_list = {
‘广州‘: ‘cn-south-1c‘, "上海": ‘cn-east-2c‘
}
zone=zone_list.get(zone_name)
#获取服务器镜像id
image_id = input("输入系统型号:")
#上海系统id列表
image_list = {
"6.5": "a90fd48b-7fc7-4805-8437-61219e3f7a8d", "6.8": "8b65a542-c3be-4878-86b4-f077d6f98872",
"6.9": "bf9829da-4788-461a-89bd-4d4f251ac034", "7.2": "95c06f9a-19d5-48b1-a52b-bd09d837536f",
"7.3": "643d831d-a69c-433f-803f-065e0e2e2911", "7.4": "af92bb51-ec9d-4b02-912f-da0b3f0f7635",
"7.5": "9c36fd0e-b73e-4f3b-90a1-6c581023a698", "7.6": "9526f9b7-423c-4fdc-92ad-f1630a524652",
"16": "7a2a8501-0194-49f3-be9d-da97e21d159b", "18": "f6e06c03-8122-4884-a8c2-88dbca90a1c8",
"2008": "2af80187-4479-40e7-bd5d-88bbc39f3fa7", "2008企业": "c90f1ca1-9b38-4e18-b013-ab43198a40a9",
"2012数据中心": "0b368aca-dd75-4c8e-9711-bd0deb8b5c18",
}
#广州系统id列表
image_list1 = {
"6.5": "008ee6d6-e104-4f19-8746-49f3d9ad735c", "6.8": "89708938-d2e8-4abf-8505-6ba412a72ee6",
"6.9": "6b9a1696-61e1-4210-b607-e58b94409e73", "7.2": "5bce3b2d-5f64-4646-b94a-d73dcc3bfeb4",
"7.3": "ae95e505-421b-4e67-923d-a5fa320ad336", "7.4": "7be2e72e-0679-4a1b-8faf-0c1865708b20",
"7.5": "c5d92e4c-88b2-4a61-8177-85a607af68b6", "7.6": "7077ec61-7553-4890-8b33-364005a590b9",
"16": "28accb67-755a-4f0a-b735-e2ad5e437be9", "18": "c5a4dd31-38df-447f-8363-1085c03cb3f1",
"2008": "9ad1a871-80d5-45f2-bc23-e3ad93472189", "2008企业": "42aea2c9-b597-4943-aae7-defa9e19bb4f",
"2008数据中心": "6ac89f1f-6fe0-49dd-afcb-ce0305f499d6", "2012标准": "8d6ad2f5-7d82-4b03-90a3-5977227e741e",
"2012数据中心": "906a8bd4-234a-429d-b540-6d2d6b86a83b",
}
if zone_name == "上海":
image = image_list.get(image_id)
else:
image = image_list1.get(image_id)
#获取服务器规格
flavorRef_id = input("规格型号:")
flavorRef_list = {
"2c4g": "s3.large.2", "2c8g": "s3.large.4",
"4c8g": "s3.xlarge.2", "4c16g": "s3.xlarge.4",
"8c16g": "s3.2xlarge.2", "8c32g": "s3.2xlarge.4",
"2c16g": "m6.large.8", "4c32g": "m3.xlarge.8",
}
flavorRef = flavorRef_list.get(flavorRef_id)
#创建包年包月型服务器
def create_server():
data = {
"availability_zone": zone,
"name": server_name,
"imageRef": image,
"root_volume": {
"volumetype": "SATA",
"size": root_vo
},
"data_volumes": [
{
"volumetype": "SATA",
"size": data_vo
}
],
"isAutoRename": "true",
"flavorRef": flavorRef,
"adminPass": root_wd,
"vpcid": vpc_id,
"security_groups": [
{
"id": sg_id
}
],
"nics": [
{
"subnet_id": subnet_id
}
],
#弹性ip 全动态按流量计费方式
"publicip": {
"eip": {
"iptype": "5_bgp",
"bandwidth": {
"size": 100,
"sharetype": "PER",
"chargemode": "traffic"
},
"extendparam": {
"chargingMode": "postPaid"
}
}
},
"count": 1,
"extendparam": {
"chargingMode": "prePaid", #包年包月型
"periodType": "month",
"periodNum": 1,
"isAutoRenew": "true",
"isAutoPay": "true",
}
}
server = conn.ecs.create_server_ext(**data)
print("服务器创建成功")
time.sleep(80)
return server
#查询服务器ip并提取ip
def list_servers():
servers = conn.compute.servers(limit=1)
for server_list in servers:
server_list = str(server_list)
servers_names = re.findall(r‘.*name=(.*?),.*‘,server_list)
servers_names = servers_names[0]
if server_name == servers_names:
ip = re.findall(r‘(?:[0-9]{1,3}.){3}[0-9]{1,3}‘, server_list)
print(server_name)
print(root_wd)
print("公网:"+ip[1]+‘ ‘+"私有:"+ip[0])
break
if __name__ == ‘__main__‘:
test_create_vpc(conn)
test_subnets(conn)
security_groups(conn)
create_server()
list_servers()
以上是关于华为云SDK调用的主要内容,如果未能解决你的问题,请参考以下文章