-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfetchchannels.py
72 lines (58 loc) · 2.53 KB
/
fetchchannels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import argparse
import os
import sys
import time
import library.localstore as store
import library.migrationlogger as migrationlogger
import library.clients.alertsclient as ac
import library.utils as utils
logger = migrationlogger.get_logger(os.path.basename(__file__))
source_api_key = ""
def configure_parser():
parser = argparse.ArgumentParser(description='Fetch and store channels by alert policy id')
parser.add_argument('--sourceAccount', type=str, nargs=1, required=True, help='Source accountId to store the alerts')
parser.add_argument('--sourceApiKey', type=str, nargs=1, required=False, help='Source API Key or \
set env var ENV_SOURCE_API_KEY')
parser.add_argument('--region', type=str, nargs=1, required=False, help='region us(default) or eu')
return parser
def print_params(args, source_api_key, region):
logger.info("Using sourceAccount : " + str(args.sourceAccount[0]))
logger.info("Using sourceApiKey : " + len(source_api_key[:-4]) * "*" + source_api_key[-4:])
logger.info("region : " + region)
# fetches all channels restructures into a dictionary as below
# channels_by_id: {
# channel_id: {}
# }
# channel_by_policy_id {
# alert_policy_id: [channel_id]
# }
# links": { "policy_ids": [] }
def get_channels_by_id_policy(api_key, region):
src_channels = ac.get_channels(api_key, region)
channels = {"channels_by_id": {}, "channels_by_policy_id": {}}
for channel in src_channels[ac.CHANNELS]:
channel_id = str(channel['id'])
channels['channels_by_id'][channel_id] = channel
for policy_id in channel['links']['policy_ids']:
policy_id_str = str(policy_id)
if policy_id_str not in channels['channels_by_policy_id']:
channels['channels_by_policy_id'][policy_id_str] = [channel_id]
else:
channels['channels_by_policy_id'][policy_id_str].append(channel_id)
return channels
def fetch_alert_channels(api_key, account_id, region):
all_channels = get_channels_by_id_policy(api_key, region)
store.save_alert_channels(account_id, all_channels)
def main():
start_time = time.time()
parser = configure_parser()
args = parser.parse_args()
args_api_key = ''
if args.sourceApiKey:
args_api_key = args.sourceApiKey[0]
region = utils.ensure_region(args)
print_params(args, args_api_key, region)
fetch_alert_channels(args_api_key, args.sourceAccount[0], region)
logger.info("Time taken : " + str(time.time() - start_time) + "seconds")
if __name__ == '__main__':
main()