def validate(self, data):
        model_set = set([t[0] for t in registry.path(data['model'])])
        model_set.add(data['model'])

        for f in data.get('filters', []):
            # Make sure filter model is in path of search model
            if f['model'] not in model_set:
                raise ValidationError('Model {} not in path of {}'.format(
                    f['model'], data['model']))

            # Make sure filter property is property of filter model
            if f['prop'] not in registry.properties(f['model']):
                raise ValidationError(
                    'Model {} does not have property {}'.format(
                        f['model'], f['prop']))

        for o in data.get('orders', []):
            # Make sure order model is in path of search model
            if o['model'] not in model_set:
                raise ValidationError('Model {} not in path of {}'.format(
                    o['model'], data['model']))

            # Make sure order property is property of order model
            if o['prop'] not in registry.properties(o['model']):
                raise ValidationError(
                    'Model {} does not have property {}'.format(
                        o['model'], o['prop']))

        return data
Beispiel #2
0
 def list(self, request):
     """List all paths to each model."""
     paths = {}
     for label, model in registry.models.items():
         paths[label] = [l for l, _ in registry.path(label)]
     serializer = ModelSerializer(paths)
     return Response(serializer.data)
Beispiel #3
0
    def __init__(self, path, identity, times, pagesize=5000):
        """Init the diff query

        :param path: Path to build a diff query around
        :type path: list
        :param identity: Starting node's identity
        :type identity: str
        :param times: Two times for comparison
        :type times: tuple
        :param pagesize: How many results to fetch at once
        :type pagesize: int
        """
        self.path = path
        self.end = self.path[-1]
        self.full_path = registry.path(self.end)
        self.t1 = times[0]
        self.t2 = times[1]
        self.identity = identity
        self.pagesize = pagesize

        self.selects = [
            '{}.{}'.format(self._var_from_label(m),
                           registry.identity_property(m)) for m in self.path
        ]

        self.params = {'t1': self.t1, 't2': self.t2, 'identity': self.identity}
Beispiel #4
0
    def start_path(self):
        """Create matching path of query."""
        # Find path to model
        datapath = registry.path(self.label) or []

        self.matches = []
        self.rels = []
        self.state_matches = []

        for i, pathtuple in enumerate(datapath):
            label, relname = pathtuple
            self.matches.append(
                (label.lower, label, '({}:{})'.format(label.lower(), label)))
            self.rels.append(
                ('r{}'.format(i), relname, '-[r{}:{}]->'.format(i, relname)))

            if registry.state_properties(label):
                self.state_matches.append(label)

            self.addreturn(label)

        self.matches.append((self.label.lower(), self.label,
                             '({}:{})'.format(self.label.lower(), self.label)))
        if registry.state_properties(self.label):
            self.state_matches.append(self.label)

        self.return_labels.append(self.label)
        return self
Beispiel #5
0
    def add_column(self, model, prop, name=None):
        """Add a column to return.

        Verify model is in path and prop belongs to model.
        Add a column tuple. These will be used in the return clause.

        :param model: Name of the model
        :type model: str
        :param prop: Name of the property belonging to the model.
        :type prop: str
        :param name: Optional name of the column
            instead of model.prop, the column name can be custom.
            RETURN model.prop vs RETURN model.prop AS custom
        :type name: str
        """
        datapath = [p[0] for p in (registry.path(self.label) or [])]
        datapath += [self.label]

        if model not in datapath and model != self.label:
            raise InvalidLabelError(model)

        if prop not in registry.properties(model):
            raise InvalidPropertyError(prop, model)

        key = '{}.{}'.format(model, prop)

        if prop in registry.state_properties(model):
            model = _model_state(model)

        if name is not None:
            key = name

        self._columns[key] = '{}.{}'.format(model.lower(), prop)
        return self
