def save(obj, parser): if parser == None: parser = obj._parser try: ruleTable = DB_SESSION.query(PolicyRuleTableModel).filter(PolicyRuleTableModel.name == obj.name).first() ruleTable.type = obj._policy ruleTable.defaultParser = obj._parser ruleTable.defaultPersistence = obj._persistenceBackend ruleTable.defaultPersistenceFlag = obj._persist DB_SESSION.add(ruleTable) DB_SESSION.commit() DB_SESSION.expunge(ruleTable) except: ruleTable = PolicyRuleTableModel(name = obj.name, uuid = obj.uuid, type = obj._policy, defaultParser = parser, defaultPersistence = obj._persistenceBackend, defaultPersistenceFlag = obj._persist) DB_SESSION.add(ruleTable) DB_SESSION.commit() DB_SESSION.expunge(ruleTable) #flag to know if save comes from addRule or removeRule() addRule = 0 uuidInRuleSet=list() for rule in obj._ruleSet: #need to removeRule later uuidInRuleSet.append(rule.rule.getUUID()) #lbergesio: Rule is saved parsed, as a string #Possibly if Rule exists (try) it is not required to update its fields.==> #==> IT IS NEEDED FOR THE RuleTable.moveRule() try: ruleModel = DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleUUID == rule.rule.getUUID()).first() except: # If #Rules == #Models then it is the PolicyRuleTableModel being # deleted, not the PolicyRuleModel (last is the normal case) if len(obj._ruleSet) != DB_SESSION.query(PolicyRuleModel).all().count(): addRule = 1 ruleModel = DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleUUID == rule.rule.getUUID()).first() ruleModel.RuleTableName = obj.name ruleModel.Rule = ParseEngine.craftRule(rule.rule, obj._parser) ruleModel.RuleIsEnabled = rule.enabled ruleModel.RulePosition = obj._ruleSet.index(rule) DB_SESSION.add(ruleModel) DB_SESSION.commit() DB_SESSION.expunge_all() #if the save() comes from a removeRule, check which one was removed and delete if not addRule: for ruleModel in DB_SESSION.query(PolicyRuleModel).filter(PolicyRuleModel.RuleTableName == obj.name).all(): if ruleModel.RuleUUID not in uuidInRuleSet: DB_SESSION.delete(ruleModel) DB_SESSION.commit() DB_SESSION.expunge_all() break DB_SESSION.expunge_all()
def delete(tableID): SQLAlchemy.logger.debug('Deleting RuleTable with id = %s...' % tableID) try: table = DB_SESSION.query(PolicyRuleTableModel).filter(PolicyRuleTableModel.id == tableID).first() # If this is the latest PolicyRuleTable, delete its associated PolicyRule's if DB_SESSION.query(PolicyRuleTableModel).filter(PolicyRuleTableModel.name==table.name).count() == 1: PolicyRuleModel.objects.filter(RuleTableName=table.name).delete() DB_SESSION.delete(table) DB_SESSION.commit() DB_SESSION.expunge_all() except Exception as e: SQLAlchemy.logger.warning('[SQLAlchemy Driver] Could not delete the PolicyRuleTable object with ID: %s. Exception: %s' % (tableID,str(e)))