Exemple #1
0
    def get_metas(service, app_names=[], namespace_names=[]):
        """
        Get the metas for multiple app and multiple namespaces
        :app_names: a list of app names
        :namespace_names: a list of namespace names, try to find all these namespace metas in each app context
        :return: a dict, keys are app_names, values are dicts whose keys are namespaces
        {
            app1:
            {
                last_modify_time: 1234,
                wizard_step: step1,
                basic_builder: {...},
                cim_builder: {...},
            },
            app2:
            {
                last_modify_time: 1234,
                wizard_step: step2,
                basic_builder: {...},
                cim_builder: {...},
            }
         }
        """
        if not isinstance(app_names, list):
            raise ValueError("app_names is not a list.")
        if not isinstance(namespace_names, list):
            raise ValueError("namespace_names is not a list.")

        builder_meta = meta_client.MetaClient(service, TA_META_COLLECTION_NAME)
        tabuilder_all_projects = builder_meta.get_state()
        # handle the app_names is empty
        if tabuilder_all_projects and len(app_names) == 0:
            app_names = list(tabuilder_all_projects.keys())
        project_meta_map = {}
        for app in app_names:
            if builder_meta.get_state(app) is None:
                _logger.warning('App %s is not found in builder meta.', app)
                continue
            project_meta_map[app] = meta_client.MetaClient(service, app)

        _logger.info("Get multiple metas from namespace %s, apps %s",
                     namespace_names, app_names)

        return_metas = {}
        for app in list(project_meta_map.keys()):
            app_meta_collection = project_meta_map[app]
            app_meta = {}
            if len(namespace_names) == 0:
                app_meta = app_meta_collection.get_state()
            else:
                for ns in namespace_names:
                    ns_meta = app_meta_collection.get_state(ns)
                    if ns_meta is not None:
                        app_meta[ns] = ns_meta
            if len(app_meta) > 0:
                app_meta.update(tabuilder_all_projects[app])
                return_metas[app] = app_meta
        # _logger.debug('get metas:%s', return_metas)
        return return_metas
Exemple #2
0
    def delete_all_project_metas(service):
        builder_meta = meta_client.MetaClient(service, TA_META_COLLECTION_NAME)
        all_projects = builder_meta.get_state()
        for app_name in list(all_projects.keys()):
            project_meta_store = meta_client.MetaClient(service, app_name)
            project_meta_store.delete_state()
            _logger.debug('Delete the project meta. app:%s', app_name)

        builder_meta.delete_state()
        _logger.debug('Delete tabuilder meta for all project.')
Exemple #3
0
    def rename_app(service, old_name, new_name):
        """
        Static method to rename app in all namespaces
        :service: an splunk service object
        :old_name: old app name to rename
        :new_name: new app name after rename

        :return: True - rename success.
                 False - rename failure.
        """
        builder_meta = meta_client.MetaClient(service, TA_META_COLLECTION_NAME)
        old_project_meta = meta_client.MetaClient(service, old_name)
        new_project_meta = meta_client.MetaClient(service, new_name)
        if builder_meta.get_state(new_name):
            _logger.error(
                'Can not rename to app name {0}, the target app already exists.'
                .format(new_name))
            return False

        _logger.info("begin to rename add-on from %s to %s", old_name,
                     new_name)
        old_meta = old_project_meta.get_state()
        if old_meta is None:
            _logger.error(
                "Rename add-on fails. Can not find any meta for add-on %s",
                old_name)
            return False

        try:
            for k, v in list(old_meta.items()):
                new_project_meta.update_state(k, v)
            old_project_meta.delete_state()

            old_builder_meta = builder_meta.get_state(old_name)
            builder_meta.update_state(new_name, old_builder_meta)
            builder_meta.delete_state(old_name)
            _logger.debug("update meta %s from app<%s> to app<%s>",
                          logger.hide_sensitive_field(old_meta), old_name,
                          new_name)
        except Exception as e:
            _logger.error("fail to rename app meta. %s",
                          traceback.format_exc())
            return False

        return True
Exemple #4
0
 def delete_app(service, app_name):
     """
     Static method to delete app in all namespaces
     :service: splunk service object
     :app_name: app name to delete
     :return: True -- delete success
              False -- delete failure
     """
     builder_meta = meta_client.MetaClient(service, TA_META_COLLECTION_NAME)
     project_meta = meta_client.MetaClient(service, app_name)
     try:
         project_meta.delete_state()
         builder_meta.delete_state(app_name)
         _logger.debug("Delete app <%s> meta.", app_name)
     except Exception as e:
         _logger.error("Fail to delete app '%s' meta. %s", app_name,
                       traceback.format_exc())
         return False
     return True
Exemple #5
0
 def is_app_created_by_aob(service, app):
     '''
     If an TA does not exist or not created by AoB. return False
     An TA might be edited by AoB, but it is not created by Aob.
     For example, adding a modular alert to an existing TA.
     '''
     builder_meta = meta_client.MetaClient(service, TA_META_COLLECTION_NAME)
     m = builder_meta.get_state(app)
     if m:
         project_meta = meta_client.MetaClient(service, app)
         basic_meta = project_meta.get_state(BASIC_BUILDER)
         if basic_meta:
             return True
         else:
             _logger.debug("No basic meta for app:%s", app)
             return False
     else:
         _logger.debug("No builder meta for app:%s", app)
         return False
Exemple #6
0
    def load_app_all_meta(service, app, meta, overwrite=False):
        """
        load the meta for the app. If loading fails, exception is thrown
        This method checks if the app already exists in the meta store
        """
        builder_meta = meta_client.MetaClient(service, TA_META_COLLECTION_NAME)
        project_meta = meta_client.MetaClient(service, app)
        app_meta = builder_meta.get_state(app)
        if app_meta:
            if overwrite:
                project_meta.delete_state()
                builder_meta.delete_state(app)
            else:
                raise CommonException(e_message='app {} exists'.format(app),
                                      err_code=36,
                                      options={'app': app})
        for k, v in list(meta.items()):
            project_meta.update_state(k, v)

        app_meta = {LAST_MODIFY_TIME_KEY: MetaManager.get_current_time()}
        builder_meta.update_state(app, app_meta)
    def __init__(self, splunk_endpoint, splunk_session_key, service=None):
        self.splunk_uri = splunk_endpoint
        self.session_key = splunk_session_key

        if service:
            self.service = service
        else:
            self.service = common_util.create_splunk_service(
                splunk_session_key, splunk_endpoint)

        self.mgr = meta_client.MetaClient(self.service,
                                          meta_const.EVENT_COLLECTION_NAME)
Exemple #8
0
 def get_app_all_meta(service, app):
     """
     Get the meta of app project, including all the namespaces
     :app: the app name
     :return: an dict which contains the meta for app
     {
         basic_builder: {...},
         cim_builder: {...},
     }
     """
     meta_cli = meta_client.MetaClient(service, app)
     m = meta_cli.get_state()
     if not m:
         m = None
     return m
Exemple #9
0
 def _get_project_meta_state_store(self):
     return meta_client.MetaClient(self.service, self.app_name)
Exemple #10
0
 def _get_ta_builder_meta_state_store(self):
     return meta_client.MetaClient(self.service, TA_META_COLLECTION_NAME)