Skip to content

Instantly share code, notes, and snippets.

@maclennann
Last active September 20, 2017 13:36
Show Gist options
  • Save maclennann/c8b978d56ddfa2afe9beca56c610dd71 to your computer and use it in GitHub Desktop.
Save maclennann/c8b978d56ddfa2afe9beca56c610dd71 to your computer and use it in GitHub Desktop.
Cloudability Dashboard JSON Tools

Cloudability has an undocumented "v2" API used by the web application to do...most things. Fortunately, your v1 API key should work just file with it and allow you to do anything as well (provided you pay close enough attention to the network pane in your browser dev tool).

Here are a couple of utilities for working with dashboards. Cloudability has a built-in "duplicate" function for dashboards, but what happens when you're trying to move dashboards between Cloudability organizations?

You can export dashboards to JSON, perform a slight bit of massaging on the data, and re-import it - widget by widget - on the other end.

Note: This works fine within a single Cloudability organization, but I have not yet been able to try it between two different organizations, so I have no idea what might happen.

require 'rest-client'
require 'json'
# This script uses your Cloudability API key to export a dashboard to a JSON file (for later re-import).
# Simply supply your API key, the id of your dashboard (from the URL), and the path you wish to save it to.
puts "Cloudability API Key: "
cloudability_api_key = STDIN.gets.chomp
puts "Dashboard Id To Export?"
dashboard_id = STDIN.gets.chomp
puts "Path to save dashboard JSON? "
file_path = STDIN.gets.chomp
dashboard = RestClient.get "https://app.cloudability.com/api/2/dashboards/#{dashboard_id}",
{params: {include_widgets: true, auth_token: cloudability_api_key }, accept: :json}
dash_json = JSON.parse dashboard
File.open(file_path, 'w') do |f|
f.write(JSON.pretty_generate(dash_json))
end
# frozen_string_literal: true
source "https://rubygems.org"
gem "rest-client"
require 'rest-client'
require 'json'
# This script uses your Cloudability API key to import a dashboard from a JSON file (exported using `export_dashboard.rb`).
# Supply your Cloudability API key and the path to your JSON file.
puts "Cloudability API Key: "
cloudability_api_key = STDIN.gets.chomp
puts "Path to JSON Dashboard to Import? "
file_path = STDIN.gets.chomp
dashboard = JSON.parse(File.read(file_path))
puts "Loaded dashboard #{dashboard["name"]}!"
puts " => Deleting invalid or unnecessary information..."
dashboard.delete "id"
dashboard.delete "category"
dashboard.delete "use_global"
dashboard.delete "annotations"
dashboard.delete "star"
dashboard.delete "author"
dashboard.delete "owned_by_user"
dashboard.delete "shares"
dashboard.delete "permission"
puts " => Done"
dashboard_name = "#{dashboard["name"]} - Import"
puts "Creating new dashboard: #{dashboard_name}..."
new_dashboard_response = RestClient.post 'https://app.cloudability.com/api/2/dashboards',
{ name: dashboard_name }.to_json, { accept: :json, content_type: :json, params: {auth_token: cloudability_api_key} }
new_dashboard = JSON.parse(new_dashboard_response.body)
puts " => New dashboard id: #{new_dashboard["id"]}"
puts " => Done"
puts "Importing Widgets..."
puts " => Removing and updating ids for new dashboard"
dashboard["widgets"].map do |widget|
widget.delete "id"
widget["dashboard_id"] = new_dashboard["id"]
widget["options"]["layers"].each do |layer|
layer.delete "report_id"
end
end
puts " => POSTing new widgets"
dashboard["widgets"].each do |widget|
puts " => Creating widget #{widget["name"]}"
new_widget_response = RestClient.post 'https://app.cloudability.com/api/2/widgets.json',
widget.to_json, { accept: :json, content_type: :json, params: {auth_token: cloudability_api_key} }
new_widget = JSON.parse(new_widget_response.body)
puts " => New widget id: #{new_widget["id"]}"
end
puts " => Done"
puts " Check out your new dashboard (hopefully!): https://app.cloudability.com/#/dashboards/#{new_dashboard["id"]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment