Exemplo n.º 1
0
    def _unmarshal(self, obj):
        """Walks an object and unmarshals any MORs into psphere objects."""
        if isinstance(obj, suds.sudsobject.Object) is False:
            logger.debug("%s is not a suds instance, skipping", obj)
            return obj

        logger.debug("Processing:")
        logger.debug(obj)
        logger.debug("...with keylist:")
        logger.debug(obj.__keylist__)
        # If the obj that we're looking at has a _type key
        # then create a class of that type and return it immediately
        if "_type" in obj.__keylist__:
            logger.debug("obj is a MOR, converting to psphere class")
            return self._mor_to_pobject(obj)

        new_object = obj.__class__()
        for sub_obj in obj:
            logger.debug("Looking at %s of type %s", sub_obj, type(sub_obj))

            if isinstance(sub_obj[1], list):
                new_embedded_objs = []
                for emb_obj in sub_obj[1]:
                    new_emb_obj = self._unmarshal(emb_obj)
                    new_embedded_objs.append(new_emb_obj)
                setattr(new_object, sub_obj[0], new_embedded_objs)
                continue

            if not issubclass(sub_obj[1].__class__, suds.sudsobject.Object):
                logger.debug("%s is not a sudsobject subclass, skipping",
                             sub_obj[1].__class__)
                setattr(new_object, sub_obj[0], sub_obj[1])
                continue

            logger.debug("Obj keylist: %s", sub_obj[1].__keylist__)
            if "_type" in sub_obj[1].__keylist__:
                logger.debug("Converting nested MOR to psphere class:")
                logger.debug(sub_obj[1])
                kls = classmapper(sub_obj[1]._type)
                logger.debug("Setting %s.%s to %s",
                             new_object.__class__.__name__,
                             sub_obj[0],
                             sub_obj[1])
                setattr(new_object, sub_obj[0], kls(sub_obj[1], self))
            else:
                logger.debug("Didn't find _type in:")
                logger.debug(sub_obj[1])
                setattr(new_object, sub_obj[0], self._unmarshal(sub_obj[1]))

        return new_object
Exemplo n.º 2
0
    def get_view(self, mo_ref, properties=None):
        """Get a view of a vSphere managed object.
        
        :param mo_ref: The MOR to get a view of
        :type mo_ref: ManagedObjectReference
        :param properties: A list of properties to retrieve from the \
        server
        :type properties: list
        :returns: A view representing the ManagedObjectReference.
        :rtype: ManagedObject

        """
        # This maps the mo_ref into a psphere class and then instantiates it
        kls = classmapper(mo_ref._type)
        view = kls(mo_ref, self)
        # Update the requested properties of the instance
        #view.update_view_data(properties=properties)

        return view
Exemplo n.º 3
0
    def find_entity_view(self, view_type, begin_entity=None, filter={},
                         properties=None):
        """Find a ManagedEntity of the requested type.

        Traverses the MOB looking for an entity matching the filter.

        :param view_type: The type of ManagedEntity to find.
        :type view_type: str
        :param begin_entity: The MOR to start searching for the entity. \
        The default is to start the search at the root folder.
        :type begin_entity: ManagedObjectReference or None
        :param filter: Key/value pairs to filter the results. The key is \
        a valid parameter of the ManagedEntity type. The value is what \
        that parameter should match.
        :type filter: dict
        :returns: If an entity is found, a ManagedEntity matching the search.
        :rtype: ManagedEntity

        """
        if properties is None:
            properties = []

        kls = classmapper(view_type)
        # Start the search at the root folder if no begin_entity was given
        if not begin_entity:
            begin_entity = self.sc.rootFolder._mo_ref
            logger.debug("Using %s", self.sc.rootFolder._mo_ref)

        property_spec = self.create('PropertySpec')
        property_spec.type = view_type
        property_spec.all = False
        property_spec.pathSet = filter.keys()

        pfs = self.get_search_filter_spec(begin_entity, property_spec)

        # Retrieve properties from server and update entity
        #obj_contents = self.propertyCollector.RetrieveProperties(specSet=pfs)
        obj_contents = self.sc.propertyCollector.RetrieveProperties(specSet=pfs)

        # TODO: Implement filtering
        if not filter:
            logger.warning('No filter specified, returning first match.')
            # If no filter is specified we just return the first item
            # in the list of returned objects
            logger.debug("Creating class in find_entity_view (filter)")
            view = kls(obj_contents[0].obj, self)
            logger.debug("Completed creating class in find_entity_view (filter)")
            #view.update_view_data(properties)
            return view

        matched = False
        # Iterate through obj_contents retrieved
        for obj_content in obj_contents:
            # If there are is no propSet, skip this one
            if not obj_content.propSet:
                continue

            matches = 0
            # Iterate through each property in the set
            for prop in obj_content.propSet:
                for key in filter.keys():
                    # If the property name is in the defined filter
                    if prop.name == key:
                        # ...and it matches the value specified
                        # TODO: Regex this?
                        if prop.val == filter[prop.name]:
                            # We've found a match
                            matches += 1
                        else:
                            break
                    else:
                        continue
            if matches == len(filter):
                filtered_obj_content = obj_content
                matched = True
                break
            else:
                continue

        if matched is not True:
            # There were no matches
            raise ObjectNotFoundError("No matching objects for filter")

        logger.debug("Creating class in find_entity_view")
        view = kls(filtered_obj_content.obj._mo_ref, self)
        logger.debug("Completed creating class in find_entity_view")
        #view.update_view_data(properties=properties)
        return view
Exemplo n.º 4
0
 def _mor_to_pobject(self, mo_ref):
     """Converts a MOR to a psphere object."""
     kls = classmapper(mo_ref._type)
     new_object = kls(mo_ref, self)
     return new_object