コード例 #1
0
        public void GetVirtualDiskFilesForHost()
        {
            try
            {
                _service = ecb.getConnection().Service;
                _sic     = ecb.getConnection().ServiceContent;

                ArrayList supportedVersions = VersionUtil.getSupportedVersions(ecb.get_option("url"));
                ManagedObjectReference hmor = _service.FindByIp(ecb.getConnection().ServiceContent.searchIndex, null, ecb.get_option("hostip"), false);
                if (hmor == null)
                {
                    Console.WriteLine("Unable to find host with IP : " + ecb.get_option("hostip") + " in Inventory");
                }
                else
                {
                    if (VersionUtil.isApiVersionSupported(supportedVersions, "2.5"))
                    {
                        Object[] datastores = getProperties(hmor, new String[] { "datastore" });
                        Console.WriteLine("Searching The Datastores");
                        ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[];
                        foreach (ManagedObjectReference dstore in dstoreArr)
                        {
                            ManagedObjectReference dsBrowser =
                                ecb.getServiceUtil().GetMoRefProp(dstore, "browser");
                            ObjectContent[]  objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" });
                            DatastoreSummary ds     = objary[0].propSet[0].val as DatastoreSummary;
                            String           dsName = ds.name;
                            Console.WriteLine("");
                            Console.WriteLine("Searching The Datastore " + dsName);
                            VmDiskFileQueryFilter vdiskFilter = new VmDiskFileQueryFilter();
                            String[] type = { "VirtualIDEController" };
                            vdiskFilter.controllerType = type;
                            Boolean flag = VersionUtil.isApiVersionSupported(supportedVersions, "4.0");
                            if (flag)
                            {
                                vdiskFilter.thin = true;
                            }
                            VmDiskFileQuery fQuery = new VmDiskFileQuery();
                            fQuery.filter = vdiskFilter;

                            HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec();

                            FileQuery[] arr = { fQuery };
                            searchSpec.query = arr;
                            //searchSpec.setMatchPattern(matchPattern);

                            ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec);

                            object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" },
                                                                                 new string[] { "state" }, // info has a property - state for state of the task
                                                                                 new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } }
                                                                                 );

                            // Wait till the task completes.
                            if (result[0].Equals(TaskInfoState.success))
                            {
                                ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" });
                                TaskInfo        tInfo       = (TaskInfo)objTaskInfo[0].propSet[0].val;;
                                HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result;

                                int len = searchResult.Length;
                                for (int j = 0; j < len; j++)
                                {
                                    HostDatastoreBrowserSearchResults sres
                                        = searchResult[j];
                                    FileInfo[] fileArray = sres.file;
                                    if (fileArray != null)
                                    {
                                        for (int z = 0; z < fileArray.Length; z++)
                                        {
                                            Console.WriteLine("Virtual Disks Files " + fileArray[z].path);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("No Thin-provisioned Virtual Disks Files found");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully");
                            }
                        }
                    }
                    else
                    {
                        Object[] datastores = getProperties(hmor, new String[] { "datastore" });
                        Console.WriteLine("Searching The Datastores");
                        ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[];
                        foreach (ManagedObjectReference dstore in dstoreArr)
                        {
                            ManagedObjectReference dsBrowser = (ManagedObjectReference)
                                                               ecb.getServiceUtil().GetMoRefProp(dstore, "browser");
                            ObjectContent[]  objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" });
                            DatastoreSummary ds     = objary[0].propSet[0].val as DatastoreSummary;

                            String dsName = ds.name;
                            Console.WriteLine("");
                            Console.WriteLine("Searching The Datastore " + dsName);
                            HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec();
                            ManagedObjectReference         taskmor    = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec);
                            object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" },
                                                                                 new string[] { "state" }, // info has a property - state for state of the task
                                                                                 new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } }
                                                                                 );
                            // Wait till the task completes.
                            if (result[0].Equals(TaskInfoState.success))
                            {
                                ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" });
                                TaskInfo        tInfo       = (TaskInfo)objTaskInfo[0].propSet[0].val;;
                                HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result;
                                int len = searchResult.Length;
                                for (int j = 0; j < len; j++)
                                {
                                    HostDatastoreBrowserSearchResults sres
                                        = searchResult[j];
                                    FileInfo[] fileArray = sres.file;
                                    for (int z = 0; z < fileArray.Length; z++)
                                    {
                                        Console.WriteLine("Virtual Disks Files " + fileArray[z].path);
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ecb.log.LogLine("VirtualDiskFiles : Failed Connect");
                throw e;
            }
            finally
            {
                ecb.log.LogLine("Ended VirtualDiskFiles");
                ecb.log.Close();
            }
        }
コード例 #2
0
        public void GetVirtualDiskFilesForHost()
        {
            try
            {
                _service = ecb.getConnection().Service;
                _sic = ecb.getConnection().ServiceContent;         
            
                ArrayList supportedVersions = VersionUtil.getSupportedVersions(ecb.get_option("url"));
                ManagedObjectReference hmor = _service.FindByIp(ecb.getConnection().ServiceContent.searchIndex, null, ecb.get_option("hostip"), false);
                if (hmor == null)
                {
                    Console.WriteLine("Unable to find host with IP : " + ecb.get_option("hostip") + " in Inventory");
                }
                else
                {
                    if (VersionUtil.isApiVersionSupported(supportedVersions, "2.5"))
                    {
                        Object[] datastores = getProperties(hmor, new String[] { "datastore" });
                        Console.WriteLine("Searching The Datastores");
                        ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[];
                        foreach (ManagedObjectReference dstore in dstoreArr)
                        {
                            ManagedObjectReference dsBrowser =
                                            ecb.getServiceUtil().GetMoRefProp(dstore, "browser");
                            ObjectContent[] objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" });
                            DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary;
                            String dsName = ds.name;
                            Console.WriteLine("");
                            Console.WriteLine("Searching The Datastore " + dsName);
                            VmDiskFileQueryFilter vdiskFilter = new VmDiskFileQueryFilter();
                            String[] type = { "VirtualIDEController" };
                            vdiskFilter.controllerType = type;
                            Boolean flag = VersionUtil.isApiVersionSupported(supportedVersions, "4.0");
                            if (flag)
                            {
                                vdiskFilter.thin = true;
                            }
                            VmDiskFileQuery fQuery = new VmDiskFileQuery();
                            fQuery.filter = vdiskFilter;

                            HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec();

                            FileQuery[] arr = { fQuery };
                            searchSpec.query = arr;
                            //searchSpec.setMatchPattern(matchPattern);

                            ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec);

                            object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" },
                                  new string[] { "state" }, // info has a property - state for state of the task
                                  new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } }
                            );

                            // Wait till the task completes.
                            if (result[0].Equals(TaskInfoState.success))
                            {
                                ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" });
                                TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val; ;
                                HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result;

                                int len = searchResult.Length;
                                for (int j = 0; j < len; j++)
                                {
                                    HostDatastoreBrowserSearchResults sres
                                        = searchResult[j];
                                    FileInfo[] fileArray = sres.file;
                                    if (fileArray != null)
                                    {
                                        for (int z = 0; z < fileArray.Length; z++)
                                        {
                                            Console.WriteLine("Virtual Disks Files " + fileArray[z].path);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("No Thin-provisioned Virtual Disks Files found");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully");
                            }
                        }
                    }
                    else
                    {
                        Object[] datastores = getProperties(hmor, new String[] { "datastore" });
                        Console.WriteLine("Searching The Datastores");
                        ManagedObjectReference[] dstoreArr = datastores[0] as ManagedObjectReference[];
                        foreach (ManagedObjectReference dstore in dstoreArr)
                        {
                            ManagedObjectReference dsBrowser = (ManagedObjectReference)
                                                         ecb.getServiceUtil().GetMoRefProp(dstore, "browser");
                            ObjectContent[] objary = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, dstore, new String[] { "summary" });
                            DatastoreSummary ds = objary[0].propSet[0].val as DatastoreSummary;

                            String dsName = ds.name;
                            Console.WriteLine("");
                            Console.WriteLine("Searching The Datastore " + dsName);
                            HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec();
                            ManagedObjectReference taskmor = _service.SearchDatastoreSubFolders_Task(dsBrowser, "[" + dsName + "]", searchSpec);
                            object[] result = ecb.getServiceUtil().WaitForValues(taskmor, new string[] { "info.state", "info.result" },
                                     new string[] { "state" }, // info has a property - state for state of the task
                                     new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } }
                            );
                            // Wait till the task completes.
                            if (result[0].Equals(TaskInfoState.success))
                            {
                                ObjectContent[] objTaskInfo = ecb.getServiceUtil().GetObjectProperties(_sic.propertyCollector, taskmor, new String[] { "info" });
                                TaskInfo tInfo = (TaskInfo)objTaskInfo[0].propSet[0].val; ;
                                HostDatastoreBrowserSearchResults[] searchResult = (HostDatastoreBrowserSearchResults[])tInfo.result;
                                int len = searchResult.Length;
                                for (int j = 0; j < len; j++)
                                {
                                    HostDatastoreBrowserSearchResults sres
                                                     = searchResult[j];
                                    FileInfo[] fileArray = sres.file;
                                    for (int z = 0; z < fileArray.Length; z++)
                                    {
                                        Console.WriteLine("Virtual Disks Files " + fileArray[z].path);
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("SearchDatastoreSubFolders Task couldn't be completed successfully");
                            }
                        }
                    }
                }
            }          
            catch (Exception e)
            {
                ecb.log.LogLine("VirtualDiskFiles : Failed Connect");
                throw e;
            }
            finally
            {
                ecb.log.LogLine("Ended VirtualDiskFiles");
                ecb.log.Close();
            }
        }
