Skip to content

Instantly share code, notes, and snippets.

@eric
Forked from jderrett/copy_librato_dashboard.rb
Last active July 15, 2016 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric/90c6a4401f07ca05a65e6cc3ba7912ce to your computer and use it in GitHub Desktop.
Save eric/90c6a4401f07ca05a65e6cc3ba7912ce to your computer and use it in GitHub Desktop.
Copy Librato Dashboard from one account to another
require 'faraday'
require 'faraday_middleware'
require 'json'
class DashboardApi
LIBRATO_API = 'https://metrics-api.librato.com'
LIBRATO_API_VERSION = 'v1'
ENDPOINT = [LIBRATO_API, LIBRATO_API_VERSION].join('/')
attr_accessor :username
def initialize(username, api_key)
@username = username
@conn = Faraday.new(ENDPOINT) do |f|
f.request :json
f.response :json
f.use Faraday::Response::RaiseError
f.adapter Faraday.default_adapter
f.basic_auth username, api_key
end
end
class Dashboard
attr_accessor :payload, :name, :instruments
def initialize(attrs)
@payload = attrs
@name = attrs['name']
@instruments = attrs['instruments']
end
def instrument_ids
instruments.map {|i| i['id']}
end
end
def conn
@conn
end
def get_entity(entity_path, entity_id)
resp = conn.get("#{entity_path}/#{entity_id}")
resp.body
end
def get_dashboard(dashboard_id)
Dashboard.new(get_entity('dashboards', dashboard_id))
end
def get_instrument(instrument_id)
get_entity('instruments', instrument_id)
end
def find_dashboard(name)
resp = conn.get('dashboards')
if dash = resp.body['dashboards'].detect {|d| d['name'] == name}
get_dashboard(dash['id'])
end
end
# Clean ids out of the instrument payload so we can post to another account
# Not even sure this is necessary - the API may just take it and discard the ids?
def get_instrument_dupe(instrument_id)
get_instrument(instrument_id).tap do |inst|
inst.delete('id')
inst['streams'].each {|s| s.delete('id')}
end
end
def create_instrument(payload)
conn.post do |req|
req.url 'instruments'
req.body = payload.to_json
end
end
def create_dashboard(payload)
conn.post do |req|
req.url 'dashboards'
req.body = payload.to_json
end
end
end # DashboardApi
# Script starts here
user1 = 'me@example.com'
api_key1 = 'abcd1234'
user2 = 'other@example.com'
api_key2 = '12341234'
dashboard_name = 'Weather'
prefix = ''
suffix = ' (copy)'
new_dashboard_name = prefix + dashboard_name + suffix
source_api = DashboardApi.new(user1, api_key1)
dest_api = DashboardApi.new(user2, api_key2)
source_dash = source_api.find_dashboard(dashboard_name)
dest_dash = dest_api.find_dashboard(new_dashboard_name)
if dest_dash
puts "Dashboard #{new_dashboard_name} already exists in #{dest_api.username}! Stopping."
exit
end
# Copy instruments
dest_instrument_ids = []
source_dash.instrument_ids.each do |id|
# Grab source instrument duplicate
inst = source_api.get_instrument_dupe(id)
# Create destination instrument
puts "Creating #{inst['name']} in #{dest_api.username}..."
resp = dest_api.create_instrument(inst)
if resp.success?
new_inst_id = resp.body['id']
dest_instrument_ids << new_inst_id
puts "Created #{new_inst_id}"
else
puts resp.status
puts resp.body
end
end
# Create destination dashboard
puts "About to create dash #{new_dashboard_name} with instrument_ids #{dest_instrument_ids}"
dash_body = {
name: new_dashboard_name,
instruments: dest_instrument_ids.map {|id| {id: id}}
}
resp = dest_api.create_dashboard(dash_body)
if resp.success?
puts "Created new dashboard '#{resp.body['name']}'"
else
puts resp.status
puts resp.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment