首页 > 代码库 > 利用Azure Rest API 创建虚拟机
利用Azure Rest API 创建虚拟机
在此之前,我曾经看过一篇文章讲叙了如何利用Azure power shell team 提供的class library。
而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Windows Azure Management Libraries For .NET Nuget package来帮助.NET 开发人员来更好的控制Auzre Platform。
相比power shell team使用的library, Windows Azure Management Libraries For .NET 将业务逻辑更好的划分了开来,同时也使用了最新的Async programing来替代.net 4.5之前非常流行的异步委托编程方式。很明显,这个class library今后将融入Azure SDK 之中,成为替代.NET 程序员直接调用Azure Management REST API的最佳选择。
那么就让我们来了解一下如何使用这个libararies吧。
第一步:
添加NuGet Package到项目中。如下图
然后输入如下命令安装包:
Install-Package Microsoft.WindowsAzure.Management.Libraries -Pre
如果上面的安装包的过程好了,包中的类库便会被引入到项目中。
第二步:
在完成第一步后,我们需要去官方查看开发文档(http://msdn.microsoft.com/en-us/library/ee460799.aspx),在Operation On Virtual Machine中点看
Create Virtual Machine Deployment项,进入该篇幅进行技术开发查看,了解如何去调用类库创建一个虚机的过程。
下面图片是官方在线文档截图:
从这里我们必须要有一个订阅ID号,有了这个订阅ID号之后,在创建虚拟机之前先要创建一个云服务,用于部署这个虚机时使用。
如何创建云服务跟上面的一样简单,查阅官方文档,如下图:
在这里我就不再重复讲解了,你们可以查看官方文档和技术案例。我接下来就直接上代码好了。
第三步:
App.config代码
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="ServiceManageBaseUrl" value="https://management.core.chinacloudapi.cn/" /> <add key="SubscriptionID" value="http://www.mamicode.com/your-Subscription-ID" /> <add key="Thrumbprint" value="http://www.mamicode.com/your-Thrumbprint-for-your-certification"/> </appSettings> </configuration>
这里面有一点需要说明一下:根据你使用Windows Azure地区版本的不同上面的订阅信息会有所不同。比如说如果你使用的是国际版,那么你的ServiceManageBaseUrl就是官方所提供的
https://management.core.windows.net/,因为我是用的是中国大陆世纪互联运营代理商的账号,所以我这里就是https://management.core.chinacloudapi.cn/。对于为什么要使用指纹,我们在接下来管理认证的时候讲解。
Package.config代码(在第一步安装好,便会自动添加这些代码)
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" /> <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" /> <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" /> <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Common" version="1.3.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management" version="2.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Compute" version="5.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Libraries" version="2.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.MediaServices" version="2.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Monitoring" version="1.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Network" version="3.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Scheduler" version="3.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Sql" version="3.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.Storage" version="3.0.0" targetFramework="net45" /> <package id="Microsoft.WindowsAzure.Management.WebSites" version="3.0.0" targetFramework="net45" /> <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" /> </packages>
逻辑代码实现:
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Management.Compute; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using Microsoft.WindowsAzure.Management.Compute.Models; using System.Net; namespace AzureTest { class Program { private static String subscriptionID = ConfigurationManager.AppSettings["SubscriptionID"]; private static String serviceManageBaseUrl = ConfigurationManager.AppSettings["ServiceManageBaseUrl"]; private static String thrumbprint = ConfigurationManager.AppSettings["Thrumbprint"]; //private static String managementCertificate = ConfigurationManager.AppSettings["ManagementCertificate"]; private static ComputeManagementClient client = new ComputeManagementClient(getCredentials(), new Uri(serviceManageBaseUrl)); static void Main(string[] args) { //从ComputeManagementDiscoveryExtensions中用CreateComputeManagementClient获得一个 //ComputeManagementClient computeManageClient = ComputeManagementDiscoveryExtensions.CreateComputeManagementClient(); //getAllCloudServicesName(); try { string serviceName = CreateCloudService("MikeService"); createVM(serviceName); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); } /// <summary> /// 获得该订阅下的所有云服务名 /// </summary> public static void getAllCloudServicesName() { var cloudServiceList = client.HostedServices.List(); foreach (var cloudService in cloudServiceList) { Console.WriteLine(cloudService.ServiceName); } } /// <summary> /// 创建云服务 /// </summary> /// <param name="serviceName">云服务名</param> /// <returns>云服务名/Null</returns> private static string CreateCloudService(string serviceName) { try { HostedServiceCreateParameters parameters = new HostedServiceCreateParameters(); parameters.ServiceName = serviceName; parameters.Location = "West China"; parameters.Description = "这是一个用于部署虚拟机的云服务"; OperationResponse operationResponse = client.HostedServices.Create(parameters); if (operationResponse.StatusCode == HttpStatusCode.OK) { return serviceName; } else return null; } catch (Exception e) { throw e; } } /// <summary> /// 创建一个虚拟机 /// </summary> /// <param name="serviceName">部署服务名</param> private static void createVM(string serviceName) { //虚机参数模型 VirtualMachineCreateParameters parameters = new VirtualMachineCreateParameters(); parameters.RoleName = "MikeVirtualMachine"; parameters.RoleSize = "Small"; //计算机配置 ConfigurationSet provisionItem = new ConfigurationSet(){ ConfigurationSetType = ConfigurationSetTypes.WindowsProvisioningConfiguration, ComputerName = "Mike-PC", AdminUserName = "Mike", AdminPassword = "Password01!", EnableAutomaticUpdates = true, ResetPasswordOnFirstLogon = true }; //网络配置 List<InputEndpoint> inputList = new List<InputEndpoint>(); InputEndpoint endpoint = new InputEndpoint() { LocalPort = 3389, Name = "RemoteDesktop", Protocol = "tcp" }; inputList.Add(endpoint); ConfigurationSet networkItem = new ConfigurationSet(){ ConfigurationSetType = ConfigurationSetTypes.NetworkConfiguration, InputEndpoints = inputList }; parameters.ConfigurationSets.Add(provisionItem); parameters.ConfigurationSets.Add(networkItem); //虚拟磁盘配置 parameters.OSVirtualHardDisk.Name = "MikeVirtualMachine-0-20121007173943"; parameters.OSVirtualHardDisk.MediaLink = new Uri("https://portalvhdsmz7vww0gb0p5k.blob.core.chinacloudapi.cn/vhds/MikeVirtualMachine.vhd"); parameters.OSVirtualHardDisk.SourceImageName = "55bc2b193643443bb879a78bda516fc8__Windows-Server-2012-R2-201408.01-zh.cn-127GB.vhd"; Task<OperationStatusResponse> responseTask = client.VirtualMachines.CreateAsync(serviceName,"MikeVirtalMachine",parameters,new System.Threading.CancellationToken()); Console.WriteLine("响应信息:/n" + responseTask.IsCompleted.ToString() ); } //获得证书 private static SubscriptionCloudCredentials getCredentials() { return new CertificateCloudCredentials(subscriptionID, GetStoreCertificate(thrumbprint)); } /// <summary> /// 从指纹获取证书 /// </summary> /// <param name="thumbprint">指纹</param> /// <returns>客户端证书</returns> private static X509Certificate2 GetStoreCertificate(string thumbprint) { List<StoreLocation> locations = new List<StoreLocation> { StoreLocation.CurrentUser, StoreLocation.LocalMachine }; foreach (var location in locations) { X509Store store = new X509Store("My", location); try { store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection certificates = store.Certificates.Find( X509FindType.FindByThumbprint, thumbprint, false); if (certificates.Count == 1) { return certificates[0]; } } finally { store.Close(); } } throw new ArgumentException(string.Format( "A Certificate with Thumbprint ‘{0}‘ could not be located.", thumbprint)); } } }
那么这个简单的类库调用创建虚拟机和云服务就做好了,是不是感觉很简单了呢?不用再像Send Http Request那样麻烦了,都是采用属性和成员变量的方式对Http Request进行了封装。习惯了面向对象编程的人来说,这给予了极大的方便,更多操作将会在以后的博客中记录下我日常Azure REST API工作和学习的遇到的问题和解决方案与大家分享。
总结:
如果你没有使用过Http Request的程序员,那么使用这个也很容易上手,只要下载这个类库,那么你就可以根据代码的提示查阅相关的资料就可以很快的入门和使用。我总结一下一个Http Request的过程。
1.订阅信息进行认证管理:
private static ComputeManagementClient client = new ComputeManagementClient(getCredentials(), new Uri(serviceManageBaseUrl));
通过指纹查找到客户端管理证书:
/// <summary> /// 从指纹获取证书 /// </summary> /// <param name="thumbprint">指纹</param> /// <returns>客户端证书</returns> private static X509Certificate2 GetStoreCertificate(string thumbprint) { List<StoreLocation> locations = new List<StoreLocation> { StoreLocation.CurrentUser, StoreLocation.LocalMachine }; foreach (var location in locations) { X509Store store = new X509Store("My", location); try { store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection certificates = store.Certificates.Find( X509FindType.FindByThumbprint, thumbprint, false); if (certificates.Count == 1) { return certificates[0]; } } finally { store.Close(); } } throw new ArgumentException(string.Format( "A Certificate with Thumbprint ‘{0}‘ could not be located.", thumbprint)); }
类库中封装的方法:将订阅号和管理证书关联绑定
//获得证书 private static SubscriptionCloudCredentials getCredentials() { return new CertificateCloudCredentials(subscriptionID, GetStoreCertificate(thrumbprint)); }
2.操作目标对象化
该类库把请求XML中的参数都封装成功了对象,用属性代替XML文件的<属性>,不用再操作XMl而直接操作对象便可以完成操作对象的参数属性设置。
本文出自 “爱咖啡” 博客,请务必保留此出处http://4837471.blog.51cto.com/4827471/1580222
利用Azure Rest API 创建虚拟机