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.

85 lines
3.1 KiB

# Copyright (C) 2005-2024 Splunk Inc. All Rights Reserved.
import json
import traceback
import http.client
from itsi_py3 import _ # noqa
import itsi_py3 # noqa
import splunk.rest as rest
from ITOA.setup_logging import getLogger
from .suite_content import SuiteContent
class ITSIApplicationRenamer(object):
# REST endpoint docs: https://docs.splunk.com/Documentation/Splunk/8.1.2/RESTREF/RESTapps#apps.2Flocal.2F.7Bname.7D
_MANAGE_ITSI_APP_ENDPOINT = "/services/apps/local/itsi?output_mode=json"
_SUITE_TO_PRODUCT_NAME = {
SuiteContent.least_permissible_suite(): "IT Essentials Work",
SuiteContent.most_permissible_suite(): "IT Service Intelligence"
}
def __init__(self, session_key, suite):
self.logger = getLogger(logger_name="itsi.feature_flagging.ITSIApplicationRenamer")
self.session_key = session_key
self.suite = suite
def rename(self):
"""
Renames ITSI application if needed
@type: object
@param self: the self reference
"""
try:
current_label = self._get_current_label()
target_label = self._SUITE_TO_PRODUCT_NAME[self.suite]
if target_label == current_label:
self.logger.info("ITSI app labels are matching. No need to change.")
return
self.logger.warn("ITSI app labels are mismatched. Expected = {}, Actual = {}".
format(target_label, current_label))
self.logger.info("Will try to set ITSI label to = {}".format(target_label))
self._set_current_label(target_label)
self.logger.info("Verifying current ITSI app label...")
current_label = self._get_current_label()
if current_label != target_label:
e = Exception("Failed to update ITSI app label to {}. Label is still {}".
format(target_label, current_label))
self.logger.exception(e)
raise e
self.logger.info("Successfully verified that current ITSI app label is {}".format(target_label))
except Exception:
self.logger.error("Failed to change ITSI label: {}".format(traceback.format_exc()))
raise
def _get_current_label(self):
response, contents = rest.simpleRequest(path=self._MANAGE_ITSI_APP_ENDPOINT,
sessionKey=self.session_key)
if response.status != http.client.OK:
e = Exception("Failed to get ITSI app label. Response={}".format(response))
self.logger.exception(e)
raise e
return json.loads(contents)["entry"][0]["content"]["label"]
def _set_current_label(self, label):
response, contents = rest.simpleRequest(path=self._MANAGE_ITSI_APP_ENDPOINT,
postargs={"label": label},
sessionKey=self.session_key)
if response.status != http.client.OK:
e = Exception("Failed to update ITSI app label to {}. Response={}".format(label, response))
self.logger.exception(e)
raise e