Skip to content

Commit

Permalink
Merge pull request #62 from DataDog/charles/embed_api_py
Browse files Browse the repository at this point in the history
[Embed API] Python Wrapper
  • Loading branch information
yannmh committed Jul 30, 2015
2 parents 8b8a45d + 5a63f24 commit 1c41915
Show file tree
Hide file tree
Showing 3 changed files with 183 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 @@ -25,7 +25,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
38 changes: 37 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,34 @@ 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, CreateableAPIResource):
"""
A wrapper around Embed HTTP API.
"""
_class_url = '/graph/embed'

@classmethod
def enable(cls, embed_id):
"""
Enable a specified embed.
:param embed_id: embed token
:type embed_id: string embed token
:returns: JSON response from HTTP API request
"""
return super(Embed, cls)._trigger_class_action('GET', id=embed_id, name='enable')

@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
"""
return super(Embed, cls)._trigger_class_action('GET', id=embed_id, name='revoke')
145 changes: 145 additions & 0 deletions tests/integration/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,5 +753,150 @@ 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
with self.assertRaises(ApiError):
dog.Embed.get(embed_id)


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

0 comments on commit 1c41915

Please sign in to comment.