0

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 .

3
  • Hello @UserP, you have to use NetworkManagementClient for the case .. please refer: doc Commented Oct 25, 2021 at 7:25
  • Thank you for the response @AnsumanBal-MT . can i use only the virtual network operation to create a vnet and subnet like in a single operation without using subnet operations? if so , can you please help with a sample ? Commented Oct 25, 2021 at 7:30
  • yes i think you can create them in single operation as if you check the vnet create operations it consists of subnets in the virtual_network class in this link. let me test it in my environment and get back to you. Commented Oct 25, 2021 at 7:36

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.