Adding multiple address prefixes to network subnet using Azure SDK

Viewed 498

I have existing network with subnet, which has its IPV4 address prefix specified. Using Azure SDK in C# i need to add additional IPV6 address prefix without removing the existing one. I managed to do this in portal and using Microsoft REST API, but my goal is to implement it using their SDK. Here is the piece of code I wrote so far:

await networkToUpdate.Update()
                        .UpdateSubnet(network.SubnetAzureId)
                        .WithAddressPrefix("10.0.0.0/21")
                        .WithAddressPrefix("ace:cab:dca:deed::/64")
                        .Parent()
                        .ApplyAsync();

Unfortunately, it allows to set only one address prefix, which is the IPV6 in this scenario. Is there any way to add both address prefixes via SDK?

1 Answers

Please use the code below:

        var clientId = "xxx";
        var clientSecret = "xxx";
        var tenantId = "xxx";

        var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);

        var azure = Azure.Configure()
            .Authenticate(creds)                
            .WithDefaultSubscription();

        var myvnet = azure.Networks.GetById("/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx");
       

        //use the code below to update multi address prefixes
        IList<string> list = new List<string>() { "172.18.3.0/24", "ace:cab:dca:deed::/64" };
        myvnet.Subnets["the_subnet_name"].Inner.AddressPrefix = "";
        myvnet.Subnets["the_subnet_name"].Inner.AddressPrefixes = list;

        myvnet.Update().Apply();  

The test result:

enter image description here

Please let me know if you still have issues about that.

Related