/// <summary> /// Adds a server to the database. If a cache of virtual machines is given, /// then we insert the virtual machines into the Master Tree. If not, then we /// obtain a list of virtual machines and then insert it into the Master Tree. /// This function is cache safe. /// </summary> /// <param name="dbHost">The computer object to insert into the database.</param> /// <param name="cache">An optional cache parameter.</param> public void AddServer(DbHostComputer dbHost, HostState state, List <VirtualMachine> cache = null) { Insert(dbHost, x => x.HostName); if (cache == null) { cache = HyperV.GetVm(dbHost.HostName); } var root = this.GetRootTreeNode(dbHost, state, cache); Directory.Add(dbHost.HostName, root); }
private MasterTreeNode GetRootTreeNode(DbHostComputer dbHost, HostState state, List <VirtualMachine> cache) { var root = (MasterTreeNode)dbHost; root.State = new NodeState(state); var vvhds = new List <string>(); foreach (var v in cache) { var vdb = GetVmDb(v.Uuid); v.Type = vdb == null ? VirtualMachineType.NONE : (VirtualMachineType)vdb.VmType; v.ParentHost = vdb == null ? v.Host : vdb.ParentHost; v.ParentUuid = vdb == null ? v.Uuid : vdb.ParentUuid; // First, add all virtual machines associated with this host var vnode = (MasterTreeNode)v; // Then, add all virtual hard disks associated with this host vvhds.AddRange(v.VhdPath); foreach (var vhd in v.VhdPath) { vnode.Children.Add(MasterTreeNode.GetTreeNode(vhd, v.Host, NodeType.VirtualHardDisks)); } root.Children.Add(vnode); } // Get orphaned VHDs try { var vhds = Interface.GetChildItems(dbHost.HostName, Settings.Default.vhdPath, "*.*vhd*"); if (vhds != null) { vhds = vhds.Except(vvhds).ToArray(); foreach (var v in vhds) { root.Children.Add(MasterTreeNode.GetTreeNode(v, dbHost.HostName, NodeType.OrphanedVirtualHardDisks)); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK); } return(root); }