PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/MyVMPortal/Default.aspx.cs

https://github.com/mwasham/wasvcmgmntapi
C# | 134 lines | 116 code | 18 blank | 0 comment | 9 complexity | 63cc7560d587792577b6f11b1647799a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using SMLibrary;
  8. using System.Xml.Linq;
  9. using System.Configuration;
  10. namespace MyVMPortal
  11. {
  12. public partial class _Default : Page
  13. {
  14. private VMManager GetVMM()
  15. {
  16. return new VMManager(ConfigurationManager.AppSettings["SubcriptionID"], ConfigurationManager.AppSettings["CertificateThumbprint"]);
  17. }
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. try
  21. {
  22. GetQuota();
  23. }
  24. catch (Exception exc)
  25. {
  26. Response.Write(exc.ToString());
  27. }
  28. }
  29. async protected void GetQuota()
  30. {
  31. try
  32. {
  33. VMManager vmm = GetVMM();
  34. XDocument subscriptionXML = await vmm.GetAzureSubscription();
  35. var quota = (from vm in subscriptionXML.Descendants(vmm.ns + "Subscription")
  36. select new
  37. {
  38. CurrentCoreCount = (string)vm.Element(vmm.ns + "CurrentCoreCount"),
  39. MaxCoreCount = (string)vm.Element(vmm.ns + "MaxCoreCount"),
  40. CurrentHostedServices = (string)vm.Element(vmm.ns + "CurrentHostedServices"),
  41. MaxHostedServices = (string)vm.Element(vmm.ns + "MaxHostedServices")
  42. }).FirstOrDefault();
  43. lblCoreCountUsed.Text = quota.CurrentCoreCount;
  44. lblCoreCountAvailable.Text = quota.MaxCoreCount;
  45. lblHostedServicesUsed.Text = quota.CurrentHostedServices;
  46. lblHostedServicesAvailable.Text = quota.MaxHostedServices;
  47. }
  48. catch (Exception exc)
  49. {
  50. String buffer = "";
  51. buffer = exc.ToString();
  52. if (exc.InnerException != null)
  53. {
  54. buffer += "<br />";
  55. buffer += exc.InnerException.ToString();
  56. }
  57. Response.Write(buffer);
  58. }
  59. }
  60. async protected void cmdListVMs_Click(object sender, EventArgs e)
  61. {
  62. VMManager vmm = GetVMM();
  63. XDocument vmsXML = await vmm.GetAzureVMs();
  64. var vms = from vm in vmsXML.Descendants(vmm.ns + "RoleInstance")
  65. select new
  66. {
  67. RoleName = (string)vm.Element(vmm.ns + "RoleName"),
  68. InstanceStatus = (string)vm.Element(vmm.ns + "InstanceStatus"),
  69. InstanceSize = (string)vm.Element(vmm.ns + "InstanceSize"),
  70. IpAddress = (string)vm.Element(vmm.ns + "IpAddress"),
  71. PowerState = (string)vm.Element(vmm.ns + "PowerState"),
  72. ServiceName = (string)vm.Element(vmm.ns + "ServiceName")
  73. };
  74. gridVMs.DataSource = vms.ToList();
  75. gridVMs.DataBind();
  76. }
  77. async protected void gridVMs_RowCommand(object sender, GridViewCommandEventArgs e)
  78. {
  79. VMManager vmm = GetVMM();
  80. int row = Convert.ToInt32(e.CommandArgument);
  81. String roleName = gridVMs.Rows[row].Cells[3].Text;
  82. String serviceName = gridVMs.Rows[row].Cells[8].Text;
  83. if (e.CommandName == "LoginRDP")
  84. {
  85. try
  86. {
  87. byte[] rdpFile = await vmm.GetRDPFile(serviceName, roleName);
  88. Response.AppendHeader("Content-Disposition", String.Format("filename={0}.rdp", roleName));
  89. Response.AppendHeader("Content-Length", rdpFile.Length.ToString());
  90. Response.ContentType = "application/octet-stream";
  91. Response.BinaryWrite(rdpFile);
  92. }
  93. catch (Exception exc)
  94. {
  95. lblLastStatus.Text = "Virtual Machine is Missing RDP Endpoint";
  96. }
  97. }
  98. if (e.CommandName == "Reboot")
  99. {
  100. String requestID = await vmm.RebootVM(serviceName, roleName);
  101. linkCheckStatus.NavigateUrl = "/GetOperationStatus.aspx?requestid=" + requestID;
  102. linkCheckStatus.Text = "Check Request Status";
  103. }
  104. else if (e.CommandName == "Shutdown")
  105. {
  106. String requestID = await vmm.ShutDownVM(serviceName, roleName);
  107. linkCheckStatus.NavigateUrl = "/GetOperationStatus.aspx?requestid=" + requestID;
  108. linkCheckStatus.Text = "Check Request Status";
  109. }
  110. }
  111. protected void cmdCreateVM_Click(object sender, EventArgs e)
  112. {
  113. Response.Redirect("~/CreateVM.aspx", false);
  114. }
  115. }
  116. }