# Copyright (C) 2005-2024 Splunk Inc. All Rights Reserved. from ITOA.setup_logging import getLogger import json import splunk.rest as rest import http.client from .cloud_config_suite_retriever import CloudConfigSuiteRetriever from .splunk_config_suite_retriever import SplunkLicenseSuiteRetriever from .suite_content import SuiteContent class LicenseRetriever(object): SERVER_INFO_ENDPOINT = '/services/server/info' def __init__(self, session_key): self.logger = getLogger(logger_name='itsi.feature_flagging.LicenseRetriever') self.session_key = session_key def get_features_and_suite(self): """ Returns features and whether they are enabled or not, and the suite based on current state object. @type: object @param self: the self reference @rtype: (dict, str) @return: (dict of this form: {feature_name1: , feature_name2: ,...,feature_nameN: enabled}, suite_name ) """ suite = self.get_suite() features = self.get_features(suite) return features, suite def get_suite(self): """ Returns the suite type based on current state object. @rtype: string @return: suite """ return self._get_suite_retriever().get_suite() def sync_suite_state(self): """ Synchronizes current suitification state """ self.logger.info("Synchronizing suite state... ") self._get_suite_retriever().sync_suite_state() self.logger.info("Synchronizing suite state completed") def _get_suite_retriever(self): if self._is_cloud(): self.logger.info("Detected Cloud environment") retriever = CloudConfigSuiteRetriever(self.session_key) else: self.logger.info("Detected non-Cloud environment") retriever = SplunkLicenseSuiteRetriever(self.session_key) return retriever def get_features(self, suite): """ Returns the suite features based on current state object. @rtype: dict @return: (dict of this form: {feature_name1: , feature_name2: ,...,feature_nameN: enabled} ) """ return SuiteContent.get_features_for_suite(suite) def _is_cloud(self): """ Check if running on the Cloud stack @type: object @param self: the self reference @rtype: boolean @return: True - in the Cloud, otherwise False """ instance_type = self._get_server_info()['entry'][0]['content'].get('instance_type') if instance_type is not None: return instance_type == 'cloud' return False def _get_server_info(self): response, contents = rest.simpleRequest( path=self.SERVER_INFO_ENDPOINT, getargs={'output_mode': 'json'}, sessionKey=self.session_key) if response.status != http.client.OK: e = Exception('Failed to get server information. Response={} Contents={}'.format(response, contents)) self.logger.exception(e) raise e return json.loads(contents)