skd——subnet

举报
玩个球球 发表于 2024/03/06 08:40:02 2024/03/06
【摘要】 # coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkvpc.v2.region.vpc_region import VpcRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom h...
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkvpc.v2.region.vpc_region import VpcRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkvpc.v2 import *
import time
def get_token():
    ak = ""
    sk = ""


    credentials = BasicCredentials(ak, sk) \

    client = VpcClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(VpcRegion.value_of("cn-north-4")) \
        .build()
    return client
class subnet:
    def __init__(self,client):
        self.client=client
    def listvpc(self,name):
        client=self.client
        request = ListVpcsRequest()
        response = client.list_vpcs(request)
        a=response.vpcs
        for i in a:
            if i.name == name:
                return i.id
    def create(self):
        client=self.client
        id=self.listvpc('vpc-default')
        print(id)
        request = CreateSubnetRequest()
        subnetbody = CreateSubnetOption(
            availability_zone="cn-north-4a",
            name="chinaskills_subnet",
            cidr="192.168.100.0/24",
            vpc_id=id,
            gateway_ip="192.168.100.1",
           
        )
        request.body = CreateSubnetRequestBody(
            subnet=subnetbody
        )
        response = client.create_subnet(request)
    def get_id(self,name):
        request = ListSubnetsRequest()
        response = client.list_subnets(request)
        a=response.subnets
        for i in a:
            if i.name == name:
                return i.id
    def delete(self,vpc_name,sub_name):
        sub_id=self.get_id(sub_name)
        vpc_id=self.listvpc(vpc_name)
        request = DeleteSubnetRequest()
        request.vpc_id = vpc_id
        request.subnet_id = sub_id
        response = client.delete_subnet(request)
    def get(self,name):
        id =self.get_id(name)
        print(id)
        request = ShowSubnetRequest()
        request.subnet_id = id
        response = client.show_subnet(request)
        print(response)
if __name__ == "__main__":
    client=get_token()
    a=subnet(client)
    a.delete('vpc-default','chinaskills_subnet')
    time.sleep(3)
    a.create()
    time.sleep(2)
    a.get('chinaskills_subnet')
   

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。