Skip to content

Commit 61f36be

Browse files
committed
add support for alert rule k:v labels and resource groups
1 parent 25e9ffe commit 61f36be

2 files changed

Lines changed: 112 additions & 4 deletions

File tree

examples/examples.py

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import random
33
import time
44
import os
5+
import json
56

67
account = os.environ.get("JUPITERONE_ACCOUNT")
78
token = os.environ.get("JUPITERONE_TOKEN")
@@ -332,12 +333,61 @@
332333
]
333334
}
334335

335-
create_alert_rule_r = j1.create_alert_rule(name="create_alert_rule-name",
336+
create_jira_ticket_action_config = {
337+
"integrationInstanceId" : "<GUID>",
338+
"type" : "CREATE_JIRA_TICKET",
339+
"entityClass" : "Record",
340+
"summary" : "Jira Task created via JupiterOne Alert Rule",
341+
"issueType" : "Task",
342+
"project" : "KEY",
343+
"additionalFields" : {
344+
"description" : {
345+
"type" : "doc",
346+
"version" : 1,
347+
"content" : [
348+
{
349+
"type" : "paragraph",
350+
"content" : [
351+
{
352+
"type" : "text",
353+
"text" : "{{alertWebLink}}\n\n**Affected Items:**\n\n* {{queries.query0.data|mapProperty('displayName')|join('\n* ')}}"
354+
}
355+
]
356+
}
357+
]
358+
},
359+
"customfield_1234": "text-value",
360+
"customfield_5678": {
361+
"value": "select-value"
362+
},
363+
"labels" : [
364+
"label1","label2"
365+
],
366+
}
367+
}
368+
369+
alert_rule_labels = [
370+
{
371+
"labelName": "tagkey1",
372+
"labelValue": "tagval"
373+
},
374+
{
375+
"labelName": "tagkey2",
376+
"labelValue": "tagval"
377+
}
378+
]
379+
380+
resource_group_id = "<GUID>"
381+
382+
create_alert_rule_r = j1.create_alert_rule(name="4-14-25-create_alert_rule-name3",
336383
description="create_alert_rule-description",
337384
tags=['tag1', 'tag2'],
385+
labels=alert_rule_labels,
338386
polling_interval="DISABLED",
339387
severity="INFO",
340-
j1ql="find jupiterone_user")
388+
j1ql="find jupiterone_user",
389+
action_configs=create_jira_ticket_action_config,
390+
resource_group_id=resource_group_id)
341391
print("create_alert_rule()")
342392
print(create_alert_rule_r)
343393

@@ -380,6 +430,41 @@
380430
}
381431
]
382432

433+
alert_rule_config_jira = [
434+
{
435+
"integrationInstanceId" : "<GUID>",
436+
"type" : "CREATE_JIRA_TICKET",
437+
"entityClass" : "Record",
438+
"summary" : "Jira Task created via JupiterOne Alert Rule",
439+
"issueType" : "Task",
440+
"project" : "KEY",
441+
"additionalFields" : {
442+
"description" : {
443+
"type" : "doc",
444+
"version" : 1,
445+
"content" : [
446+
{
447+
"type" : "paragraph",
448+
"content" : [
449+
{
450+
"type" : "text",
451+
"text" : "{{alertWebLink}}\n\n**Affected Items:**\n\n* {{queries.query0.data|mapProperty('displayName')|join('\n* ')}}"
452+
}
453+
]
454+
}
455+
]
456+
},
457+
"customfield_1234": "text-value",
458+
"customfield_5678": {
459+
"value": "select-value"
460+
},
461+
"labels" : [
462+
"label1","label2"
463+
],
464+
}
465+
}
466+
]
467+
383468
alert_rule_config_multiple = [
384469
{
385470
"type": "WEBHOOK",
@@ -404,6 +489,19 @@
404489
}
405490
]
406491

492+
alert_rule_labels = [
493+
{
494+
"labelName": "tagkey1",
495+
"labelValue": "tagval"
496+
},
497+
{
498+
"labelName": "tagkey2",
499+
"labelValue": "tagval"
500+
}
501+
]
502+
503+
resource_group_id = "<GUID>"
504+
407505
# polling_interval can be DISABLED, THIRTY_MINUTES, ONE_HOUR, FOUR_HOURS, EIGHT_HOURS, TWELVE_HOURS, ONE_DAY, or ONE_WEEK
408506
# tag_op can be OVERWRITE or APPEND
409507
# severity can be INFO, LOW, MEDIUM, HIGH, or CRITICAL

jupiterone/client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,10 +887,12 @@ def create_alert_rule(
887887
name: str = None,
888888
description: str = None,
889889
tags: List[str] = None,
890+
labels: List[dict] = None,
890891
polling_interval: str = None,
891892
severity: str = None,
892893
j1ql: str = None,
893894
action_configs: Dict = None,
895+
resource_group_id: str = None,
894896
):
895897
"""Create Alert Rule Configuration in J1 account"""
896898

@@ -931,15 +933,15 @@ def create_alert_rule(
931933
},
932934
"specVersion": 1,
933935
"tags": tags,
936+
"labels": labels,
934937
"templates": {},
938+
"resourceGroupId": resource_group_id,
935939
}
936940
}
937941

938942
if action_configs:
939943
variables["instance"]["operations"][0]["actions"].append(action_configs)
940944

941-
print(variables)
942-
943945
response = self._execute_query(CREATE_RULE_INSTANCE, variables=variables)
944946

945947
return response["data"]["createInlineQuestionRuleInstance"]
@@ -962,8 +964,10 @@ def update_alert_rule(
962964
severity: str = None,
963965
tags: List[str] = None,
964966
tag_op: str = None,
967+
labels: List[dict] = None,
965968
action_configs: List[dict] = None,
966969
action_configs_op: str = None,
970+
resource_group_id: str = None,
967971
):
968972
"""Update Alert Rule Configuration in J1 account"""
969973
# fetch existing alert rule
@@ -1020,6 +1024,10 @@ def update_alert_rule(
10201024
else:
10211025
tags_config = alert_rule_config["tags"]
10221026

1027+
# update labels list if provided
1028+
if labels is not None:
1029+
label_config = labels
1030+
10231031
# update action_configs list if provided
10241032
if action_configs is not None:
10251033

@@ -1054,6 +1062,8 @@ def update_alert_rule(
10541062
"operations": operations,
10551063
"pollingInterval": interval_config,
10561064
"tags": tags_config,
1065+
"labels": label_config,
1066+
"resourceGroupId": resource_group_id,
10571067
}
10581068
}
10591069

0 commit comments

Comments
 (0)