Skip to content

Commit bf6d74c

Browse files
api-clients-generation-pipeline[bot]nogatesci.datadog-api-spec
authored
Add is_iac support to send X-Datadog-Managed-By header (#4239)
* Add tests for IaC managed-by header * Regenerate client from commit 9324879 of spec repo --------- Co-authored-by: David Tapiador <david.tapiadordeldujo@datadoghq.com> Co-authored-by: ci.datadog-api-spec <packages@datadoghq.com>
1 parent edc5ffc commit bf6d74c

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/main/java/com/datadog/api/client/ApiClient.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class ApiClient {
7777
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
7878
protected String basePath = "https://api.datadoghq.com";
7979
protected String userAgent;
80+
private boolean isIaC = false;
8081
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
8182

8283
protected List<ServerConfiguration> servers =
@@ -1937,6 +1938,33 @@ public ApiClient addDefaultHeader(String key, String value) {
19371938
return this;
19381939
}
19391940

1941+
/**
1942+
* Set whether this API client is managed by Infrastructure as Code (IaC) tooling. When enabled,
1943+
* the {@code X-Datadog-Managed-By: iac} header is sent on every request made through this client
1944+
* (by adding to the default header map).
1945+
*
1946+
* @param isIaC Whether calls made with this client originate from IaC tooling
1947+
* @return API client
1948+
*/
1949+
public ApiClient setIsIaC(boolean isIaC) {
1950+
this.isIaC = isIaC;
1951+
if (isIaC) {
1952+
addDefaultHeader("X-Datadog-Managed-By", "iac");
1953+
} else {
1954+
defaultHeaderMap.remove("X-Datadog-Managed-By");
1955+
}
1956+
return this;
1957+
}
1958+
1959+
/**
1960+
* Get whether this API client is managed by Infrastructure as Code (IaC) tooling.
1961+
*
1962+
* @return Whether this client is managed by IaC tooling
1963+
*/
1964+
public boolean getIsIaC() {
1965+
return isIaC;
1966+
}
1967+
19401968
/**
19411969
* Add a default cookie.
19421970
*
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.datadog.api.client;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertSame;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import org.junit.Test;
9+
10+
public class ApiClientTest {
11+
12+
@Test
13+
public void testIsIaCDisabledByDefault() {
14+
ApiClient client = new ApiClient();
15+
assertFalse(client.getIsIaC());
16+
assertFalse(client.defaultHeaderMap.containsKey("X-Datadog-Managed-By"));
17+
}
18+
19+
@Test
20+
public void testSetIsIaCTrueAddsManagedByHeaderToDefaultHeaders() {
21+
ApiClient client = new ApiClient();
22+
23+
ApiClient returned = client.setIsIaC(true);
24+
25+
assertSame(client, returned);
26+
assertTrue(client.getIsIaC());
27+
// defaultHeaderMap is merged into the headers of every request made through this client,
28+
// see ApiClient#createBuilder.
29+
assertEquals("iac", client.defaultHeaderMap.get("X-Datadog-Managed-By"));
30+
}
31+
32+
@Test
33+
public void testSetIsIaCFalseRemovesManagedByHeader() {
34+
ApiClient client = new ApiClient();
35+
client.setIsIaC(true);
36+
37+
client.setIsIaC(false);
38+
39+
assertFalse(client.getIsIaC());
40+
assertFalse(client.defaultHeaderMap.containsKey("X-Datadog-Managed-By"));
41+
}
42+
43+
@Test
44+
public void testSetIsIaCIsPerClientInstance() {
45+
ApiClient iacClient = new ApiClient();
46+
ApiClient defaultClient = new ApiClient();
47+
48+
iacClient.setIsIaC(true);
49+
50+
assertTrue(iacClient.getIsIaC());
51+
assertFalse(defaultClient.getIsIaC());
52+
assertFalse(defaultClient.defaultHeaderMap.containsKey("X-Datadog-Managed-By"));
53+
}
54+
}

0 commit comments

Comments
 (0)