Skip to content

Commit 6e8e489

Browse files
api-clients-generation-pipeline[bot]ci.datadog-api-spec
andauthored
Add conditional recipients to notification rule (#2855)
Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 82856d6 commit 6e8e489

14 files changed

+435
-3
lines changed

.generator/schemas/v2/openapi.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29565,6 +29565,8 @@ components:
2956529565
additionalProperties: false
2956629566
description: Attributes of the monitor notification rule.
2956729567
properties:
29568+
conditional_recipients:
29569+
$ref: '#/components/schemas/MonitorNotificationRuleConditionalRecipients'
2956829570
filter:
2956929571
$ref: '#/components/schemas/MonitorNotificationRuleFilter'
2957029572
name:
@@ -29573,8 +29575,36 @@ components:
2957329575
$ref: '#/components/schemas/MonitorNotificationRuleRecipients'
2957429576
required:
2957529577
- name
29578+
type: object
29579+
MonitorNotificationRuleCondition:
29580+
description: Conditions for `conditional_recipients`.
29581+
properties:
29582+
recipients:
29583+
$ref: '#/components/schemas/MonitorNotificationRuleRecipients'
29584+
scope:
29585+
$ref: '#/components/schemas/MonitorNotificationRuleScope'
29586+
required:
29587+
- scope
2957629588
- recipients
2957729589
type: object
29590+
MonitorNotificationRuleConditionalRecipients:
29591+
description: Use conditional recipients to define different recipients for different
29592+
situations.
29593+
properties:
29594+
conditions:
29595+
description: Conditions of the notification rule.
29596+
items:
29597+
$ref: '#/components/schemas/MonitorNotificationRuleCondition'
29598+
maxItems: 10
29599+
minItems: 1
29600+
type: array
29601+
fallback_recipients:
29602+
$ref: '#/components/schemas/MonitorNotificationRuleRecipients'
29603+
description: If none of the `conditions` applied, `fallback_recipients`
29604+
will get notified.
29605+
required:
29606+
- conditions
29607+
type: object
2957829608
MonitorNotificationRuleCreateRequest:
2957929609
description: Request for creating a monitor notification rule.
2958029610
properties:
@@ -29714,6 +29744,8 @@ components:
2971429744
additionalProperties: {}
2971529745
description: Attributes of the monitor notification rule.
2971629746
properties:
29747+
conditional_recipients:
29748+
$ref: '#/components/schemas/MonitorNotificationRuleConditionalRecipients'
2971729749
created:
2971829750
description: Creation time of the monitor notification rule.
2971929751
example: 2020-01-02 03:04:00+00:00
@@ -29735,6 +29767,12 @@ components:
2973529767
description: An object related to a monitor notification rule.
2973629768
oneOf:
2973729769
- $ref: '#/components/schemas/User'
29770+
MonitorNotificationRuleScope:
29771+
description: The scope to which the monitor applied.
29772+
example: transition_type:alert
29773+
maxLength: 3000
29774+
minLength: 1
29775+
type: string
2973829776
MonitorNotificationRuleUpdateRequest:
2973929777
description: Request for updating a monitor notification rule.
2974029778
properties:

docs/datadog_api_client.v2.model.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12835,6 +12835,20 @@ datadog\_api\_client.v2.model.monitor\_notification\_rule\_attributes module
1283512835
:members:
1283612836
:show-inheritance:
1283712837

12838+
datadog\_api\_client.v2.model.monitor\_notification\_rule\_condition module
12839+
---------------------------------------------------------------------------
12840+
12841+
.. automodule:: datadog_api_client.v2.model.monitor_notification_rule_condition
12842+
:members:
12843+
:show-inheritance:
12844+
12845+
datadog\_api\_client.v2.model.monitor\_notification\_rule\_conditional\_recipients module
12846+
-----------------------------------------------------------------------------------------
12847+
12848+
.. automodule:: datadog_api_client.v2.model.monitor_notification_rule_conditional_recipients
12849+
:members:
12850+
:show-inheritance:
12851+
1283812852
datadog\_api\_client.v2.model.monitor\_notification\_rule\_create\_request module
1283912853
---------------------------------------------------------------------------------
1284012854

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Create a monitor notification rule with conditional recipients returns "OK" response
3+
"""
4+
5+
from datadog_api_client import ApiClient, Configuration
6+
from datadog_api_client.v2.api.monitors_api import MonitorsApi
7+
from datadog_api_client.v2.model.monitor_notification_rule_attributes import MonitorNotificationRuleAttributes
8+
from datadog_api_client.v2.model.monitor_notification_rule_condition import MonitorNotificationRuleCondition
9+
from datadog_api_client.v2.model.monitor_notification_rule_conditional_recipients import (
10+
MonitorNotificationRuleConditionalRecipients,
11+
)
12+
from datadog_api_client.v2.model.monitor_notification_rule_create_request import MonitorNotificationRuleCreateRequest
13+
from datadog_api_client.v2.model.monitor_notification_rule_create_request_data import (
14+
MonitorNotificationRuleCreateRequestData,
15+
)
16+
from datadog_api_client.v2.model.monitor_notification_rule_filter_tags import MonitorNotificationRuleFilterTags
17+
from datadog_api_client.v2.model.monitor_notification_rule_resource_type import MonitorNotificationRuleResourceType
18+
19+
body = MonitorNotificationRuleCreateRequest(
20+
data=MonitorNotificationRuleCreateRequestData(
21+
attributes=MonitorNotificationRuleAttributes(
22+
filter=MonitorNotificationRuleFilterTags(
23+
tags=[
24+
"test:example-monitor",
25+
],
26+
),
27+
name="test rule",
28+
conditional_recipients=MonitorNotificationRuleConditionalRecipients(
29+
conditions=[
30+
MonitorNotificationRuleCondition(
31+
scope="transition_type:is_alert",
32+
recipients=[
33+
"slack-test-channel",
34+
"jira-test",
35+
],
36+
),
37+
],
38+
),
39+
),
40+
type=MonitorNotificationRuleResourceType.MONITOR_NOTIFICATION_RULE,
41+
),
42+
)
43+
44+
configuration = Configuration()
45+
with ApiClient(configuration) as api_client:
46+
api_instance = MonitorsApi(api_client)
47+
response = api_instance.create_monitor_notification_rule(body=body)
48+
49+
print(response)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Update a monitor notification rule with conditional_recipients returns "OK" response
3+
"""
4+
5+
from os import environ
6+
from datadog_api_client import ApiClient, Configuration
7+
from datadog_api_client.v2.api.monitors_api import MonitorsApi
8+
from datadog_api_client.v2.model.monitor_notification_rule_attributes import MonitorNotificationRuleAttributes
9+
from datadog_api_client.v2.model.monitor_notification_rule_condition import MonitorNotificationRuleCondition
10+
from datadog_api_client.v2.model.monitor_notification_rule_conditional_recipients import (
11+
MonitorNotificationRuleConditionalRecipients,
12+
)
13+
from datadog_api_client.v2.model.monitor_notification_rule_filter_tags import MonitorNotificationRuleFilterTags
14+
from datadog_api_client.v2.model.monitor_notification_rule_resource_type import MonitorNotificationRuleResourceType
15+
from datadog_api_client.v2.model.monitor_notification_rule_update_request import MonitorNotificationRuleUpdateRequest
16+
from datadog_api_client.v2.model.monitor_notification_rule_update_request_data import (
17+
MonitorNotificationRuleUpdateRequestData,
18+
)
19+
20+
# there is a valid "monitor_notification_rule" in the system
21+
MONITOR_NOTIFICATION_RULE_DATA_ID = environ["MONITOR_NOTIFICATION_RULE_DATA_ID"]
22+
23+
body = MonitorNotificationRuleUpdateRequest(
24+
data=MonitorNotificationRuleUpdateRequestData(
25+
attributes=MonitorNotificationRuleAttributes(
26+
filter=MonitorNotificationRuleFilterTags(
27+
tags=[
28+
"test:example-monitor",
29+
"host:abc",
30+
],
31+
),
32+
name="updated rule",
33+
conditional_recipients=MonitorNotificationRuleConditionalRecipients(
34+
conditions=[
35+
MonitorNotificationRuleCondition(
36+
scope="transition_type:is_alert",
37+
recipients=[
38+
"slack-test-channel",
39+
"jira-test",
40+
],
41+
),
42+
],
43+
),
44+
),
45+
id=MONITOR_NOTIFICATION_RULE_DATA_ID,
46+
type=MonitorNotificationRuleResourceType.MONITOR_NOTIFICATION_RULE,
47+
),
48+
)
49+
50+
configuration = Configuration()
51+
with ApiClient(configuration) as api_client:
52+
api_instance = MonitorsApi(api_client)
53+
response = api_instance.update_monitor_notification_rule(rule_id=MONITOR_NOTIFICATION_RULE_DATA_ID, body=body)
54+
55+
print(response)

src/datadog_api_client/v2/model/monitor_notification_rule_attributes.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515

1616
if TYPE_CHECKING:
17+
from datadog_api_client.v2.model.monitor_notification_rule_conditional_recipients import (
18+
MonitorNotificationRuleConditionalRecipients,
19+
)
1720
from datadog_api_client.v2.model.monitor_notification_rule_filter import MonitorNotificationRuleFilter
1821
from datadog_api_client.v2.model.monitor_notification_rule_filter_tags import MonitorNotificationRuleFilterTags
1922

@@ -36,15 +39,20 @@ def additional_properties_type(_):
3639

3740
@cached_property
3841
def openapi_types(_):
42+
from datadog_api_client.v2.model.monitor_notification_rule_conditional_recipients import (
43+
MonitorNotificationRuleConditionalRecipients,
44+
)
3945
from datadog_api_client.v2.model.monitor_notification_rule_filter import MonitorNotificationRuleFilter
4046

4147
return {
48+
"conditional_recipients": (MonitorNotificationRuleConditionalRecipients,),
4249
"filter": (MonitorNotificationRuleFilter,),
4350
"name": (str,),
4451
"recipients": ([str],),
4552
}
4653

4754
attribute_map = {
55+
"conditional_recipients": "conditional_recipients",
4856
"filter": "filter",
4957
"name": "name",
5058
"recipients": "recipients",
@@ -53,25 +61,32 @@ def openapi_types(_):
5361
def __init__(
5462
self_,
5563
name: str,
56-
recipients: List[str],
64+
conditional_recipients: Union[MonitorNotificationRuleConditionalRecipients, UnsetType] = unset,
5765
filter: Union[MonitorNotificationRuleFilter, MonitorNotificationRuleFilterTags, UnsetType] = unset,
66+
recipients: Union[List[str], UnsetType] = unset,
5867
**kwargs,
5968
):
6069
"""
6170
Attributes of the monitor notification rule.
6271
72+
:param conditional_recipients: Use conditional recipients to define different recipients for different situations.
73+
:type conditional_recipients: MonitorNotificationRuleConditionalRecipients, optional
74+
6375
:param filter: Filter used to associate the notification rule with monitors.
6476
:type filter: MonitorNotificationRuleFilter, optional
6577
6678
:param name: The name of the monitor notification rule.
6779
:type name: str
6880
6981
:param recipients: A list of recipients to notify. Uses the same format as the monitor ``message`` field. Must not start with an '@'.
70-
:type recipients: [str]
82+
:type recipients: [str], optional
7183
"""
84+
if conditional_recipients is not unset:
85+
kwargs["conditional_recipients"] = conditional_recipients
7286
if filter is not unset:
7387
kwargs["filter"] = filter
88+
if recipients is not unset:
89+
kwargs["recipients"] = recipients
7490
super().__init__(kwargs)
7591

7692
self_.name = name
77-
self_.recipients = recipients
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
3+
# Copyright 2019-Present Datadog, Inc.
4+
from __future__ import annotations
5+
6+
from typing import List
7+
8+
from datadog_api_client.model_utils import (
9+
ModelNormal,
10+
cached_property,
11+
)
12+
13+
14+
class MonitorNotificationRuleCondition(ModelNormal):
15+
validations = {
16+
"recipients": {
17+
"max_items": 20,
18+
"min_items": 1,
19+
},
20+
"scope": {
21+
"max_length": 3000,
22+
"min_length": 1,
23+
},
24+
}
25+
26+
@cached_property
27+
def openapi_types(_):
28+
return {
29+
"recipients": ([str],),
30+
"scope": (str,),
31+
}
32+
33+
attribute_map = {
34+
"recipients": "recipients",
35+
"scope": "scope",
36+
}
37+
38+
def __init__(self_, recipients: List[str], scope: str, **kwargs):
39+
"""
40+
Conditions for ``conditional_recipients``.
41+
42+
:param recipients: A list of recipients to notify. Uses the same format as the monitor ``message`` field. Must not start with an '@'.
43+
:type recipients: [str]
44+
45+
:param scope: The scope to which the monitor applied.
46+
:type scope: str
47+
"""
48+
super().__init__(kwargs)
49+
50+
self_.recipients = recipients
51+
self_.scope = scope
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
3+
# Copyright 2019-Present Datadog, Inc.
4+
from __future__ import annotations
5+
6+
from typing import List, Union, TYPE_CHECKING
7+
8+
from datadog_api_client.model_utils import (
9+
ModelNormal,
10+
cached_property,
11+
unset,
12+
UnsetType,
13+
)
14+
15+
16+
if TYPE_CHECKING:
17+
from datadog_api_client.v2.model.monitor_notification_rule_condition import MonitorNotificationRuleCondition
18+
19+
20+
class MonitorNotificationRuleConditionalRecipients(ModelNormal):
21+
validations = {
22+
"conditions": {
23+
"max_items": 10,
24+
"min_items": 1,
25+
},
26+
"fallback_recipients": {
27+
"max_items": 20,
28+
"min_items": 1,
29+
},
30+
}
31+
32+
@cached_property
33+
def openapi_types(_):
34+
from datadog_api_client.v2.model.monitor_notification_rule_condition import MonitorNotificationRuleCondition
35+
36+
return {
37+
"conditions": ([MonitorNotificationRuleCondition],),
38+
"fallback_recipients": ([str],),
39+
}
40+
41+
attribute_map = {
42+
"conditions": "conditions",
43+
"fallback_recipients": "fallback_recipients",
44+
}
45+
46+
def __init__(
47+
self_,
48+
conditions: List[MonitorNotificationRuleCondition],
49+
fallback_recipients: Union[List[str], UnsetType] = unset,
50+
**kwargs,
51+
):
52+
"""
53+
Use conditional recipients to define different recipients for different situations.
54+
55+
:param conditions: Conditions of the notification rule.
56+
:type conditions: [MonitorNotificationRuleCondition]
57+
58+
:param fallback_recipients: A list of recipients to notify. Uses the same format as the monitor ``message`` field. Must not start with an '@'.
59+
:type fallback_recipients: [str], optional
60+
"""
61+
if fallback_recipients is not unset:
62+
kwargs["fallback_recipients"] = fallback_recipients
63+
super().__init__(kwargs)
64+
65+
self_.conditions = conditions

0 commit comments

Comments
 (0)