gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:23:09 25 4
gpt4 key购买 nike

我有一些 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 的结果是成功,但网关仍然在我的环境中。不幸的是,该文档不是最佳的,所以我不明白我做错了什么。

最佳答案

我在我的环境中进行了尝试并得到了以下结果:

您可以使用 python 代码删除虚拟网络 (VPN) 网关,也可以将它们添加到您的 azure function 应用代码中。

代码:

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)

输出: enter image description here

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

代码:

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!")

Note: According to this MS-Docs a virtual vpn gateway can take 45 minutes or more to fully create and deploy.

引用:

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

关于python - 使用 python Azure SDK 删除和创建 Azure VPN 网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75748240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com