public JsonResult AssignVM(ExecuteVM executeVm, int id)
 {
     var check = _vmUserService.CheckUser(id, executeVm.RoleName);
     if (!check)
     {
         var assignVm = new VMUser();
         assignVm.UserId = id;
         assignVm.ServiceName = executeVm.ServiceName;
         assignVm.DeploymentName = executeVm.DeploymentName;
         assignVm.VMName = executeVm.RoleName;
         _vmUserService.AssignUserToVM(assignVm);
         return Json("Success");
     }
     else
     {
         return Json("This user already has " + executeVm.RoleName);
     }
 }
 public async Task<ActionResult> Detail(ExecuteVM executeVm)
 {
     var credential = CertificateAuthenticationHelper.GetCredential(AMUWHelper.GetAppSetting("Azure-SubscriptionId"), AMUWHelper.GetAppSetting("Azure-Credential"));
     var cloudService = await CloudContext.Clients.CreateComputeManagementClient(credential).HostedServices.GetDetailedAsync(executeVm.ServiceName);
     var deployment = cloudService.Deployments.FirstOrDefault();
     var vmDetail = deployment.RoleInstances.FirstOrDefault(x => x.RoleName == executeVm.RoleName);
     var viewModel = new VirtualMachineDetailViewModel();
     viewModel.DeploymentId = cloudService.Deployments.FirstOrDefault().PrivateId;
     viewModel.VirtualIP = deployment.VirtualIPAddresses.Count > 0 ? deployment.VirtualIPAddresses.FirstOrDefault().Address : "";
     viewModel.InternalIP = vmDetail.IPAddress;
     viewModel.RoleName = executeVm.RoleName;
     viewModel.Size = vmDetail.InstanceSize;
     viewModel.Status = vmDetail.PowerState.ToString();
     var userVmList = _vmUserService.GetByVMName(executeVm.RoleName);
     viewModel.UserList = new List<string>();
     foreach (var item in userVmList)
     {
         var emailAddress = UserManager.FindById(item.User.UserId).Email;
         viewModel.UserList.Add(item.User.Username + "(" + emailAddress +")");
     }
     viewModel.UserRole = AMUW.Helpers.AMUWHelper.GetRole();
     return View(viewModel);
 }
 public async Task<HttpResponseMessage> Execute(ExecuteVM executeVm)
 {
     var credential = CertificateAuthenticationHelper.GetCredential(AMUWHelper.GetAppSetting("Azure-SubscriptionId"), AMUWHelper.GetAppSetting("Azure-Credential"));
     var requestId = string.Empty;
     if (executeVm.Action == "start")
     {
         var execute = await CloudContext.Clients.CreateComputeManagementClient(credential).VirtualMachines.StartAsync(executeVm.ServiceName, executeVm.DeploymentName, executeVm.RoleName);
         requestId = execute.RequestId;
     }
     else
     {
         var execute = await CloudContext.Clients.CreateComputeManagementClient(credential).VirtualMachines.ShutdownAsync(
             executeVm.ServiceName,
             executeVm.DeploymentName,
             executeVm.RoleName, new VirtualMachineShutdownParameters()
             {
                 PostShutdownAction = PostShutdownAction.StoppedDeallocated
             });
         requestId = execute.RequestId;
     }
     return Request.CreateResponse<string>(HttpStatusCode.OK, requestId);
 }