forked from demisto/demisto-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrations_example.py
62 lines (52 loc) · 2.25 KB
/
integrations_example.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
#!/usr/bin/env python2.7
# example
# An example to retrieve all availabile integrations and commands for each
# Works on Demisto 2.5 onwards
#
# Author: Slavik Markovich
# Version: 1.0
#
import argparse
import csv
import demisto
def p(what):
if verbose:
print(what)
def options_handler():
parser = argparse.ArgumentParser(description='Integrations and commands')
parser.add_argument(
'key', help='The API key to access the server')
parser.add_argument(
'server', help='The server URL to connect to')
parser.add_argument(
'-q', '--quiet', action='store_false',
dest='verbose', help="no extra prints")
parser.add_argument(
'-o', '--output', help='The output CSV file', default='integrations.csv')
options = parser.parse_args()
global verbose
verbose = options.verbose
return options
def main():
options = options_handler()
c = demisto.DemistoClient(options.key, options.server)
integrationsResponse = c.req('GET', 'settings/integration-commands', None)
if integrationsResponse.status_code != 200:
raise RuntimeError('Error getting integration data - %d (%s)' %
(integrationsResponse.status_code, integrationsResponse.reason))
integrations = sorted(integrationsResponse.json(),
key=lambda m: m['category'].lower() + '---' + m['name'].lower())
with open(options.output, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=[
'Category', 'Product', 'ProductDescription', 'Command', 'CommandDescription'])
writer.writeheader()
for m in integrations:
if 'commands' in m:
for cmd in m['commands']:
writer.writerow({'Category': m['category'], 'Product': m['display'], 'ProductDescription': m['description'].encode('ascii', 'ignore'),
'Command': cmd['name'], 'CommandDescription': cmd['description'].encode('ascii', 'ignore')})
else:
writer.writerow({'Category': m['category'], 'Product': m['display'],
'ProductDescription': m['description'].encode('ascii', 'ignore')})
if __name__ == '__main__':
main()