예제 #1
0
        //Runs when snapshots page is opened
        public ActionResult Snapshots()
        {
            string username = Session["SkyscapeUsername"] as string;
            string password = Session["SkyscapePassword"] as string;
            var api = new APIMethods();

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

            SnapshotViewModel snapshotViewModel = new SnapshotViewModel();
            snapshotViewModel.skyscapeAccounts = new List<Account>();
            snapshotViewModel.Vms = new List<QueryResultRecords>();
            snapshotViewModel.vCloudAccounts = new Dictionary<int, Account>();
            int vmId = 0;

            //Deserializes JSON string into account objects
            snapshotViewModel.skyscapeAccounts = JsonConvert.DeserializeObject<List<Account>>(accounts.Content);

            //Loop through deserialized accounts
            foreach (Account account in snapshotViewModel.skyscapeAccounts)
            {
                Account vCloudAccount = new Account();
                {
                    vCloudAccount.vCloudCredentials = new List<Dictionary<string, vCloudIdentifiers>>();
                    vCloudAccount.vCloudToken = new Dictionary<string, int>();
                    var vCloudCredentialObject = (JsonConvert.DeserializeObject<Dictionary<string, vCloudIdentifiers>>(api.getVCloudCreds(account.id).Content));
                    vCloudAccount.id = account.id;
                    vCloudAccount.vCloudCredentials.Add(vCloudCredentialObject);
                    foreach (var vCloudCredential in vCloudAccount.vCloudCredentials)
                    {
                        foreach (var key in vCloudCredential.Keys)
                        {
                            if (key.Contains("-" + account.id.ToString() + "-"))
                            {
                                byte[] credentialsAsBytes = System.Text.Encoding.ASCII.GetBytes(vCloudCredential[key].username.ToString() + ":" + password);
                                string encodedCredentials = Convert.ToBase64String(credentialsAsBytes);
                                string token = api.authenticateVCloud(encodedCredentials);
                                if (token != null)
                                {
                                    vCloudAccount.vCloudToken.Add(token, account.id);
                                }
                                else
                                {
                                    //Variable used to show non authenticated accounts in snapshot view
                                    account.hasAccess = false;
                                }
                            }
                        }
                    }
                }
                //Match the account id and the vcloud account in the dictionary
                snapshotViewModel.vCloudAccounts.Add(account.id, vCloudAccount);
            }
            foreach (var vCloudAccount in snapshotViewModel.vCloudAccounts.Values)
            {
                foreach (string vCloudToken in vCloudAccount.vCloudToken.Keys)
                {
                    //pass vCloudToken into API call to get VMs
                    var vmsXml = api.getVCloudVms(vCloudToken).GetResponseStream();

                    //To create snapshot objects, must loop through the different levels of XML using a streamreader
                    var vmSerializer = new XmlSerializer(typeof(QueryResultRecords));
                    using (var vmStreamReader = new StreamReader(vmsXml))
                    {
                        //Deserialize XML into VMs
                        QueryResultRecords Vms = (QueryResultRecords)vmSerializer.Deserialize(vmStreamReader);
                        Vms.vCloudId = vCloudAccount.id;
                        snapshotViewModel.Vms.Add(Vms);
                        foreach (var vm in Vms.VMRecord)
                        {
                            if (vm.CatalogName == null)
                            {
                                //if no catalog name for VM
                                var snapshotXml = api.getVCloudVmsSnapshots(vm.Href, vCloudToken).GetResponseStream();
                                var snapshotSerializer = new XmlSerializer(typeof(SnapshotSection));
                                using (var snapshotStreamReader = new StreamReader(snapshotXml))
                                {
                                    SnapshotSection snapshot = (SnapshotSection)snapshotSerializer.Deserialize(snapshotStreamReader);
                                    if (snapshot.Snapshot != null)
                                    {
                                        //set up snapshot object if snapshot exists
                                        vm.unofficialId = vmId;
                                        vmId++;
                                        vm.Snapshot = snapshot;
                                        vm.Snapshot.Snapshot.SizeInGB = long.Parse(vm.Snapshot.Snapshot.Size) / 1073741824;
                                        vm.Snapshot.Snapshot.accountId = vCloudAccount.vCloudToken[vCloudToken];
                                        if (vm.Snapshot.Snapshot.Created.AddDays(3) < DateTime.Now.Date) //check if snapshot is older than 3 days
                                        {
                                            foreach (var account in snapshotViewModel.skyscapeAccounts)
                                            {
                                                if (account.id == vCloudAccount.id)
                                                {
                                                    //add 1 to count of old snapshots for this account
                                                    account.numberOldSnapshots++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return View(snapshotViewModel);
        }