diff --git a/parameter-sets/basic-auth/parameter-set.json b/parameter-sets/basic-auth/parameter-set.json index 13e76de..c360b6b 100644 --- a/parameter-sets/basic-auth/parameter-set.json +++ b/parameter-sets/basic-auth/parameter-set.json @@ -57,6 +57,18 @@ "credentialRequestSettings": { "type": "BASIC" } + }, + { + "name": "old_domain", + "label": "Old domain", + "type": "STRING", + "description": "(optional)" + }, + { + "name": "new_domain", + "label": "New domain", + "type": "STRING", + "description": "(optional)" } ] } diff --git a/python-lib/osisoft_domain_handling.py b/python-lib/osisoft_domain_handling.py new file mode 100644 index 0000000..1ecb280 --- /dev/null +++ b/python-lib/osisoft_domain_handling.py @@ -0,0 +1,42 @@ +class DomainHandler(): + def __init__(self, config): + credentials = config.get('credentials', {}) + self.old_domain = credentials.get("old_domain") + self.new_domain = credentials.get("new_domain") + + def ui_side_url(self, input): + if not self.old_domain: + return input + if isinstance(input, list): + return swap_in_list(input, self.new_domain, self.old_domain) + input = swap(input, self.new_domain, self.old_domain) + return input + + def client_side_url(self, url): + if not self.old_domain: + return url + url = swap(url, self.old_domain, self.new_domain) + return url + + +def swap_in_list(items, domain_to_replace, domain_to_replace_with): + new_items = [] + for item in items: + value = item.get("value") + label = item.get("label") + value = swap(value, domain_to_replace, domain_to_replace_with) + new_items.append( + { + "value": "{}".format(value), + "label": "{}".format(label) + } + ) + return new_items + + +def swap(url, domain_to_replace, domain_to_replace_with): + if not url: + return url + if domain_to_replace in url: + return url.replace(domain_to_replace, domain_to_replace_with) + return url diff --git a/resource/browse_attributes.py b/resource/browse_attributes.py index cd39fd1..ce08526 100644 --- a/resource/browse_attributes.py +++ b/resource/browse_attributes.py @@ -2,6 +2,7 @@ import dataiku from osisoft_client import OSIsoftClient from osisoft_plugin_common import get_credentials, build_select_choices, check_debug_mode +from osisoft_domain_handling import DomainHandler def do(payload, config, plugin_config, inputs): @@ -11,6 +12,7 @@ def do(payload, config, plugin_config, inputs): return {"choices": [{"label": "Requires DSS v10.0.4 or above. Please use the OSIsoft Search custom dataset instead"}]} elif config.get("credentials") == {}: return {"choices": [{"label": "Pick a credential"}]} + domain_handler = DomainHandler(config) auth_type, username, password, server_url, is_ssl_check_disabled, credential_error = get_credentials(config, can_raise=False) @@ -37,14 +39,20 @@ def do(payload, config, plugin_config, inputs): if parameter_name == "server_name": choices = [] - choices.extend(client.get_asset_servers(can_raise=False)) + choices.extend( + domain_handler.ui_side_url(client.get_asset_servers(can_raise=False)) + ) return build_select_choices(choices) if parameter_name == "database_name": choices = [] next_url = config.get("server_name") if next_url: - choices.extend(client.get_next_choices(next_url, "Self")) + choices.extend( + domain_handler.ui_side_url( + client.get_next_choices(domain_handler.client_side_url(next_url), "Self") + ) + ) return build_select_choices(choices) else: return build_select_choices() @@ -58,7 +66,9 @@ def do(payload, config, plugin_config, inputs): if not next_links: return build_select_choices() next_url = next_links + "/elementcategories" - choices.extend(client.get_next_choices(next_url, "Self", use_name_as_link=True)) + choices.extend( + client.get_next_choices(domain_handler.client_side_url(next_url), "Self", use_name_as_link=True) + ) return build_select_choices(choices) if parameter_name == "element_template": @@ -70,7 +80,7 @@ def do(payload, config, plugin_config, inputs): if not next_links: return build_select_choices() next_url = next_links + "/elementtemplates" - choices.extend(client.get_next_choices(next_url, "Self", use_name_as_link=True, filter={'InstanceType': 'Element'})) + choices.extend(client.get_next_choices(domain_handler.client_side_url(next_url), "Self", use_name_as_link=True, filter={'InstanceType': 'Element'})) choices.append({"label": "✍️ Enter manually", "value": "_DKU_manual_input"}) choices.append({"label": "🗄️ Use a variable", "value": "_DKU_variable_select"}) return build_select_choices(choices) @@ -98,14 +108,16 @@ def do(payload, config, plugin_config, inputs): if not next_links: return build_select_choices() next_url = next_links + "/attributecategories" - choices.extend(client.get_next_choices(next_url, "Self", use_name_as_link=True)) + next_url = domain_handler.ui_side_url(next_url) + choices.extend(client.get_next_choices(domain_handler.client_side_url(next_url), "Self", use_name_as_link=True)) return build_select_choices(choices) if parameter_name == "element_1": choices = [] next_url = config.get("database_name", None) + next_url = domain_handler.client_side_url(next_url) if next_url: - choices.extend(client.get_next_choices_as_json(next_url+"/elements", "Elements")) + choices.extend(domain_handler.ui_side_url(client.get_next_choices_as_json(next_url+"/elements", "Elements"))) return build_select_choices(choices) else: return build_select_choices() @@ -116,8 +128,9 @@ def do(payload, config, plugin_config, inputs): json_string = config.get("element_{}".format(element_number - 1), "{}") json_choice = json.loads(json_string) next_url = json_choice.get("url") + next_url = domain_handler.client_side_url(next_url) if next_url: - choices.extend(client.get_next_choices_as_json(next_url, "Elements")) + choices.extend(domain_handler.ui_side_url(client.get_next_choices_as_json(next_url, "Elements"))) return build_select_choices(choices) else: return build_select_choices() @@ -128,10 +141,14 @@ def do(payload, config, plugin_config, inputs): json_string = json_string or "{}" json_choice = json.loads(json_string) next_url = json_choice.get("url") + next_url = domain_handler.client_side_url(next_url) if next_url: - choices.extend(client.get_next_choices( - next_url.replace("/elements", "/attributes").replace("/{}/attributes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path())), - "Self") + choices.extend( + domain_handler.ui_site_url( + client.get_next_choices( + next_url.replace("/elements", "/attributes").replace("/{}/attributes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path())), + "Self") + ) ) return build_select_choices(choices) else: @@ -143,10 +160,15 @@ def do(payload, config, plugin_config, inputs): json_string = json_string or "{}" json_choice = json.loads(json_string) next_url = json_choice.get("url") + next_url = domain_handler.client_side_url(next_url) if next_url: - choices.extend(client.get_next_choices( - next_url.replace("/elements", "/eventframes").replace("/{}/eventframes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path())), - "Self") + choices.extend( + domain_handler.ui_site_url( + client.get_next_choices( + next_url.replace("/elements", "/eventframes").replace("/{}/eventframes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path())), + "Self" + ) + ) ) return build_select_choices(choices) else: diff --git a/resource/browse_event_frames.py b/resource/browse_event_frames.py index 25723a8..970e54e 100644 --- a/resource/browse_event_frames.py +++ b/resource/browse_event_frames.py @@ -1,5 +1,6 @@ from osisoft_client import OSIsoftClient from osisoft_plugin_common import get_credentials, build_select_choices, build_requests_params +from osisoft_domain_handling import DomainHandler def do(payload, config, plugin_config, inputs): @@ -9,6 +10,7 @@ def do(payload, config, plugin_config, inputs): return build_select_choices("Requires DSS v10.0.4 or above. Please use the OSIsoft Search custom dataset instead") elif config.get("credentials") == {}: return build_select_choices("Pick a credential") + domain_handler = DomainHandler(config) auth_type, username, password, server_url, is_ssl_check_disabled, credential_error = get_credentials(config, can_raise=False) @@ -32,14 +34,19 @@ def do(payload, config, plugin_config, inputs): if parameter_name == "server_name": choices = [] - choices.extend(client.get_asset_servers()) + choices.extend(domain_handler.ui_side_url(client.get_asset_servers())) return build_select_choices(choices) if parameter_name == "database_name": choices = [] next_url = config.get("server_name") + next_url = domain_handler.client_side_url(next_url) if next_url: - choices.extend(client.get_next_choices(next_url, "Self")) + choices.extend( + domain_handler.ui_side_url( + client.get_next_choices(next_url, "Self") + ) + ) return build_select_choices(choices) else: return build_select_choices() @@ -53,7 +60,12 @@ def do(payload, config, plugin_config, inputs): if not next_links: return build_select_choices() next_url = next_links + "/elementcategories" - choices.extend(client.get_next_choices(next_url, "Self", use_name_as_link=True)) + next_url = domain_handler.client_side_url(next_url) + choices.extend( + domain_handler.ui_side_url( + client.get_next_choices(next_url, "Self", use_name_as_link=True) + ) + ) return build_select_choices(choices) if parameter_name == "template_name": @@ -65,7 +77,10 @@ def do(payload, config, plugin_config, inputs): if not next_links: return build_select_choices() next_url = next_links + "/elementtemplates" - next_choices = client.get_next_choices(next_url, "Self", use_name_as_link=True, filter={'InstanceType': 'EventFrame'}) + next_url = domain_handler.client_side_url(next_url) + next_choices = domain_handler.ui_side_url( + client.get_next_choices(next_url, "Self", use_name_as_link=True, filter={'InstanceType': 'EventFrame'}) + ) choices.extend(next_choices) return build_select_choices(choices) @@ -75,13 +90,18 @@ def do(payload, config, plugin_config, inputs): if not next_links: return build_select_choices(choices) next_url = next_links + "/eventframes" + next_url = domain_handler.client_side_url(next_url) params = build_requests_params( **config ) endpoint_name = "Self" if config.get("must_retrieve_metrics"): endpoint_name = config.get("data_type", "Self") - choices.extend(client.get_next_choices(next_url, endpoint_name, params=params)) + choices.extend( + domain_handler.ui_side_url( + client.get_next_choices(next_url, endpoint_name, params=params) + ) + ) return build_select_choices(choices) return build_select_choices() diff --git a/resource/browse_tags.py b/resource/browse_tags.py index ae0f619..f296f5d 100644 --- a/resource/browse_tags.py +++ b/resource/browse_tags.py @@ -1,5 +1,6 @@ from osisoft_client import OSIsoftClient from osisoft_plugin_common import get_credentials, build_select_choices, check_debug_mode +from osisoft_domain_handling import DomainHandler def do(payload, config, plugin_config, inputs): @@ -9,6 +10,7 @@ def do(payload, config, plugin_config, inputs): return {"choices": [{"label": "Requires DSS v10.0.4 or above. Please use the OSIsoft Search custom dataset instead"}]} elif config.get("credentials") == {}: return {"choices": [{"label": "Pick a credential"}]} + domain_handler = DomainHandler(config) auth_type, username, password, server_url, is_ssl_check_disabled, credential_error = get_credentials(config, can_raise=False) @@ -35,7 +37,11 @@ def do(payload, config, plugin_config, inputs): if parameter_name == "data_server_url": choices = [] - choices.extend(client.get_data_servers(can_raise=False)) + choices.extend( + domain_handler.ui_side_url( + client.get_data_servers(can_raise=False) + ) + ) return build_select_choices(choices) return build_select_choices()