コード例 #1
0
ファイル: HomeController.cs プロジェクト: eoghanb6/SkyDash
        //Create the view for the Backups screen
        public ActionResult Backups()
        {
            //Set user credentials as session variables
            string username = Session["SkyscapeUsername"] as string;
            string password = Session["SkyscapePassword"] as string;

            //Initializes a new api call to Skyscape
            var api = new APIMethods();

            //Authentication details passed through from config class
            var authenticate = api.authenticateSkyscape(username, password);

            var accounts = api.getAccounts();

            //Generates viewModels for view
            VmViewModel vmViewModel = new VmViewModel();

            vmViewModel.names      = new List <string>();
            vmViewModel.accountVms = new List <PanelVM>();
            vmViewModel.accounts   = new List <Account>();
            vmViewModel.backups    = new List <Backup>();

            // "expires_after : 900" means a successful authentication
            if (authenticate.Content == "{\"expires_after\":900}")
            {
                //Generates counter to uniquely identify JQuery dialogs in Backup view
                int k = 0;
                ViewBag.response = "Authentication successful";

                //Deserializes JSON string into account objects
                vmViewModel.accounts = JsonConvert.DeserializeObject <List <Account> >(accounts.Content);
                //Loop through deserialized accounts
                for (int i = 0; i < vmViewModel.accounts.Count; i++)
                {
                    //getVms makes the call to Skyscape to retrieve vm/backup information
                    var vms = api.getSkyscapeVms(vmViewModel.accounts[i].id);

                    //result is the objects of the returned deserialized JSON String from the API with a set DateTimeFormat included
                    var result = JsonConvert.DeserializeObject <SkyscapeResponse>(vms.Content, new IsoDateTimeConverter {
                        DateTimeFormat = "dd/MM/yyyy HH:mm"
                    });

                    //To create VM objects, must loop through the different levels of JSON String
                    foreach (var vOrg in result.vOrgs)
                    {
                        foreach (var vDC in vOrg.vDCs)
                        {
                            foreach (var vApp in vDC.vApps)
                            {
                                foreach (var virtualMachine in vApp.VMs)
                                {
                                    //If any VM within an account has at a last backup which failed, count this fail to display in accordion view
                                    if (virtualMachine.LastBackupStatus.Contains("Failed"))
                                    {
                                        vmViewModel.accounts[i].allBackupsStatus = false;
                                        vmViewModel.accounts[i].numberFailedBackups++;
                                    }
                                    //create new instance of PanelVM
                                    PanelVM panelVm = new PanelVM();
                                    {
                                        panelVm.AccountId        = result.Account.id;
                                        panelVm.Name             = virtualMachine.Name;
                                        panelVm.LastBackupStatus = virtualMachine.LastBackupStatus;
                                        panelVm.LastBackup       = virtualMachine.LastBackup;
                                        panelVm.backups          = virtualMachine.Backups;
                                        panelVm.Id                    = virtualMachine.Id; //Not suitable as unique identifier for VM as may be duplicate in other accounts
                                        panelVm.Size                  = virtualMachine.Size;
                                        panelVm.MonthToDate           = virtualMachine.MonthToDate;
                                        panelVm.EstimatedMonthlyTotal = virtualMachine.EstimatedMonthlyTotal;
                                        panelVm.BilledHoursPoweredOn  = virtualMachine.BilledHoursPoweredOn;
                                        panelVm.BilledHoursPoweredOff = virtualMachine.BilledHoursPoweredOff;
                                        panelVm.PowerStatus           = virtualMachine.PowerStatus;
                                        panelVm.OperatingSystem       = virtualMachine.OperatingSystem;
                                        panelVm.NumberOfCPUs          = virtualMachine.NumberOfCPUs;
                                        panelVm.Memory                = virtualMachine.Memory;
                                        panelVm.Storage               = virtualMachine.Storage;
                                        panelVm.counter               = k++; //counter allows unique ID of VM without using any Skyscape data as key for VM
                                    }
                                    //Must add panelVm to view model in order to use in Backups view
                                    vmViewModel.accountVms.Add(panelVm);
                                }
                            }
                        }
                    }
                }
            }
            else //API Authentication has failed
            {
                ViewBag.response = "Authentication Failed";
                return(RedirectToAction("Login", "Home"));
            }
            return(View(vmViewModel)); // Must return viewModel to create view
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: jordanhoey/hello
        //Create the view for the Backups screen
        public ActionResult Backups()
        {
            //Set user credentials as session variables
            string username = Session["SkyscapeUsername"] as string;
            string password = Session["SkyscapePassword"] as string;

            //Initializes a new api call to Skyscape
            var api = new APIMethods();

            //Authentication details passed through from config class
            var authenticate = api.authenticateSkyscape(username, password);

            var accounts = api.getAccounts();

            //Generates viewModels for view
            VmViewModel vmViewModel = new VmViewModel();
            vmViewModel.names = new List<string>();
            vmViewModel.accountVms = new List<PanelVM>();
            vmViewModel.accounts = new List<Account>();
            vmViewModel.backups = new List<Backup>();

            // "expires_after : 900" means a successful authentication
            if (authenticate.Content == "{\"expires_after\":900}")
            {
                //Generates counter to uniquely identify JQuery dialogs in Backup view
                int k = 0;
                ViewBag.response = "Authentication successful";

                //Deserializes JSON string into account objects
                vmViewModel.accounts = JsonConvert.DeserializeObject<List<Account>>(accounts.Content);
                //Loop through deserialized accounts
                for (int i = 0; i < vmViewModel.accounts.Count; i++)
                {
                    //getVms makes the call to Skyscape to retrieve vm/backup information
                    var vms = api.getSkyscapeVms(vmViewModel.accounts[i].id);

                    //result is the objects of the returned deserialized JSON String from the API with a set DateTimeFormat included
                    var result = JsonConvert.DeserializeObject<SkyscapeResponse>(vms.Content, new IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy HH:mm" });

                    //To create VM objects, must loop through the different levels of JSON String
                    foreach (var vOrg in result.vOrgs)
                    {
                        foreach (var vDC in vOrg.vDCs)
                        {
                            foreach (var vApp in vDC.vApps)
                            {
                                foreach (var virtualMachine in vApp.VMs)
                                {
                                    //If any VM within an account has at a last backup which failed, count this fail to display in accordion view
                                    if (virtualMachine.LastBackupStatus.Contains("Failed"))
                                    {
                                        vmViewModel.accounts[i].allBackupsStatus = false;
                                        vmViewModel.accounts[i].numberFailedBackups++;
                                    }
                                    //create new instance of PanelVM
                                    PanelVM panelVm = new PanelVM();
                                    {
                                        panelVm.AccountId = result.Account.id;
                                        panelVm.Name = virtualMachine.Name;
                                        panelVm.LastBackupStatus = virtualMachine.LastBackupStatus;
                                        panelVm.LastBackup = virtualMachine.LastBackup;
                                        panelVm.backups = virtualMachine.Backups;
                                        panelVm.Id = virtualMachine.Id; //Not suitable as unique identifier for VM as may be duplicate in other accounts
                                        panelVm.Size = virtualMachine.Size;
                                        panelVm.MonthToDate = virtualMachine.MonthToDate;
                                        panelVm.EstimatedMonthlyTotal = virtualMachine.EstimatedMonthlyTotal;
                                        panelVm.BilledHoursPoweredOn = virtualMachine.BilledHoursPoweredOn;
                                        panelVm.BilledHoursPoweredOff = virtualMachine.BilledHoursPoweredOff;
                                        panelVm.PowerStatus = virtualMachine.PowerStatus;
                                        panelVm.OperatingSystem = virtualMachine.OperatingSystem;
                                        panelVm.NumberOfCPUs = virtualMachine.NumberOfCPUs;
                                        panelVm.Memory = virtualMachine.Memory;
                                        panelVm.Storage = virtualMachine.Storage;
                                        panelVm.counter = k++; //counter allows unique ID of VM without using any Skyscape data as key for VM
                                    }
                                    //Must add panelVm to view model in order to use in Backups view
                                    vmViewModel.accountVms.Add(panelVm);
                                }
                            }
                        }
                    }
                }
            }
            else //API Authentication has failed
            {
                ViewBag.response = "Authentication Failed";
                return RedirectToAction("Login", "Home");
            }
            return View(vmViewModel); // Must return viewModel to create view
        }