skd——vpc
【摘要】 #!/usr/bin/env python3# -*- coding: utf-8 -*-# @author YWLBTWTK# @date 2023/9/18from fastapi import FastAPIimport uvicornfrom huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweic...
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @author YWLBTWTK
# @date 2023/9/18
from fastapi import FastAPI
import uvicorn
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkvpc.v2.region.vpc_region import VpcRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkvpc.v2 import *
app = FastAPI()
ak = ""
sk = ""
g_region = "cn-north-4"
def api_list_vpc(name):
credentials = BasicCredentials(ak, sk)
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of(g_region)) \
.build()
try:
request = ListVpcsRequest()
response = list(map(lambda x: {'name': x['name'], 'id': x['id']}, client.list_vpcs(request).to_dict()['vpcs']))
for i in response:
if i['name'] == name:
print(i['id'])
return i['id']
return None
except Exception:
return None
@app.post('/cloud_vpc/create_vpc')
def api_create_vpc(data: dict):
credentials = BasicCredentials(ak, sk)
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of(g_region)) \
.build()
try:
request = CreateVpcRequest()
vpcbody = CreateVpcOption(
cidr=data['cidr'],
name=data['name']
)
request.body = CreateVpcRequestBody(
vpc=vpcbody
)
response = client.create_vpc(request)
print(response)
return response
except exceptions.ClientRequestException as e:
print(e.status_code)
print(e.request_id)
print(e.error_code)
print(e.error_msg)
return e
except Exception as e:
return e
@app.get('/cloud_vpc/vpc/{vpc_name}')
def api_get_vpc(vpc_name: str):
credentials = BasicCredentials(ak, sk)
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of(g_region)) \
.build()
try:
request = ShowVpcRequest()
request.vpc_id = api_list_vpc(vpc_name)
response = client.show_vpc(request)
print(response)
return response
except exceptions.ClientRequestException as e:
print(e.status_code)
print(e.request_id)
print(e.error_code)
print(e.error_msg)
return e
except Exception as e:
return e
@app.get('/cloud_vpc/vpc')
def api_get_list_vpc():
credentials = BasicCredentials(ak, sk)
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of(g_region)) \
.build()
try:
request = ListVpcsRequest()
response = client.list_vpcs(request).to_dict()
print(response)
return response
except Exception as e:
return e
@app.put('/cloud_vpc/update_vpc')
def api_update_vpc(data: dict):
credentials = BasicCredentials(ak, sk)
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of(g_region)) \
.build()
try:
request = UpdateVpcRequest()
request.vpc_id = api_list_vpc(data['old_name'])
vpcbody = UpdateVpcOption(
name=data['new_name']
)
request.body = UpdateVpcRequestBody(
vpc=vpcbody
)
response = client.update_vpc(request)
print(response)
return response.to_dict()
except Exception as e:
return e
@app.delete('/cloud_vpc/delete_vpc')
def api_delete_vpc(data: dict):
credentials = BasicCredentials(ak, sk)
client = VpcClient.new_builder() \
.with_credentials(credentials) \
.with_region(VpcRegion.value_of(g_region)) \
.build()
try:
request = DeleteVpcRequest()
request.vpc_id = api_list_vpc(data['vpc_name'])
response = client.delete_vpc(request)
print(response)
return response.to_dict()
except Exception as e:
return e
if __name__ == '__main__':
uvicorn.run(app='main:app', host='0.0.0.0', port=7045, reload=True)
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)