Example #1
0
    def find_file_by_name(folder_path, target_name):
        """Find a file on disk by its name.

        This method locates a file with `target_name` in a `folder_path`. The method
        raises a `NotFoundError` in case the file is not found.

        :param folder_path: the folder where to look for the file
        :param target_name: the name of the target file

        :returns: the file path of the file
        """
        if not Manager.folder_exists(folder_path):
            cause = "Folder %s not found" % folder_path
            logger.error(cause)
            raise NotFoundError(cause=cause)

        files = Manager.get_files(folder_path)
        found = None
        for name in files:
            if target_name == name:
                found = os.path.join(folder_path, target_name)
                break

        if not found:
            cause = "File %s not found in %s" % (target_name, folder_path)
            logger.error(cause)
            raise NotFoundError(cause=cause)

        return found
Example #2
0
    def find_file_by_content_title(folder_path, content_title):
        """Find a file on disk by its content title.

        This method locates a file with a title equal to `content_title` in
        a `folder_path`. The method raises a `NotFoundError` in case the file
        is not found.

        :param folder_path: the folder where to look for the file
        :param content_title: the content title of the target file

        :returns: the file path of the file
        """
        if not Manager.folder_exists(folder_path):
            cause = "Folder %s not found" % folder_path
            logger.error(cause)
            raise NotFoundError(cause=cause)

        files = Manager.get_files(folder_path)
        found = None
        for file_name in files:
            file_path = os.path.join(folder_path, file_name)
            content = load_json(file_path)
            if content['attributes']['title'] == content_title:
                found = file_path
                break

        if not found:
            cause = "File with content title %s not found in %s" % (
                content_title, folder_path)
            logger.error(cause)
            raise NotFoundError(cause=cause)

        return found
Example #3
0
    def export_by_title(self, obj_type, obj_title):
        """Export an object identified by its title.

        This method returns an object saved in Kibana based on its type and title.

        An `ObjectTypeError` is thrown if the type of the object is not one of
        the following: dashboard, index pattern, search, visualization.

        A `NotFoundError` is thrown if the object is not found in the Kibana instance.

        :param obj_type: type of the target object
        :param obj_title: title of the target object

        :returns the target Kibana object
        """
        if obj_type not in [DASHBOARD, INDEX_PATTERN, SEARCH, VISUALIZATION]:
            cause = "Unknown type %s" % obj_type
            logger.error(cause)
            raise ObjectTypeError(cause=cause)

        obj = self.find_by_title(obj_type, obj_title)

        if obj_type == DASHBOARD:
            obj_id = obj['id']
            obj = self.dashboard.export_dashboard(obj_id)

        if not obj:
            cause = "Impossible to export %s with title %s, not found" % (
                obj_type, obj_title)
            logger.error(cause)
            raise NotFoundError(cause=cause)

        return obj
Example #4
0
    def find_by_id(self, obj_type, obj_id):
        """Find an object by its type and ID.

        This methods returns a Kibana object based on its type and ID.

        A `NotFoundError` is thrown if the object is not found in the Kibana instance.

        :param obj_type: type of the target object
        :param obj_id: ID of the target object

        :returns the target object or None if not found
        """
        url = urijoin(self.base_url, self.saved_objects.API_SAVED_OBJECTS_URL)
        found_obj = None

        for page_objs in self.saved_objects.fetch_objs(url):
            for obj in page_objs:
                if obj['type'] == obj_type and obj['id'] == obj_id:
                    found_obj = obj
                    break

        if not found_obj:
            cause = "No %s found with ID: %s" % (obj_type, obj_id)
            logger.error(cause)
            raise NotFoundError(cause=cause)

        return found_obj
Example #5
0
    def update(self, old_alias, new_alias):
        """Update the name of an alias with a new one.

        This method replaces the name of `old_alias` with `new_alias`.

        A `NotFoundError` is thrown if the `old_alias` is not found in the registry.
        A `RegistryError` is thrown if the `new_alias` is already in use.

        :param old_alias: target alias
        :param new_alias: new alias
        """
        if old_alias not in self.content:
            cause = "Alias %s not found in registry" % old_alias
            logger.error(cause)
            raise NotFoundError(cause=cause)

        if new_alias in self.content:
            cause = "Alias %s already in use" % new_alias
            logger.error(cause)
            raise RegistryError(cause=cause)

        self.content[new_alias] = self.content.pop(old_alias)

        self.__save_registry(self.content)
        logger.info("Alias %s updated with %s", old_alias, new_alias)
Example #6
0
    def find_by_title(self, obj_type, obj_title):
        """Find an object by its type and title.

        This methods returns a Kibana object based on its type and title.

        A `NotFoundError` is thrown if the object is not found in the Kibana instance.

        :param obj_type: type of the target object
        :param obj_title: title of the target object

        :returns the target object or None if not found
        """
        url = urijoin(self.base_url, self.saved_objects.API_SAVED_OBJECTS_URL)
        found_obj = None

        for page_objs in self.saved_objects.find(obj_type):
            for obj in page_objs:
                if obj['attributes']['title'] == obj_title:
                    found_obj = obj
                    break

        if not found_obj:
            cause = "No %s found with title: %s" % (obj_type, obj_title)
            logger.error(cause)
            raise NotFoundError(cause=cause)

        return found_obj
Example #7
0
    def find(self, alias):
        """Find the meta information of a Kibana object based on its alias.

        This method retrieves from the registry the target alias and
        KibanaObjMeta linked to it.

        :param alias: target alias

        :returns a tuple composed of an alias and metadata
        """
        if alias not in self.content:
            cause = "Alias %s not found in registry" % alias
            logger.error(cause)
            raise NotFoundError(cause=cause)

        meta = KibanaObjMeta.create_from_registry(self.content[alias])
        return alias, meta
Example #8
0
    def delete(self, alias=None):
        """Delete an alias from the registry.

        This method deletes an alias (and its associated KibanaObjMeta) from the registry
        based on a given `alias`.

        A `NotFoundError` is thrown if the alias is not found.

        :param alias: target alias
        """
        if alias not in self.content:
            cause = "Alias %s not found in registry" % alias
            logger.error(cause)
            raise NotFoundError(cause=cause)

        self.content.pop(alias)

        self.__save_registry(self.content)
        logger.info("Alias %s deleted", alias)