Created
February 8, 2019 00:13
-
-
Save milkfarm/d7b187a485e61f20bb794c9b3eb942d5 to your computer and use it in GitHub Desktop.
Get time stamp of last backup for given Aiven service
This file contains hidden or 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 ruby | |
# Binaries and Defaults | |
# ----------------------------------------------------------------------- | |
####################################################################### | |
# Do not change anything below | |
####################################################################### | |
require 'optparse' | |
require 'ostruct' | |
require 'rest-client' | |
require 'json' | |
require 'time' | |
# Classes, modules, methods | |
# ----------------------------------------------------------------------- | |
class Client | |
API_HOST = 'https://api.aiven.io' | |
API_VERSION = 'v1' | |
def initialize(email:, password:, otp: nil, project: nil, service: nil, options: {}) | |
@email = email | |
@password = password | |
@otp = otp | |
@host = options.fetch(:host, API_HOST) | |
@version = options.fetch(:path, API_VERSION) | |
@token = get_token | |
@project = project | |
@service = service | |
end | |
def last_backup_time | |
str = last_backup.fetch('backup_time') | |
Time.parse(str) | |
end | |
def last_backup | |
service_backups.last | |
end | |
def service_backups | |
get_service_details(project: @project, service: @service) unless @service_details | |
@service_details.fetch('backups') | |
end | |
def get_service_details(project:, service:) | |
url = [base_url, 'project', project, 'service', service].join('/') | |
response = RestClient.get url, authorization_header | |
@service_details = JSON.parse(response.body).fetch('service') | |
end | |
private | |
def get_token | |
payload = { email: @email, password: @password } | |
payload[:otp] = @otp unless @otp.nil? || @otp.empty? | |
response = RestClient.post token_url, payload.to_json, {content_type: :json, accept: :json} | |
raise RunTimeError unless response.code == 200 | |
JSON.parse(response.body).fetch('token') | |
end | |
def base_url | |
[@host, @version].join('/') | |
end | |
def token_url | |
[base_url, 'userauth'].join('/') | |
end | |
def authorization_header | |
{ | |
authorization: "aivenv1 #{@token}", | |
content_type: :json, | |
accept: :json | |
} | |
end | |
end | |
# Process arguments | |
# ----------------------------------------------------------------------- | |
options = OpenStruct.new | |
options.email = '' | |
options.otp = '' | |
options.password = '' | |
options.project = '' | |
options.service = '' | |
options.verbose = false | |
opts = OptionParser.new do |opts| | |
opts.banner = "Description: Retrieves the date of the latest Aiven snapshot.\n\n" | |
opts.banner += "Usage: #{opts.program_name}.rb [options]" | |
opts.separator "" | |
opts.separator "Options: " | |
opts.on("-e", "--email EMAIL", "The account email address") do |x| | |
options.email = x | |
end | |
opts.on("-o", "--otp OTP", "The account one-time password") do |x| | |
options.otp = x | |
end | |
opts.on("-p", "--password PASSWORD", "The account password") do |x| | |
options.password = x | |
end | |
opts.on("-j", "--project PROJECT", "The aiven project") do |x| | |
options.project = x | |
end | |
opts.on("-s", "--service SERVICE", "The aiven service") do |x| | |
options.service = x | |
end | |
opts.on("-v", "--verbose", "Display verbose output.") do |x| | |
options.verbose = x | |
end | |
opts.on_tail("-h", "--help", "Show this usage statement") do |x| | |
puts opts | |
exit | |
end | |
opts.on_tail "" | |
opts.on_tail "Example: #{opts.program_name}.rb -e [email protected] -p ABCXYZ -o 123 -j ax -s chop" | |
end | |
begin | |
opts.parse!(ARGV) | |
rescue Exception => e | |
puts e | |
exit | |
end | |
# Test | |
#---------------------------------------------------------------------- | |
if options.email.empty? | |
puts 'Enter your email address: ' | |
options.email = gets.chomp | |
end | |
if options.password.empty? | |
begin | |
system 'stty -echo' | |
puts 'Enter your password: ' | |
options.password = gets.chomp | |
ensure | |
system 'stty echo' | |
end | |
if options.otp.empty? | |
puts 'Enter your one-time password: ' | |
options.otp = gets.chomp | |
end | |
end | |
if options.project.empty? | |
puts 'Enter the aiven project: ' | |
options.project = gets.chomp | |
end | |
if options.service.empty? | |
puts 'Enter the aiven service: ' | |
options.service = gets.chomp | |
end | |
# Main | |
#---------------------------------------------------------------------- | |
client = Client.new( | |
email: options.email, | |
password: options.password, | |
otp: options.otp, | |
project: options.project, | |
service: options.service | |
) | |
puts "Last backup: #{client.last_backup_time}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment