本文由 发布,转载请注明出处,如有问题请联系我们! 发布时间: 2021-05-18【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法

加载中

【Azure 自然环境】在Azurevm虚拟机(經典) 的資源中,应用SDK导出来VM目录的方法

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 ...

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年,经历了十一年的发展趋势,在其中的构架也经历了大的变化,最显著的就是以最开始的經典方式到现阶段的ARM(Azure Resource Manager)方式,这两中方式简易差别的示意图如:

大量ARM实体模型与經典实体模型的差别,能够参照官方网站详细介绍:https://docs.microsoft.com/zh-cn/azure/azure-resource-manager/management/deployment-models

 

难题叙述

由于二种实体模型的不一样,如果是较早应用經典布署建立的vm虚拟机,则务必再次根据經典实际操作对其开展实际操作。因此 怎样才可以获得到VM目录呢?

针对vm虚拟机、储存账号和互联网络,假如資源是根据經典布署建立的,则务必再次根据經典实际操作对其开展实际操作。 假如vm虚拟机、储存账号或互联网络是根据 Resource Manager 布署建立的,则务必再次应用 Resource Manager 实际操作。

 

 

解决方案

如果是想获得ARM布署下的vm虚拟机資源,能够根据SDK获得,实际操作实例编码由此可见(【Azure Developer】应用Java SDK编码建立Azure VM (包括设定NSG,及加上数据信息硬盘SSD)),用  azure.virtualMachines().list() 这句话编码,就可以列举当今定阅下的全部VM目录。

可是,获得經典方式下的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

  1. 以服务项目管理人员或一同管理人员的真实身份登陆到 Azure 门户网。

  2. 开启定阅并挑选一个定阅。

    只有在定阅范畴分派一同管理人员。

  3. 点击“密钥管理(IAM)”。

  4. 点击“經典管理人员”菜单栏。

    打开经典管理员的屏幕截图

  5. 点击“加上” > “加上一同管理人员”开启“加上一同管理人员”视图。

    假如“加上一同管理人员”选择项已禁止使用,则表明你没有相对应的管理权限。

  6. 挑选要加上的客户,随后点击“加上”。

 

 

2)  获得一个资格证书文档

  • 点一下连接:https://portal.azure.cn/#blade/Microsoft_Azure_ClassicResources/PublishingProfileBlade
  • 挑选第一步中设定的定阅账户,点一下Validate按键,随后Download Publish Settings。免费下载的就是在第三步API启用中所必须的资格证书文档

 

 

3)  在编码中根据REST API的方法获得經典vm虚拟机信息内容

在Azure门户网中先查询到經典云服务器和布署的名字,随后根据 Get Deployment 方式获得到布署信息内容,回到的人物角色信息内容连接点中包括經典vm虚拟机的信息内容。在下面的编码中更换掉下列四个主要参数

  • 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方式获得到全部云服务器,随后解析xml在其中的布署信息内容获得經典vm虚拟机的信息内容

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迅速查询經典vm虚拟机目录(无编码),随后另存本地文件。

点一下“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

 

评论(0条)

刀客源码 游客评论