Add http class for api token requests

This commit is contained in:
Brian Cain 2015-09-14 22:50:01 -07:00
parent e194f04ea9
commit eee7aab760
2 changed files with 32 additions and 13 deletions

View file

@ -36,4 +36,25 @@ class Http
return conn
end
def self.get_conn_with_token(verbose, url, token)
if url.nil?
STDERR.puts "The url you provided was empty"
exit 1
end
if token.nil?
STDERR.puts "The token you provided was empty"
exit 1
end
conn = Faraday.new(:url => url) do |faraday|
faraday.request :url_encoded
faraday.request :token_auth, token
faraday.response :logger if verbose
faraday.adapter Faraday.default_adapter
end
return conn
end
end

View file

@ -20,20 +20,19 @@ class Pooler
def self.retrieve(verbose, os_type, token, url)
os = os_type.split(',')
conn = Http.get_conn(verbose, url)
conn = Http.get_conn_with_token(verbose, url, token)
os_body = {}
os.each do |os_type|
unless os_body.has_key?(os_type)
os_body[os_type] = 1
else
os_body[os_type]++
os_body[os_type] += 1
end
end
response = conn.post do |req|
req.url '/vm'
req.headers['Content-Type'] = 'application/json'
req.body = os_body
end
@ -43,10 +42,11 @@ class Pooler
def self.modify(verbose, url, hostname, token, lifetime, tags)
modify_body = {'lifetime'=>lifetime, 'tags'=>tags}
conn = Http.get_conn(verbose, url)
conn = Http.get_conn_with_token(verbose, url, token)
# need to use token
response = conn.put "/#{hostname}"
response = conn.put do |req|
req.url "/vm/#{hostname}"
end
res_body = JSON.parse(response.body)
res_body
@ -58,7 +58,7 @@ class Pooler
hosts.each do |host|
puts "Scheduling host #{host} for deletion"
response = conn.delete "/#{host}"
response = conn.delete "/vm/#{host}"
res_body = JSON.parse(response.body)
puts res_body
end
@ -90,19 +90,17 @@ class Pooler
end
def self.snapshot(verbose, url, hostname, token)
conn = Http.get_conn(verbose, url)
conn = Http.get_conn_with_token(verbose, url, token)
# need to use token
response = conn.post "/#{hostname}/snapshot"
response = conn.post "/vm/#{hostname}/snapshot"
res_body = JSON.parse(response.body)
res_body
end
def self.revert(verbose, url, hostname, token, snapshot_sha)
conn = Http.get_conn(verbose, url)
conn = Http.get_conn_with_token(verbose, url, token)
# need to use token
response = conn.post "/#{hostname}/snapshot/#{snapshot}"
response = conn.post "/vm/#{hostname}/snapshot/#{snapshot}"
res_body = JSON.parse(response.body)
res_body
end