diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 47f892a..336097f 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -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 diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 2308712..5eda873 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -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