You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
5.1 KiB
113 lines
5.1 KiB
# Copyright (C) 2005-2025 Splunk Inc. All Rights Reserved.
|
|
|
|
import json
|
|
from ITOA import itoa_common as utils
|
|
from SA_ITOA_app_common.apiiconcollection.iconcollection import IconService
|
|
|
|
from .base_migration_interface import BaseMigrationInterface
|
|
|
|
|
|
class IconCollectionMigrationInterface(BaseMigrationInterface):
|
|
"""
|
|
Interface to access Icon Collection
|
|
"""
|
|
|
|
def _iterator_from_kvstore(self, object_type):
|
|
"""
|
|
Helper method to obtain content from kvstore.
|
|
This method is specific to glass_table_images objects.
|
|
@type mi_obj: object type
|
|
@param mi_obj: actual object based on the object_type
|
|
@type limit: int
|
|
@param limit: batch limit to pull from kvstore
|
|
"""
|
|
results = []
|
|
icon_service = IconService(app_name='SA-ITOA',
|
|
session_id=self.session_key,
|
|
user_name='nobody',
|
|
collection_name='SA-ITOA_icon_collection')
|
|
data = icon_service.get_all({})
|
|
try:
|
|
results = json.loads(data)['result']
|
|
except: # noqa E722
|
|
message = "Failed to convert object contents to JSON format."
|
|
self.logger.error(message)
|
|
raise Exception(message)
|
|
|
|
for result in results:
|
|
yield result
|
|
|
|
def migration_get(self, object_type, limit=100):
|
|
"""
|
|
Method to retrieve object content either from local storage or kvstore
|
|
If this is the first version migration, there is no content from the local
|
|
storage, an attempt will be made to retrieve content from kvstore.
|
|
Any subsequent GET will be from the local storage.
|
|
@type object_type: basestring
|
|
@param object_type: object_type
|
|
@type limit: int
|
|
@param limit: get bulk batch size, default to 100
|
|
@return: iterator, an iterator contains retrieved json objects
|
|
"""
|
|
target_file_list = []
|
|
self.logger.info("Migration helper directory: %s, processing object_type: %s." %
|
|
(self.migration_helper_directory, object_type))
|
|
|
|
if utils.FileManager.is_exists(self.migration_helper_directory):
|
|
target_file_list = self._get_object_file_list(object_type)
|
|
self.logger.info("Retrieving content from local storage: %s." % target_file_list)
|
|
|
|
if target_file_list:
|
|
self.logger.info("Trying to obtain data from the local file system...")
|
|
data = self._iterator_from_filesystem(target_file_list)
|
|
else:
|
|
self.logger.info("Trying to obtain data from KV store...")
|
|
data = self._iterator_from_kvstore(object_type)
|
|
|
|
return data
|
|
|
|
def migration_save_single_object_to_kvstore(self, object_type, validation=True, dupname_tag=None,
|
|
skip_local_failure=False, transaction_id=None):
|
|
"""
|
|
Actual method to save content to the kvstore for a single object.
|
|
The coming data are coming from the local storage.
|
|
@type object_type: basestring
|
|
@param object_type: ITSI object types
|
|
@type validation: boolean
|
|
@param validation: require validation when saving to kvstore
|
|
@type dupname_tag: basestring
|
|
@param dupname_tag: a special tag to the duplicated titles.
|
|
@return: boolean
|
|
"""
|
|
self.logger.info("Saving single object: {} with transaction_id:{}".format(object_type, transaction_id))
|
|
icon_service = IconService(app_name='SA-ITOA',
|
|
session_id=self.session_key,
|
|
user_name='nobody',
|
|
collection_name='SA-ITOA_icon_collection')
|
|
|
|
target_file_list = self._get_object_file_list(object_type)
|
|
for target_file in target_file_list:
|
|
self.logger.info("Retrieving info from: {} with transaction_id:{}".format(target_file, transaction_id))
|
|
data = utils.FileManager.read_data(target_file)
|
|
for icon in data:
|
|
try:
|
|
icon_service.create([icon])
|
|
except Exception:
|
|
self.logger.info("Icon with key {} already found in collection. tranaction_id:{}".format(icon['_key'], transaction_id))
|
|
continue
|
|
self.logger.info("Successfully added {} glass table icons with transaction_id:{}".format(str(len(data)), transaction_id))
|
|
|
|
def migration_delete_kvstore(self, object_type):
|
|
"""
|
|
Actual method to delete content from the kvstore for the object.
|
|
@type object_type: basestring
|
|
@param object_type: ITSI object types
|
|
@return: boolean
|
|
"""
|
|
self.logger.info("Deleting existing object_type: %s.", object_type)
|
|
icon_service = IconService(app_name='SA-ITOA',
|
|
session_id=self.session_key,
|
|
user_name='nobody',
|
|
collection_name='SA-ITOA_icon_collection')
|
|
icon_service.bulk_delete_category('*')
|