Azure Virtual Machine 之 如何利用Management Class Libraries 创建VM
2013-12-10 11:05
441 查看
之前发的blog简单的介绍了如何使用Management Class Libraries 来控制Azure platform。
但由于官方并没有提供文档,所以我们只能够通过自己研究来摸索使用该类库的方法。
在使用过程中我发现可以再抽象出一层中间层,从而让该类库的使用更趋近于Management REST API。
下面以创建一个Virtual Machine 为例让我们来了解如何使用该类库来操作Virtual Machine
首先我们要知道,在Azure Platform中,任何一个VM Instance都要运行在一个HostedService 里面的Deployment下面的。
所以在使用过程中我们需要首先创建一个Hosted Service。
我们可以通过以下两个方法来进行创建:
View Code
但由于官方并没有提供文档,所以我们只能够通过自己研究来摸索使用该类库的方法。
在使用过程中我发现可以再抽象出一层中间层,从而让该类库的使用更趋近于Management REST API。
下面以创建一个Virtual Machine 为例让我们来了解如何使用该类库来操作Virtual Machine
首先我们要知道,在Azure Platform中,任何一个VM Instance都要运行在一个HostedService 里面的Deployment下面的。
所以在使用过程中我们需要首先创建一个Hosted Service。
我们可以通过以下两个方法来进行创建:
class Program { public const string base64EncodedCertificate = ""; public const string subscriptionId = ""; public static string vmName = "Azure111"; public static string location = "East Asia"; public static string storageAccountName = "superdino"; public static string userName = "Dino"; public static string password = "PassWord1!"; public static string imageFamiliyName = "Windows Server 2012 Datacenter"; static void Main(string[] args) { ComputeManagementClient client = new ComputeManagementClient(getCredentials()); //You need a hosted service host VM. try { client.HostedServices.Get(vmName); } catch (CloudException e) { createCloudServiceByLocation(vmName, location); } var OSVHD = client.VirtualMachineImages.CreateOSVHD(vmName, vmName, storageAccountName, imageFamiliyName); var VMROle = client.VirtualMachines.CreateVMRole(vmName, vmName, VirtualMachineRoleSize.Small, userName, password, OSVHD); List<Role> roleList = new List<Role>{ VMROle }; client.VirtualMachines.CreateVMDeployment(vmName, vmName, roleList); Console.WriteLine("Create VM success"); client.VirtualMachines.AddRole("CloudServiceName", "ExsitDeploymentName", VMROle); Console.ReadLine(); } public static void createCloudServiceByLocation(string cloudServiceName, string location) { ComputeManagementClient client = new ComputeManagementClient(getCredentials()); HostedServiceCreateParameters hostedServiceCreateParams = new HostedServiceCreateParameters { ServiceName = cloudServiceName, Location = location, Label = EncodeToBase64(cloudServiceName), }; try { client.HostedServices.Create(hostedServiceCreateParams); } catch (CloudException e) { throw e; } } public static void createCloudServiceByAffinityGroup(string cloudServiceName, string affinityGroupName) { ComputeManagementClient client = new ComputeManagementClient(getCredentials()); HostedServiceCreateParameters hostedServiceCreateParams = new HostedServiceCreateParameters { ServiceName = cloudServiceName, AffinityGroup = affinityGroupName, Label = EncodeToBase64(cloudServiceName), }; try { client.HostedServices.Create(hostedServiceCreateParams); } catch (CloudException e) { throw e; } } public static string EncodeToBase64(string toEncode) { byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode); string returnValue = System.Convert.ToBase64String(toEncodeAsBytes); return returnValue; } public static SubscriptionCloudCredentials getCredentials() { return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate))); } }
View Code
相关文章推荐
- 如何使用 Jenkins、GitHub 和 Docker 在 Azure 中的 Linux VM 上创建开发基础结构
- 如何使用 Jenkins、GitHub 和 Docker 在 Azure 中的 Linux VM 上创建开发基础结构
- Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform
- 如何利用Skyline6.1的接口创建动态对象DynamicObject
- 如何利用Chart FX for WPF创建Expression黑色图表样式
- 在开测试环境下,如何利用makecert.exe命令创建各种certificate
- 如何在WSS中利用KeywordQuery创建搜索查询(转)
- SCVMM2012功能测试(5)―创建VM模板,并利用模板新建VM
- 如何实现利用类成员函数创建线程
- 上传通用化 VHD 并使用它在 Azure 中创建新 VM
- 排查在 Azure 中创建、重启 Windows VM 或调整其大小时发生的分配失败
- 如何利用C#创建和调用DLL(转)
- 如何创建一个FeatureClass,IFeatureWorkspace 接口漫谈
- 在 Azure 中的 Linux VM 上创建 MongoDB、Express、AngularJS 和 Node.js (MEAN) 堆栈
- 如何利用CEF3创建一个简单的应用程序 (Windows Platform)
- 如何利用WordPress创建自定义注册表单插件
- 如何利用C#创建和调用DLL
- 如何利用C++ Builder 5.0 Enterprise 创建用户自己的网上聊天程序
- 如何利用C++ Builder 5.0 Enterprise 创建用户自己的网上聊天程序
- sketchup技巧分享-如何利用组件创建无缝图案艺