Ejemplo n.º 1
0
    def DiffDataObjects(self, oldObj, newObj):
        """Diff Data Objects"""
        if oldObj == newObj:
            return True
        if not oldObj or not newObj:
            __Log__.debug('DiffDataObjects: One of the objects in None')
            return False
        oldType = Type(oldObj)
        newType = Type(newObj)
        if oldType != newType:
            __Log__.debug(
                'DiffDataObjects: Types do not match for dataobjects. %s != %s'
                % (oldObj._wsdlName, newObj._wsdlName))
            return False
        for prop in oldObj._GetPropertyList():
            oldProp = getattr(oldObj, prop.name)
            newProp = getattr(newObj, prop.name)
            propType = oldObj._GetPropertyInfo(prop.name).type
            if not oldProp and not newProp:
                continue
            elif ((prop.flags & VmomiSupport.F_OPTIONAL) and self._looseMatch
                  and (not newProp or not oldProp)):
                continue
            elif not oldProp or not newProp:
                __Log__.debug(
                    'DiffDataObjects: One of the objects has property %s unset'
                    % prop.name)
                return False

            bMatch = True
            if IsPrimitiveType(oldProp):
                bMatch = oldProp == newProp
            elif isinstance(oldProp, types.ManagedObject):
                bMatch = self.DiffAnyObjects(oldProp, newProp, prop.flags
                                             & VmomiSupport.F_LINK)
            elif isinstance(oldProp, types.DataObject):
                if prop.flags & VmomiSupport.F_LINK:
                    bMatch = oldObj.GetKey() == newObj.GetKey()
                    LogIf(
                        not bMatch,
                        'DiffDataObjects: Key match failed %s != %s' %
                        (oldObj.GetKey(), newObj.GetKey()))
                else:
                    bMatch = self.DiffAnyObjects(
                        oldProp, newProp, prop.flags
                        & VmomiSupport.F_LINK)
            elif isinstance(oldProp, list):
                bMatch = self.DiffArrayObjects(
                    oldProp, newProp, prop.flags
                    & VmomiSupport.F_LINK)
            else:
                raise TypeError("Unknown type: " + repr(propType))

            if not bMatch:
                __Log__.debug(
                    'DiffDataObjects: Objects differ in property %s' %
                    prop.name)
                return False
        return True
Ejemplo n.º 2
0
    def DiffAnyObjects(self, oldObj, newObj, isObjLink=False):
        """Diff any two Objects"""
        if oldObj == newObj:
            return True
        if not oldObj or not newObj:
            __Log__.debug('DiffAnyObjects: One of the objects is unset.')
            return self._looseMatch
        oldObjInstance = oldObj
        newObjInstance = newObj
        if isinstance(oldObj, list):
            oldObjInstance = oldObj[0]
        if isinstance(newObj, list):
            newObjInstance = newObj[0]
        # Need to see if it is a primitive type first since type information
        #   will not be available for them.
        if (IsPrimitiveType(oldObj) and IsPrimitiveType(newObj)
                and oldObj.__class__.__name__ == newObj.__class__.__name__):
            if oldObj == newObj:
                return True
            elif oldObj == None or newObj == None:
                __Log__.debug('DiffAnyObjects: One of the objects in None')
            return False
        oldType = Type(oldObjInstance)
        newType = Type(newObjInstance)
        if oldType != newType:
            __Log__.debug('DiffAnyObjects: Types do not match %s != %s' %
                          (repr(GetWsdlName(oldObjInstance.__class__)),
                           repr(GetWsdlName(newObjInstance.__class__))))
            return False
        elif isinstance(oldObj, list):
            return self.DiffArrayObjects(oldObj, newObj, isObjLink)
        elif isinstance(oldObjInstance, types.ManagedObject):
            return (not oldObj
                    and not newObj) or (oldObj and newObj
                                        and oldObj._moId == newObj._moId)
        elif isinstance(oldObjInstance, types.DataObject):
            if isObjLink:
                bMatch = oldObj.GetKey() == newObj.GetKey()
                LogIf(
                    not bMatch, 'DiffAnyObjects: Keys do not match %s != %s' %
                    (oldObj.GetKey(), newObj.GetKey()))
                return bMatch
            return self.DiffDataObjects(oldObj, newObj)

        else:
            raise TypeError("Unknown type: " +
                            repr(GetWsdlName(oldObj.__class__)))