Ejemplo n.º 1
0
    def delete_entity(self, id_):
        """Delete an entity identified by id_ from the context.

        If needed, the fields impact/depends of the related entity will be
        updated.

        If the entity does not exist exist in database, a ValueError will be
        raise.

        Other exception maybe raised, see __update_dependancies.

        :param id_: the id of the entity to delete.
        """

        try:
            entity = list(self.ent_storage.get_elements(
                query={'_id': id_}))[0]
        except IndexError:
            desc = "No entity found for the following id : {0}".format(id_)
            raise ValueError(desc)

        # update depends/impact links
        status = {"deletions": entity["depends"], "insertions": []}
        updated_entities = self.__update_dependancies(id_, status, "depends")
        self.ent_storage.put_elements(updated_entities)

        # update impact/depends links
        status = {"deletions": entity["impact"], "insertions": []}
        updated_entities = self.__update_dependancies(id_, status, "impact")
        self.ent_storage.put_elements(updated_entities)

        self.ent_storage.remove_elements(ids=[id_])

        build_all_links(self)
Ejemplo n.º 2
0
    def delete_entity(self, id_):
        """Delete an entity identified by id_ from the context.

        If needed, the fields impact/depends of the related entity will be
        updated.

        If the entity does not exist exist in database, a ValueError will be
        raise.

        Other exception maybe raised, see __update_dependancies.

        :param id_: the id of the entity to delete.
        """

        try:
            entity = list(self.ent_storage.get_elements(query={'_id': id_}))[0]
        except IndexError:
            desc = "No entity found for the following id : {0}".format(id_)
            raise ValueError(desc)

        # update depends/impact links
        status = {"deletions": entity["depends"], "insertions": []}
        updated_entities = self.__update_dependancies(id_, status, "depends")
        self.ent_storage.put_elements(updated_entities)

        # update impact/depends links
        status = {"deletions": entity["impact"], "insertions": []}
        updated_entities = self.__update_dependancies(id_, status, "impact")
        self.ent_storage.put_elements(updated_entities)

        self.ent_storage.remove_elements(ids=[id_])

        build_all_links(self)
Ejemplo n.º 3
0
    def compute_watchers_links():
        """
        Force compute of all watchers, once per 10s

        :rtype: bool
        """
        ws.logger.info('Force compute of watcher links')
        build_all_links(watcher.context_graph)

        return gen_json(True)
Ejemplo n.º 4
0
    def update_entity(self, entity):
        """Update an entity identified by id_ with the given entity.
        If needed, the fields impact/depends of the related entity will be
        updated.

        If the entity does not exist exist in database, a ValueError will be
        raise.

        Other exception maybe raised, see __update_dependancies.

        :param entity: the entity updated
        """

        def compare_change(old, new):
            """Retourn a dict with the insertion and the deletion."""

            s_old = set(list(old))
            s_new = set(list(new))
            deletions = s_old.difference(s_new)
            insertions = s_new.difference(s_old)

            return {
                "deletions": list(deletions),
                "insertions": list(insertions)
            }

        try:
            old_entity = self.get_entities_by_id(entity["_id"])[0]
        except IndexError:
            raise ValueError(
                "The _id {0} does not match any entity in database.".format(
                    entity["_id"]))

        # check if the old entity differ from the updated one
        if self.is_equals(old_entity, entity):
            # nothing to do.
            return

        # update depends/impact links
        status = compare_change(old_entity["depends"], entity["depends"])
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "depends")
        self.ent_storage.put_elements(updated_entities)

        # update impact/depends links
        status = compare_change(old_entity["impact"], entity["impact"])
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "impact")

        updated_entities.append(entity)
        self.ent_storage.put_elements(updated_entities)

        # rebuild selectors links
        if entity['type'] != 'watcher':
            build_all_links(self)
Ejemplo n.º 5
0
    def update_entity(self, entity):
        """Update an entity identified by id_ with the given entity.
        If needed, the fields impact/depends of the related entity will be
        updated.

        If the entity does not exist exist in database, a ValueError will be
        raise.

        Other exception maybe raised, see __update_dependancies.

        :param entity: the entity updated
        """
        def compare_change(old, new):
            """Retourn a dict with the insertion and the deletion."""

            s_old = set(list(old))
            s_new = set(list(new))
            deletions = s_old.difference(s_new)
            insertions = s_new.difference(s_old)

            return {
                "deletions": list(deletions),
                "insertions": list(insertions)
            }

        try:
            old_entity = self.get_entities_by_id(entity["_id"])[0]
        except IndexError:
            raise ValueError(
                "The _id {0} does not match any entity in database.".format(
                    entity["_id"]))

        # check if the old entity differ from the updated one
        if self.is_equals(old_entity, entity):
            # nothing to do.
            return

        # update depends/impact links
        status = compare_change(old_entity["depends"], entity["depends"])
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "depends")
        self.ent_storage.put_elements(updated_entities)

        # update impact/depends links
        status = compare_change(old_entity["impact"], entity["impact"])
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "impact")

        updated_entities.append(entity)
        self.ent_storage.put_elements(updated_entities)

        # rebuild selectors links
        if entity['type'] != 'watcher':
            build_all_links(self)
