示例#1
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));
        }
示例#2
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));
        }