wasvcmgmntapi /MyVMPortal/CreateVM.aspx.cs

Language C# Lines 204
MD5 Hash 99fe2a1254d939f535e94a06f8e48c20
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using SMLibrary;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;

namespace MyVMPortal
{
    public partial class CreateVM : System.Web.UI.Page
    {
        private String _StorageAccount = String.Empty;

        private String GetStorageAccount()
        {
            string[] accounts = ConfigurationManager.AppSettings["StorageAccount"].Split(new char[] { ',' });
            Random r = new Random(DateTime.Now.Millisecond);
            return accounts[r.Next(0, accounts.Length)];
        }

        async protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                VMManager vmm = GetVMM();
                List<String> images = await vmm.GetAzureVMImages();
                ddlImages.DataSource = images;
                ddlImages.DataBind();

                lblStorageAccount.Text = GetStorageAccount();
                _StorageAccount = lblStorageAccount.Text;
                ViewState["StorageAccount"] = _StorageAccount; 
            }
            else
            {
                txtPassword.Attributes["value"] = txtPassword.Text;
                _StorageAccount = ViewState["StorageAccount"].ToString();
            }

            GetQuota();
        }

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

        private String GetOSDiskMediaLocation()
        {
            String osdiskmedialocation = String.Format("https://{0}.blob.core.windows.net/vhds/{1}-OS-{2}.vhd", _StorageAccount, txtVMName.Text, Guid.NewGuid().ToString());
            return osdiskmedialocation;
        }
        private String GetDataDiskMediaLocation()
        {
            String osdiskmedialocation = String.Format("https://{0}.blob.core.windows.net/vhds/{1}-Data-{2}.vhd", _StorageAccount, txtVMName.Text, Guid.NewGuid().ToString());
            return osdiskmedialocation;
        }

        protected void cmdCreateVMConfig_Click(object sender, EventArgs e)
        {
            GenerateVMConfig(false);
        }
        
        async public void GenerateVMConfig(bool Create)
        {
            if (String.IsNullOrEmpty(txtServiceName.Text))
            {
                lblStatus.Text = "Missing Service Name";
                return;
            }
            if (String.IsNullOrEmpty(txtVMName.Text))
            {
                lblStatus.Text = "Missing VM Name";
                return;
            }
            if (String.IsNullOrEmpty(txtPassword.Text))
            {
                lblStatus.Text = "Missing Pasword";
                return;
            }
            if (ValidationHelpers.IsWindowsComputerNameValid(txtVMName.Text) == false)
            {
                lblStatus.Text = "VM Name is either too long or has invalid characters";
                return;
            }
            if (ValidationHelpers.IsWindowsPasswordValid(txtPassword.Text) == false)
            {
                lblStatus.Text = "Windows Password does not meet complexity requirements.";
                return;
            }

            lblStatus.Text = "";

            VMManager vmm = GetVMM();

            if(await vmm.IsServiceNameAvailable(txtServiceName.Text) == false)
            {
                lblStatus.Text = "Service Name is not available. Must be unique";
                return;
            }

            XDocument vm = vmm.NewAzureVMConfig(txtVMName.Text, ddlVMSize.Text, ddlImages.Text, GetOSDiskMediaLocation(), true);

            vm = vmm.NewWindowsProvisioningConfig(vm, txtVMName.Text, txtPassword.Text);
            vm = vmm.NewNetworkConfigurationSet(vm);

            if (chkAddDataDisk.Checked == true)
            {
                vm = vmm.AddNewDataDisk(vm, 10, 0, "MyDataDisk", GetDataDiskMediaLocation());
            }
            if (chkAddHTTPEndpoint.Checked == true)
            {
                vm = vmm.AddNewInputEndpoint(vm, "web", "TCP", 80, 80);
            }
            if (chkAddRDPEndpoint.Checked == true)
            {
                vm = vmm.AddNewInputEndpoint(vm, "rdp", "TCP", 3389, 3389);
            }

            var builder = new StringBuilder();
            var settings = new XmlWriterSettings()
            {
                Indent = true
            };
            using (var writer = XmlWriter.Create(builder, settings))
            {
                vm.WriteTo(writer);
            }


            divXML.Visible = true;
            lblVMXML.Text = Server.HtmlEncode(builder.ToString());

            tblConfig.Visible = true;
            cmdCreateVM.Visible = true;
            cmdCreateVMConfig.Visible = false;

            if (Create == true)
            {
                String requestID_cloudService = await vmm.NewAzureCloudService(txtServiceName.Text, "West US", String.Empty);

                OperationResult result = await vmm.PollGetOperationStatus(requestID_cloudService, 5, 120);

                if (result.Status == OperationStatus.Succeeded)
                {
                    // VM creation takes too long so we'll check it later
                    String requestID_createDeployment = await vmm.NewAzureVMDeployment(txtServiceName.Text, txtVMName.Text, String.Empty, vm, null);

                    Response.Redirect("/GetOperationStatus.aspx?requestid=" + requestID_createDeployment);
                }
                else
                {
                    lblStatus.Text = String.Format("Creating Cloud Service Failed. Message: {0}", result.Message);
                }
            }
        }

        protected void chkAddDataDisk_Click(object sender, EventArgs e)
        {
            GenerateVMConfig(false);
        }

        protected void chkAddRDPEndpoint_Click(object sender, EventArgs e)
        {
            GenerateVMConfig(false);
        }

        protected void chkAddHTTPEndpoint_Click(object sender, EventArgs e)
        {
            GenerateVMConfig(false);
        }

        protected void cmdCreateVM_Click(object sender, EventArgs e)
        {
            GenerateVMConfig(true);
        }

        async protected void GetQuota()
        {
            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;

        }
    }
}
Back to Top