UserP
UserP

Reputation: 314

creating vnet and subnet using python sdk

Can anyone please guide me on how to achieve the below using python sdk which i have tested from powershell .

$vnet = @{
    Name = 'myVNet'
    ResourceGroupName = 'CreateVNetQS-rg'
    Location = 'EastUS'
    AddressPrefix = '10.0.0.0/16'    
}
$virtualNetwork = New-AzVirtualNetwork @vnet
$subnet = @{
    Name = 'default'
    VirtualNetwork = $virtualNetwork
    AddressPrefix = '10.0.0.0/24'
}
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subnet
$virtualNetwork | Set-AzVirtualNetwork

I have followed this document : https://learn.microsoft.com/en-us/azure/virtual-network/quick-create-powershell and tested the same from powershell but my use case is from python and i can't seem to find any relevant document or sample for it .

Upvotes: 0

Views: 888

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11411

Yes, we can create virtual network and subnet using the Virtual Network Operations only. I tested using the below code :

from azure.identity import AzureCliCredential
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.models import (VirtualNetwork,
                                       AddressSpace,
                                        Subnet)
credential = AzureCliCredential()
subscription_id = "subID"
network_client = NetworkManagementClient(credential, subscription_id)
resource_group = "ansumantest"
virtual_network_name = "ansuman-vnet"
vnet_parameters = VirtualNetwork(location='east us',address_space=AddressSpace(address_prefixes=['10.0.0.0/16']),subnets=[Subnet(name='default',address_prefix='10.0.0.0/24')])
create_network=network_client.virtual_networks.begin_create_or_update(resource_group, virtual_network_name,parameters=vnet_parameters)

Output:

enter image description here

enter image description here

Reference:

You can change the parameters as per your requirement , for that you can refer the below references:

VirtualNetwork Class

Subnet Class

Upvotes: 1

Related Questions