def delete_node(cls, team_id, label, node, detach=False): team = TeamController._get({"Team": {"id": team_id}}) neo = Neo4jClient() query = 'MATCH(n:{topic}_{label}{{name: "{name}"}})'.format( topic=team.kafka_topic, label=label, name=node) # First we check if the node has dependencies has_deps = query + "-[r:DEPENDS_ON]->() RETURN count(r)" count = neo.query(has_deps, returns=(int, ))[0][0] # return is weird : [[100]] if count and not detach: msg = ( "Can not delete node {node} because it still has {count} relationships" ) raise RequirementsNotSatisfiedError( msg.format(node=node, count=count)) # We can delete the node and its relationships query += " DETACH DELETE n" try: # Neo4j returns nothing when deleting a node neo.query(query) except TransactionException as e: raise RequirementsNotSatisfiedError(str(e)) return {}
def before_delete(cls, obj): # Does the rule still have checks if obj.checks: msg = "Rule {0} contains {1} check{2}, please remove it before.".format( obj.name, len(obj.checks), "s" if len(obj.checks) > 1 else "" ) raise RequirementsNotSatisfiedError(msg) # Does the rule is used in the configuration from ..controllers.configs import ConfigController try: config = ConfigController.get_current_config(team_id=obj.team.id) # There is no config yet except NotFoundError: return labels_queries = [c["qos"] for c in config["data"].values()] for query in labels_queries: if query.startswith("rule"): rule = query.split(".")[1] if obj.name in rule: msg = ( "Rule {0} is used in your configuration, " "please remove it before.".format(obj.name) ) raise RequirementsNotSatisfiedError(msg)
def update(cls, data, filters): rule_name = cls.get(filters=filters)["name"] # Does the rule is used in the configuration from ..controllers.configs import ConfigController try: config = ConfigController.get_current_config( team_id=filters["Rule"]["team_id"] ) # There is no config yet except NotFoundError: return super(RuleController, cls).update(data, filters) labels_queries = [c["qos"] for c in config["data"].values()] for query in labels_queries: if query.startswith("rule"): rule = query.split(".")[1] if rule_name in rule: msg = ( "Rule {0} is used in your configuration, " "please remove it before.".format(rule_name) ) raise RequirementsNotSatisfiedError(msg) return super(RuleController, cls).update(data, filters)
def before_delete(cls, obj): if obj.checks: msg = "Source {0} contains {1} check{2}, please remove it before.".format( obj.name, len(obj.checks), "s" if len(obj.checks) > 1 else "" ) raise RequirementsNotSatisfiedError(msg)
def validate_date(date_text): try: datetime.datetime.strptime(date_text, "%Y-%m-%d") except ValueError: raise RequirementsNotSatisfiedError( "Invalid date format (expected: YYYY-MM-DD)")