Skip to content
Merged
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
1 change: 1 addition & 0 deletions datadog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
from datadog.api.service_checks import ServiceCheck
from datadog.api.tags import Tag
from datadog.api.users import User
from datadog.api.service_level_objectives import ServiceLevelObjective
13 changes: 10 additions & 3 deletions datadog/api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _get_http_client(cls):

@classmethod
def submit(cls, method, path, api_version=None, body=None, attach_host_name=False,
response_formatter=None, error_formatter=None, **params):
response_formatter=None, error_formatter=None, suppress_response_errors_on_codes=None, **params):
"""
Make an HTTP API request

Expand All @@ -70,6 +70,10 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
:param attach_host_name: link the new resource object to the host name
:type attach_host_name: bool

:param suppress_response_errors_on_codes: suppress ApiError on `errors` key in the response for the given HTTP
status codes
:type suppress_response_errors_on_codes: None|list(int)

:param params: dictionary to be sent in the query string of the request
:type params: dictionary

Expand Down Expand Up @@ -151,7 +155,10 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
raise ValueError('Invalid JSON response: {0}'.format(content))

if response_obj and 'errors' in response_obj:
raise ApiError(response_obj)
# suppress ApiError when specified and just return the response
if not (suppress_response_errors_on_codes and
result.status_code in suppress_response_errors_on_codes):
raise ApiError(response_obj)
else:
response_obj = None

Expand All @@ -177,7 +184,7 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
raise
except ApiError as e:
if _mute:
for error in e.args[0]['errors']:
for error in (e.args[0].get('errors') or []):
log.error(error)
if error_formatter is None:
return e.args[0]
Expand Down
139 changes: 139 additions & 0 deletions datadog/api/service_level_objectives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from datadog.api.resources import (
GetableAPIResource,
CreateableAPIResource,
UpdatableAPIResource,
ListableAPIResource,
DeletableAPIResource,
ActionAPIResource,
)


class ServiceLevelObjective(
GetableAPIResource,
CreateableAPIResource,
UpdatableAPIResource,
ListableAPIResource,
DeletableAPIResource,
ActionAPIResource,
):
"""
A wrapper around Service Level Objective HTTP API.
"""

_resource_name = "slo"

@classmethod
def create(
cls, attach_host_name=False, method="POST", id=None, params=None, **body
):
"""
Create a SLO

:returns: created SLO details
"""
return super(ServiceLevelObjective, cls).create(
attach_host_name=False, method="POST", id=None, params=params, **body
)

@classmethod
def get(cls, id, **params):
"""
Get a specific SLO details.

:param id: SLO id to get details for
:type id: str

:returns: SLO details
"""
return super(ServiceLevelObjective, cls).get(id, **params)

@classmethod
def get_all(cls, query=None, ids=None, offset=0, limit=100, **params):
"""
Get all SLO details.

:param query: optional search query - syntax in UI && online documentation
:type query: str

:param ids: optional list of SLO ids to get many specific SLOs at once.
:type ids: list(str)

:param offset: offset of results to use (default 0)
:type offset: int

:param limit: limit of results to return (default: 1000)
:type limit: int

:returns: SLOs matching the query
"""
search_terms = {}
if query:
search_terms["query"] = query
if ids:
search_terms["ids"] = ids
search_terms["offset"] = offset
search_terms["limit"] = limit

return super(ServiceLevelObjective, cls).get_all(**search_terms)

@classmethod
def update(cls, id, params=None, **body):
"""
Update a specific SLO details.

:param id: SLO id to update details for
:type id: str

:returns: SLO details
"""
return super(ServiceLevelObjective, cls).update(id, params, **body)

@classmethod
def delete(cls, id, **params):
"""
Delete a specific SLO.

:param id: SLO id to delete
:type id: str

:returns: SLO ids removed
"""
return super(ServiceLevelObjective, cls).delete(id, **params)

@classmethod
def bulk_delete(cls, ops, **params):
"""
Bulk Delete Timeframes from multiple SLOs.

:param ops: a dictionary mapping of SLO ID to timeframes to remove.
:type ops: dict(str, list(str))

:returns: Dictionary representing the API's JSON response
`errors` - errors with operation
`data` - updates and deletions
"""
return super(ServiceLevelObjective, cls)._trigger_class_action(
"POST",
"bulk_delete",
body=ops,
params=params,
suppress_response_errors_on_codes=[200],
)

@classmethod
def delete_many(cls, ids, **params):
"""
Delete Multiple SLOs

:param ids: a list of SLO IDs to remove
:type ids: list(str)

:returns: Dictionary representing the API's JSON response see `data` list(slo ids) && `errors`
"""
return super(ServiceLevelObjective, cls)._trigger_class_action(
"DELETE",
"",
params=params,
body={"ids": ids},
suppress_response_errors_on_codes=[200],
)
Loading