Skip to content
Draft
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
12 changes: 11 additions & 1 deletion datadog/api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import time
import zlib
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type

# datadog
from datadog.api import _api_version, _max_timeouts, _backoff_period
Expand All @@ -14,6 +15,9 @@
from datadog.util.compat import is_p3k
from datadog.util.format import construct_url, normalize_tags

if TYPE_CHECKING:
from datadog.api.http_client import HTTPClient # noqa: F401


log = logging.getLogger("datadog.api")

Expand All @@ -27,7 +31,7 @@ class APIClient(object):
# HTTP transport parameters
_backoff_period = _backoff_period
_max_timeouts = _max_timeouts
_backoff_timestamp = None
_backoff_timestamp = None # type: Optional[float]
_timeout_counter = 0
_sort_keys = False

Expand All @@ -36,6 +40,7 @@ class APIClient(object):

@classmethod
def _get_http_client(cls):
# type: () -> Type[HTTPClient]
"""
Getter for the embedded HTTP client.
"""
Expand All @@ -58,6 +63,7 @@ def submit(
compress_payload=False,
**params
):
# type: (str, str, Optional[str], Optional[Any], bool, Optional[Any], Optional[Any], Optional[List[int]], bool, **Any) -> Any
"""
Make an HTTP API request

Expand Down Expand Up @@ -154,6 +160,7 @@ def submit(
headers["Content-Type"] = "application/json"

if compress_payload:
assert body is not None
body = zlib.compress(body.encode("utf-8"))
headers["Content-Encoding"] = "deflate"

Expand Down Expand Up @@ -237,6 +244,7 @@ def submit(

@classmethod
def _should_submit(cls):
# type: () -> bool
"""
Returns True if we're in a state where we should make a request
(backoff expired, no backoff in effect), false otherwise.
Expand Down Expand Up @@ -281,10 +289,12 @@ def _should_submit(cls):

@classmethod
def _backoff_status(cls):
# type: () -> Tuple[float, float]
"""
Get a backoff report, i.e. backoff total and remaining time.
"""
now = time.time()
assert cls._backoff_timestamp is not None
backed_off_time = now - cls._backoff_timestamp
backoff_time_left = cls._backoff_period - backed_off_time
return round(backed_off_time, 2), round(backoff_time_left, 2)
17 changes: 13 additions & 4 deletions datadog/api/aws_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
from typing import Any, Optional

