Esempio n. 1
0
    def _create_property_filter(self, property_collector, config):
        connection = self.connection
        vim = connection.vim
        service_content = vim.service_content
        client_factory = vim.client.factory

        if not config.cluster_name:
            LOG.info("No cluster specified")
            container = service_content.rootFolder
        else:
            container = get_cluster_ref_by_name(connection, config.cluster_name)
            if not container:
                LOG.error(_LE("Cannot find cluster with name '{}'").format(config.cluster_name))
                exit(2)

        container_view = connection.invoke_api(vim, 'CreateContainerView', service_content.viewManager,
                                               container=container,
                                               type=['VirtualMachine'],
                                               recursive=True)

        traversal_spec = vim_util.build_traversal_spec(client_factory, 'traverseEntities', 'ContainerView', 'view',
                                                       False, None)
        object_spec = vim_util.build_object_spec(client_factory, container_view, [traversal_spec])

        # Only static types work, so we have to get all hardware, still faster than retrieving individual items
        vm_properties = ['runtime.powerState', 'config.hardware.device']
        property_specs = [vim_util.build_property_spec(client_factory, 'VirtualMachine', vm_properties)]

        property_filter_spec = vim_util.build_property_filter_spec(client_factory, property_specs, [object_spec])

        return connection.invoke_api(vim, 'CreateFilter', property_collector, spec=property_filter_spec,
                                     partialUpdates=True)  # -> PropertyFilter
Esempio n. 2
0
 def _get_properties_for_a_collection_of_objects(self, vim, type, obj_list,
                                                 properties):
     """Gets the list of properties for the collection of
     objects of the type specified.
     """
     client_factory = vim.client.factory
     if len(obj_list) == 0:
         return []
     prop_spec = vim_util.build_property_spec(client_factory,
                                              type,
                                              properties)
     lst_obj_specs = []
     for obj in obj_list:
         lst_obj_specs.append(vim_util.build_object_spec(
             client_factory,
             obj, []))
     prop_filter_spec = vim_util.build_property_filter_spec(client_factory,
                                                            [prop_spec],
                                                            lst_obj_specs)
     options = client_factory.create('ns0:RetrieveOptions')
     options.maxObjects = self._max_objects
     return vim.RetrievePropertiesEx(
         vim.service_content.propertyCollector,
         specSet=[prop_filter_spec],
         options=options)
Esempio n. 3
0
def get_inner_objects(vim,
                      base_obj,
                      path,
                      inner_type,
                      properties_to_collect=None,
                      all=False):
    """Gets the list of inner objects of the type specified."""
    client_factory = vim.client.factory
    base_type = base_obj._type
    traversal_spec = vutil.build_traversal_spec(client_factory, 'inner',
                                                base_type, path, False, [])
    object_spec = vutil.build_object_spec(client_factory, base_obj,
                                          [traversal_spec])
    property_spec = vutil.build_property_spec(
        client_factory,
        type_=inner_type,
        properties_to_collect=properties_to_collect,
        all_properties=all)
    property_filter_spec = vutil.build_property_filter_spec(
        client_factory, [property_spec], [object_spec])
    options = client_factory.create('ns0:RetrieveOptions')
    options.maxObjects = CONF.vmware.maximum_objects
    return vim.RetrievePropertiesEx(vim.service_content.propertyCollector,
                                    specSet=[property_filter_spec],
                                    options=options)
Esempio n. 4
0
    def _get_datacenter(vim, entity_ref, max_objects=100):
        """Get the inventory path of a managed entity.
        :param vim: Vim object
        :param entity_ref: managed entity reference
        :return: the datacenter of the entity_ref
        """
        client_factory = vim.client.factory
        property_collector = vim.service_content.propertyCollector

        prop_spec = vim_util.build_property_spec(client_factory, 'Datacenter', ['name'])
        select_set = vim_util.build_selection_spec(client_factory, 'ParentTraversalSpec')
        select_set = vim_util.build_traversal_spec(
            client_factory, 'ParentTraversalSpec', 'ManagedEntity', 'parent',
            False, [select_set])
        obj_spec = vim_util.build_object_spec(client_factory, entity_ref, select_set)
        prop_filter_spec = vim_util.build_property_filter_spec(client_factory,
                                                               [prop_spec], [obj_spec])
        options = client_factory.create('ns0:RetrieveOptions')
        options.maxObjects = max_objects
        retrieve_result = vim.RetrievePropertiesEx(
            property_collector,
            specSet=[prop_filter_spec],
            options=options)

        with vim_util.WithRetrieval(vim, retrieve_result) as objects:
            for obj in objects:
                if obj.obj._type == 'Datacenter':
                    return obj.obj
Esempio n. 5
0
 def test_build_object_spec(self):
     client_factory = mock.Mock()
     root_folder = mock.Mock()
     specs = [mock.Mock()]
     obj_spec = vim_util.build_object_spec(client_factory, root_folder,
                                           specs)
     self.assertEqual(root_folder, obj_spec.obj)
     self.assertEqual(specs, obj_spec.selectSet)
     self.assertFalse(obj_spec.skip)
