public void OnGet()
 {
     Model.User user = new Model.User();
     using (var context = new CADContext())
     {
         if (context.Users.Where(u => u.IsSuperUser == true).Count() == 0)
         {
             user.UserName    = this.User.Identity.Name;
             user.IsSuperUser = true;
             context.Users.Add(user);
             context.SaveChanges();
         }
         if (context.Users.FirstOrDefault(u => u.UserName == this.User.Identity.Name) == null)
         {
             user.UserName    = this.User.Identity.Name;
             user.IsSuperUser = false;
             context.Users.Add(user);
             context.SaveChanges();
         }
         user = context.Users.Include(u => u.Dashboard).FirstOrDefault(u => u.UserName == this.User.Identity.Name);
         if (user.Dashboard != null)
         {
             Title       = user.Dashboard.DashboardName;
             Logo        = user.Dashboard.DashboardLogoUrl;
             DashboardId = user.Dashboard.DashboardId;
         }
     }
 }
        private void UpdateByVM(VM _vm, VM _azureVm)
        {
            VM vm = new VM();

            using (var context = new CADContext())
            {
                try
                {
                    _vm.PowerState        = _azureVm.PowerState;
                    _vm.ProvisioningState = _azureVm.ProvisioningState;
                    _vm.ResourceGroupName = _azureVm.ResourceGroupName;
                    _vm.SubscriptionId    = _azureVm.SubscriptionId;
                    _vm.Tags   = _azureVm.Tags;
                    _vm.VMId   = _azureVm.VMId;
                    _vm.VMName = _azureVm.VMName;
                    _vm.VMSize = _azureVm.VMSize;

                    context.VMs.Update(_vm);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
        }
        public static void Initialize(CADContext context)
        {
            Application app = new Application();

            app.ClientId       = "912fabb6-a061-4286-a476-aba6e7edb0e3";
            app.ClientSecret   = "6LGURivA4/gRNUzjRGuvWPZxKnwmESxOgdx1G+5EZgY=";
            app.SubscriptionId = "1d52c7db-0f59-4b3e-artd-de3df0624ab1";
            app.TenantId       = "test.com";
            app.Description    = "test subscription";
            context.Applications.Add(app);
            context.SaveChanges();

            Dashboard dashBoard = new Dashboard();

            dashBoard.DashboardName           = "DemoTestDashboard";
            dashBoard.DashboardAnonAccessCode = "testtesttest";
            dashBoard.DashboardLogoUrl        = "https://ciellospublic.z21.web.core.windows.net/Ciellos Logo_Standard.png";

            context.Dashboards.Add(dashBoard);
            context.SaveChanges();

            User user = new User();

            user.UserName    = "******";
            user.IsSuperUser = false;
            context.Users.Add(user);
            context.SaveChanges();

            Setting setting = new Setting();

            setting.MaxNumEventsLogStorePerVM = 1;
            context.Settings.Add(setting);
            context.SaveChanges();
        }
 public void UpdateVMList(IEnumerable <IVirtualMachine> _list, int _appId)
 {
     using (var context = new CADContext())
     {
         try
         {
             foreach (var _vm in _list)
             {
                 VM vm = new VM();
                 vm = azureHelper.createLocalVMFromAzureVM(_vm, _appId);
                 FindOrCreateVM(vm);
                 vm = null;
             }
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Log log = new Log();
             log.name      = ex.ToString();
             log.timestamp = DateTime.UtcNow;
             context.Logs.Add(log);
             context.SaveChanges();
         }
     }
 }
        /// <summary>
        /// GetJOVirtualMachine
        /// </summary>
        /// <param name="jobject"></param>
        /// <returns></returns>
        public JsonResult GetJOVirtualMachine(JObject jobject)
        {
            VM vm = new VM();

            using (CADContext context = new CADContext())
            {
                try
                {
                    IVirtualMachine virtualMachine = GetVMFromAzure(jobject["vmid"].ToString());
                    if (virtualMachine != null)
                    {
                        vm = VirtualMashinesList.FindOrCreateVM(createLocalVMFromAzureVM(virtualMachine));
                    }
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(new JsonResult(new { data = vm }));
        }
        /// <summary>
        /// GetVirtualMachinesByUser
        /// </summary>
        /// <param name="_currentUser"></param>
        /// <returns></returns>
        public JsonResult GetVirtualMachinesByUser(ClaimsPrincipal _currentUser)
        {
            List <VM> vMashines = new List <VM>();

            using (var context = new CADContext())
            {
                try
                {
                    User currentUser = context.Users
                                       .Include(u => u.Dashboard)
                                       .ThenInclude(d => d.DashboardApplications)
                                       .FirstOrDefault(u => u.UserName == _currentUser.Identity.Name);
                    if (currentUser?.Dashboard == null)
                    {
                        return(new JsonResult(new { data = "" }));
                    }

                    var dashboard = context.Dashboards.Include(d => d.DashboardApplications).FirstOrDefault(d => d.DashboardId == currentUser.Dashboard.DashboardId);
                    if (currentUser.Dashboard.DashboardId != 0)
                    {
                        vMashines = VirtualMashinesList.GetVMs(currentUser.Dashboard.DashboardId);
                    }
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(new JsonResult(new { data = vMashines }));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            using (var ctx = new CADContext())
            {
                Schedules schedules = ctx.Schedules.Find(id);
                schedules.ScheduleVMsList.Clear();
                schedules.Name      = Schedule.Name;
                schedules.StartTime = Schedule.StartTime;
                schedules.StopTime  = Schedule.StopTime;

                var sceduleVMs = ctx.ScheduleVM.Where(s => s.ScheduleId == Schedule.Id).ToList();
                foreach (var res in sceduleVMs)
                {
                    ctx.ScheduleVM.Remove(res);
                }

                foreach (var res in SelectedVmsId)
                {
                    var        vm         = ctx.VMs.Find(res);
                    ScheduleVM scheduleVM = new ScheduleVM();
                    scheduleVM.ScheduleId = 1;
                    scheduleVM.Schedules  = Schedule;
                    scheduleVM.VMId       = vm.Id;
                    scheduleVM.VM         = vm;
                    ctx.ScheduleVM.Add(scheduleVM);
                    Schedule.ScheduleVMsList.Add(scheduleVM);
                }

                ctx.SaveChanges();
            }

            return(RedirectToPage("./Index"));
        }
        public IVirtualMachine GetVMFromAzure(string _vmId)
        {
            IVirtualMachine machine = null;

            using (CADContext context = new CADContext())
            {
                try
                {
                    IAzure      azure;
                    VM          vm   = context.VMs.FirstOrDefault(v => v.VMId == _vmId);
                    Application apps = context.Applications.FirstOrDefault(app => app.AppId == vm.ApplicationId);
                    azure   = GetAzureConnection(apps);
                    machine = azure.VirtualMachines.List().FirstOrDefault(v => v.VMId == _vmId);
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(machine);
        }
        public IActionResult OnPost()
        {
            User.Dashboard = _context.Dashboards.FirstOrDefault(d => d.DashboardId == SelectedDashboard);
            _context.Users.Add(User);
            _context.SaveChanges();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostSetAsDefaultAsync([FromBody] JObject jobject)
        {
            var dash = DBContext.Dashboards.Include(d => d.DashboardApplications).FirstOrDefault(d => d.DashboardId == (int)(jobject["id"]));
            var user = DBContext.Users.FirstOrDefault(u => u.UserName == this.User.Identity.Name);

            user.Dashboard = dash;

            DBContext.SaveChanges();
            return(await Task.Run(() => new JsonResult(new { data = true })));
        }
Esempio n. 11
0
 public void RemoveActiveVM(int _dashboardId, string _vmId)
 {
     using (var context = new CADContext())
     {
         ActiveVM vm = context.ActiveVMs.FirstOrDefault(iv => iv.DashboardId == _dashboardId && iv.VMId == _vmId);
         if (vm != null)
         {
             context.ActiveVMs.Remove(vm);
             context.SaveChanges();
         }
     }
 }
Esempio n. 12
0
 public IndexModel(IAzureHelper _azureHelper, CADContext _context, IHostingEnvironment hostingEnvironment)
 {
     _hostingEnvironment = hostingEnvironment;
     azureHelper         = _azureHelper as AzureHelper;
     context             = _context;
     if (context.Settings.FirstOrDefault() == null)
     {
         Setting set = new Setting();
         context.Settings.Add(set);
         context.SaveChanges();
     }
     Message = "Dashboard settings";
 }
Esempio n. 13
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            User.Dashboard = _context.Dashboards.Include(d => d.DashboardApplications).FirstOrDefault(d => d.DashboardId == SelectedDashboard.DashboardId);
            _context.Users.Update(User);

            _context.SaveChanges(true);

            return(RedirectToPage("./Index"));
        }
Esempio n. 14
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            switch (SelectedDashboardType)
            {
            case DisplayType.ShowAll:
            {
                azureHelper.GetVirtualMashinesList().UpdateActiveVMs((int)id, new int[0]);
                break;
            }

            case DisplayType.ExcludeType:
            {
                azureHelper.GetVirtualMashinesList().UpdateActiveVMs((int)id, ExcludedVMs);
                break;
            }

            case DisplayType.SelectType:
            {
                azureHelper.GetVirtualMashinesList().UpdateActiveVMs((int)id, SelectedVMs);
                break;
            }
            }

            using (var ctx = new CADContext())
            {
                if (Dashboard.DashboardAnonAccessCode == null)
                {
                    Dashboard.DashboardAnonAccessCode = DashboardHelper.CalculateHash(Dashboard.DashboardName);
                }

                var dash = await ctx.Dashboards.Include(d => d.DashboardApplications).Include(l => l.Links).FirstOrDefaultAsync(d => d.DashboardId == Dashboard.DashboardId);

                dash.DashboardApplications.Clear();
                foreach (var sa in SelectedApplications)
                {
                    DashboardApplication app = new DashboardApplication();
                    app.Dashboard   = Dashboard;
                    app.Application = ctx.Applications.First(a => a.AppId == sa);
                    dash.DashboardApplications.Add(app);
                }
                dash.Links.Clear();
                dash.Links = Dashboard.Links;
                dash.DashboardAnonAccessCode = Dashboard.DashboardAnonAccessCode;
                dash.DashboardName           = Dashboard.DashboardName;
                dash.DashboardLogoUrl        = Dashboard.DashboardLogoUrl;
                dash.DisplayType             = SelectedDashboardType;
                ctx.SaveChanges();
            }
            return(RedirectToPage("./Index"));
        }
Esempio n. 15
0
        private VM CreateVMByVM(VM _vm)
        {
            VM vm = new VM();

            using (var context = new CADContext())
            {
                try
                {
                    context.VMs.Add(_vm);
                    vm = context.VMs.FirstOrDefault(v => v.VMId == _vm.VMId);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(vm);
        }
        public static void CleanLogs(CADContext _context)
        {
            var settings = _context.Settings.FirstOrDefault();

            var vmInLogTable = _context.Logs.GroupBy(l => new { l.vmname, l.resourcegroup }).ToList();

            foreach (var vm in vmInLogTable)
            {
                var logs_count = _context.Logs.Where(l => l.vmname == vm.Key.vmname && l.resourcegroup == vm.Key.resourcegroup).Count();
                if (logs_count > settings.MaxNumEventsLogStorePerVM)
                {
                    var vmLastLogRecord = _context.Logs.Where(l => l.vmname == vm.Key.vmname && l.resourcegroup == vm.Key.resourcegroup).OrderByDescending(u => u.timestamp).Take(settings.MaxNumEventsLogStorePerVM).Last();
                    var logsForRemove   = _context.Logs.Where(l => l.vmname == vm.Key.vmname && l.resourcegroup == vm.Key.resourcegroup && l.timestamp < vmLastLogRecord.timestamp).ToList();
                    _context.Logs.RemoveRange(logsForRemove);
                }
            }
            _context.SaveChanges();

            var errorLogsToRemove = _context.Logs.Where(l => string.IsNullOrEmpty(l.vmname)).ToList();

            _context.Logs.RemoveRange(errorLogsToRemove);
            _context.SaveChanges();
        }
Esempio n. 17
0
 public void AddActiveVM(int _dashboardId, string _vmId)
 {
     using (var context = new CADContext())
     {
         ActiveVM vm = context.ActiveVMs.FirstOrDefault(iv => iv.DashboardId == _dashboardId && iv.VMId == _vmId);
         if (vm == null)
         {
             vm             = new ActiveVM();
             vm.DashboardId = _dashboardId;
             vm.VMId        = _vmId;
             context.ActiveVMs.Add(vm);
             context.SaveChanges();
             vm = null;
         }
     }
 }
Esempio n. 18
0
        public IActionResult OnPost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Dashboard = _context.Dashboards.First(d => d.DashboardId == id);

            if (Dashboard != null)
            {
                _context.Remove(Dashboard);
                _context.SaveChanges();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 19
0
        public IActionResult OnPost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Application = _context.Applications.First(a => a.AppId == id);

            if (Application != null)
            {
                _context.Remove(Application);
                _context.SaveChanges();
            }

            return(RedirectToPage("./Index"));
        }
        public IActionResult OnPost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            User = _context.Users.FirstOrDefault(u => u.UserId == id);

            if (User != null)
            {
                _context.Users.Remove(User);
                _context.SaveChanges();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 21
0
        public void UpdateActiveVMs(int _dashboardId, int[] _VMList)
        {
            using (var context = new CADContext())
            {
                var activeList = context.ActiveVMs.Where(v => v.DashboardId == _dashboardId);

                foreach (var vm in activeList)
                {
                    context.ActiveVMs.Remove(vm);
                    context.SaveChanges();
                }

                foreach (int vmId in _VMList)
                {
                    AddActiveVM(_dashboardId, vmId);
                }
                activeList = null;
            }
        }
Esempio n. 22
0
        /// <summary>
        /// GetDashboardByAccessCode
        /// </summary>
        /// <param name="_accessCode"> _accessCode</param>
        /// <returns></returns>
        public Dashboard GetDashboardByAccessCode(string _accessCode)
        {
            Dashboard dashboard = new Dashboard();

            using (var context = new CADContext())
            {
                try
                {
                    dashboard = context.Dashboards.Include(d => d.DashboardApplications).FirstOrDefault(d => d.DashboardAnonAccessCode == _accessCode);
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(dashboard);
        }
Esempio n. 23
0
        public List <VM> GetVMsByAppID(int _appId)
        {
            List <VM> list = new List <VM>();

            using (var context = new CADContext())
            {
                try
                {
                    list = context.VMs.Where(vms => vms.ApplicationId == _appId).ToList();
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }

            return(list);
        }
Esempio n. 24
0
        public List <VM> GetByDashboardId(int _dashboardId, bool _exceptInactive = false)
        {
            List <VM> list      = new List <VM>();
            Dashboard dashboard = new Dashboard();

            using (var context = new CADContext())
            {
                try
                {
                    dashboard = context.Dashboards.Include(vm => vm.DashboardApplications).FirstOrDefault(d => d.DashboardId == _dashboardId);

                    foreach (var app in dashboard.DashboardApplications)
                    {
                        list.AddRange(GetVMsByAppID(app.ApplicationId));
                    }
                    if (_exceptInactive)
                    {
                        List <VM> excludeInactiveList = new List <VM>();
                        foreach (VM vm in list)
                        {
                            if (GetVMs(_dashboardId).Find(v => v.VMId == vm.VMId) == null)
                            {
                                excludeInactiveList.Add(vm);
                            }
                        }
                        list = excludeInactiveList;
                    }
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(list);
        }
Esempio n. 25
0
 public void UpdateAllVirtualMashines()
 {
     using (CADContext context = new CADContext())
     {
         var vmList = context.VMs.AsNoTracking().ToList();
         context.VMs.RemoveRange(vmList);
         context.SaveChanges();
     }
     using (CADContext context = new CADContext())
     {
         try
         {
             IAzure azure;
             foreach (var apps in context.Applications)
             {
                 try
                 {
                     azure = GetAzureConnection(apps);
                     var curVMList = azure.VirtualMachines.List();
                     VirtualMashinesList.UpdateVMList(curVMList, apps.AppId);
                     azure = null;
                 }
                 catch
                 {
                     continue;
                 }
             }
         }
         catch (Exception ex)
         {
             Log log = new Log();
             log.name      = ex.ToString();
             log.timestamp = DateTime.UtcNow;
             context.Logs.Add(log);
             context.SaveChanges();
         }
     }
 }
Esempio n. 26
0
        /// <summary>
        /// GetVirtualMachinesByAccessCode
        /// </summary>
        /// <param name="_accessCode"></param>
        /// <returns></returns>
        public List <VM> GetVirtualMachinesByAccessCode(string _accessCode)
        {
            Dashboard dashboard;
            List <VM> vMashines = new List <VM>();

            using (var context = new CADContext())
            {
                try
                {
                    dashboard = context.Dashboards.Include(d => d.DashboardApplications).FirstOrDefault(d => d.DashboardAnonAccessCode == _accessCode);
                    vMashines = VirtualMashinesList.GetVMs(dashboard.DashboardId);
                }
                catch (Exception ex)
                {
                    Log log = new Log();
                    log.name      = ex.ToString();
                    log.timestamp = DateTime.UtcNow;
                    context.Logs.Add(log);
                    context.SaveChanges();
                }
            }
            return(vMashines);
        }
Esempio n. 27
0
        public IActionResult OnPost()
        {
            if (Dashboard.DashboardAnonAccessCode == null)
            {
                Dashboard.DashboardAnonAccessCode = DashboardHelper.CalculateHash(Dashboard.DashboardName);
            }
            _context.Dashboards.Add(Dashboard);
            _context.SaveChanges();

            using (var ctx = new CADContext())
            {
                var dash = ctx.Dashboards.Include(d => d.DashboardApplications).First(d => d.DashboardId == Dashboard.DashboardId);
                foreach (var sa in SelectedApplications)
                {
                    DashboardApplication app = new DashboardApplication();
                    app.Dashboard   = Dashboard;
                    app.Application = ctx.Applications.First(a => a.AppId == sa);
                    dash.DashboardApplications.Add(app);
                }
                ctx.SaveChanges();
            }
            return(RedirectToPage("./Index"));
        }