Skip to content

Instantly share code, notes, and snippets.

@vdel26
Last active May 31, 2016 15:43
Show Gist options
  • Save vdel26/f5a9976e449783038dd3e050a3d7b74e to your computer and use it in GitHub Desktop.
Save vdel26/f5a9976e449783038dd3e050a3d7b74e to your computer and use it in GitHub Desktop.
CSV export of all applications (including keys)
#!/usr/bin/env ruby
require 'HTTParty'
require 'optparse'
require 'csv'
class ApplicationsExport
def initialize(base_url, provider_key, outfile)
@applications = []
@base_url = base_url
@provider_key = provider_key
@outfile = outfile || 'keys.csv'
end
def run
get_all_applications
write_output_csv
end
def get_applications(page, per_page)
options = {
query: {
provider_key: @provider_key,
page: page,
per_page: per_page
}
}
response = HTTParty.get("#{@base_url}/admin/api/applications.xml", options)
raise 'Error while fetching applications' unless response.code == 200
response['applications'].nil? ? false : response['applications']['application']
end
def get_all_applications
i = 1
while apps = get_applications(i, 100)
break unless apps
@applications << apps
pp "Fetching page #{i}"
i += 1
end
end
def write_output_csv
CSV.open(@outfile, 'w') do |csv|
csv << [ 'user_key' , 'service_id', 'name', 'created_at', 'plan name', 'plan id' ]
@applications.flatten(1).each do |app|
row = [ app['user_key'], app['service_id'], app['name'], app['created_at'], app['plan']['name'], app['plan']['id'] ]
csv << row
end
end
puts 'All applications exported successfully'
end
end
options = {}
parser = OptionParser.new do |parser|
parser.banner = 'ruby export-keys.rb [options]'
parser.on('-s', '--source SOURCE', 'Account. Format: https://<providerkey>@<orgname>-admin.3scale.net') do |url|
begin
options[:provider_key] = url[/\w*@/][0..-2]
options[:base_url] = url.sub /\w*@/, ''
rescue
puts 'Invalid source'
exit
end
end
parser.on('-f', '--fileout FILENAME', 'Name of the CSV file that will be created') do |file|
options[:outfile] = file
end
parser.on('-h', '--help', 'Prints this help') do
puts parser
puts
exit
end
end
parser.parse!
required_opts = [:provider_key, :base_url, :outfile].all? { |e| options.include?(e) }
if not required_opts
puts 'Error: missing arguments'
puts
puts parser
exit
end
client = ApplicationsExport.new(
options[:base_url],
options[:provider_key],
options[:outfile])
client.run
@vdel26
Copy link
Author

vdel26 commented May 31, 2016

Usage:

ruby export-keys.rb [options]
    -s, --source SOURCE              Account. Format: https://<providerkey>@<orgname>-admin.3scale.net
    -f, --fileout FILENAME           Name of the CSV file that will be created
    -h, --help                       Prints this help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment