使用.NET SDK从Azure network inferface中删除和删除PublicIP
我正在研究如何使用Azure.NET SDK从属于Azure VM的网络接口分离公共IP对象的示例使用.NET SDK从Azure network inferface中删除和删除PublicIP,azure,azure-sdk-.net,Azure,Azure Sdk .net,我正在研究如何使用Azure.NET SDK从属于Azure VM的网络接口分离公共IP对象的示例 我们的想法是在VM被释放时删除公共IP,这样我们就不会不必要地消耗公共IP配额。Azure Network Interface有一些IP配置。每个IP配置都有一个公共IP地址。因此,如果我们想从Azure网络接口分离公共IP,我们只需要从IP配置中删除公共IP。有关更多详细信息,请参阅 关于如何用Net实现它,我们可以使用sdk。具体步骤如下 a。创建服务主体(我使用Azure CLI来实现这一点
我们的想法是在VM被释放时删除公共IP,这样我们就不会不必要地消耗公共IP配额。Azure Network Interface有一些IP配置。每个IP配置都有一个公共IP地址。因此,如果我们想从Azure网络接口分离公共IP,我们只需要从IP配置中删除公共IP。有关更多详细信息,请参阅 关于如何用Net实现它,我们可以使用sdk。具体步骤如下 a。创建服务主体(我使用Azure CLI来实现这一点)
az login
az account set --subscription "<your subscription id>"
# the sp will have Azure Contributor role
az ad sp create-for-rbac -n "readMetric"
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId, // the sp appId
clientSecret, // the sp password
tenantId, // the sp tenant
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure.Configure()
.Authenticate(credentials)
.WithSubscription(subscriptionId);// the subscription you use
var resourceGroupName = "testapi06"; // the vm resource group name
var vmName = "testvs";// the vm name
//get the Azure VM
var vm =await azure.VirtualMachines.GetByResourceGroupAsync(resourceGroupName, vmName);
// get Azure VM's network interfaces
foreach (var nicId in vm.NetworkInterfaceIds) {
var nic = await azure.NetworkInterfaces.GetByIdAsync(nicId);
// get network interface's ip configurations
foreach (var r in nic.IPConfigurations)
{
var ipConfigNmae = r.Key;
// detach a Public IP object from network interface
await nic.Update().UpdateIPConfiguration(ipConfigNmae)
.WithoutPublicIPAddress()
.Parent()
.ApplyAsync();
// delete public ip
var publicIpId = r.Value.GetPublicIPAddress().Id;
await azure.PublicIPAddresses.DeleteByIdAsync(publicIpId);
};
};