forked from demisto/demisto-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_automation_example.py
136 lines (121 loc) · 4.6 KB
/
update_automation_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
import argparse
import os
import sys
import json
import unittest
import demisto
def options_handler():
parser = argparse.ArgumentParser(
description='an example of update automation by demisto.client',
usage="""
update automation example:
%(prog)s $DEMISTO_API_KEY https://demisto.example.com/ -f my_automation_script.py [-n automation_name]
unit test of demisto.client.(Search|Load|Update|Save|Delete)Automation:
%(prog)s $DEMISTO_API_KEY https://demisto.example.com/ -t"""
)
parser.add_argument(
'key', help='The API key to access the server')
parser.add_argument(
'url', help='Base URL of API')
parser.add_argument(
'-t', '--test', help='run unit test instead of updating automation', action='store_true')
parser.add_argument(
'-n', '--name', help='automation name')
parser.add_argument(
'-f', '--file', nargs='?', type=argparse.FileType('r'),
help='automation script name(.py or .js)',
default=sys.stdin)
options = parser.parse_args()
if options.name is None:
if options.file.name == '<stdin>':
if options.test:
options.name = "test"
else:
raise RuntimeError(
'Error parsing options - name is required')
else:
options.name = os.path.splitext(
os.path.basename(options.file.name))[0]
return options
class TestAutomation(unittest.TestCase):
"""
tests for search/delete/update/load/save automation.
demisto server is required.
if automation cls.name is already exist at setUpClass, this will throw RuntimeError.
"""
@classmethod
def setUpClass(cls):
automations = cls.client.SearchAutomation("name:{}".format(cls.name))
if len(automations["scripts"]) > 0:
raise RuntimeError("automation {} is already exist. test is cancelled. use another name, or delete it.")
def setUp(self):
automation = {
"name": self.name,
"script": "demisto.results('Hello, world!')"
}
automations = self.client.SaveAutomation(automation)
self.id = automations["scripts"][0]["id"]
def tearDown(self):
if self.id:
automation = {
"name": self.name,
"id": self.id
}
self.client.DeleteAutomation(automation)
self.id = None
def test_search(self):
automations = self.client.SearchAutomation("name:{}".format(self.name))
self.assertIn("scripts", automations)
self.assertEqual(len(automations["scripts"]), 1)
self.assertIn("id", automations["scripts"][0])
def test_delete(self):
automation = {
"name": self.name,
"id": self.id
}
client.DeleteAutomation(automation)
self.id = None
automations = self.client.SearchAutomation("name:{}".format(self.name))
self.assertIn("scripts", automations)
self.assertEqual(len(automations["scripts"]), 0)
def test_save_new(self):
automation = {
"name": self.name,
"id": self.id
}
client.DeleteAutomation(automation)
automation = {
"name": self.name,
"script": "demisto.results('Hello, world!')"
}
automations = self.client.SaveAutomation(automation)
self.assertIn("scripts", automations)
self.assertEqual(len(automations["scripts"]), 1)
self.assertIn("id", automations["scripts"][0])
self.id = automations["scripts"][0]["id"]
def test_load(self):
automation = self.client.LoadAutomation(self.id)
self.assertIn("id", automation)
self.assertEqual(automation["id"], self.id)
def test_update(self):
script = "demisto.results('Hello, world!!!!')"
automations = self.client.UpdateAutomation(
None, self.name, script=script)
self.assertIn("scripts", automations)
self.assertEqual(len(automations["scripts"]), 1)
self.assertIn("version", automations["scripts"][0])
self.assertEqual(automations["scripts"][0]["version"], 2)
if __name__ == '__main__':
options = options_handler()
client = demisto.DemistoClient(options.key, options.url)
if options.test:
TestAutomation.name = options.name
TestAutomation.client = client
if sys.argv:
del sys.argv[1:]
unittest.main()
sys.exit(0)
script = options.file.read()
automations = client.UpdateAutomation(None, options.name, script=script)
print(json.dumps(automations))