Esempio n. 6
0
    def _get_perf_counters(self):
        try:
            self.log.info("Loading VmWare performance counters")
            perf_counters = []
            property_dict = {'perf_counter': ['perfCounter']}

            prop_collector = self.session.vim.service_content.propertyCollector
            self.client_factory = self.session.vim.client.factory
            perf_manager = self.session.vim.service_content.perfManager
            options = self.client_factory.create('ns0:RetrieveOptions')
            options.maxObjects = 1

            prop_spec = vim_util.build_property_spec(
                self.client_factory,
                PERF_MANAGER_TYPE,
                property_dict.get('perf_counter'))
            obj_spec = vim_util.build_object_spec(
                self.client_factory,
                perf_manager,
                None)

            filter_spec = vim_util.build_property_filter_spec(
                self.client_factory, [prop_spec], [obj_spec])

            object_contents = self.session.invoke_api(
                self.session.vim,
                "RetrievePropertiesEx",
                prop_collector,
                specSet=[filter_spec],
                options=options)

            if object_contents is not None:
                for object_content in object_contents:
                    dynamic_properties = object_content[1][0].propSet
                    for dynamic_property in dynamic_properties:
                        perf_counters = dynamic_property.val.PerfCounterInfo

            return perf_counters
        except Exception as ex:
            self.log.error("Exception in _get_perf_counters: %s"
                           % str(ex.message))
Esempio n. 7
0
    def _init_perf_counter_id_lookup_map(self):

        # Query details of all the performance counters from VC
        session = self._api_session
        client_factory = session.vim.client.factory
        perf_manager = session.vim.service_content.perfManager

        prop_spec = vim_util.build_property_spec(client_factory,
                                                 PERF_MANAGER_TYPE,
                                                 [PERF_COUNTER_PROPERTY])

        obj_spec = vim_util.build_object_spec(client_factory, perf_manager,
                                              None)

        filter_spec = vim_util.build_property_filter_spec(
            client_factory, [prop_spec], [obj_spec])

        options = client_factory.create('ns0:RetrieveOptions')
        options.maxObjects = 1

        prop_collector = session.vim.service_content.propertyCollector
        result = session.invoke_api(session.vim,
                                    "RetrievePropertiesEx",
                                    prop_collector,
                                    specSet=[filter_spec],
                                    options=options)

        perf_counter_infos = result.objects[0].propSet[0].val.PerfCounterInfo

        # Extract the counter Id for each counter and populate the map
        self._perf_counter_id_lookup_map = {}
        for perf_counter_info in perf_counter_infos:

            counter_group = perf_counter_info.groupInfo.key
            counter_name = perf_counter_info.nameInfo.key
            counter_rollup_type = perf_counter_info.rollupType
            counter_id = perf_counter_info.key

            counter_full_name = (counter_group + ":" + counter_name + ":" +
                                 counter_rollup_type)
            self._perf_counter_id_lookup_map[counter_full_name] = counter_id
Esempio n. 8
0
    def _get_portgroups(self, max_objects=100):
        """Get all portgroups on the switch"""
        vim = self.connection.vim
        property_collector = vim.service_content.propertyCollector

        traversal_spec = vim_util.build_traversal_spec(
                vim.client.factory,
                "dvs_to_dvpg",
                "DistributedVirtualSwitch",
                "portgroup",
                False,
                [])
        object_spec = vim_util.build_object_spec(
                vim.client.factory,
                self._dvs,
                [traversal_spec])
        property_spec = vim_util.build_property_spec(
                vim.client.factory,
                "DistributedVirtualPortgroup",
                ["key", "name", "config.configVersion", "customValue", "vm"])

        property_filter_spec = vim_util.build_property_filter_spec(
                vim.client.factory,
                [property_spec],
                [object_spec])
        options = vim.client.factory.create('ns0:RetrieveOptions')
        options.maxObjects = max_objects

        pc_result = vim.RetrievePropertiesEx(property_collector, specSet=[property_filter_spec],
                options=options)
        result = []

        with vim_util.WithRetrieval(vim, pc_result) as pc_objects:
            for objContent in pc_objects:
                props = {prop.name : prop.val for prop in objContent.propSet}
                props["ref"] = objContent.obj
                props["configVersion"] = props["config.configVersion"]
                result.append(props)

        return result
Esempio n. 9
0
def _get_property_filter_spec(vim, prop_spec_map=None):
    if not prop_spec_map:
        prop_spec_map = constants.prop_spec_map

    client_factory = vim.client.factory
    recur_trav_spec = vim_util.build_recursive_traversal_spec(client_factory)
    # Build the object spec
    object_spec = vim_util.build_object_spec(client_factory,
                                             vim.service_content.rootFolder,
                                             [recur_trav_spec])
    # Build property spec
    propertySpecList = []
    for prop_spec in prop_spec_map:
        propertySpec = vim_util.build_property_spec(
            client_factory,
            type_=prop_spec,
            properties_to_collect=prop_spec_map[prop_spec])
        propertySpecList.append(propertySpec)

    return vim_util.build_property_filter_spec(client_factory,
                                               property_specs=propertySpecList,
                                               object_specs=object_spec)