forked from demisto/demisto-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_upload.py
50 lines (40 loc) · 1.3 KB
/
widget_upload.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
#!/usr/bin/env python2.7
# example
# Upload a new widget to the available widget library
#
# Author: Slavik Markovich
# Version: 1.0
#
import json
import argparse
import demisto
def p(what):
if verbose:
print(what)
def options_handler():
parser = argparse.ArgumentParser(
description='Upload a new widget to Demisto')
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(
'widget', help='The new widget JSON file')
parser.add_argument('-q', '--quiet', action='store_false',
dest='verbose', help="no extra prints")
options = parser.parse_args()
global verbose
verbose = options.verbose
return options
def main():
options = options_handler()
c = demisto.DemistoClient(options.key, options.server)
with open(options.widget, 'r') as widget_file:
widget_data = widget_file.read()
res = c.req('POST', 'widgets', json.loads(widget_data))
if res.status_code != 200:
raise RuntimeError('Error uploading widget - %d (%s)' %
(res.status_code, res.reason))
p('Widget successfully uploaded')
if __name__ == '__main__':
main()