【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法
阅读原文时间:2021年05月19日阅读:1

Azure,  在2008年10月的Microsoft专业开发人员大会(PDC)上宣布,当时使用内部项目代号“Project Red Dog”,并于2010年2月正式发布为Windows Azure,然后在 2014年3月25日更名为Microsoft Azure

Azure, announced at Microsoft's Professional Developers Conference (PDC) in October 2008, went by the internal project codename "Project Red Dog", and formally released in February 2010, as Windows Azure before being renamed to Microsoft Azure on March 25, 2014

到2021年,经历了11年的发展,其中的架构也经历了大的变动,最明显的就是从最早的经典模式到目前的ARM(Azure Resource Manager)模式,这两中模式简单区别的模式图如:

更多ARM模型与经典模型的区别,可以参考官网介绍:https://docs.microsoft.com/zh-cn/azure/azure-resource-manager/management/deployment-models

问题描述

因为两种模型的不同,如果是较早使用经典部署创建的虚拟机,则必须继续通过经典操作对其进行操作。所以如何才能获取到VM列表呢?

对于虚拟机、存储帐户和虚拟网络,如果资源是通过经典部署创建的,则必须继续通过经典操作对其进行操作。 如果虚拟机、存储帐户或虚拟网络是通过 Resource Manager 部署创建的,则必须继续使用 Resource Manager 操作。

解决方法

如果是想获取ARM部署下的虚拟机资源,可以通过SDK获取,操作示例代码可见(【Azure Developer】使用Java SDK代码创建Azure VM (包含设置NSG,及添加数据磁盘SSD)),用  azure.virtualMachines().list() 这句代码,即可列出当前订阅下的所有VM列表。

但是,获取经典模式下的虚拟机则完全不同。经过研究,代码方式可以通过REST API的方式来解决,只是操作步骤也最复杂:

REST API 接口:https://management.core.chinacloudapi.cn/{订阅号ID}/services/hostedservices/{云服务名称}/deployments/{部署名}

1)  在调用接口前,需要保证您的当前订阅有经典管理员的权限,请参考:https://docs.azure.cn/zh-cn/role-based-access-control/classic-administrators

2)  获取一个证书文件

3) 在代码中通过REST API的方式获取经典虚拟机信息

在Azure门户中先查看到经典云服务和部署的名称,然后通过 Get Deployment 方法获取到部署信息,返回的角色信息节点中包含经典虚拟机的信息。在下面的代码中替换掉以下四个参数

  • credentialsPath = @"your.publishsettings" ## 第二步中所下载的文件
  • subscriptionId = "subscriptionId"                 ##第一步中的订阅号
  • cloudServiceName = "cloudServiceName" ##在云服务中查看
  • deploymentName = "deploymentName"     ##在云服务中查看

/****************************** Module Header ******************************\
* Module Name: Program.cs
* Project: GetClassicVMInfo
* Copyright (c) Microsoft Corporation.
*
* Managing Azure in Role instance may be difficult, because it requires a client
* certificate. This sample will show how to use the base64 string certificate
* instead of getting the certificate from Certificates store
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using System.Net;
using System.IO;

namespace GetClassicVMInfo
{
class Program
{
public static string credentialsPath = @"your.publishsettings";
public static string subscriptionId = "subscriptionId";
public static string cloudServiceName = "cloudServiceName";
public static string deploymentName = "deploymentName";

    static void Main(string\[\] args)  
    {  
        XElement x=XElement.Load(credentialsPath);

        var Base64cer = (from c in x.Descendants("Subscription")  
                            where c.Attribute("Id").Value == subscriptionId  
                            select c.Attribute("ManagementCertificate").Value).FirstOrDefault();

        X509Certificate2 cer = null;  
        if (Base64cer != null)  
        {  
            cer = new X509Certificate2(Convert.FromBase64String(Base64cer));  
        }  
        if (cer != null)  
        {  
            GetHostedServicesByRESTAPI(subscriptionId, cer);  
        }  
        Console.ReadLine();  
    }

    static void GetHostedServicesByRESTAPI(string subscriptionId, X509Certificate2 certificate)  
    {  
        string uri = string.Format("https://management.core.chinacloudapi.cn/{0}/services/hostedservices/{1}/deployments/{2}", subscriptionId, cloudServiceName, deploymentName);  
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri));  
        request.Method = "GET";  
        request.ClientCertificates.Add(certificate);  
        request.ContentType = "application/xml";  
        request.Headers.Add("x-ms-version", "2016-06-01");

        try  
        {  
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
            // Parse the web response.  
            Stream responseStream = response.GetResponseStream();  
            StreamReader reader = new StreamReader(responseStream);

            string xml = reader.ReadToEnd();  
            XDocument doc = XDocument.Parse(xml);  
            XNamespace ns = doc.Root.Name.Namespace;  
            XElement roleInstanceList = doc.Root.Element(ns + "RoleInstanceList");  
            if (roleInstanceList != null)  
            {  
                foreach (XElement roleInstance in roleInstanceList.Elements())  
                {  
                    Console.WriteLine("InstanceName:"+roleInstance.Element(ns + "InstanceName").Value);  
                    Console.WriteLine("InstanceStatus:" + roleInstance.Element(ns + "InstanceStatus").Value);  
                }  
            }  
            // Close the no longer needed resources.  
            response.Close();  
            responseStream.Close();  
            reader.Close();  
            Console.ReadKey();  
        }  
        catch (WebException ex)  
        {  
            Console.Write(ex.Response.Headers.ToString());  
            Console.Read();  
        }

    }  
}  

}

PS: 也可以通过List Cloud Services方法获取到所有云服务,然后遍历其中的部署信息获取经典虚拟机的信息

Get Deployment : https://docs.microsoft.com/en-us/previous-versions/azure/reference/ee460804(v=azure.100)

List Cloud Services : https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-list-cloud-services

以上文档中的URL均为Global Azure,如果在中国区使用,则需要转换成:management.core.chinacloudapi.cn, 这点非常重要。

附录一:在Azure 门户中使用Azure Resource Graph快速查看经典虚拟机列表(无代码),然后另存为本地文件。

点击“https://portal.azure.cn/#blade/HubsExtension/ArgQueryBlade”进入 **Azure Resource Graph Explorer** 页面,输入以下查询语句:

Resources
| project name, location, type
| where type =~ 'Microsoft.ClassicCompute/virtualMachines'
| order by name desc

查询结果:

参考资料

Azure 经典订阅管理员: https://docs.azure.cn/zh-cn/role-based-access-control/classic-administrators

Azure 资源管理器和经典部署:了解部署模型和资源状态 : https://docs.microsoft.com/zh-cn/azure/azure-resource-manager/management/deployment-models

Get Deployment : https://docs.microsoft.com/en-us/previous-versions/azure/reference/ee460804(v=azure.100)

List Cloud Services : https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-list-cloud-services

中国区Azure 终结点 Endpoint:https://docs.azure.cn/zh-cn/articles/guidance/developerdifferences#check-endpoints-in-azure