def update_specific_rule(self, tenantId, serverId, ruleId, rule): """Updates a specific rule :param str tenantId: The id of the tenant :param str serverId: The id of the server :param str ruleId: The id of the rule :param str rule: The rule description in json format """ rule_db = SpecificRule.objects.get(specificRule_Id__exact=ruleId, tenantId__exact=tenantId, entity__exact=serverId) try: condition = self.getContition(rule) action = self.getAction(rule) name = self.getName(rule) except Exception as err: raise ValueError(str(err) + " is missing") self.checkRule(name, condition, action) rule_db.action = action rule_db.name = name rule_db.condition = condition modifiedAction = self.pimp_rule_action(action, name, serverId) modifiedCondition = self.pimp_rule_condition(condition, name, serverId) rule_db.clips_action = modifiedAction rule_db.clips_condition = modifiedCondition rule_db.save() ruleResult = RuleModel() ruleResult.ruleId = str(ruleId) ruleResult.name = name ruleResult.condition = condition ruleResult.action = action logger.info("RuleId %s was updated" % str(ruleId)) return ruleResult
def get_rule(self, ruleId): """Returns information about a general rule.""" r_query = Rule.objects.get(ruleId__exact=ruleId) rule = RuleModel() rule.ruleId = r_query.__getattribute__("ruleId") rule.name = r_query.__getattribute__("name") rule.condition = r_query.__getattribute__("condition") rule.action = r_query.__getattribute__("action") return rule
def get_specific_rule(self, tenantId, serverId, ruleId): """Returns information about a specific rule. :param str tenantId: The id of the tenant :param str serverId: The id of the server :param str ruleId: The id of the rule """ r_query = SpecificRule.objects.get(specificRule_Id__exact=ruleId, tenantId__exact=tenantId, entity__exact=serverId) rule = RuleModel() rule.ruleId = r_query.__getattribute__("specificRule_Id") rule.name = r_query.__getattribute__("name") rule.condition = r_query.__getattribute__("condition") rule.action = r_query.__getattribute__("action") return rule
def create_specific_rule(self, tenantId, serverId, rule): """Creates a new specific rule for a server :param str tenantId: The id of the tenant :param str serverId: The id of the server :param str rule: The rule description in json format """ try: entity = Entity.objects.get(serverId__exact=serverId) except Entity.DoesNotExist as err: entity = Entity(serverId=serverId, tenantId=tenantId) entity.save() try: condition = self.getContition(rule) action = self.getAction(rule) name = self.getName(rule) except Exception as err: raise ValueError(str(err) + " is missing") self.checkRule(name, condition, action) #Its necesary modify action to get subscriptionId modifiedAction = self.pimp_rule_action(action, name, serverId) modifiedCondition = self.pimp_rule_condition(condition, name, serverId) createdAt = datetime.datetime.now(tz=timezone.get_default_timezone()) ruleId = uuid.uuid1() rule = SpecificRule(specificRule_Id=ruleId, tenantId=tenantId, name=name, condition=condition, action=action, clips_condition=modifiedCondition, clips_action=modifiedAction, createdAt=createdAt) """try: rule = SpecificRule.objects.get(serverId__exact=serverId, ) raise ValueError("rule name already exists") except Entity.DoesNotExist as err: entity = Entity(serverId=serverId, tenantId=tenantId)""" rule.save() entity.specificrules.add(rule) rule.save() ruleResult = RuleModel() ruleResult.ruleId = str(ruleId) logger.info("RuleId %s was created for server %s" % (str(ruleId), serverId)) return ruleResult
def create_general_rule(self, tenantId, rule): """Creates new general rule """ try: condition = self.getContition(rule) action = self.getAction(rule) name = self.getName(rule) except Exception as err: raise ValueError(str(err) + " is missing") self.checkRule(name, condition, action) createdAt = datetime.datetime.now(tz=timezone.get_default_timezone()) ruleId = uuid.uuid1() rule = Rule(ruleId=ruleId, tenantId=tenantId, name=name, condition=condition, action=action, createdAt=createdAt) rule.save() ruleResult = RuleModel() ruleResult.ruleId = str(ruleId) return ruleResult
def update_rule(self, tenantId, ruleId, rule): """Updates a general rule """ rule_db = Rule.objects.get(ruleId__exact=ruleId, tenantId__exact=tenantId) try: condition = self.getContition(rule) action = self.getAction(rule) name = self.getName(rule) except Exception as err: raise ValueError(str(err) + " is missing") self.checkRule(name, condition, action) rule_db.action = action rule_db.name = name rule_db.condition = condition rule_db.save() ruleResult = RuleModel() ruleResult.ruleId = str(ruleId) ruleResult.name = name ruleResult.condition = condition ruleResult.action = action return ruleResult