Example #1
0
        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));
        }
Example #2
0
        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));
        }
Example #3
0
        public ActionResult Settings()
        {
            var viewModel = new SettingViewModel();

            viewModel.SubscriptionId = AMUWHelper.GetAppSetting("Azure-SubscriptionId");
            viewModel.Credential     = AMUWHelper.GetAppSetting("Azure-Credential");
            return(View(viewModel));
        }
Example #4
0
        public async Task <HttpResponseMessage> Get(IEnumerable <ExecuteVM> vms)
        {
            var credential = CertificateAuthenticationHelper.GetCredential(AMUWHelper.GetAppSetting("Azure-SubscriptionId"), AMUWHelper.GetAppSetting("Azure-Credential"));

            if (vms != null)
            {
                foreach (var vm in vms)
                {
                    var vmDetail = await CloudContext.Clients.CreateComputeManagementClient(credential).VirtualMachines.GetAsync(vm.ServiceName, vm.DeploymentName, vm.RoleName);
                }
            }
            return(Request.CreateResponse <string>(HttpStatusCode.OK, ""));
        }
Example #5
0
 // GET: VirtualMachine
 public ActionResult Index()
 {
     if (AMUW.Helpers.AMUWHelper.GetRole() == "User")
     {
         string currentUserId = User.Identity.GetUserId();
         ViewBag.UserId = _userService.GetId(currentUserId);
     }
     else
     {
         if (string.IsNullOrEmpty(AMUWHelper.GetAppSetting("Azure-SubscriptionId")) || string.IsNullOrEmpty(AMUWHelper.GetAppSetting("Azure-Credential")))
         {
             return(RedirectToAction("Settings"));
         }
     }
     return(View());
 }
 public VirtualMachineApiController(IVMUserService vmUserService, AMUWHelper helper)
 {
     _vmUserService = vmUserService;
     _helper = helper;
 }
Example #7
0
        public async Task <HttpResponseMessage> Get(int?id)
        {
            List <VirtualMachineViewModel> vms = new List <VirtualMachineViewModel>();
            var credential = CertificateAuthenticationHelper.GetCredential(AMUWHelper.GetAppSetting("Azure-SubscriptionId"), AMUWHelper.GetAppSetting("Azure-Credential"));

            var cloudServiceList = new List <string>();

            if (id != null)
            {
                var vmUser = _vmUserService.GetAll(id.Value);
                foreach (var item in vmUser)
                {
                    var cloudService = await CloudContext.Clients.CreateComputeManagementClient(credential).HostedServices.GetAsync(item.ServiceName);

                    var detailService = await CloudContext.Clients.CreateComputeManagementClient(credential).HostedServices.GetDetailedAsync(cloudService.ServiceName);

                    var deplyoments      = detailService.Deployments;
                    var resourceLocation = cloudService.Properties.ExtendedProperties.FirstOrDefault(x => x.Key == "ResourceLocation").Value;
                    foreach (var deployment in deplyoments)
                    {
                        var roleInstances = deployment.RoleInstances;
                        foreach (var roleInstance in roleInstances)
                        {
                            vms.Add(new VirtualMachineViewModel
                            {
                                ServiceName    = cloudService.ServiceName,
                                DeploymentName = deployment.Name,
                                Name           = roleInstance.RoleName,
                                Status         = roleInstance.PowerState.ToString(),
                                DnsName        = deployment.Uri.ToString(),
                                Location       = cloudService.Properties.Location != null ? resourceLocation : cloudService.Properties.AffinityGroup + " (" + resourceLocation + ")"
                            });
                        }
                    }
                }
            }
            else
            {
                var cloudServices = await CloudContext.Clients.CreateComputeManagementClient(credential).HostedServices.ListAsync();

                foreach (var cloudService in cloudServices)
                {
                    var detailService = await CloudContext.Clients.CreateComputeManagementClient(credential).HostedServices.GetDetailedAsync(cloudService.ServiceName);

                    var deplyoments      = detailService.Deployments;
                    var resourceLocation = cloudService.Properties.ExtendedProperties.FirstOrDefault(x => x.Key == "ResourceLocation").Value;
                    foreach (var deployment in deplyoments)
                    {
                        var roleInstances = deployment.RoleInstances;
                        foreach (var roleInstance in roleInstances)
                        {
                            var userVmList = _vmUserService.GetByVMName(roleInstance.RoleName);
                            var userList   = new List <string>();
                            foreach (var item in userVmList)
                            {
                                var emailAddress = _helper.GetEmail(item.User.UserId);
                                userList.Add(item.User.Username + "(" + emailAddress + ")");
                            }
                            vms.Add(new VirtualMachineViewModel
                            {
                                ServiceName    = cloudService.ServiceName,
                                DeploymentName = deployment.Name,
                                Name           = roleInstance.RoleName,
                                Status         = roleInstance.PowerState.ToString(),
                                DnsName        = deployment.Uri.ToString(),
                                Location       = cloudService.Properties.Location != null ? resourceLocation : cloudService.Properties.AffinityGroup + " (" + resourceLocation + ")",
                                UserList       = userList.Count > 0 ? string.Join("<br/>", userList) : string.Empty
                            });
                        }
                    }
                }
            }
            return(Request.CreateResponse <List <VirtualMachineViewModel> >(HttpStatusCode.OK, vms));
        }
Example #8
0
 public VirtualMachineApiController(IVMUserService vmUserService, AMUWHelper helper)
 {
     _vmUserService = vmUserService;
     _helper        = helper;
 }