Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hosts API #261

Merged
merged 4 commits into from
Apr 27, 2018
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
2 changes: 1 addition & 1 deletion datadog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from datadog.api.monitors import Monitor
from datadog.api.screenboards import Screenboard
from datadog.api.graphs import Graph, Embed
from datadog.api.hosts import Host
from datadog.api.hosts import Host, Hosts
from datadog.api.service_checks import ServiceCheck
from datadog.api.tags import Tag
from datadog.api.users import User
44 changes: 43 additions & 1 deletion datadog/api/hosts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datadog.api.resources import ActionAPIResource
from datadog.api.resources import ActionAPIResource, SearchableAPIResource


class Host(ActionAPIResource):
Expand Down Expand Up @@ -42,3 +42,45 @@ def unmute(cls, host_name):
"""
return super(Host, cls)._trigger_class_action('POST', 'unmute', host_name)


class Hosts(ActionAPIResource, SearchableAPIResource):
"""
A wrapper around Hosts HTTP API.
"""
_resource_name = 'hosts'

@classmethod
def search(cls, **params):
"""
Search among hosts live within the past 2 hours. Max 100
results at a time.
:param filter: query to filter search results
:type filter: string
:param sort_field: "status", "apps", "cpu", "iowait", or "load"
:type sort_field: string
:param sort_dir: "asc" or "desc"
:type sort_dir: string
:param start: host result to start at
:type start: integer
:param count: number of host results to return
:type count: integer
:returns: Dictionary representing the API's JSOn response
"""
return super(Hosts, cls)._search(**params)

@classmethod
def totals(cls):
"""
Get total number of hosts active and up.
:returns: Dictionary representing the API's JSON response
"""
return super(Hosts, cls)._trigger_class_action('GET', 'totals')
5 changes: 5 additions & 0 deletions datadog/api/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ def search(cls, **params):
:returns: Dictionary representing the API's JSON response
"""
# Deprecate the hosts search param
query = params.get('q', '').split(':')
if len(query) > 1 and query[0] == 'hosts':
print("[DEPRECATION] Infrastructure.search() is deprecated for ",
"hosts. Use `Hosts.search` instead.")
return super(Infrastructure, cls)._search(**params)