Azure SDK - Add/Remove Virtual Network from App Service

304 views Asked by At

We have a need to Add or Remove Web Apps from Virtual Neworks. We have the virtual network already setup, so we created an app to create subnets and then allocate the Web App to the VN using that subnet.

I have the following SDK's consumed in our C# project:

    <PackageReference Include="Microsoft.Azure.Management.AppService.Fluent" Version="1.38.0" />
    <PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.38.0" />
    <PackageReference Include="Microsoft.Azure.Management.Network.Fluent" Version="1.38.0" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />

I have tried to find a way to update the network settings for my Web App, but cannot seem to find an Update that works with it?

In the code below I have added a comment on the bit I don't know how to do.

var networks = azure.Networks.List().ToList();
var mainVN = networks.Find(n => n.Key == "MainVn");
webApp = azure.WebApps.Find(w => w.Name == "PrimaryApp")
var cidr = $"10.0.1.0/24";
string subNetName = webApp.Name + "-subnet";
await mainVN.Update().WithSubnet(subNetName, cidr).ApplyAsync();
webApp.Update() // <----- how do I do this bit? To add the subnet/VN to the app service?
1

There are 1 answers

0
Leonardo Oliveira On BEST ANSWER
    // DELETE 
    await azure.WebApps.Manager.Inner.WebApps.DeleteSwiftVirtualNetworkAsync("rg", "webapp");

    // ADD
    await azure.WebApps.Manager.Inner.WebApps.CreateOrUpdateSwiftVirtualNetworkConnectionAsync("rg", "webapp", 
        new Microsoft.Azure.Management.AppService.Fluent.Models.SwiftVirtualNetworkInner { 
    });