Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .generator/src/generator/templates/api_client.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ApiClient:
self.default_headers = {}
if self.configuration.compress:
self.default_headers["Accept-Encoding"] = "gzip"
if self.configuration.is_iac:
self.default_headers["X-Datadog-Managed-By"] = "iac"
# Set default User-Agent.
self.user_agent = user_agent()

Expand Down
9 changes: 9 additions & 0 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class Configuration:
:type delegated_auth_provider: str
:param delegated_auth_org_uuid: The organization UUID for delegated authentication.
:type delegated_auth_org_uuid: str
:param is_iac: Set to True to indicate that requests made through this client originate from
infrastructure-as-code or other automation tooling. When set, the client sends the
``X-Datadog-Managed-By: iac`` header on every request.
:type is_iac: bool
"""

def __init__(
Expand Down Expand Up @@ -179,6 +183,7 @@ class Configuration:
retry_policy=None,
delegated_auth_provider=None,
delegated_auth_org_uuid=None,
is_iac=False,
):
"""Constructor."""
self._base_path = "https://api.datadoghq.com" if host is None else host
Expand Down Expand Up @@ -230,6 +235,10 @@ class Configuration:
# Will translate to a Accept-Encoding header
self.compress = compress

# Set to True to indicate that requests are made from infrastructure-as-code tooling.
# Will translate to a X-Datadog-Managed-By header.
self.is_iac = is_iac

self.return_http_data_only = return_http_data_only
self.preload_content = preload_content
self.request_timeout = request_timeout
Expand Down
2 changes: 2 additions & 0 deletions src/datadog_api_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def __init__(self, configuration: Configuration):
self.default_headers = {}
if self.configuration.compress:
self.default_headers["Accept-Encoding"] = "gzip"
if self.configuration.is_iac:
self.default_headers["X-Datadog-Managed-By"] = "iac"
# Set default User-Agent.
self.user_agent = user_agent()

Expand Down
9 changes: 9 additions & 0 deletions src/datadog_api_client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ class Configuration:
:type delegated_auth_provider: str
:param delegated_auth_org_uuid: The organization UUID for delegated authentication.
:type delegated_auth_org_uuid: str
:param is_iac: Set to True to indicate that requests made through this client originate from
infrastructure-as-code or other automation tooling. When set, the client sends the
``X-Datadog-Managed-By: iac`` header on every request.
:type is_iac: bool
"""

def __init__(
Expand Down Expand Up @@ -180,6 +184,7 @@ def __init__(
retry_policy=None,
delegated_auth_provider=None,
delegated_auth_org_uuid=None,
is_iac=False,
):
"""Constructor."""
self._base_path = "https://api.datadoghq.com" if host is None else host
Expand Down Expand Up @@ -231,6 +236,10 @@ def __init__(
# Will translate to a Accept-Encoding header
self.compress = compress

# Set to True to indicate that requests are made from infrastructure-as-code tooling.
# Will translate to a X-Datadog-Managed-By header.
self.is_iac = is_iac

self.return_http_data_only = return_http_data_only
self.preload_content = preload_content
self.request_timeout = request_timeout
Expand Down
18 changes: 18 additions & 0 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,21 @@ def authenticate(self, config, api_config):
api_client.use_delegated_token_auth(headers3)
assert headers3["Authorization"] == "Bearer token-2"
assert call_count == 2 # New call made


class TestClientIsIac:
"""Test that the is_iac configuration flag controls the X-Datadog-Managed-By header."""

def test_is_iac_header_absent_by_default(self):
"""Test that the header is not set when is_iac is left at its default."""
config = Configuration()
api_client = ApiClient(config)

assert "X-Datadog-Managed-By" not in api_client.default_headers

def test_is_iac_header_present_when_enabled(self):
"""Test that the header is set when is_iac is enabled on the configuration."""
config = Configuration(is_iac=True)
api_client = ApiClient(config)

assert api_client.default_headers["X-Datadog-Managed-By"] == "iac"
Loading