Skip to content

Instantly share code, notes, and snippets.

@danfaizer
Created September 1, 2014 20:14
Show Gist options
  • Save danfaizer/95c03440bd841a40b436 to your computer and use it in GitHub Desktop.
Save danfaizer/95c03440bd841a40b436 to your computer and use it in GitHub Desktop.
ac-check-credentials-registration
#!/usr/bin/ruby
#
# Authors: Abiquo Support Team
# Version: 1.0
# Last moodify: 2014-09-01
#
# Access dependencies:
# This script requires access to Abiquo API
#
# Dependencies:
# This script requires ruby version >= 1.9.2
#
# Required packages and gems:
# gem install rest-client logger json
#
require 'rubygems'
require 'restclient'
require 'logger'
require 'json'
#
# Abiquo API cofiguration parameters
#########################################
Conf_apiurl = 'http://10.60.13.25/api'
Conf_apiuser = 'admin'
Conf_apipass = 'xabiquo'
Conf_enterprise_id = '1'
Conf_content_type = 'application/vnd.abiquo.publiccloudcredentials+json'
Conf_providername_array = ['DigitalOcean','Amazon']
Conf_apikey_array = ['digitalocean_apikey'\
,'amazon_apikey']
Conf_apisecret_array = ['digitalocean_apisecret'\
,'amazon_apisecret']
Conf_hvlink_array = ['[{"title":"DigitalOcean","rel":"hypervisortype","type":"application/vnd.abiquo.hypervisortype+json","href":"'+Conf_apiurl+'/config/hypervisortypes/digitalocean"}]'\
,'[{"title":"Amazon","rel":"hypervisortype","type":"application/vnd.abiquo.hypervisortype+json","href":"'+Conf_apiurl+'/config/hypervisortypes/AMAZON"}]']
# GET request connection timeout
Conf_timeout = 120
Conf_otimeout = 120
#
# Logger configuration
#########################################
$log = Logger.new(STDOUT)
$log.level = Logger::INFO
#$log.level = Logger::DEBUG
############################################################################################
####################### Do not modify any parameter beyond this line #######################
############################################################################################
#
# Method to perform GET requests
#########################################
def get(url,headers)
begin
resource = RestClient::Resource.new(url, :user => Conf_apiuser, :password => Conf_apipass, :timeout => Conf_timeout, :open_timeout => Conf_otimeout , :verify_ssl => OpenSSL::SSL::VERIFY_NONE )
response = resource.get(headers)
if response.code == 200 then
$log.info "Got response from GET to resource: #{url}"
return JSON.parse(response.body)
else
raise "Error performing GET with code: #{response.code} to resource: #{url}"
end
rescue => e
$log.error e
end
end
#
# Method to perform POST requests
#########################################
def post(url,headers,data)
begin
resource = RestClient::Resource.new(url, :user => Conf_apiuser, :password => Conf_apipass, :timeout => Conf_timeout, :open_timeout => Conf_otimeout , :verify_ssl => OpenSSL::SSL::VERIFY_NONE )
response = resource.post(data,headers)
if response.code == 201 then
$log.info "POST performed successfully"
return true
else
raise "Error performing POST with code: #{response.code} to resource: #{url}"
end
rescue => e
$log.error e
return false
end
end
#
# Method to perform DELETE requests
#########################################
def delete(url)
begin
resource = RestClient::Resource.new(url,:user => Conf_apiuser, :password => Conf_apipass, :timeout => Conf_timeout, :open_timeout => Conf_otimeout , :verify_ssl => OpenSSL::SSL::VERIFY_NONE )
response = resource.delete()
if response.code == 204 then
$log.info "Resource href: #{url} DELETED successfully"
else
raise "Error performing DELETE with code: #{response.code} to resource #{url}"
end
rescue => e
$log.error e
end
end
def clean_creds()
begin
headers = { 'Content-type' => Conf_content_type }
registered_providers = get(Conf_apiurl+'/admin/enterprises/'+Conf_enterprise_id+'/credentials?limit=0',headers)
registered_providers['collection'].each do |provider|
provider['links'].each do |link|
if link['type'] == Conf_content_type
delete(link['href'])
end
end
end
rescue => e
$log.error e
end
end
def add_creds(apikey,apisecret,hvlink)
headers = { "Accept" => Conf_content_type, "Content-type" => Conf_content_type }
data = {"access" => apikey , "key" => apisecret , "links" => JSON.parse(hvlink) }
result = post(Conf_apiurl+'/admin/enterprises/'+Conf_enterprise_id+'/credentials',headers,data.to_json)
return result
end
# MAIN
begin
$log.info "Starting credential registration check process"
$log.info "Cleaning up current credential"
clean_creds()
array_counter = 0
Conf_providername_array.each do |provider|
$log.info "Registering provider: #{provider}"
result = add_creds(Conf_apikey_array[array_counter],Conf_apisecret_array[array_counter],Conf_hvlink_array[array_counter])
if result
$log.info "#{provider} SUCCESSFULLY registered"
else
$log.error "#{provider} registration FAILED"
end
array_counter += 1
end
$log.info "Registration check process finished"
rescue => e
$log.error "Unexpected error:"
$log.error e
ensure
$log.info "Cleaning up current credential"
clean_creds()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment