Skip to content

Commit 590979b

Browse files
api-clients-generation-pipeline[bot]ci.datadog-api-spec
andauthored
Add filter.scope to Monitor Notification Rules (#3246)
Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 3fefc12 commit 590979b

15 files changed

+478
-24
lines changed

.generator/schemas/v2/openapi.yaml

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32691,15 +32691,23 @@ components:
3269132691
properties:
3269232692
recipients:
3269332693
$ref: '#/components/schemas/MonitorNotificationRuleRecipients'
32694+
description: A list of recipients to notify. Uses the same format as the
32695+
monitor `message` field. Must not start with an '@'.
3269432696
scope:
32695-
$ref: '#/components/schemas/MonitorNotificationRuleScope'
32697+
$ref: '#/components/schemas/MonitorNotificationRuleConditionScope'
3269632698
required:
3269732699
- scope
3269832700
- recipients
3269932701
type: object
32702+
MonitorNotificationRuleConditionScope:
32703+
description: The scope to which the monitor applied.
32704+
example: transition_type:alert
32705+
maxLength: 3000
32706+
minLength: 1
32707+
type: string
3270032708
MonitorNotificationRuleConditionalRecipients:
3270132709
description: Use conditional recipients to define different recipients for different
32702-
situations.
32710+
situations. Cannot be used with `recipients`.
3270332711
properties:
3270432712
conditions:
3270532713
description: Conditions of the notification rule.
@@ -32749,12 +32757,30 @@ components:
3274932757
description: Filter used to associate the notification rule with monitors.
3275032758
oneOf:
3275132759
- $ref: '#/components/schemas/MonitorNotificationRuleFilterTags'
32760+
- $ref: '#/components/schemas/MonitorNotificationRuleFilterScope'
32761+
MonitorNotificationRuleFilterScope:
32762+
additionalProperties: false
32763+
description: Filter monitor notifications. A monitor notification must match
32764+
the scope.
32765+
properties:
32766+
scope:
32767+
description: A scope composed of one or several key:value pairs, which can
32768+
be used to filter monitor notifications on monitor and group tags.
32769+
example: service:(foo OR bar) AND team:test NOT environment:staging
32770+
maxLength: 3000
32771+
minLength: 1
32772+
type: string
32773+
required:
32774+
- scope
32775+
type: object
3275232776
MonitorNotificationRuleFilterTags:
3275332777
additionalProperties: false
32754-
description: Filter monitors by tags. Monitors must match all tags.
32778+
description: Filter monitor notifications by tags. A monitor notification must
32779+
match all tags.
3275532780
properties:
3275632781
tags:
32757-
description: A list of monitor tags.
32782+
description: A list of tags (key:value pairs), which can be used to filter
32783+
monitor notifications on monitor and group tags.
3275832784
example:
3275932785
- team:product
3276032786
- host:abc
@@ -32794,7 +32820,7 @@ components:
3279432820
type: string
3279532821
MonitorNotificationRuleRecipients:
3279632822
description: A list of recipients to notify. Uses the same format as the monitor
32797-
`message` field. Must not start with an '@'.
32823+
`message` field. Must not start with an '@'. Cannot be used with `conditional_recipients`.
3279832824
example:
3279932825
- slack-test-channel
3280032826
- jira-test
@@ -32877,12 +32903,6 @@ components:
3287732903
description: An object related to a monitor notification rule.
3287832904
oneOf:
3287932905
- $ref: '#/components/schemas/User'
32880-
MonitorNotificationRuleScope:
32881-
description: The scope to which the monitor applied.
32882-
example: transition_type:alert
32883-
maxLength: 3000
32884-
minLength: 1
32885-
type: string
3288632906
MonitorNotificationRuleUpdateRequest:
3288732907
description: Request for updating a monitor notification rule.
3288832908
properties:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Create a monitor notification rule with scope returns "OK" response
2+
3+
import com.datadog.api.client.ApiClient;
4+
import com.datadog.api.client.ApiException;
5+
import com.datadog.api.client.v2.api.MonitorsApi;
6+
import com.datadog.api.client.v2.model.MonitorNotificationRuleAttributes;
7+
import com.datadog.api.client.v2.model.MonitorNotificationRuleCreateRequest;
8+
import com.datadog.api.client.v2.model.MonitorNotificationRuleCreateRequestData;
9+
import com.datadog.api.client.v2.model.MonitorNotificationRuleFilter;
10+
import com.datadog.api.client.v2.model.MonitorNotificationRuleFilterScope;
11+
import com.datadog.api.client.v2.model.MonitorNotificationRuleResourceType;
12+
import com.datadog.api.client.v2.model.MonitorNotificationRuleResponse;
13+
import java.util.Arrays;
14+
15+
public class Example {
16+
public static void main(String[] args) {
17+
ApiClient defaultClient = ApiClient.getDefaultApiClient();
18+
MonitorsApi apiInstance = new MonitorsApi(defaultClient);
19+
20+
MonitorNotificationRuleCreateRequest body =
21+
new MonitorNotificationRuleCreateRequest()
22+
.data(
23+
new MonitorNotificationRuleCreateRequestData()
24+
.attributes(
25+
new MonitorNotificationRuleAttributes()
26+
.filter(
27+
new MonitorNotificationRuleFilter(
28+
new MonitorNotificationRuleFilterScope()
29+
.scope("test:example-monitor")))
30+
.name("test rule")
31+
.recipients(Arrays.asList("slack-test-channel", "jira-test")))
32+
.type(MonitorNotificationRuleResourceType.MONITOR_NOTIFICATION_RULE));
33+
34+
try {
35+
MonitorNotificationRuleResponse result = apiInstance.createMonitorNotificationRule(body);
36+
System.out.println(result);
37+
} catch (ApiException e) {
38+
System.err.println("Exception when calling MonitorsApi#createMonitorNotificationRule");
39+
System.err.println("Status code: " + e.getCode());
40+
System.err.println("Reason: " + e.getResponseBody());
41+
System.err.println("Response headers: " + e.getResponseHeaders());
42+
e.printStackTrace();
43+
}
44+
}
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Update a monitor notification rule with scope returns "OK" response
2+
3+
import com.datadog.api.client.ApiClient;
4+
import com.datadog.api.client.ApiException;
5+
import com.datadog.api.client.v2.api.MonitorsApi;
6+
import com.datadog.api.client.v2.model.MonitorNotificationRuleAttributes;
7+
import com.datadog.api.client.v2.model.MonitorNotificationRuleFilter;
8+
import com.datadog.api.client.v2.model.MonitorNotificationRuleFilterScope;
9+
import com.datadog.api.client.v2.model.MonitorNotificationRuleResourceType;
10+
import com.datadog.api.client.v2.model.MonitorNotificationRuleResponse;
11+
import com.datadog.api.client.v2.model.MonitorNotificationRuleUpdateRequest;
12+
import com.datadog.api.client.v2.model.MonitorNotificationRuleUpdateRequestData;
13+
import java.util.Collections;
14+
15+
public class Example {
16+
public static void main(String[] args) {
17+
ApiClient defaultClient = ApiClient.getDefaultApiClient();
18+
MonitorsApi apiInstance = new MonitorsApi(defaultClient);
19+
20+
// there is a valid "monitor_notification_rule" in the system
21+
String MONITOR_NOTIFICATION_RULE_DATA_ID = System.getenv("MONITOR_NOTIFICATION_RULE_DATA_ID");
22+
23+
MonitorNotificationRuleUpdateRequest body =
24+
new MonitorNotificationRuleUpdateRequest()
25+
.data(
26+
new MonitorNotificationRuleUpdateRequestData()
27+
.attributes(
28+
new MonitorNotificationRuleAttributes()
29+
.filter(
30+
new MonitorNotificationRuleFilter(
31+
new MonitorNotificationRuleFilterScope()
32+
.scope("test:example-monitor")))
33+
.name("updated rule")
34+
.recipients(Collections.singletonList("slack-test-channel")))
35+
.id(MONITOR_NOTIFICATION_RULE_DATA_ID)
36+
.type(MonitorNotificationRuleResourceType.MONITOR_NOTIFICATION_RULE));
37+
38+
try {
39+
MonitorNotificationRuleResponse result =
40+
apiInstance.updateMonitorNotificationRule(MONITOR_NOTIFICATION_RULE_DATA_ID, body);
41+
System.out.println(result);
42+
} catch (ApiException e) {
43+
System.err.println("Exception when calling MonitorsApi#updateMonitorNotificationRule");
44+
System.err.println("Status code: " + e.getCode());
45+
System.err.println("Reason: " + e.getResponseBody());
46+
System.err.println("Response headers: " + e.getResponseHeaders());
47+
e.printStackTrace();
48+
}
49+
}
50+
}

src/main/java/com/datadog/api/client/v2/model/MonitorNotificationRuleAttributes.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public MonitorNotificationRuleAttributes conditionalRecipients(
5454
}
5555

5656
/**
57-
* Use conditional recipients to define different recipients for different situations.
57+
* Use conditional recipients to define different recipients for different situations. Cannot be
58+
* used with <code>recipients</code>.
5859
*
5960
* @return conditionalRecipients
6061
*/
@@ -127,7 +128,7 @@ public MonitorNotificationRuleAttributes addRecipientsItem(String recipientsItem
127128

128129
/**
129130
* A list of recipients to notify. Uses the same format as the monitor <code>message</code> field.
130-
* Must not start with an '@'.
131+
* Must not start with an '@'. Cannot be used with <code>conditional_recipients</code>.
131132
*
132133
* @return recipients
133134
*/

src/main/java/com/datadog/api/client/v2/model/MonitorNotificationRuleCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public MonitorNotificationRuleCondition addRecipientsItem(String recipientsItem)
5656

5757
/**
5858
* A list of recipients to notify. Uses the same format as the monitor <code>message</code> field.
59-
* Must not start with an '@'.
59+
* Must not start with an '@'. Cannot be used with <code>conditional_recipients</code>.
6060
*
6161
* @return recipients
6262
*/

src/main/java/com/datadog/api/client/v2/model/MonitorNotificationRuleConditionalRecipients.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import java.util.Map;
2020
import java.util.Objects;
2121

22-
/** Use conditional recipients to define different recipients for different situations. */
22+
/**
23+
* Use conditional recipients to define different recipients for different situations. Cannot be
24+
* used with <code>recipients</code>.
25+
*/
2326
@JsonPropertyOrder({
2427
MonitorNotificationRuleConditionalRecipients.JSON_PROPERTY_CONDITIONS,
2528
MonitorNotificationRuleConditionalRecipients.JSON_PROPERTY_FALLBACK_RECIPIENTS
@@ -91,7 +94,7 @@ public MonitorNotificationRuleConditionalRecipients addFallbackRecipientsItem(
9194

9295
/**
9396
* A list of recipients to notify. Uses the same format as the monitor <code>message</code> field.
94-
* Must not start with an '@'.
97+
* Must not start with an '@'. Cannot be used with <code>conditional_recipients</code>.
9598
*
9699
* @return fallbackRecipients
97100
*/

src/main/java/com/datadog/api/client/v2/model/MonitorNotificationRuleFilter.java

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,54 @@ public MonitorNotificationRuleFilter deserialize(JsonParser jp, DeserializationC
127127
Level.FINER, "Input data does not match schema 'MonitorNotificationRuleFilterTags'", e);
128128
}
129129

130+
// deserialize MonitorNotificationRuleFilterScope
131+
try {
132+
boolean attemptParsing = true;
133+
// ensure that we respect type coercion as set on the client ObjectMapper
134+
if (MonitorNotificationRuleFilterScope.class.equals(Integer.class)
135+
|| MonitorNotificationRuleFilterScope.class.equals(Long.class)
136+
|| MonitorNotificationRuleFilterScope.class.equals(Float.class)
137+
|| MonitorNotificationRuleFilterScope.class.equals(Double.class)
138+
|| MonitorNotificationRuleFilterScope.class.equals(Boolean.class)
139+
|| MonitorNotificationRuleFilterScope.class.equals(String.class)) {
140+
attemptParsing = typeCoercion;
141+
if (!attemptParsing) {
142+
attemptParsing |=
143+
((MonitorNotificationRuleFilterScope.class.equals(Integer.class)
144+
|| MonitorNotificationRuleFilterScope.class.equals(Long.class))
145+
&& token == JsonToken.VALUE_NUMBER_INT);
146+
attemptParsing |=
147+
((MonitorNotificationRuleFilterScope.class.equals(Float.class)
148+
|| MonitorNotificationRuleFilterScope.class.equals(Double.class))
149+
&& (token == JsonToken.VALUE_NUMBER_FLOAT
150+
|| token == JsonToken.VALUE_NUMBER_INT));
151+
attemptParsing |=
152+
(MonitorNotificationRuleFilterScope.class.equals(Boolean.class)
153+
&& (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE));
154+
attemptParsing |=
155+
(MonitorNotificationRuleFilterScope.class.equals(String.class)
156+
&& token == JsonToken.VALUE_STRING);
157+
}
158+
}
159+
if (attemptParsing) {
160+
tmp = tree.traverse(jp.getCodec()).readValueAs(MonitorNotificationRuleFilterScope.class);
161+
// TODO: there is no validation against JSON schema constraints
162+
// (min, max, enum, pattern...), this does not perform a strict JSON
163+
// validation, which means the 'match' count may be higher than it should be.
164+
if (!((MonitorNotificationRuleFilterScope) tmp).unparsed) {
165+
deserialized = tmp;
166+
match++;
167+
}
168+
log.log(Level.FINER, "Input data matches schema 'MonitorNotificationRuleFilterScope'");
169+
}
170+
} catch (Exception e) {
171+
// deserialization failed, continue
172+
log.log(
173+
Level.FINER,
174+
"Input data does not match schema 'MonitorNotificationRuleFilterScope'",
175+
e);
176+
}
177+
130178
MonitorNotificationRuleFilter ret = new MonitorNotificationRuleFilter();
131179
if (match == 1) {
132180
ret.setActualInstance(deserialized);
@@ -162,10 +210,18 @@ public MonitorNotificationRuleFilter(MonitorNotificationRuleFilterTags o) {
162210
setActualInstance(o);
163211
}
164212

213+
public MonitorNotificationRuleFilter(MonitorNotificationRuleFilterScope o) {
214+
super("oneOf", Boolean.FALSE);
215+
setActualInstance(o);
216+
}
217+
165218
static {
166219
schemas.put(
167220
"MonitorNotificationRuleFilterTags",
168221
new GenericType<MonitorNotificationRuleFilterTags>() {});
222+
schemas.put(
223+
"MonitorNotificationRuleFilterScope",
224+
new GenericType<MonitorNotificationRuleFilterScope>() {});
169225
JSON.registerDescendants(
170226
MonitorNotificationRuleFilter.class, Collections.unmodifiableMap(schemas));
171227
}
@@ -177,7 +233,8 @@ public Map<String, GenericType> getSchemas() {
177233

178234
/**
179235
* Set the instance that matches the oneOf child schema, check the instance parameter is valid
180-
* against the oneOf child schemas: MonitorNotificationRuleFilterTags
236+
* against the oneOf child schemas: MonitorNotificationRuleFilterTags,
237+
* MonitorNotificationRuleFilterScope
181238
*
182239
* <p>It could be an instance of the 'oneOf' schemas. The oneOf child schemas may themselves be a
183240
* composed schema (allOf, anyOf, oneOf).
@@ -189,18 +246,27 @@ public void setActualInstance(Object instance) {
189246
super.setActualInstance(instance);
190247
return;
191248
}
249+
if (JSON.isInstanceOf(
250+
MonitorNotificationRuleFilterScope.class, instance, new HashSet<Class<?>>())) {
251+
super.setActualInstance(instance);
252+
return;
253+
}
192254

193255
if (JSON.isInstanceOf(UnparsedObject.class, instance, new HashSet<Class<?>>())) {
194256
super.setActualInstance(instance);
195257
return;
196258
}
197-
throw new RuntimeException("Invalid instance type. Must be MonitorNotificationRuleFilterTags");
259+
throw new RuntimeException(
260+
"Invalid instance type. Must be MonitorNotificationRuleFilterTags,"
261+
+ " MonitorNotificationRuleFilterScope");
198262
}
199263

200264
/**
201-
* Get the actual instance, which can be the following: MonitorNotificationRuleFilterTags
265+
* Get the actual instance, which can be the following: MonitorNotificationRuleFilterTags,
266+
* MonitorNotificationRuleFilterScope
202267
*
203-
* @return The actual instance (MonitorNotificationRuleFilterTags)
268+
* @return The actual instance (MonitorNotificationRuleFilterTags,
269+
* MonitorNotificationRuleFilterScope)
204270
*/
205271
@Override
206272
public Object getActualInstance() {
@@ -218,4 +284,16 @@ public MonitorNotificationRuleFilterTags getMonitorNotificationRuleFilterTags()
218284
throws ClassCastException {
219285
return (MonitorNotificationRuleFilterTags) super.getActualInstance();
220286
}
287+
288+
/**
289+
* Get the actual instance of `MonitorNotificationRuleFilterScope`. If the actual instance is not
290+
* `MonitorNotificationRuleFilterScope`, the ClassCastException will be thrown.
291+
*
292+
* @return The actual instance of `MonitorNotificationRuleFilterScope`
293+
* @throws ClassCastException if the instance is not `MonitorNotificationRuleFilterScope`
294+
*/
295+
public MonitorNotificationRuleFilterScope getMonitorNotificationRuleFilterScope()
296+
throws ClassCastException {
297+
return (MonitorNotificationRuleFilterScope) super.getActualInstance();
298+
}
221299
}

0 commit comments

Comments
 (0)