コード例 #3
0
        /** Excercise the SearchIndex API
         *
         * @param args Usage is:
         *  <url> <user> <password> <Datacenter Name> <A VM DNS Name>
         *  <A Host DNS Name> <A VM Inventory Path>
         */
        public static void Main(String[] args)
        {
            try {
                SearchIndex app = new SearchIndex();
                cb = AppUtil.AppUtil.initialize("SearchIndex"
                                                , SearchIndex.constructOptions()
                                                , args);
                cb.connect();
                String dcName      = cb.get_option("dcName");
                String vmDnsName   = cb.get_option("vmDnsName");
                String hostDnsName = cb.get_option("hostDnsName");
                String vmPath      = cb.get_option("vmPath");
                String vmIP        = cb.get_option("vmIP");

                content = cb.getConnection()._sic;
                service = cb.getConnection()._service;
                // Find the Datacenter by using findChild()
                ManagedObjectReference dcMoRef =
                    cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", dcName);
                if (dcMoRef != null)
                {
                    Console.WriteLine("Found Datacenter with name: "
                                      + dcName + ", MoRef: " +
                                      dcMoRef.Value);
                }
                else
                {
                    Console.WriteLine("Datacenter not Found with name: " + dcName);
                }
                if (vmDnsName != null)
                {
                    ManagedObjectReference vmMoRef =
                        service.FindByDnsName(content.searchIndex,
                                              dcMoRef,
                                              vmDnsName,
                                              true);
                    if (vmMoRef != null)
                    {
                        Console.WriteLine("Found VirtualMachine with DNS name: " +
                                          vmDnsName + ", MoRef: " + vmMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("VirtualMachine not Found with DNS name: "
                                          + vmDnsName);
                    }
                }
                if (vmPath != null)
                {
                    ManagedObjectReference vmMoRef = service.FindByInventoryPath(
                        content.searchIndex, vmPath);
                    if (vmMoRef != null)
                    {
                        Console.WriteLine("Found VirtualMachine with Path: " +
                                          vmPath + ", MoRef: " + vmMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("VirtualMachine not found with vmPath " +
                                          "address: " + vmPath);
                    }
                }
                if (vmIP != null)
                {
                    ManagedObjectReference vmMoRef =
                        service.FindByIp(content.searchIndex,
                                         dcMoRef,
                                         vmIP,
                                         true);
                    if (vmMoRef != null)
                    {
                        Console.WriteLine("Found VirtualMachine with IP " +
                                          "address " + vmIP + ", MoRef: " + vmMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("VirtualMachine not found with IP " +
                                          "address: " + vmIP);
                    }
                }
                if (hostDnsName != null)
                {
                    ManagedObjectReference hostMoRef =
                        service.FindByDnsName(content.searchIndex,
                                              null,
                                              hostDnsName,
                                              false);
                    if (hostMoRef != null)
                    {
                        Console.WriteLine("Found HostSystem with DNS name " +
                                          hostDnsName + ", MoRef: " + hostMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("HostSystem not Found with DNS name:" +
                                          hostDnsName);
                    }
                }
                cb.disConnect();
                Console.WriteLine("Press enter to exit: ");
                Console.Read();
            }
            catch (SoapException e) {
            }
        }
コード例 #4
0
    /** Excercise the SearchIndex API
     * 
     * @param args Usage is:
     *  <url> <user> <password> <Datacenter Name> <A VM DNS Name>
     *  <A Host DNS Name> <A VM Inventory Path> 
     */
    public static void Main(String[] args) {
        
       try {
          SearchIndex app = new SearchIndex();
            cb = AppUtil.AppUtil.initialize("SearchIndex"
                                    ,SearchIndex.constructOptions()
                                    ,args);
            cb.connect();
            String dcName = cb.get_option("dcName");
            String vmDnsName = cb.get_option("vmDnsName");
            String hostDnsName =cb.get_option("hostDnsName");
            String vmPath = cb.get_option("vmPath");
            String vmIP = cb.get_option("vmIP");
            
            content = cb.getConnection()._sic;
            service = cb.getConnection()._service;
            // Find the Datacenter by using findChild()
            ManagedObjectReference dcMoRef = 
            cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", dcName);
            if (dcMoRef !=null){
                    Console.WriteLine("Found Datacenter with name: "
                                       +dcName+", MoRef: " + 
                    dcMoRef.Value);
            }else{
                   Console.WriteLine("Datacenter not Found with name: "+dcName);
            }
            if (vmDnsName != null){
                ManagedObjectReference vmMoRef = 
                service.FindByDnsName(content.searchIndex,
                                      dcMoRef,
                                      vmDnsName,
                                      true);
               if (vmMoRef !=null){
                 Console.WriteLine("Found VirtualMachine with DNS name: "+
                      vmDnsName+", MoRef: " + vmMoRef.Value);
               }
               else{
                 Console.WriteLine("VirtualMachine not Found with DNS name: "
                                  + vmDnsName);
               }
            }
            if (vmPath != null){
              ManagedObjectReference   vmMoRef = service.FindByInventoryPath(
                    content.searchIndex, vmPath);
               if (vmMoRef !=null) {
                  Console.WriteLine("Found VirtualMachine with Path: "+
                      vmPath+", MoRef: " + vmMoRef.Value);
            
               }
               else{
                      Console.WriteLine("VirtualMachine not found with vmPath "+
                      "address: "+ vmPath);
                }
             }
             if (vmIP != null){
                ManagedObjectReference vmMoRef =
                service.FindByIp(content.searchIndex,
                                 dcMoRef,
                                 vmIP,
                                 true);
              if (vmMoRef !=null){
                    Console.WriteLine("Found VirtualMachine with IP "+
                    "address "+ vmIP + ", MoRef: " + vmMoRef.Value);
              }else{
                   Console.WriteLine("VirtualMachine not found with IP "+
                   "address: "+vmIP);
               }
            }
            if (hostDnsName != null) {
               ManagedObjectReference hostMoRef =
                service.FindByDnsName(content.searchIndex,
                        null,
                        hostDnsName,
                        false);
               if (hostMoRef !=null) {
                  Console.WriteLine("Found HostSystem with DNS name "+
                  hostDnsName+", MoRef: " + hostMoRef.Value);
               }
               else{
                  Console.WriteLine("HostSystem not Found with DNS name:"+
                  hostDnsName);
               }
            }
          cb.disConnect();
          Console.WriteLine("Press enter to exit: ");
          Console.Read();
          
       } 
       catch(SoapException e) {

       }
    }