Skip to content

Commit 485e67a

Browse files
authored
Merge pull request #43 from JupiterOne/KNO-575
Kno 575 - add support for alert rule k:v labels and resource groups
2 parents 9bd8449 + 9e9780a commit 485e67a

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

examples/examples.py

Lines changed: 99 additions & 1 deletion
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

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+
335382
create_alert_rule_r = j1.create_alert_rule(name="create_alert_rule-name",
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
@@ -897,10 +897,12 @@ def create_alert_rule(
897897
name: str = None,
898898
description: str = None,
899899
tags: List[str] = None,
900+
labels: List[dict] = None,
900901
polling_interval: str = None,
901902
severity: str = None,
902903
j1ql: str = None,
903904
action_configs: Dict = None,
905+
resource_group_id: str = None,
904906
):
905907
"""Create Alert Rule Configuration in J1 account"""
906908

@@ -941,15 +943,15 @@ def create_alert_rule(
941943
},
942944
"specVersion": 1,
943945
"tags": tags,
946+
"labels": labels,
944947
"templates": {},
948+
"resourceGroupId": resource_group_id,
945949
}
946950
}
947951

948952
if action_configs:
949953
variables["instance"]["operations"][0]["actions"].append(action_configs)
950954

951-
print(variables)
952-
953955
response = self._execute_query(CREATE_RULE_INSTANCE, variables=variables)
954956

955957
return response["data"]["createInlineQuestionRuleInstance"]
@@ -972,8 +974,10 @@ def update_alert_rule(
972974
severity: str = None,
973975
tags: List[str] = None,
974976
tag_op: str = None,
977+
labels: List[dict] = None,
975978
action_configs: List[dict] = None,
976979
action_configs_op: str = None,
980+
resource_group_id: str = None,
977981
):
978982
"""Update Alert Rule Configuration in J1 account"""
979983
# fetch existing alert rule
@@ -1030,6 +1034,10 @@ def update_alert_rule(
10301034
else:
10311035
tags_config = alert_rule_config["tags"]
10321036

1037+
# update labels list if provided
1038+
if labels is not None:
1039+
label_config = labels
1040+
10331041
# update action_configs list if provided
10341042
if action_configs is not None:
10351043

@@ -1064,6 +1072,8 @@ def update_alert_rule(
10641072
"operations": operations,
10651073
"pollingInterval": interval_config,
10661074
"tags": tags_config,
1075+
"labels": label_config,
1076+
"resourceGroupId": resource_group_id,
10671077
}
10681078
}
10691079

0 commit comments

Comments
 (0)