Created
February 26, 2016 11:34
-
-
Save sacreman/32ef355f9ec52ac6acf8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import sys | |
import time | |
import yaml | |
import json | |
import requests | |
url = 'https://app.dataloop.io' | |
api_key = 'redacted' | |
org = 'organisation' | |
account = 'account' | |
default_dashboard = 'base' | |
automated_dashboard = 'automated' | |
perf_data = '' | |
def check_dashboard_exists(dashboard_to_check): | |
dashboards = requests.get(url + '/api/v1/orgs/' + org + '/accounts/' + account + '/dashboards', headers={"Token": api_key}).json() | |
for dashboard in dashboards: | |
if dashboard['name'] == dashboard_to_check: | |
return True | |
return False | |
def delete_dashboard(dashboard_to_delete): | |
return requests.delete(url + '/api/v1/orgs/' + org + '/accounts/' + account + '/dashboards/' + dashboard_to_delete, headers={"Token": api_key}) | |
#### get all dashboards | |
test_name = 'get_all_dashboards' | |
t0 = time.time() | |
try: | |
# delete the automated dashboard if it exists | |
if check_dashboard_exists(automated_dashboard): | |
delete_dashboard(automated_dashboard) | |
# check if the default dashboard exists | |
if check_dashboard_exists(default_dashboard) == False: | |
print "dashboard %s does not exist" % default_dashboard | |
sys.exit(2) | |
except Exception, e: | |
print test_name + ' failed: %s' % e | |
sys.exit(2) | |
t1 = time.time() | |
perf_data += test_name + '=' + str(round(t1 - t0, 2)) + 's;;;; ' | |
#### export dashboard | |
test_name = 'export_dashboard' | |
t0 = time.time() | |
try: | |
headers = {"Token": api_key, "Accept": "application/yaml"} | |
resp = requests.get(url + '/api/v1/orgs/' + org + '/accounts/' + account + '/dashboards/' + default_dashboard, headers=headers) | |
# dashboard export should return a 200 response | |
if resp.status_code != 200: | |
print "export dashboard failed" | |
sys.exit(2) | |
# verify the exported dashboard yaml | |
dashboard_yaml = yaml.load(resp.text) | |
if dashboard_yaml['title'] != 'All Systems': | |
print "export dashboard verify failed" | |
sys.exit(2) | |
except Exception, e: | |
print test_name + ' failed: %s' % e | |
sys.exit(2) | |
t1 = time.time() | |
perf_data += test_name + '=' + str(round(t1 - t0, 2)) + 's;;;; ' | |
#### import dashboard | |
test_name = 'import_dashboard' | |
t0 = time.time() | |
try: | |
# import dashboard we just exported | |
headers = {"Token": api_key, "Content-Type": "application/yaml"} | |
requests.put(url + '/api/v1/orgs/' + org + '/accounts/' + account + '/dashboards/' + automated_dashboard, headers=headers, data=json.dumps(dashboard_yaml)) | |
# verify the imported dashboard yaml | |
headers = {"Token": api_key, "Accept": "application/yaml"} | |
resp = requests.get(url + '/api/v1/orgs/' + org + '/accounts/' + account + '/dashboards/' + automated_dashboard, headers=headers) | |
dashboard_yaml = yaml.load(resp.text) | |
if dashboard_yaml['title'] != 'All Systems': | |
print "export dashboard verify failed" | |
sys.exit(2) | |
except Exception, e: | |
print test_name + ' failed: %s' % e | |
sys.exit(2) | |
t1 = time.time() | |
perf_data += test_name + '=' + str(round(t1 - t0, 2)) + 's;;;; ' | |
### delete dashboard | |
test_name = 'delete_dashboard' | |
t0 = time.time() | |
try: | |
resp = delete_dashboard(automated_dashboard) | |
if resp.status_code != 204 or check_dashboard_exists(automated_dashboard): | |
print "delete dashboard verify failed" | |
sys.exit(2) | |
except Exception, e: | |
print test_name + ' failed: %s' % e | |
sys.exit(2) | |
t1 = time.time() | |
perf_data += test_name + '=' + str(round(t1 - t0, 2)) + 's;;;; ' | |
print 'OK | ' + perf_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment