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