from datadog.api.resources import (
GetableAPIResource,
CreateableAPIResource,
Expand All @@ -25,9 +27,11 @@ class AwsIntegration(

_resource_name = "integration"
_resource_id = "aws"
_sub_resource_name = "" # type: str

@classmethod
def list(cls, **params):
# type: (**Any) -> Any
"""
List all Datadog-AWS integrations available in your Datadog organization.

Expand All @@ -36,7 +40,8 @@ def list(cls, **params):
return super(AwsIntegration, cls).get(id=cls._resource_id, **params)

@classmethod
def create(cls, **params):
def create(cls, attach_host_name=False, method="POST", id=None, params=None, **body):
# type: (bool, str, Optional[Any], Optional[Any], **Any) -> Any
"""
Add a new AWS integration config.

Expand Down Expand Up @@ -94,10 +99,11 @@ def create(cls, **params):
account_specific_namespace_rules=account_specific_namespace_rules \
excluded_regions=excluded_regions)
"""
return super(AwsIntegration, cls).create(id=cls._resource_id, **params)
return super(AwsIntegration, cls).create(id=cls._resource_id, **body)

@classmethod
def update(cls, **body):
def update(cls, id=None, params=None, **body):
# type: (Optional[Any], Optional[Any], **Any) -> Any
"""
Update an AWS integration config.

Expand Down Expand Up @@ -213,7 +219,8 @@ def update(cls, **body):
return super(AwsIntegration, cls).update(id=cls._resource_id, params=params, **body)

@classmethod
def delete(cls, **body):
def delete(cls, id=None, **body):
# type: (Optional[Any], **Any) -> Any
"""
Delete a given Datadog-AWS integration.

Expand All @@ -226,6 +233,7 @@ def delete(cls, **body):

@classmethod
def list_namespace_rules(cls, **params):
# type: (**Any) -> Any
"""
List all namespace rules available as options.

Expand All @@ -236,6 +244,7 @@ def list_namespace_rules(cls, **params):

@classmethod
def generate_new_external_id(cls, **params):
# type: (**Any) -> Any
"""
Generate a new AWS external id for a given AWS account id and role name pair.

Expand Down
10 changes: 10 additions & 0 deletions datadog/api/aws_log_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
from typing import Any

from datadog.api.resources import DeletableAPISubResource, ListableAPISubResource, AddableAPISubResource


Expand All @@ -11,9 +13,11 @@ class AwsLogsIntegration(DeletableAPISubResource, ListableAPISubResource, Addabl

_resource_name = "integration"
_resource_id = "aws"
_sub_resource_name = "" # type: str

@classmethod
def list_log_services(cls, **params):
# type: (**Any) -> Any
"""
List all namespace rules available as options.

Expand All @@ -24,6 +28,7 @@ def list_log_services(cls, **params):

@classmethod
def add_log_lambda_arn(cls, **params):
# type: (**Any) -> Any
"""
Attach the Lambda ARN of the Lambda created for the Datadog-AWS \
log collection to your AWS account ID to enable log collection.
Expand All @@ -38,6 +43,7 @@ def add_log_lambda_arn(cls, **params):

@classmethod
def save_services(cls, **params):
# type: (**Any) -> Any
"""
Enable Automatic Log collection for your AWS services.

Expand All @@ -51,6 +57,7 @@ def save_services(cls, **params):

@classmethod
def delete_config(cls, **params):
# type: (**Any) -> Any
"""
Delete a Datadog-AWS log collection configuration by removing the specific Lambda ARN \
associated with a given AWS account.
Expand All @@ -65,6 +72,7 @@ def delete_config(cls, **params):

@classmethod
def check_lambda(cls, **params):
# type: (**Any) -> Any
"""
Check function to see if a lambda_arn exists within an account. \
This sends a job on our side if it does not exist, then immediately returns \
Expand All @@ -86,6 +94,7 @@ def check_lambda(cls, **params):

@classmethod
def check_services(cls, **params):
# type: (**Any) -> Any
"""
Test if permissions are present to add log-forwarding triggers for the \
given services + AWS account. Input is the same as for save_services.
Expand All @@ -102,6 +111,7 @@ def check_services(cls, **params):

@classmethod
def list(cls, **params):
# type: (**Any) -> Any
"""
List all Datadog-AWS Logs integrations available in your Datadog organization.

Expand Down
20 changes: 14 additions & 6 deletions datadog/api/azure_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
from typing import Any, Dict, Optional

from datadog.api.resources import (
GetableAPIResource,
CreateableAPIResource,
Expand All @@ -19,9 +21,11 @@ class AzureIntegration(

_resource_name = "integration"
_resource_id = "azure"
_sub_resource_name = "" # type: str

@classmethod
def list(cls, **params):
# type: (**Any) -> Any
"""
List all Datadog-Azure integrations available in your Datadog organization.

Expand All @@ -30,7 +34,8 @@ def list(cls, **params):
return super(AzureIntegration, cls).get(id=cls._resource_id, **params)

@classmethod
def create(cls, **params):
def create(cls, attach_host_name=False, method="POST", id=None, params=None, **body):
# type: (bool, str, Optional[Any], Optional[Dict[str, Any]], **Any) -> Any
"""
Add a new Azure integration config.

Expand All @@ -42,10 +47,11 @@ def create(cls, **params):
>>> api.AzureIntegration.create(tenant_name=tenant_name, client_id=client_id, \
client_secret=client_secret,host_filters=host_filters)
"""
return super(AzureIntegration, cls).create(id=cls._resource_id, **params)
return super(AzureIntegration, cls).create(id=cls._resource_id, **body)

@classmethod
def delete(cls, **body):
def delete(cls, id=None, **body):
# type: (Optional[Any], **Any) -> Any
"""
Delete a given Datadog-Azure integration.

Expand All @@ -58,6 +64,7 @@ def delete(cls, **body):

@classmethod
def update_host_filters(cls, **params):
# type: (**Any) -> Any
"""
Update the defined list of host filters for a given Datadog-Azure integration. \

Expand All @@ -72,7 +79,8 @@ def update_host_filters(cls, **params):
return super(AzureIntegration, cls).add_items(id=cls._resource_id, **params)

@classmethod
def update(cls, **body):
def update(cls, id=None, params=None, **body):
# type: (Optional[Any], Optional[Dict[str, Any]], **Any) -> Any
"""
Update an Azure account configuration.

Expand All @@ -87,5 +95,5 @@ def update(cls, **body):
new_tenant_name=new_tenant_name, new_client_id=new_client_id,\
client_secret=client_secret, host_filters=host_filters)
"""
params = {}
return super(AzureIntegration, cls).update(id=cls._resource_id, params=params, **body)
actual_params = {} # type: Dict[str, Any]
return super(AzureIntegration, cls).update(id=cls._resource_id, params=actual_params, **body)
3 changes: 3 additions & 0 deletions datadog/api/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
# datadog
from typing import Any, Optional

from datadog.api.format import format_points
from datadog.api.resources import SendableAPIResource

Expand All @@ -13,6 +15,7 @@ class Distribution(SendableAPIResource):

@classmethod
def send(cls, distributions=None, attach_host_name=True, compress_payload=False, **distribution):
# type: (Optional[Any], bool, bool, **Any) -> Any
"""
Submit a distribution metric or a list of distribution metrics to the distribution metric
API
Expand Down
3 changes: 3 additions & 0 deletions datadog/api/downtimes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
from typing import Any

from datadog.api.resources import (
GetableAPIResource,
CreateableAPIResource,
Expand All @@ -27,6 +29,7 @@ class Downtime(

@classmethod
def cancel_downtime_by_scope(cls, **body):
# type: (**Any) -> Any
"""
Cancels all downtimes matching the scope.

Expand Down
13 changes: 9 additions & 4 deletions datadog/api/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
from typing import Any, Dict, Optional

from datadog.api.exceptions import ApiError
from datadog.api.resources import GetableAPIResource, CreateableAPIResource, SearchableAPIResource
from datadog.util.compat import iteritems
Expand All @@ -15,7 +17,8 @@ class Event(GetableAPIResource, CreateableAPIResource, SearchableAPIResource):
_timestamp_keys = set(["start", "end"])

@classmethod
def create(cls, attach_host_name=True, **params):
def create(cls, attach_host_name=True, method="POST", id=None, params=None, **body):
# type: (bool, str, Optional[Any], Optional[Dict[str, Any]], **Any) -> Any
"""
Post an event.

Expand Down Expand Up @@ -62,14 +65,15 @@ def create(cls, attach_host_name=True, **params):

>>> api.Event.create(title=title, text=text, tags=tags)
"""
if params.get("alert_type"):
if params["alert_type"] not in ["error", "warning", "info", "success"]:
if body.get("alert_type"):
if body["alert_type"] not in ["error", "warning", "info", "success"]:
raise ApiError("Parameter alert_type must be either error, warning, info or success")

return super(Event, cls).create(attach_host_name=attach_host_name, **params)
return super(Event, cls).create(attach_host_name=attach_host_name, method=method, id=id, params=params, **body)

@classmethod
def query(cls, **params):
# type: (**Any) -> Any
"""
Get the events that occurred between the *start* and *end* POSIX timestamps,
optional filtered by *priority* ("low" or "normal"), *sources* and
Expand All @@ -85,6 +89,7 @@ def query(cls, **params):
"""

def timestamp_to_integer(k, v):
# type: (str, Any) -> Any
if k in cls._timestamp_keys:
return int(v)
else:
Expand Down
Loading
Loading