Created
October 16, 2015 17:06
-
-
Save jderrett/fa9128b4a446e3337f01 to your computer and use it in GitHub Desktop.
Update the source on all streams inside a set of Librato Spaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, json | |
username = '[email protected] | |
token = 'abcd1234' | |
api = "https://metrics-api.librato.com" | |
# Find spaces | |
search = 'string to search' | |
url = api + "/v1/spaces?name=%s" % search | |
response = requests.get(url, auth=(username, token)) | |
spaces = response.json()['spaces'] | |
# Iterate through the spaces and update the charts | |
updated_streams = [] | |
old_source = 'austin' | |
new_source = 'sf' | |
for space in spaces: | |
print space['name'] | |
# http://dev.librato.com/v1/get/spaces/:id/charts | |
path = "/v1/spaces/%d/charts" % space['id'] | |
print path | |
response = requests.get(api + path, auth=(username, token)) | |
charts = response.json() | |
# Now find all the streams with the source we want to replace | |
for chart in charts: | |
# Iterate through the streams and do the replacement | |
changed = False | |
for stream in chart['streams']: | |
if stream.get('source') == old_source: | |
print "replace source in " + str({"chart_name": chart.get('name'), "stream_id": stream['id'], "source": stream.get('source')}) + " with " + new_source | |
# Here's where the magic happens | |
# Update the source | |
stream['source'] = new_source | |
updated_streams.append(stream) | |
changed = True | |
if changed: | |
# Now update the chart | |
# http://dev.librato.com/v1/put/spaces/:id/charts/:id | |
print "Updating chart " + chart.get('name') | |
path = "/v1/spaces/%d/charts/%d" % (space['id'], chart['id']) | |
url = api + path | |
print url | |
# The chart is the payload - it includes the streams | |
# Only updating the streams here | |
payload = {"streams": chart['streams']} | |
response = requests.put(url, json=payload, auth=(username, token), headers={'Content-type': 'application/json'}) | |
print "Status code: %s" % response.status_code | |
else: | |
print "Found nothing to update in %s" % chart.get('name') | |
# print updated_streams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment