PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/MyVMPortal/CreateVM.aspx.cs

https://github.com/mwasham/wasvcmgmntapi
C# | 204 lines | 172 code | 31 blank | 1 comment | 20 complexity | 99fe2a1254d939f535e94a06f8e48c20 MD5 | raw file
  1. using SMLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. namespace MyVMPortal
  13. {
  14. public partial class CreateVM : System.Web.UI.Page
  15. {
  16. private String _StorageAccount = String.Empty;
  17. private String GetStorageAccount()
  18. {
  19. string[] accounts = ConfigurationManager.AppSettings["StorageAccount"].Split(new char[] { ',' });
  20. Random r = new Random(DateTime.Now.Millisecond);
  21. return accounts[r.Next(0, accounts.Length)];
  22. }
  23. async protected void Page_Load(object sender, EventArgs e)
  24. {
  25. if (!Page.IsPostBack)
  26. {
  27. VMManager vmm = GetVMM();
  28. List<String> images = await vmm.GetAzureVMImages();
  29. ddlImages.DataSource = images;
  30. ddlImages.DataBind();
  31. lblStorageAccount.Text = GetStorageAccount();
  32. _StorageAccount = lblStorageAccount.Text;
  33. ViewState["StorageAccount"] = _StorageAccount;
  34. }
  35. else
  36. {
  37. txtPassword.Attributes["value"] = txtPassword.Text;
  38. _StorageAccount = ViewState["StorageAccount"].ToString();
  39. }
  40. GetQuota();
  41. }
  42. private VMManager GetVMM()
  43. {
  44. return new VMManager(ConfigurationManager.AppSettings["SubcriptionID"], ConfigurationManager.AppSettings["CertificateThumbprint"]);
  45. }
  46. private String GetOSDiskMediaLocation()
  47. {
  48. String osdiskmedialocation = String.Format("https://{0}.blob.core.windows.net/vhds/{1}-OS-{2}.vhd", _StorageAccount, txtVMName.Text, Guid.NewGuid().ToString());
  49. return osdiskmedialocation;
  50. }
  51. private String GetDataDiskMediaLocation()
  52. {
  53. String osdiskmedialocation = String.Format("https://{0}.blob.core.windows.net/vhds/{1}-Data-{2}.vhd", _StorageAccount, txtVMName.Text, Guid.NewGuid().ToString());
  54. return osdiskmedialocation;
  55. }
  56. protected void cmdCreateVMConfig_Click(object sender, EventArgs e)
  57. {
  58. GenerateVMConfig(false);
  59. }
  60. async public void GenerateVMConfig(bool Create)
  61. {
  62. if (String.IsNullOrEmpty(txtServiceName.Text))
  63. {
  64. lblStatus.Text = "Missing Service Name";
  65. return;
  66. }
  67. if (String.IsNullOrEmpty(txtVMName.Text))
  68. {
  69. lblStatus.Text = "Missing VM Name";
  70. return;
  71. }
  72. if (String.IsNullOrEmpty(txtPassword.Text))
  73. {
  74. lblStatus.Text = "Missing Pasword";
  75. return;
  76. }
  77. if (ValidationHelpers.IsWindowsComputerNameValid(txtVMName.Text) == false)
  78. {
  79. lblStatus.Text = "VM Name is either too long or has invalid characters";
  80. return;
  81. }
  82. if (ValidationHelpers.IsWindowsPasswordValid(txtPassword.Text) == false)
  83. {
  84. lblStatus.Text = "Windows Password does not meet complexity requirements.";
  85. return;
  86. }
  87. lblStatus.Text = "";
  88. VMManager vmm = GetVMM();
  89. if(await vmm.IsServiceNameAvailable(txtServiceName.Text) == false)
  90. {
  91. lblStatus.Text = "Service Name is not available. Must be unique";
  92. return;
  93. }
  94. XDocument vm = vmm.NewAzureVMConfig(txtVMName.Text, ddlVMSize.Text, ddlImages.Text, GetOSDiskMediaLocation(), true);
  95. vm = vmm.NewWindowsProvisioningConfig(vm, txtVMName.Text, txtPassword.Text);
  96. vm = vmm.NewNetworkConfigurationSet(vm);
  97. if (chkAddDataDisk.Checked == true)
  98. {
  99. vm = vmm.AddNewDataDisk(vm, 10, 0, "MyDataDisk", GetDataDiskMediaLocation());
  100. }
  101. if (chkAddHTTPEndpoint.Checked == true)
  102. {
  103. vm = vmm.AddNewInputEndpoint(vm, "web", "TCP", 80, 80);
  104. }
  105. if (chkAddRDPEndpoint.Checked == true)
  106. {
  107. vm = vmm.AddNewInputEndpoint(vm, "rdp", "TCP", 3389, 3389);
  108. }
  109. var builder = new StringBuilder();
  110. var settings = new XmlWriterSettings()
  111. {
  112. Indent = true
  113. };
  114. using (var writer = XmlWriter.Create(builder, settings))
  115. {
  116. vm.WriteTo(writer);
  117. }
  118. divXML.Visible = true;
  119. lblVMXML.Text = Server.HtmlEncode(builder.ToString());
  120. tblConfig.Visible = true;
  121. cmdCreateVM.Visible = true;
  122. cmdCreateVMConfig.Visible = false;
  123. if (Create == true)
  124. {
  125. String requestID_cloudService = await vmm.NewAzureCloudService(txtServiceName.Text, "West US", String.Empty);
  126. OperationResult result = await vmm.PollGetOperationStatus(requestID_cloudService, 5, 120);
  127. if (result.Status == OperationStatus.Succeeded)
  128. {
  129. // VM creation takes too long so we'll check it later
  130. String requestID_createDeployment = await vmm.NewAzureVMDeployment(txtServiceName.Text, txtVMName.Text, String.Empty, vm, null);
  131. Response.Redirect("/GetOperationStatus.aspx?requestid=" + requestID_createDeployment);
  132. }
  133. else
  134. {
  135. lblStatus.Text = String.Format("Creating Cloud Service Failed. Message: {0}", result.Message);
  136. }
  137. }
  138. }
  139. protected void chkAddDataDisk_Click(object sender, EventArgs e)
  140. {
  141. GenerateVMConfig(false);
  142. }
  143. protected void chkAddRDPEndpoint_Click(object sender, EventArgs e)
  144. {
  145. GenerateVMConfig(false);
  146. }
  147. protected void chkAddHTTPEndpoint_Click(object sender, EventArgs e)
  148. {
  149. GenerateVMConfig(false);
  150. }
  151. protected void cmdCreateVM_Click(object sender, EventArgs e)
  152. {
  153. GenerateVMConfig(true);
  154. }
  155. async protected void GetQuota()
  156. {
  157. VMManager vmm = GetVMM();
  158. XDocument subscriptionXML = await vmm.GetAzureSubscription();
  159. var quota = (from vm in subscriptionXML.Descendants(vmm.ns + "Subscription")
  160. select new
  161. {
  162. CurrentCoreCount = (string)vm.Element(vmm.ns + "CurrentCoreCount"),
  163. MaxCoreCount = (string)vm.Element(vmm.ns + "MaxCoreCount"),
  164. CurrentHostedServices = (string)vm.Element(vmm.ns + "CurrentHostedServices"),
  165. MaxHostedServices = (string)vm.Element(vmm.ns + "MaxHostedServices")
  166. }).FirstOrDefault();
  167. lblCoreCountUsed.Text = quota.CurrentCoreCount;
  168. lblCoreCountAvailable.Text = quota.MaxCoreCount;
  169. lblHostedServicesUsed.Text = quota.CurrentHostedServices;
  170. lblHostedServicesAvailable.Text = quota.MaxHostedServices;
  171. }
  172. }
  173. }