I'm trying to set Use in virtual machine creation
and Allow public IP creation
to Yes
programmatically. I found the right method, here's my code for that:
var fragment = new VirtualNetworkFragment(
subnetOverrides: virtualNetwork.Subnets.Select(s =>
new SubnetOverrideFragment(
labSubnetName: s.Key,
resourceId: s.Value.Inner.Id,
useInVmCreationPermission: "Allow",
usePublicIpAddressPermission: "Allow")).ToList());
var vnet = await BaseClientFactory.CreateDevTestLabClient(azureClient)
.VirtualNetworks
.UpdateWithHttpMessagesAsync(dbLab.ResourceGroupName, dbLab.LabId.ToString(), virtualNetworkId, fragment);
Unfortunately the code doesn't work but it doesn't throw any exception, either. When I serialize fragment
variable to compare it with the request that's sent when doing this manually on Azure, there is a small difference.
Here is how the body looks like in the code when it's serialized using NewtonsoftJson (I took it from the Watch window):
Here is how the body looks like when sent via Azure:
The only two changes is that my code generates a request where allowedSubnets
are null
and (more importantly, I reckon) the structure differs a little in the way that my code generates properties.subnetOverrides
instead of a proper json structure. I'm not sure if this is a bug or I'm doing something wrong - any ideas?
According to my research, the update action just can be used to modify tags of virtual networks and all other properties will be ignored. If you want to modify the properties of virtual networks, please use Create Or Update action. Regarding how to implement with c# sdk Microsoft.Azure.Management.DevTestLabs
, please refer to the following steps
string clientId = " sp appId";
string clientSecret = "sp password";
string tenantId = "";
string subscriptionId = "";
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
DevTestLabsClient client = new DevTestLabsClient(credentials);
client.SubscriptionId = subscriptionId;
VirtualNetwork vnet = await client.VirtualNetworks.GetAsync("testdevlab", "test", "Dtltest");
// modify the vent's properties
vnet.SubnetOverrides = vnet.SubnetOverrides.Select(s => { s.UseInVmCreationPermission = "Allow"; s.UsePublicIpAddressPermission = "Allow"; return s; }).ToList();
//update vnet
var res =await client.VirtualNetworks.CreateOrUpdateAsync("testdevlab", "test", "Dtltest", vnet);