From ba3c7abb2661c6bf73e05cc0d849cf3f00d816f8 Mon Sep 17 00:00:00 2001 From: Craig Shanks Date: Fri, 10 Feb 2023 13:48:41 +0000 Subject: [PATCH] Various fixes and updates --- fetchentities.py | 9 ++++++--- fetchnotifications.py | 18 ++++++++++++------ fetchworkflows.py | 8 ++++++-- library/clients/entityclient.py | 5 +++++ library/clients/notificationsclient.py | 2 -- library/localstore.py | 3 ++- migrate_account.py | 6 +++--- migrate_dashboards.py | 2 +- 8 files changed, 35 insertions(+), 18 deletions(-) diff --git a/fetchentities.py b/fetchentities.py index 4fda443..39e99ef 100644 --- a/fetchentities.py +++ b/fetchentities.py @@ -31,6 +31,7 @@ def configure_parser(): help='Pass --workloads to list matching Workload entities') parser.add_argument('--tagName', nargs=1, required=False, help='(Optional) Tag name to use when filtering results. Required if --tagValue is passed.') parser.add_argument('--tagValue', nargs=1, required=False, help='(Optional) Tag value to use when filtering results. Required if --tagName is passed.') + parser.add_argument('--assessment', dest='assessment', required=False, action='store_true', help='Pass --assessment to prefix entities, with account id') return parser @@ -76,12 +77,14 @@ def fetch_entities(src_account_id, src_api_key, entity_types, output_file, *, for entity_type in entity_types: entities = ec.gql_get_entities_by_type(src_api_key, entity_type, src_account_id, tag_name, tag_value, src_region) for entity in entities['entities']: - entity_names.append(entity['name']) + entity_names.append(store.sanitize(entity['name'])) entity_names_file = store.create_output_file(output_file) with entity_names_file.open('a') as entity_names_out: for entity_name in entity_names: - name = store.sanitize(entity_name) - entity_names_out.write(name + "\n") + if assessment: + entity_names_out.write(src_account_id + "," + entity_name + "\n") + else: + entity_names_out.write(entity_name + "\n") entity_names_out.close() logger.info("Wrote %s entities to file %s",len(entity_names), output_file) diff --git a/fetchnotifications.py b/fetchnotifications.py index de2c99e..567c8db 100644 --- a/fetchnotifications.py +++ b/fetchnotifications.py @@ -23,22 +23,20 @@ def configure_parser(): def fetch_destinations(user_api_key, account_id, region, accounts_file=None): destinations_by_id = get_config(nc.destinations, user_api_key, account_id, region, accounts_file) - store.save_notification_destinations(account_id, destinations_by_id) return destinations_by_id def fetch_channels(user_api_key, account_id, region, accounts_file=None): channels_by_id = get_config(nc.channels, user_api_key, account_id, region, accounts_file) - store.save_notification_channels(account_id, channels_by_id) return channels_by_id -def get_config(func, user_api_key, account_id, region, from_file): +def get_config(func, user_api_key, account_id, region, accounts_file): acct_ids = [] if account_id: acct_ids = [account_id] else: - acct_ids = store.load_names(from_file) + acct_ids = store.load_names(accounts_file) configs_by_id = {} # Strip the class name field = func.__name__ @@ -65,9 +63,15 @@ def get_config(func, user_api_key, account_id, region, from_file): except: logger.error(f'Error querying {field} for account {acct_id}') else: + account_configs_by_id = {} for element in config: element['accountId'] = acct_id configs_by_id.setdefault(element['id'], element) + account_configs_by_id.setdefault(element['id'], element) + if field == 'destinations': + store.save_notification_destinations(acct_id, account_configs_by_id) + if field == 'channels': + store.save_notification_channels(acct_id, account_configs_by_id) logger.info(configs_by_id) store.save_config_csv(field, configs_by_id) return configs_by_id @@ -80,10 +84,12 @@ def main(): if not user_api_key: utils.error_and_exit('userApiKey', 'ENV_USER_API_KEY') region = utils.ensure_region(args) + account_id = args.account[0] if args.account else None + accounts_file = args.accounts[0] if args.accounts else None if args.destinations: - fetch_destinations(user_api_key, args.account[0], region, args.accounts[0] if args.accounts else None) + fetch_destinations(user_api_key, account_id, region, accounts_file) elif args.channels: - fetch_channels(user_api_key, args.account[0], region, args.accounts[0] if args.accounts else None) + fetch_channels(user_api_key, account_id, region, accounts_file) else: logger.info("pass [--destinations | --channels] to fetch configuration") diff --git a/fetchworkflows.py b/fetchworkflows.py index b73a9e8..e21a7c2 100644 --- a/fetchworkflows.py +++ b/fetchworkflows.py @@ -21,7 +21,6 @@ def configure_parser(): def fetch_workflows(user_api_key, account_id, region, accounts_file=None): workflow_by_source_id = get_config(wc.workflows, user_api_key, account_id, region, accounts_file) - store.save_workflows(account_id, workflow_by_source_id) return workflow_by_source_id @@ -53,9 +52,12 @@ def get_config(func, user_api_key, account_id, region, from_file): except: logger.error(f'Error querying {field} for account {acct_id}') else: + account_configs_by_id = {} for element in config: element['accountId'] = acct_id configs_by_id.setdefault(element['id'], element) + account_configs_by_id.setdefault(element['id'], element) + store.save_workflows(acct_id, account_configs_by_id) logger.info(configs_by_id) store.save_config_csv(field, configs_by_id) return configs_by_id @@ -68,7 +70,9 @@ def main(): if not user_api_key: utils.error_and_exit('userApiKey', 'ENV_USER_API_KEY') region = utils.ensure_region(args) - fetch_workflows(user_api_key, args.account[0], args.accounts[0], region) + account_id = args.account[0] if args.account else None + accounts_file = args.accounts[0] if args.accounts else None + fetch_workflows(user_api_key, account_id, region, accounts_file) if __name__ == '__main__': diff --git a/library/clients/entityclient.py b/library/clients/entityclient.py index a33f662..b8a4d8b 100644 --- a/library/clients/entityclient.py +++ b/library/clients/entityclient.py @@ -952,10 +952,15 @@ def get_nrql_condition_payload(account_id, condition_id): policyId runbookUrl signal { + aggregationDelay + aggregationMethod + aggregationTimer aggregationWindow + evaluationDelay evaluationOffset fillOption fillValue + slideBy } terms { operator diff --git a/library/clients/notificationsclient.py b/library/clients/notificationsclient.py index f6c49a5..01cddb6 100644 --- a/library/clients/notificationsclient.py +++ b/library/clients/notificationsclient.py @@ -19,7 +19,6 @@ SUPPORTED_DESTINATIONS = [ DESTINATION_TYPE_EMAIL, DESTINATION_TYPE_MOBILE_PUSH, - DESTINATION_TYPE_SLACK, DESTINATION_TYPE_SLACK_LEGACY, DESTINATION_TYPE_WEBHOOK ] @@ -49,7 +48,6 @@ SUPPORTED_CHANNELS = [ CHANNEL_TYPE_EMAIL, CHANNEL_TYPE_MOBILE_PUSH, - CHANNEL_TYPE_SLACK, CHANNEL_TYPE_SLACK_LEGACY, CHANNEL_TYPE_WEBHOOK ] diff --git a/library/localstore.py b/library/localstore.py index 6fe1e15..9db8738 100644 --- a/library/localstore.py +++ b/library/localstore.py @@ -270,7 +270,8 @@ def create_output_file(file_name): def sanitize(name): - illegal_characters = ['/', '?', '<', '>', '\\', ':', '*', '|'] + # illegal_characters = ['/', '?', '<', '>', '\\', ':', '*', '|'] + illegal_characters = ['/', '?', '<', '>', '\\', '*', '|'] characters = list(name) for index, character in enumerate(characters): if characters[index] in illegal_characters: diff --git a/migrate_account.py b/migrate_account.py index e45fce8..f5af20a 100644 --- a/migrate_account.py +++ b/migrate_account.py @@ -94,6 +94,9 @@ def migrate_step1(): mm.migrate_monitors('output/' + SRC_MON_LIST_FILE, SRC_ACCT, SRC_REGION, SRC_API_KEY, src_mon_time_stamp, TGT_ACCT, TGT_REGION, TGT_API_KEY, MINION_MAPPING_FILE) # Migrate Synthetic monitor entity tags mt.migrate_tags('output/' + SRC_MON_LIST_FILE, SRC_ACCT, SRC_REGION, SRC_API_KEY, TGT_ACCT, TGT_REGION, TGT_API_KEY, [ec.SYNTH_MONITOR]) + + +def migrate_step2(): # Migrate alert policies policies_by_source_id = mp.migrate(POLICY_NAME_FILE, ENTITY_NAME_FILE, SRC_ACCT, SRC_REGION, TGT_ACCT, TGT_REGION, SRC_API_KEY, TGT_API_KEY, USE_LOCAL) # Migrate alert conditions @@ -104,9 +107,6 @@ def migrate_step1(): channels_by_source_id = mn.migrate_channels(SRC_ACCT, SRC_API_KEY, SRC_REGION, TGT_ACCT, TGT_API_KEY, TGT_REGION, destinations_by_source_id) # Migrate workflows workflows_by_source_id = mw.migrate_workflows(SRC_ACCT, SRC_API_KEY, SRC_REGION, TGT_ACCT, TGT_API_KEY, TGT_REGION, channels_by_source_id, policies_by_source_id) - - -def migrate_step2(): # Migrate APM app_apdex_threshold, end_user_apdex_threshold, and enable_real_user_monitoring settings mapm.migrate_apps(APP_FILE, SRC_ACCT, SRC_API_KEY, SRC_REGION, TGT_ACCT, TGT_API_KEY, TGT_REGION) # Migrate dashboards diff --git a/migrate_dashboards.py b/migrate_dashboards.py index 8e3e83d..a164d48 100644 --- a/migrate_dashboards.py +++ b/migrate_dashboards.py @@ -113,7 +113,7 @@ def migrate_dashboards(from_file, src_acct, src_api_key, src_region, tgt_acct, t update_nrql_account_ids(src_acct, tgt_acct, tgt_dashboard, account_mappings) result = ec.post_dashboard(tgt_api_key, tgt_dashboard, tgt_acct, tgt_region) all_db_status[db_name][ds.STATUS] = result['status'] - if result['entityCreated']: + if 'entityCreated' in result: log.info('Created target dashboard ' + db_name) all_db_status[db_name][ds.DASHBOARD_CREATED] = True all_db_status[db_name][ds.TARGET_DASHBOARD] = result['entity']['guid']