ネットワークインタフェースに関連付けているネットワークセキュリティグループの付け替えも、Azure.ResourceManager.Network を使えばできた。
using Azure; using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.Network; using Azure.ResourceManager.Resources; const string ClientId = "クライアントID"; const string ClientSecret = "クライアントシークレット"; const string TenantId = "テナントID"; const string SubscriptionId = "サブスクリプションID"; const string ResourceGroupName = "リソースグループ名"; const string NetworkInterfaceName = "ネットワークインタフェース名"; const string DestNetworkSecurityGroupName = "新しいネットワークセキュリティグループ名"; var credential = new ClientSecretCredential( tenantId: TenantId, clientId: ClientId, clientSecret: ClientSecret); var client = new ArmClient(credential); ResourceGroupResource resourceGroup = await client.GetSubscriptionResource( SubscriptionResource.CreateResourceIdentifier(SubscriptionId)) .GetResourceGroupAsync(ResourceGroupName); // ネットワークセキュリティグループを変更したいネットワークインタフェースを取得 NetworkInterfaceResource nic = await resourceGroup.GetNetworkInterfaceAsync(NetworkInterfaceName); Console.WriteLine(nic.Data.NetworkSecurityGroup.Id); // 新しく関連付けたいネットワークセキュリティグループを取得 NetworkSecurityGroupResource destNsg = await resourceGroup.GetNetworkSecurityGroupAsync(DestNetworkSecurityGroupName); // 更新時はすべてのプロパティに値をセットする必要があるっぽいので、 // 取得しておいたデータを再利用して省力化。 // with が使えたらイミュータブルにできるんだけど…。 nic.Data.NetworkSecurityGroup = destNsg.Data; // CreateOrUpdate で更新 var networkInterfaces = resourceGroup.GetNetworkInterfaces(); var updateOperation = await networkInterfaces.CreateOrUpdateAsync( waitUntil: WaitUntil.Completed, networkInterfaceName: NetworkInterfaceName, data: nic.Data); NetworkInterfaceResource updatedNic = await updateOperation.WaitForCompletionAsync(); Console.WriteLine(updatedNic.Data.NetworkSecurityGroup.Id);