def diffObject(self, user, ref): """ Opens a copy of the object given as ref and returns a diff - if any. """ if not ref in self.__stack: return None if not self.__check_user(ref, user): raise ValueError(C.make_error("NOT_OBJECT_OWNER")) # Load current object item = self.__stack[ref] current_obj = ObjectProxy(item['object']['dn']) # Load cache object cache_obj = item['object']['object'] ## ## Generate delta ## delta = {'attributes': {'added': {}, 'removed': [], 'changed': {}, 'blocked_by': {}}, 'extensions': {'added': [], 'removed': []}} # Compare extension list crnt_extensions = set(current_obj.get_object_info()['extensions'].items()) cche_extensions = set(cache_obj.get_object_info()['extensions'].items()) for _e, _s in crnt_extensions - cche_extensions: if _s: delta['extensions']['added'].append(_e) else: delta['extensions']['removed'].append(_e) # Compare attribute contents crnt_attributes = dict(filter(lambda x: x[1] is not None, current_obj.get_attribute_values()['value'].items())) cche_attributes = dict(filter(lambda x: x[1] is not None, cache_obj.get_attribute_values()['value'].items())) all_attributes = [] for _k, _v in crnt_attributes.items(): if _k in cche_attributes: if _v != cche_attributes[_k]: delta['attributes']['changed'][_k] = _v all_attributes.append(_k) else: delta['attributes']['added'][_k] = _v all_attributes.append(_k) for _k, _v in cche_attributes.items(): # Don't add the individual attributes of extensions that are removed anyway if current_obj.get_extension_off_attribute(_k) in delta['extensions']['removed']: continue if not _k in crnt_attributes: delta['attributes']['removed'].append(_k) all_attributes.append(_k) # Find blocking dependencies between attributes details = current_obj.get_attributes(detail=True) for attribute_name in all_attributes: delta['attributes']['blocked_by'][attribute_name] = list(map(lambda x: x['name'], details[attribute_name]['blocked_by'])) return delta
def get_object(self, object_type, oid, create=True, data=None): backend_attributes = self.factory.getObjectBackendProperties(object_type) foreman_object = None if "Foreman" not in backend_attributes: self.log.warning("no foreman backend attributes found for '%s' object" % object_type) return types = self.factory.getObjectTypes()[object_type] base_type = object_type if types["base"] is True else types["extends"][0] index = PluginRegistry.getInstance("ObjectIndex") # check if the object already exists query = { '_type': base_type, backend_attributes["Foreman"]["_uuidAttribute"]: str(oid) } if types["base"] is False: query["extension"] = object_type res = index.search(query, {'dn': 1}) if len(res) == 0: if create is True: # no object found -> create one self.log.debug(">>> creating new %s" % object_type) base_dn = self.env.base if object_type == "ForemanHost": # get the IncomingDevice-Container res = index.search({"_type": "IncomingDeviceContainer", "_parent_dn": self.type_bases["ForemanHost"]}, {"dn": 1}) if len(res) > 0: base_dn = res[0]["dn"] else: base_dn = self.type_bases["ForemanHost"] elif object_type in self.type_bases: base_dn = self.type_bases[object_type] foreman_object = ObjectProxy(base_dn, base_type) uuid_extension = foreman_object.get_extension_off_attribute(backend_attributes["Foreman"]["_uuidAttribute"]) if base_type != uuid_extension and not foreman_object.is_extended_by(uuid_extension): foreman_object.extend(uuid_extension) setattr(foreman_object, backend_attributes["Foreman"]["_uuidAttribute"], str(oid)) else: # open existing object self.log.debug(">>> open existing %s with DN: %s" % (object_type, res[0]["dn"])) foreman_object = ObjectProxy(res[0]["dn"], data={object_type: {"Foreman": data}} if data is not None else None) return foreman_object
def diffObject(self, user, ref): """ Opens a copy of the object given as ref and returns a diff - if any. """ if not ref in self.__stack: return None if not self.__check_user(ref, user): raise ValueError(C.make_error("NOT_OBJECT_OWNER")) # Load current object item = self.__stack[ref] current_obj = ObjectProxy(item['object']['dn']) # Load cache object cache_obj = item['object']['object'] ## ## Generate delta ## delta = { 'attributes': { 'added': {}, 'removed': [], 'changed': {}, 'blocked_by': {} }, 'extensions': { 'added': [], 'removed': [] } } # Compare extension list crnt_extensions = set( current_obj.get_object_info()['extensions'].items()) cche_extensions = set( cache_obj.get_object_info()['extensions'].items()) for _e, _s in crnt_extensions - cche_extensions: if _s: delta['extensions']['added'].append(_e) else: delta['extensions']['removed'].append(_e) # Compare attribute contents crnt_attributes = dict( filter(lambda x: x[1] is not None, current_obj.get_attribute_values()['value'].items())) cche_attributes = dict( filter(lambda x: x[1] is not None, cache_obj.get_attribute_values()['value'].items())) all_attributes = [] for _k, _v in crnt_attributes.items(): if _k in cche_attributes: if _v != cche_attributes[_k]: delta['attributes']['changed'][_k] = _v all_attributes.append(_k) else: delta['attributes']['added'][_k] = _v all_attributes.append(_k) for _k, _v in cche_attributes.items(): # Don't add the individual attributes of extensions that are removed anyway if current_obj.get_extension_off_attribute( _k) in delta['extensions']['removed']: continue if not _k in crnt_attributes: delta['attributes']['removed'].append(_k) all_attributes.append(_k) # Find blocking dependencies between attributes details = current_obj.get_attributes(detail=True) for attribute_name in all_attributes: delta['attributes']['blocked_by'][attribute_name] = list( map(lambda x: x['name'], details[attribute_name]['blocked_by'])) return delta