Skip to content

Commit 7ceec62

Browse files
authored
feat(scanning_alerts): implement add scanning alert object method (#244)
* feat(scanning_alerts): implement add scanning alert object method * feat(scanning_alerts): fix blank line and the end of file * feat(scanning_alerts): add client test for adding scanning alert
1 parent b663d00 commit 7ceec62

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

sdcclient/secure/scanning/_alerts.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,39 @@ def delete_alert(self, policyid): # FIXME: policyid must be maintained for back
379379
if not self._checkResponse(res):
380380
return [False, self.lasterr]
381381
return [True, res.text]
382+
383+
def add_alert_object(self, object):
384+
'''
385+
Adds alert object as raw JSON object.
386+
387+
Args:
388+
object: JSON repsentation of the alert.
389+
390+
Examples:
391+
>>> client = ScanningAlertsClientV1(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"),
392+
>>> token=os.getenv("SDC_SECURE_TOKEN"))
393+
>>> alert = {
394+
>>> "enabled": True,
395+
>>> "type": "runtime",
396+
>>> "name": "runtime-scanning-alert",
397+
>>> "triggers": {
398+
>>> "unscanned": True,
399+
>>> "analysis_update": False,
400+
>>> "vuln_update": False,
401+
>>> "policy_eval": False,
402+
>>> "failed": False
403+
>>> },
404+
>>> "autoscan": False,
405+
>>> "onlyPassFail": False,
406+
>>> "skipEventSend": False,
407+
>>> "notificationChannelIds": []
408+
>>> }
409+
>>> client.add_alert_object(alert)
410+
'''
411+
url = self.url + '/api/scanning/v1/alerts'
412+
data = json.dumps(object)
413+
res = self.http.post(url, headers=self.hdrs, data=data, verify=self.ssl_verify)
414+
if not self._checkResponse(res):
415+
return [False, self.lasterr]
416+
417+
return [True, res.json()]

specs/secure/scanning/alerts_spec.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
2+
import uuid
23

3-
from expects import be_empty, be_false, be_true, contain, contain_exactly, expect, have_keys
4+
from expects import be_empty, be_false, be_true, contain, contain_exactly, expect, have_keys, equal
45
from mamba import after, before, context, description, it
56

67
from sdcclient import SdScanningClient
78
from specs import be_successful_api_call
89

9-
with description("Scanning Alerts") as self:
10+
with description("Scanning Alerts", "integration") as self:
1011
with before.all:
1112
self.client = SdScanningClient(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"),
1213
token=os.getenv("SDC_SECURE_TOKEN"))
@@ -18,6 +19,34 @@
1819
for alert in res["alerts"]:
1920
self.client.delete_alert(alert["alertId"])
2021

22+
with it("add alert object"):
23+
alert = {
24+
"enabled": True,
25+
"type": "runtime",
26+
"name": f"runtime-scanning-alert-{uuid.uuid4()}",
27+
"triggers": {
28+
"unscanned": True,
29+
"analysis_update": False,
30+
"vuln_update": False,
31+
"policy_eval": False,
32+
"failed": False
33+
},
34+
"autoscan": False,
35+
"onlyPassFail": False,
36+
"skipEventSend": False,
37+
"notificationChannelIds": []
38+
}
39+
ok, res = self.client.add_alert_object(alert)
40+
expect((ok, res)).to(be_successful_api_call)
41+
expect(res['enabled']).to(equal(alert['enabled']))
42+
expect(res['type']).to(equal(alert['type']))
43+
expect(res['name']).to(equal(alert['name']))
44+
expect(res['triggers']).to(equal(alert['triggers']))
45+
expect(res['autoscan']).to(equal(alert['autoscan']))
46+
expect(res['onlyPassFail']).to(equal(alert['onlyPassFail']))
47+
expect(res['skipEventSend']).to(equal(alert['skipEventSend']))
48+
expect(res['notificationChannelIds']).to(equal(alert['notificationChannelIds']))
49+
2150
with it("lists all the scanning alerts"):
2251
ok, res = self.client.add_runtime_alert(
2352
name="A name",

0 commit comments

Comments
 (0)