Skip to content

Commit

Permalink
add python api wrapper and tests for the embed api
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Lai committed Jul 15, 2015
1 parent 75bff4b commit 46a7592
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 2 deletions.
2 changes: 1 addition & 1 deletion datadog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from datadog.api.metrics import Metric
from datadog.api.monitors import Monitor
from datadog.api.screenboards import Screenboard
from datadog.api.graphs import Graph
from datadog.api.graphs import Graph, Embed
from datadog.api.hosts import Host
from datadog.api.service_checks import ServiceCheck
from datadog.api.tags import Tag
Expand Down
95 changes: 94 additions & 1 deletion datadog/api/graphs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from datadog.util.compat import urlparse
from datadog.api.base import CreateableAPIResource, ActionAPIResource
from datadog.api.base import (
CreateableAPIResource,
ActionAPIResource,
GetableAPIResource,
ListableAPIResource
)


class Graph(CreateableAPIResource, ActionAPIResource):
Expand Down Expand Up @@ -45,3 +50,91 @@ def status(cls, snapshot_url):
snapshot_status_url = '/graph/snapshot_status/{0}'.format(snap_path)

return super(Graph, cls)._trigger_action('GET', snapshot_status_url)


class Embed(ListableAPIResource, GetableAPIResource, ActionAPIResource):
"""
A wrapper around Embed HTTP API.
"""
_class_url = '/graph/embed'

@classmethod
def get_all(cls):
"""
Returns a JSON object containing a list of all embeddable graphs
in the API user's organization.
:returns: JSON response from HTTP API request
"""
return super(Embed, cls).get_all()

@classmethod
def get(cls, embed_id, **params):
"""
Returns a JSON object representing the specified embed.
:param embed_id: embed token
:type embed_id: string embed token
:param size: graph size
:type size: string graph size
:param legend: legend flag
:type legend: string legend flag
:param template_vars: template variable values
:type template_vars: string values (each var is a kv pair of **params)
:returns: JSON response from HTTP API request
"""
return super(Embed, cls).get(embed_id, **params)

@classmethod
def create(cls, **params):
"""
Returns a JSON object representing the specified embed.
:param graph_json: graph definition
:type graph_json: JSON string graph definition
:param timeframe: graph timeframe
:type timeframe: string graph timeframe
:param size: graph size
:type size: string graph size
:param legend: legend flag
:type legend: string legend flag
:param title: graph title
:type title: string title
:returns: JSON response from HTTP API request
"""
return super(Embed, cls)._trigger_action('POST', name=cls._class_url, **params)

@classmethod
def enable(cls, embed_id, **params):
"""
Enable a specified embed.
:param embed_id: embed token
:type embed_id: string embed token
:returns: JSON response from HTTP API request
"""
handle = embed_id + "/enable"
return super(Embed, cls).get(handle)

@classmethod
def revoke(cls, embed_id):
"""
Revoke a specified embed.
:param embed_id: embed token
:type embed_id: string embed token
:returns: JSON response from HTTP API request
"""
handle = embed_id + "/revoke"
return super(Embed, cls).get(handle)
147 changes: 147 additions & 0 deletions tests/integration/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,5 +753,152 @@ def test_host_muting(self):

dog.Host.unmute(hostname)

@attr('embed')
def test_get_all_embeds(self):
all_embeds = dog.Embed.get_all()
# Check all embeds is a valid response
assert "embedded_graphs" in all_embeds

@attr('embed')
def test_create_embed(self):
# Initialize a graph definition
graph_def = {
"viz": "toplist",
"requests": [{
"q": "top(system.disk.free{$var} by {device}, 10, 'mean', 'desc')",
"style": {
"palette": "dog_classic"
},
"conditional_formats": [{
"palette": "red",
"comparator": ">",
"value": 50000000000
}, {
"palette": "green",
"comparator": ">",
"value": 30000000000
}]
}]
}
timeframe = "1_hour"
size = "medium"
legend = "no"
title = "Custom titles!"
# Dump the dictionary to a JSON string and make an API call
graph_json = json.dumps(graph_def)
result = dog.Embed.create(graph_json=graph_json, timeframe=timeframe, size=size, legend=legend, title=title)
# Check various result attributes
assert "embed_id" in result
assert result["revoked"] is False
assert len(result["template_variables"]) == 1
assert result["template_variables"][0] == "var"
assert "html" in result
assert result["graph_title"] == title

@attr('embed')
def test_get_embed(self):
# Create a graph that we can try getting
graph_def = {
"viz": "toplist",
"requests": [{
"q": "top(system.disk.free{$var} by {device}, 10, 'mean', 'desc')",
"style": {
"palette": "dog_classic"
},
"conditional_formats": [{
"palette": "red",
"comparator": ">",
"value": 50000000000
}, {
"palette": "green",
"comparator": ">",
"value": 30000000000
}]
}]
}
timeframe = "1_hour"
size = "medium"
legend = "no"
graph_json = json.dumps(graph_def)
created_graph = dog.Embed.create(graph_json=graph_json, timeframe=timeframe, size=size, legend=legend)
# Save the html to check against replaced var get
html = created_graph["html"]
# Save the embed_id into a variable and get it again
embed_id = created_graph["embed_id"]
response_graph = dog.Embed.get(embed_id, var="asdfasdfasdf")
# Check the graph has the same embed_id and the template_var is added to the url
assert "embed_id" in response_graph
assert response_graph["embed_id"] == embed_id
assert len(response_graph["html"]) > len(html)

@attr('embed')
def test_enable_embed(self):
# Create a graph that we can try getting
graph_def = {
"viz": "toplist",
"requests": [{
"q": "top(system.disk.free{$var} by {device}, 10, 'mean', 'desc')",
"style": {
"palette": "dog_classic"
},
"conditional_formats": [{
"palette": "red",
"comparator": ">",
"value": 50000000000
}, {
"palette": "green",
"comparator": ">",
"value": 30000000000
}]
}]
}
timeframe = "1_hour"
size = "medium"
legend = "no"
graph_json = json.dumps(graph_def)
created_graph = dog.Embed.create(graph_json=graph_json, timeframe=timeframe, size=size, legend=legend)
# Save the embed_id into a variable and enable it again
embed_id = created_graph["embed_id"]
result = dog.Embed.enable(embed_id)
# Check that the graph is enabled again
assert "success" in result

@attr('embed')
def test_revoke_embed(self):
# Create a graph that we can try getting
graph_def = {
"viz": "toplist",
"requests": [{
"q": "top(system.disk.free{$var} by {device}, 10, 'mean', 'desc')",
"style": {
"palette": "dog_classic"
},
"conditional_formats": [{
"palette": "red",
"comparator": ">",
"value": 50000000000
}, {
"palette": "green",
"comparator": ">",
"value": 30000000000
}]
}]
}
timeframe = "1_hour"
size = "medium"
legend = "no"
graph_json = json.dumps(graph_def)
created_graph = dog.Embed.create(graph_json=graph_json, timeframe=timeframe, size=size, legend=legend)
# Save the embed_id into a variable and enable it again
embed_id = created_graph["embed_id"]
result = dog.Embed.revoke(embed_id)
# Check embed is revoked and that we can't get it again
assert "success" in result
try:
dog.Embed.get(embed_id)
except ApiError, e:
assert True


if __name__ == '__main__':
unittest.main()

0 comments on commit 46a7592

Please sign in to comment.