From 809234ed635a1cee6b351a6a4df52e79f8875040 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Thu, 22 Sep 2022 13:01:02 -0700 Subject: [PATCH 01/12] adding alerts functionality, update to 40sdk --- looker_deployer/commands/deploy_boards.py | 2 +- .../commands/deploy_connections.py | 2 +- looker_deployer/commands/deploy_content.py | 75 +++++++++++++++++-- .../commands/deploy_content_export.py | 2 +- .../commands/deploy_group_in_group.py | 2 +- looker_deployer/commands/deploy_groups.py | 2 +- looker_deployer/commands/deploy_model_sets.py | 2 +- .../commands/deploy_permission_sets.py | 2 +- looker_deployer/commands/deploy_roles.py | 2 +- .../commands/deploy_user_attributes.py | 2 +- looker_deployer/utils/get_client.py | 14 ++-- 11 files changed, 84 insertions(+), 23 deletions(-) diff --git a/looker_deployer/commands/deploy_boards.py b/looker_deployer/commands/deploy_boards.py index 85601b3..257c6fb 100644 --- a/looker_deployer/commands/deploy_boards.py +++ b/looker_deployer/commands/deploy_boards.py @@ -13,7 +13,7 @@ # limitations under the License. import logging -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client diff --git a/looker_deployer/commands/deploy_connections.py b/looker_deployer/commands/deploy_connections.py index 37f6f89..3a1ea9d 100644 --- a/looker_deployer/commands/deploy_connections.py +++ b/looker_deployer/commands/deploy_connections.py @@ -14,7 +14,7 @@ import logging import re -from looker_sdk import models, error +from looker_sdk import models40 as models, error from looker_deployer.utils import deploy_logging from looker_deployer.utils import parse_ini from looker_deployer.utils.get_client import get_client diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 77d8122..59a2b8b 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -19,28 +19,36 @@ import tempfile import shutil import threading +import json from concurrent.futures import ThreadPoolExecutor from itertools import repeat from looker_deployer.utils import deploy_logging from looker_deployer.utils import parse_ini from looker_deployer.utils.get_client import get_client -from looker_sdk import models +from looker_sdk import models40 as models logger = deploy_logging.get_logger(__name__) +def alert_cleanup(sdk): + logger.debug("cleaning up orphaned alerts") + disabled_alerts = sdk.search_alerts(disabled="true") + for alert in disabled_alerts: + if alert['disabled_reason'] == "Dashboard element has been removed.": + sdk.delete_alert(alert['id']) + def get_space_ids_from_name(space_name, parent_id, sdk): if (space_name == "Shared" and parent_id == "0"): return ["1"] elif (space_name == "Embed Groups" and parent_id == "0"): - return sdk.search_spaces(name=space_name, parent_id=None)[0].id + return sdk.search_folders(name=space_name, parent_id=None)[0].id elif (space_name == "Users" and parent_id == "0"): - return sdk.search_spaces(name=space_name, parent_id=None)[0].id + return sdk.search_folders(name=space_name, parent_id=None)[0].id elif (space_name == "Embed Users" and parent_id == "0"): - return sdk.search_spaces(name=space_name, parent_id=None)[0].id + return sdk.search_folders(name=space_name, parent_id=None)[0].id logger.debug("space info", extra={"space_name": space_name, "parent_id": parent_id}) - space_list = sdk.search_spaces(name=space_name, parent_id=parent_id) + space_list = sdk.search_folders(name=space_name, parent_id=parent_id) id_list = [i.id for i in space_list] return id_list @@ -61,8 +69,8 @@ def create_or_return_space(space_name, parent_id, sdk): raise e else: logger.warning("No folders found. Creating folder now") - new_space = models.CreateSpace(name=space_name, parent_id=parent_id) - res = sdk.create_space(new_space) + new_space = models.CreateFolder(name=space_name, parent_id=parent_id) + res = sdk.create_folder(new_space) return res.id logger.info("Found Space ID", extra={"id": target_id}) @@ -125,8 +133,56 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): win_exec = ["cmd.exe", "/c"] gzr_command = win_exec + gzr_command + is_new_dash = "false" + existing_dash_alerts = [] + existing_elements_w_alerts = [] + + if content_type == "dashboard": ## only run for dashboards, looks can't have alerts + sdk = get_client(ini, env) ## should we update the function def and pass sdk? + with open(content_json) as file: + json_dash = json.load(file) + existing_dash = sdk.search_dashboards(slug=json_dash['slug']) ## search with slug first, fall back to name + folder + if len(existing_dash) < 1: + existing_dash = sdk.search_dashboards(title=json_dash['title'],folder_id=space_id) + if len(existing_dash) < 1: + is_new_dash = "true" + if is_new_dash == "false": ## if it's an existing dashboard, save the alerts and elements + for element in existing_dash[0]['dashboard_elements']: + start = len(existing_dash_alerts) + alerts = list(filter(lambda alert: alert['dashboard_element_id'] == element['id'], enabled_alerts)) + if len(alerts) > 0: + existing_dash_alerts.extend(alerts) + if len(existing_dash_alerts) > start: + if element not in existing_elements_w_alerts: + existing_elements_w_alerts.append(element) + + + #logger.debug("space info", extra={"space_name": space_name, "parent_id": parent_id}) subprocess.run(gzr_command) + + if is_new_dash == "false" and content_type == "dashboard" and len(existing_dash_alerts) > 0: ## get the new dashboard + updated_dash = sdk.search_dashboards(slug=json_dash['slug']) + if len(updated_dash) < 1: + updated_dash = sdk.search_dashboards(title=json_dash['title'],folder_id=space_id) + old_to_new_ids = {} + for element in existing_elements_w_alerts: ## match old element ids to new element ids + updated_dash_element = list(filter(lambda item: item['title'] == element['title'] and item['query_id'] == element['query_id'], updated_dash[0]['dashboard_elements'])) + if len(updated_dash_element) == 1: ## what should we do if more than one match? + old_to_new_ids[element['id']] = updated_dash_element[0]['id'] + for alert in existing_dash_alerts: ## create new alerts + logger.debug('processing alert for element', extra={"element_id": alert['dashboard_element_id']}) + new_alert = {} + for key in alert.keys(): + new_alert[key] = alert[key] + if alert['dashboard_element_id'] in old_to_new_ids.keys(): + logger.debug("creating alert", extra={"old_element_id": alert['dashboard_element_id'], "new_element_id": old_to_new_ids[alert['dashboard_element_id']]}) + new_alert['dashboard_element_id'] = old_to_new_ids[alert['dashboard_element_id']] + try: + sdk.create_alert(new_alert) + except Exception as e: + print(e) + def build_spaces(spaces, sdk): # seeding initial value of parent id to Shared @@ -303,6 +359,8 @@ def main(args): args.target_base = 'Shared' sdk = get_client(args.ini, args.env) + global enabled_alerts + enabled_alerts = sdk.search_alerts(disabled="false") send_content( sdk, args.env, @@ -315,3 +373,6 @@ def main(args): args.debug, args.target_base ) + + alert_cleanup(sdk) + diff --git a/looker_deployer/commands/deploy_content_export.py b/looker_deployer/commands/deploy_content_export.py index 1f48176..1cf7ddc 100644 --- a/looker_deployer/commands/deploy_content_export.py +++ b/looker_deployer/commands/deploy_content_export.py @@ -59,7 +59,7 @@ def export_spaces(folder_id, env, ini, path, debug=False): def recurse_folders(folder_id, folder_list, sdk, debug=False): - space = sdk.space(str(folder_id)) + space = sdk.folder(str(folder_id)) folder_list.append(space.name) logger.debug( "recursive folder crawl status", diff --git a/looker_deployer/commands/deploy_group_in_group.py b/looker_deployer/commands/deploy_group_in_group.py index 3f8dff9..6a29b8c 100644 --- a/looker_deployer/commands/deploy_group_in_group.py +++ b/looker_deployer/commands/deploy_group_in_group.py @@ -1,6 +1,6 @@ import logging import re -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client from looker_deployer.utils.match_by_key import match_by_key diff --git a/looker_deployer/commands/deploy_groups.py b/looker_deployer/commands/deploy_groups.py index 576b64b..b8d48e6 100644 --- a/looker_deployer/commands/deploy_groups.py +++ b/looker_deployer/commands/deploy_groups.py @@ -1,6 +1,6 @@ import logging import re -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client from looker_deployer.utils.match_by_key import match_by_key diff --git a/looker_deployer/commands/deploy_model_sets.py b/looker_deployer/commands/deploy_model_sets.py index d40a3f5..a71e56d 100644 --- a/looker_deployer/commands/deploy_model_sets.py +++ b/looker_deployer/commands/deploy_model_sets.py @@ -1,6 +1,6 @@ import logging import re -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client from looker_deployer.utils.match_by_key import match_by_key diff --git a/looker_deployer/commands/deploy_permission_sets.py b/looker_deployer/commands/deploy_permission_sets.py index dc4283f..71df1d6 100644 --- a/looker_deployer/commands/deploy_permission_sets.py +++ b/looker_deployer/commands/deploy_permission_sets.py @@ -1,6 +1,6 @@ import logging import re -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client from looker_deployer.utils.match_by_key import match_by_key diff --git a/looker_deployer/commands/deploy_roles.py b/looker_deployer/commands/deploy_roles.py index 196b95e..57059f6 100644 --- a/looker_deployer/commands/deploy_roles.py +++ b/looker_deployer/commands/deploy_roles.py @@ -1,6 +1,6 @@ import logging import re -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client from looker_deployer.utils.match_by_key import match_by_key diff --git a/looker_deployer/commands/deploy_user_attributes.py b/looker_deployer/commands/deploy_user_attributes.py index 56411e3..d3a3aa4 100644 --- a/looker_deployer/commands/deploy_user_attributes.py +++ b/looker_deployer/commands/deploy_user_attributes.py @@ -1,6 +1,6 @@ import logging import re -from looker_sdk import models +from looker_sdk import models40 as models from looker_deployer.utils import deploy_logging from looker_deployer.utils.get_client import get_client from looker_deployer.utils.match_by_key import match_by_key diff --git a/looker_deployer/utils/get_client.py b/looker_deployer/utils/get_client.py index 763e3cf..17198db 100644 --- a/looker_deployer/utils/get_client.py +++ b/looker_deployer/utils/get_client.py @@ -13,7 +13,7 @@ # limitations under the License. from looker_deployer import version -from looker_sdk import api_settings, methods, requests_transport, auth_session, serialize, _settings +from looker_sdk import api_settings, methods40 as methods, requests_transport, auth_session, serialize, _settings from typing import Optional @@ -21,7 +21,7 @@ def configure_sdk( config_file: str = "looker.ini", section: Optional[str] = None, config_settings: Optional[api_settings.ApiSettings] = None, -) -> methods.Looker31SDK: +) -> methods.Looker40SDK: """Default dependency configuration""" settings = ( _settings(config_file, section) if config_settings is None else config_settings @@ -29,12 +29,12 @@ def configure_sdk( settings.is_configured() settings.headers['User-Agent'] = f"Looker Deployer {version.__version__}" transport = requests_transport.RequestsTransport.configure(settings) - return methods.Looker31SDK( - auth_session.AuthSession(settings, transport, serialize.deserialize31, "3.1"), - serialize.deserialize31, - serialize.serialize31, + return methods.Looker40SDK( + auth_session.AuthSession(settings, transport, serialize.deserialize40, "4.0"), + serialize.deserialize40, + serialize.serialize40, transport, - "3.1", + "4.0", ) From ee4b846df05ffd3670dcd0014feea45a6ce77a18 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Fri, 2 Dec 2022 08:53:44 -0800 Subject: [PATCH 02/12] all owners for alert search --- looker_deployer/commands/deploy_content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index bb1624e..6b24bed 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -360,7 +360,7 @@ def main(args): sdk = get_client(args.ini, args.env) global enabled_alerts - enabled_alerts = sdk.search_alerts(disabled="false") + enabled_alerts = sdk.search_alerts(disabled="false", all_owners=True) send_content( sdk, args.env, From bf4e18caf4c5a7f9d82cd1adfb7468d451df66d4 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Fri, 2 Dec 2022 08:56:44 -0800 Subject: [PATCH 03/12] str cast for matching --- looker_deployer/commands/deploy_content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 6b24bed..1965a2c 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -149,7 +149,7 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): if is_new_dash == "false": ## if it's an existing dashboard, save the alerts and elements for element in existing_dash[0]['dashboard_elements']: start = len(existing_dash_alerts) - alerts = list(filter(lambda alert: alert['dashboard_element_id'] == element['id'], enabled_alerts)) + alerts = list(filter(lambda alert: str(alert['dashboard_element_id']) == str(element['id']), enabled_alerts)) if len(alerts) > 0: existing_dash_alerts.extend(alerts) if len(existing_dash_alerts) > start: From 42611dfcad71213a356de05e4ef624943d7ab917 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Tue, 10 Jan 2023 06:32:37 -0800 Subject: [PATCH 04/12] add log for alert removal --- looker_deployer/commands/deploy_content.py | 1 + 1 file changed, 1 insertion(+) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 1965a2c..20e416c 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -36,6 +36,7 @@ def alert_cleanup(sdk): for alert in disabled_alerts: if alert['disabled_reason'] == "Dashboard element has been removed.": sdk.delete_alert(alert['id']) + logger.info("Alert removed", extra={"alert title": alert['title']}) def get_space_ids_from_name(space_name, parent_id, sdk): From dbe11c8212bea8da433a8471ab847cf5d519b778 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Tue, 10 Jan 2023 07:00:47 -0800 Subject: [PATCH 05/12] correctly preserve owner --- looker_deployer/commands/deploy_content.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 20e416c..7944139 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -174,13 +174,16 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): for alert in existing_dash_alerts: ## create new alerts logger.debug('processing alert for element', extra={"element_id": alert['dashboard_element_id']}) new_alert = {} + update_owner = {} #alerts are assigned to creator, need to update after + update_owner['owner_id'] = alert['owner_id'] for key in alert.keys(): new_alert[key] = alert[key] if alert['dashboard_element_id'] in old_to_new_ids.keys(): logger.debug("creating alert", extra={"old_element_id": alert['dashboard_element_id'], "new_element_id": old_to_new_ids[alert['dashboard_element_id']]}) new_alert['dashboard_element_id'] = old_to_new_ids[alert['dashboard_element_id']] try: - sdk.create_alert(new_alert) + created_alert = sdk.create_alert(new_alert) + sdk.updated_alert(created_alert['id'], update_owner) except Exception as e: print(e) From fccc0acadba41606dc3f52b0af9c34791b90b647 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Tue, 10 Jan 2023 07:03:00 -0800 Subject: [PATCH 06/12] update alert removal info --- looker_deployer/commands/deploy_content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 7944139..f4ddc09 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -36,7 +36,7 @@ def alert_cleanup(sdk): for alert in disabled_alerts: if alert['disabled_reason'] == "Dashboard element has been removed.": sdk.delete_alert(alert['id']) - logger.info("Alert removed", extra={"alert title": alert['title']}) + logger.info("Alert removed", extra={"alert title": alert['custom_title'], "owner": alert['owner_display_name']}) def get_space_ids_from_name(space_name, parent_id, sdk): From 4d772c401be208c4de213b5f6c26383a1966b0a4 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Tue, 10 Jan 2023 07:21:08 -0800 Subject: [PATCH 07/12] use update_alert_field not update_alert --- looker_deployer/commands/deploy_content.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index f4ddc09..2da0746 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -183,7 +183,7 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): new_alert['dashboard_element_id'] = old_to_new_ids[alert['dashboard_element_id']] try: created_alert = sdk.create_alert(new_alert) - sdk.updated_alert(created_alert['id'], update_owner) + sdk.update_alert_field(created_alert['id'], update_owner) #update new alert to correct owner except Exception as e: print(e) From b2d79553113ac9bd4632d42578f174301f0d076c Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Wed, 11 Jan 2023 11:13:58 -0800 Subject: [PATCH 08/12] remove old alerts when creating new --- looker_deployer/commands/deploy_content.py | 1 + 1 file changed, 1 insertion(+) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 2da0746..5529a4b 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -184,6 +184,7 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): try: created_alert = sdk.create_alert(new_alert) sdk.update_alert_field(created_alert['id'], update_owner) #update new alert to correct owner + sdk.delete_alert(alert['id']) except Exception as e: print(e) From 17c4d4a84104c66bf00637dc0c94c6928f647d06 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Wed, 15 Feb 2023 09:34:06 -0800 Subject: [PATCH 09/12] fix for null to none filter --- looker_deployer/commands/deploy_content.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 5529a4b..3293517 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -177,7 +177,14 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): update_owner = {} #alerts are assigned to creator, need to update after update_owner['owner_id'] = alert['owner_id'] for key in alert.keys(): - new_alert[key] = alert[key] + new_alert[key] = alert[key] + new_alert['applied_dashboard_filters'] = [] + new_filter = {} + for old_filter in alert['applied_dashboard_filters']: + new_filter = old_filter.__dict__ + if new_filter['filter_value'] == 'None': + new_filter['filter_value'] = "" + new_alert['applied_dashboard_filters'].append(new_filter) if alert['dashboard_element_id'] in old_to_new_ids.keys(): logger.debug("creating alert", extra={"old_element_id": alert['dashboard_element_id'], "new_element_id": old_to_new_ids[alert['dashboard_element_id']]}) new_alert['dashboard_element_id'] = old_to_new_ids[alert['dashboard_element_id']] From 66a69a62693fad8d3c5b53f800c75ccaf94a43e9 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Fri, 17 Feb 2023 09:59:04 -0800 Subject: [PATCH 10/12] fix error email when deleting alerts --- looker_deployer/commands/deploy_content.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 3293517..ca2bd9d 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -191,6 +191,9 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): try: created_alert = sdk.create_alert(new_alert) sdk.update_alert_field(created_alert['id'], update_owner) #update new alert to correct owner + update_owner['owner_id'] = sdk.me()['id'] #get id of current user + if alert['id'] != update_owner['owner_id']: + sdk.update_alert_field(alert['id'], update_owner) #update old alert to current user to prevent errors when deleting sdk.delete_alert(alert['id']) except Exception as e: print(e) From af836546ad4a29766b545aea75f52ae3b5845492 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Wed, 22 Feb 2023 13:55:06 -0800 Subject: [PATCH 11/12] alerts unfollowable fix --- looker_deployer/commands/deploy_content.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index ca2bd9d..2ca4f56 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -191,9 +191,12 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): try: created_alert = sdk.create_alert(new_alert) sdk.update_alert_field(created_alert['id'], update_owner) #update new alert to correct owner - update_owner['owner_id'] = sdk.me()['id'] #get id of current user + update_fields = [] + update_fields['owner_id'] = sdk.me()['id'] #get id of current user + update_fields['is_disabled'] = "true" + update_fields['disabled_reason'] = "dashboard update" if alert['id'] != update_owner['owner_id']: - sdk.update_alert_field(alert['id'], update_owner) #update old alert to current user to prevent errors when deleting + sdk.update_alert_field(alert['id'], update_fields) #update old alert to current user to prevent errors when deleting sdk.delete_alert(alert['id']) except Exception as e: print(e) From 532f33e3f0012dc0b593be7eb8ac07526f486004 Mon Sep 17 00:00:00 2001 From: michaelcramer Date: Mon, 6 Mar 2023 08:09:57 -0800 Subject: [PATCH 12/12] potential fix for error emails --- looker_deployer/commands/deploy_content.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/looker_deployer/commands/deploy_content.py b/looker_deployer/commands/deploy_content.py index 2ca4f56..72afead 100644 --- a/looker_deployer/commands/deploy_content.py +++ b/looker_deployer/commands/deploy_content.py @@ -159,6 +159,15 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): #logger.debug("space info", extra={"space_name": space_name, "parent_id": parent_id}) + update_fields = {} + update_fields['owner_id'] = sdk.me()['id'] #get id of current user + update_fields['is_disabled'] = "true" + update_fields['disabled_reason'] = "dashboard update" + if len(existing_dash_alerts) > 0: + for alert in existing_dash_alerts: + if alert['id'] != update_fields['owner_id']: + sdk.update_alert_field(alert['id'], update_fields) #update old alert to current user to prevent errors when deleting + subprocess.run(gzr_command) @@ -191,12 +200,6 @@ def import_content(content_type, content_json, space_id, env, ini, debug=False): try: created_alert = sdk.create_alert(new_alert) sdk.update_alert_field(created_alert['id'], update_owner) #update new alert to correct owner - update_fields = [] - update_fields['owner_id'] = sdk.me()['id'] #get id of current user - update_fields['is_disabled'] = "true" - update_fields['disabled_reason'] = "dashboard update" - if alert['id'] != update_owner['owner_id']: - sdk.update_alert_field(alert['id'], update_fields) #update old alert to current user to prevent errors when deleting sdk.delete_alert(alert['id']) except Exception as e: print(e)