使用 .NET SDK 从 Azure 网络接口分离和删除公共 IP
Detatch and delete PublicIP from Azure network inferface using .NET SDK
我正在研究如何使用 Azure .NET SDK 从属于 Azure VM 的网络接口分离 Public IP 对象的示例。
想法是在解除分配 VM 时删除 public IP,这样我们就不会不必要地消耗 public IP 配额。
Azure 网络接口有一些 IP 配置。每个 IP 配置都有一个 public IP 地址。因此,如果我们想从 Azure 网络接口分离 Public IP,我们只需要从 IP 配置中删除 public IP。详情请参考document
关于Net如何实现,可以使用sdkAzure Management Libraries for .NET。详细步骤如下
一个。创建一个服务主体(我使用 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);
};
};
我正在研究如何使用 Azure .NET SDK 从属于 Azure VM 的网络接口分离 Public IP 对象的示例。
想法是在解除分配 VM 时删除 public IP,这样我们就不会不必要地消耗 public IP 配额。
Azure 网络接口有一些 IP 配置。每个 IP 配置都有一个 public IP 地址。因此,如果我们想从 Azure 网络接口分离 Public IP,我们只需要从 IP 配置中删除 public IP。详情请参考document
关于Net如何实现,可以使用sdkAzure Management Libraries for .NET。详细步骤如下
一个。创建一个服务主体(我使用 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);
};
};