wasvcmgmntapi /MyVMPortal/Default.aspx.cs

Language C# Lines 134
MD5 Hash 63cc7560d587792577b6f11b1647799a
Repository https://github.com/mwasham/wasvcmgmntapi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMLibrary;
using System.Xml.Linq;
using System.Configuration;
namespace MyVMPortal
{
    public partial class _Default : Page
    {


        private VMManager GetVMM()
        {
            return new VMManager(ConfigurationManager.AppSettings["SubcriptionID"], ConfigurationManager.AppSettings["CertificateThumbprint"]);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                GetQuota();
            }
            catch (Exception exc)
            {
                Response.Write(exc.ToString());
            }
        }

        async protected void GetQuota()
        {
            try
            {
                VMManager vmm = GetVMM();

                XDocument subscriptionXML = await vmm.GetAzureSubscription();
                var quota = (from vm in subscriptionXML.Descendants(vmm.ns + "Subscription")
                             select new
                             {
                                 CurrentCoreCount = (string)vm.Element(vmm.ns + "CurrentCoreCount"),
                                 MaxCoreCount = (string)vm.Element(vmm.ns + "MaxCoreCount"),
                                 CurrentHostedServices = (string)vm.Element(vmm.ns + "CurrentHostedServices"),
                                 MaxHostedServices = (string)vm.Element(vmm.ns + "MaxHostedServices")
                             }).FirstOrDefault();

                lblCoreCountUsed.Text = quota.CurrentCoreCount;
                lblCoreCountAvailable.Text = quota.MaxCoreCount;
                lblHostedServicesUsed.Text = quota.CurrentHostedServices;
                lblHostedServicesAvailable.Text = quota.MaxHostedServices;
            }
            catch (Exception exc)
            {
                String buffer = "";

                buffer = exc.ToString();
                if (exc.InnerException != null)
                {
                    buffer += "<br />";
                    buffer += exc.InnerException.ToString();
                }

                Response.Write(buffer);

            }
        }

        async protected void cmdListVMs_Click(object sender, EventArgs e)
        {

            VMManager vmm = GetVMM();

            XDocument vmsXML = await vmm.GetAzureVMs();

            var vms = from vm in vmsXML.Descendants(vmm.ns + "RoleInstance")
                           select new
                           {
                               RoleName = (string)vm.Element(vmm.ns + "RoleName"),
                               InstanceStatus = (string)vm.Element(vmm.ns + "InstanceStatus"),
                               InstanceSize = (string)vm.Element(vmm.ns + "InstanceSize"),
                               IpAddress = (string)vm.Element(vmm.ns + "IpAddress"),
                               PowerState = (string)vm.Element(vmm.ns + "PowerState"),
                               ServiceName = (string)vm.Element(vmm.ns + "ServiceName")
                           };

            gridVMs.DataSource = vms.ToList();
            gridVMs.DataBind();

            
        }

        async protected void gridVMs_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            VMManager vmm = GetVMM();
            int row = Convert.ToInt32(e.CommandArgument);
            String roleName = gridVMs.Rows[row].Cells[3].Text;
            String serviceName = gridVMs.Rows[row].Cells[8].Text;
            if (e.CommandName == "LoginRDP")
            {
                try
                {
                    byte[] rdpFile = await vmm.GetRDPFile(serviceName, roleName);
                    Response.AppendHeader("Content-Disposition", String.Format("filename={0}.rdp", roleName));
                    Response.AppendHeader("Content-Length", rdpFile.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(rdpFile);
                }
                catch (Exception exc)
                {
                    lblLastStatus.Text = "Virtual Machine is Missing RDP Endpoint";
                }
            }
            if (e.CommandName == "Reboot")
            {
                String requestID = await vmm.RebootVM(serviceName, roleName);
                linkCheckStatus.NavigateUrl = "/GetOperationStatus.aspx?requestid=" + requestID;
                linkCheckStatus.Text = "Check Request Status";
            }
            else if (e.CommandName == "Shutdown")
            {
                String requestID = await vmm.ShutDownVM(serviceName, roleName);
                linkCheckStatus.NavigateUrl = "/GetOperationStatus.aspx?requestid=" + requestID;
                linkCheckStatus.Text = "Check Request Status";
            }
        }

        protected void cmdCreateVM_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CreateVM.aspx", false);
        }
    }
}
Back to Top