Skip to content

Instantly share code, notes, and snippets.

@tsubauaaa
Last active October 19, 2015 10:20
Show Gist options
  • Save tsubauaaa/e16125ac493616864bff to your computer and use it in GitHub Desktop.
Save tsubauaaa/e16125ac493616864bff to your computer and use it in GitHub Desktop.
cloudstack api rest response
#!/usr/bin/env ruby
require 'net/http'
require 'openssl'
require 'base64'
require 'cgi'
require 'yaml'
require 'json'
require 'time'
begin
vmsnap_cnt = 0
config = YAML.load(File.read(".cloudstack/config.yml"))
command_param = "apikey=#{config['APIKEY']}&command=listVMSnapshot&listall=true&response=json"
urlenc_command_param = URI.escape(command_param)
data = urlenc_command_param.downcase
base64_sig = Base64.encode64(OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha1'), config['SECKEY'], data)).strip
enc_sig = CGI.escape(base64_sig)
access_uri = URI.parse("#{config['URL']}/client/api?#{command_param}&apikey=#{config['APIKEY']}&signature=#{enc_sig}")
http = Net::HTTP.new(access_uri.host, access_uri.port)
res = http.get(access_uri)
json_res = JSON.parse(res.body)
results = json_res['listvmsnapshotresponse']['vmSnapshot']
results.each do |result|
if Time.parse(result['created']) < Time.now - 24*60*60
# puts result['id']
vmsnap_cnt += 1
end
end
puts vmsnap_cnt
rescue => e
puts "Error: exception: #{e}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment