使用Python Azure SDK删除和创建Azure VPN网关

iyfamqjs  于 2023-03-24  发布在  Python
关注(0)|答案(1)|浏览(108)

我有一些Azure Python函数,我每天都会使用它们来构建和销毁VPN网关。在第一步中,我尝试使用该代码删除现有网关:

import azure.functions as func
from azure.identity import ClientSecretCredential
from azure.mgmt.network import NetworkManagementClient
import logging

def main(mytimer: func.TimerRequest) -> None:

    logger = logging.getLogger("azure.core.pipeline.policies.http_logging_policy")
    logger.setLevel(logging.WARNING)

    subscription_id ="xxx"
    client_id ="xxx"
    secret="xxx"
    tenant="xxx"
    rgroup = "xxx"
    gateway = "xxx"
    credential = ClientSecretCredential(
        tenant_id=tenant,
        client_id=client_id,
        client_secret=secret
    )
    network_client = NetworkManagementClient(credential, subscription_id )
    LROPoller = network_client.vpn_gateways.begin_delete(rgroup, gateway)

    logging.info(str(LROPoller.status()))

从LROPoller.status的结果是成功的,但仍然网关是在我的环境中。不幸的是,文档是次优,所以我不明白我做错了什么。

eqqqjvef

eqqqjvef1#

我在自己的环境中尝试,得到了以下结果:

您可以使用Python代码删除虚拟网络(VPN)网关,也可以将它们添加到Azure函数应用程序代码中。

代码:

from azure.identity import DefaultAzureCredential
from azure.mgmt.network import NetworkManagementClient
import time

start=time.time()
credential=DefaultAzureCredential()
subscription_id="Your sub id"
gateway="your gateway name"
network_client = NetworkManagementClient(credential, subscription_id )
resource_group_name = network_client.virtual_network_gateways.get(
    "<your resource grp name>", gateway).id.split('/')[4]
network_client.virtual_network_gateways.begin_delete(resource_group_name, gateway).result()
end=time.time()
print("VPN Gateway is deleted with time taken",end-start)

输出:

对于创建,您可以使用以下代码:

代码:

from azure.identity import DefaultAzureCredential
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2021_03_01.models import (VirtualNetworkGateway,
                                                   VirtualNetworkGatewayIpConfiguration,
                                                   SubResource)

# Set the subscription ID and resource group name
subscription_id = 'your-sub-id'
resource_group_name = 'your resources -grp name '

# Initialize the Network Management client
credential = DefaultAzureCredential()
network_client = NetworkManagementClient(credential, subscription_id)

# Create a Virtual Network Gateway object
gateway = VirtualNetworkGateway(
    gateway_type='Vpn',
    vpn_type='RouteBased',
    sku={'name': 'VpnGw1', 'tier': 'VpnGw1'},
    location='<your-location>',
    ip_configurations=[
        VirtualNetworkGatewayIpConfiguration(
            name='GatewayIpConfig',
            subnet=SubResource(id='<your-subnet-id>'),
            public_ip_address=SubResource(id='<your-public-ip-id>')
        )
    ]
)

# Create the VPN gateway
async_operation = network_client.virtual_network_gateways.create_or_update(
    resource_group_name,
    '<your-vpn-gateway-name>',
    gateway
)

async_operation.wait()
print("Virtual Network Gateway created successfully!")

注意:根据MS-Docs,一个虚拟vpn网关可能需要45分钟或更长的时间来完全创建和部署。

参考:

azure.mgmt.network.v2016_12_01.operations.VirtualNetworkGatewaysOperations class | Microsoft Learn

相关问题