Ejemplo n.º 6
0
    def create_entity(self, entity):
        """Create an entity in the context with the given entity. This will
        update the depends and impact links between entities. If they are
        one or several id in the fields depends and/or impact, this function
        will update every related entity by adding the entity id in the correct
        field.

        If the entity contains one or several unknowned entity id, a ValueError
        will be same id as

        If an entity is already store with same id as than entity, a ValueError
        will be raised with a description.

        If an entity does not have a field infos.state, it will enable
        the entity with the current timestamp. This will create two fields
        infos.state and a infos.enable_history. Infos.state will store the
        current state of the entity (enable) and infos.enable_history will
        store the enable time.

        Other exception maybe raised, see __update_dependancies.

        :param entity: the new entity.
        """

        # TODO add treatment to check if every required field are present
        self._enable_entity(entity)

        entities = list(
            self.ent_storage.get_elements(query={'_id': entity["_id"]}))

        if len(entities) > 0:
            desc = "An entity id {0} already exist".format(entities[0]["_id"])
            raise ValueError(desc)

        # update depends/impact links
        status = {"insertions": entity["depends"], "deletions": []}
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "depends")
        self.ent_storage.put_elements(updated_entities)

        # update impact/depends links
        status = {"insertions": entity["impact"], "deletions": []}
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "impact")
        updated_entities.append(entity)

        for entity in updated_entities:
            self.filter_.filter(entity["infos"])

        self.ent_storage.put_elements(updated_entities)

        # rebuild selectors links
        if entity['type'] != 'watcher':
            build_all_links(self)
Ejemplo n.º 7
0
    def create_entity(self, entity):
        """Create an entity in the context with the given entity. This will
        update the depends and impact links between entities. If they are
        one or several id in the fields depends and/or impact, this function
        will update every related entity by adding the entity id in the correct
        field.

        If the entity contains one or several unknowned entity id, a ValueError
        will be same id as

        If an entity is already store with same id as than entity, a ValueError
        will be raised with a description.

        If an entity does not have a field infos.state, it will enable
        the entity with the current timestamp. This will create two fields
        infos.state and a infos.enable_history. Infos.state will store the
        current state of the entity (enable) and infos.enable_history will
        store the enable time.

        Other exception maybe raised, see __update_dependancies.

        :param entity: the new entity.
        """

        # TODO add treatment to check if every required field are present
        self._enable_entity(entity)

        entities = list(self.ent_storage.get_elements(
            query={'_id': entity["_id"]}))

        if len(entities) > 0:
            desc = "An entity id {0} already exist".format(entities[0]["_id"])
            raise ValueError(desc)

        # update depends/impact links
        status = {"insertions": entity["depends"], "deletions": []}
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "depends")
        self.ent_storage.put_elements(updated_entities)

        # update impact/depends links
        status = {"insertions": entity["impact"], "deletions": []}
        updated_entities = self.__update_dependancies(entity["_id"], status,
                                                      "impact")
        updated_entities.append(entity)

        for entity in updated_entities:
            self.filter_.filter(entity["infos"])

        self.ent_storage.put_elements(updated_entities)

        # rebuild selectors links
        if entity['type'] != 'watcher':
            build_all_links(self)
Ejemplo n.º 8
0
    def _put_entities(self, entities):
        """
        Store entities into database. Do no use this function unless you know
        exactly what you are doing. This function does not update the
        impact and depends links between entities. If you want these feature,
        use create_entity. Nor add/update the the enable, enable_history or
        disable_history.

        :param entities: the entities to store in database
        """
        if not isinstance(entities, list):
            entities = [entities]

        for entity in entities:
            self.filter_.filter(entity["infos"])

        self.ent_storage.put_elements(entities)

        # rebuild selectors links
        build_all_links(self)
Ejemplo n.º 9
0
    def _put_entities(self, entities):
        """
        Store entities into database. Do no use this function unless you know
        exactly what you are doing. This function does not update the
        impact and depends links between entities. If you want these feature,
        use create_entity. Nor add/update the the enable, enable_history or
        disable_history.

        :param entities: the entities to store in database
        """
        if not isinstance(entities, list):
            entities = [entities]

        for entity in entities:
            self.filter_.filter(entity["infos"])

        self.ent_storage.put_elements(entities)

        # rebuild selectors links
        build_all_links(self)