Beispiel #6
0
    def __init__(self, model, identity, t1, t2):
        """Init the NodeDiff

        :param model: Type of the node
        :type model: str
        :param identity: Identity of the node
        :type identity: str
        :param t1: A time in milliseconds
        :type t1: int
        :param t2: A time in milliseconds
        :type t2: int
        """
        self.model = model
        self.t1 = t1
        self.t2 = t2
        self.full_path = registry.path(self.model)
        self.params = {'identity': identity}

        self.node_t1 = self.node_at_time(self.t1)
        self.node_t2 = self.node_at_time(self.t2)
Beispiel #7
0
    def validate(self, data):
        """Custom validation.

        Ensure that all columns are properties to models in the path
        to the chosen model.

        Ensure that all filters are on properties to models in the path
        to the chosen model.

        :param data: Data to validate
        :type data: dict
        """
        model_set = set([t[0] for t in registry.path(data['model'])])
        model_set.add(data['model'])

        column_errors = OrderedDict()
        for i, c in enumerate(data.get('columns', [])):
            if c['model'] not in model_set:
                column_errors[i] = (
                    'Model {} is not in path of {}'
                    .format(c['model'], data['model'])
                )
        if column_errors:
            raise ValidationError({'columns': column_errors})

        filter_errors = OrderedDict()
        for i, f in enumerate(data.get('filters', [])):
            if f['model'] not in model_set:
                filter_errors[i] = (
                    'Model {} not in path of {}'
                    .format(f['model'], data['model'])
                )

        if filter_errors:
            raise ValidationError({'filters': filter_errors})

        return data
def remove(uuid, skip=False):
    """Remove all data associated with an environment."""
    driver = GraphDatabase.driver(settings.NEO4J_URI,
                                  auth=(settings.NEO4J_USERNAME,
                                        settings.NEO4J_PASSWORD))
    with driver.session() as session:
        # Attempt to locate environment by account number and name
        env = find_environment(session, uuid)

        # If found, confirm deletion
        msg = 'Confirm deleting of environment with uuid \'{}\''.format(uuid)
        confirmed = confirm_env_action(msg, skip=skip)
        if not confirmed:
            logger.info("Removal unconfirmed...cancelling.")
            exit(0)

        # Acquire lock and delete
        with lock_environment(driver, env):

            logger.debug("Acquired the lock.!!!")
            # get all paths
            paths = []
            for label, model in registry.models.items():
                path = [l for l, r in registry.path(label)]
                path.append(label)
                paths.append(path)

            paths.sort(key=lambda x: len(x))
            logger.debug(pprint.pformat(paths))

            stats = {}
            for path in reversed(paths):
                prune(session, env, path, stats)

            logger.info("Deleted node counts by type:\n{}".format(
                pprint.pformat(stats)))
Beispiel #9
0
    def addreturn(self, label):
        """Optionally return additional labels in current label's path.

        :param label: Label in parent path
        :type label: str
        :returns: Modified self
        :rtype: Query
        """

        model = registry.models.get(label)

        # Ensure model exists
        if model is None:
            raise InvalidLabelError(label)

        # Ensure model is in parent path
        datapath = registry.path(self.label)
        if label not in ([p[0] for p in datapath]) and label != self.label:
            raise InvalidLabelError(label)

        # Add tuple to return list
        if label not in self.return_labels:
            self.return_labels.append(label)
        return self
Beispiel #10
0
            'device': (Property('Device', 'name_host'), )
        }),
        ('Uservar', {
            'name_environment':
            (Property('Uservar', 'name'), Property('Environment', 'uuid')),
            'environment': (Property('Environment', 'uuid'), )
        }),
        ('Virtualenv', {
            'path_host':
            (Property('Virtualenv',
                      'path'), Property('Host', 'hostname_environment')),
            'host': (Property('Host', 'hostname_environment'), )
        })
    ]

    label_tuples = [(l, p, registry.path(l)) for l, p in label_tuples]
    label_tuples.sort(key=lambda x: len(x[2]))
    queries = [UpdateQuery(*t) for t in label_tuples]

    if args.cipher_only:
        for query in queries:
            print(query)
        for query in cleanup():
            print(query)
        print(add_state())
        exit()

    node_counts = node_count()

    # Perform migration
    for q in queries: