lib/gaptool-api.rb in gaptool-api-0.6.5 vs lib/gaptool-api.rb in gaptool-api-0.6.6

- old
+ new

@@ -15,90 +15,100 @@ @default_aws_region = default_aws_az[0..-2] @version = File.read(File.realpath(File.join(File.dirname(__FILE__), "..", 'VERSION'))).strip GaptoolServer.base_uri uri end + def call(verb, url, opts={}) + options = opts || {} + options[:headers] ||= {} + options[:headers].merge!(@auth) + options[:headers]['Accept'] = 'application/json' + options[:headers]['Content-Type'] = 'application/json' + case verb + when :post + options[:body] ||= '' + end + JSON.parse(self.class.send(verb, url, options).body) + end + + def get(url, opts={}) + call(:get, url, opts) + end + + def post(url, opts={}) + call(:post, url, opts) + end + + def patch(url, opts={}) + call(:patch, url, opts) + end + def rehash() - options = {:body => '', :headers => @auth} - JSON::parse self.class.post("/rehash", options) + post("/rehash") end def getonenode(id) - options = {:headers => @auth} - JSON::parse self.class.get("/instance/#{id}", options) + get("/instance/#{id}") end def getenvroles(role, environment, params={}) - options = {:headers => @auth, :query => params} - JSON::parse self.class.get("/hosts/#{role}/#{environment}", options) + get("/hosts/#{role}/#{environment}", {query: params}) end def addnode(zone, itype, role, environment, mirror=nil, security_group=nil, ami=nil, chef_repo=nil, chef_branch=nil, chef_runlist=nil, no_terminate=nil) - @body = { + body = { 'zone' => zone || @default_aws_az, 'itype' => itype, 'role' => role, 'environment' => environment, 'ami' => ami, 'chef_repo' => chef_repo, 'chef_branch' => chef_branch } unless security_group.nil? - @body['security_group'] = security_group + body['security_group'] = security_group end unless chef_runlist.nil? || chef_runlist.empty? - @body['chef_runlist'] = [*chef_runlist] + body['chef_runlist'] = [*chef_runlist] end if no_terminate - @body['terminate'] = false + body['terminate'] = false end - @body = @body.to_json - options = { :body => @body, :headers => @auth} - JSON::parse self.class.post("/init", options) + post('/init', {body: body.to_json}) end def terminatenode(id, zone) - @body = {'id' => id, 'zone' => zone || @default_aws_region}.to_json - options = {:body => @body, :headers => @auth} - JSON::parse self.class.post("/terminate", options) + post('/terminate', {body: {'id' => id, 'zone' => zone || @default_aws_region}.to_json}) end - def setparameters(instance, params) - @body = params.to_json - options = {:body => @body, :headers => @auth} - JSON::parse self.class.patch("/instance/#{instance}", options) + def setparameters(instance, params={}) + patch("/instance/#{instance}", {body: params.to_json}) end def getappnodes(app, environment, params={}) - options = {:headers => @auth, :query => params} - role = JSON::parse(self.class.get("/apps", options))["app:#{app}"]['role'] - JSON::parse self.class.get("/hosts/#{role}/#{environment}", options) + role = get("/apps", {query: params})["app:#{app}"]['role'] + get("/hosts/#{role}/#{environment}", {query: params}) end def ssh(role, environment, id) - options = { :headers => @auth} - JSON::parse self.class.get("/ssh/#{role}/#{environment}/#{id}", options) + get("/ssh/#{role}/#{environment}/#{id}") end def getrolenodes(role, params={}) - options = {:headers => @auth, :query => params} - JSON::parse self.class.get("/hosts/#{role}", options) + get("/hosts/#{role}", {query: params}) end def getenvnodes(environment, params={}) - options = {:headers => @auth, :query => params} - JSON::parse self.class.get("/hosts/ALL/#{environment}", options) + get("/hosts/ALL/#{environment}", {query: params}) end def getallnodes(params) - options = {:headers => @auth, :query => params} - JSON::parse self.class.get("/hosts", options) + get("/hosts", {query: params}) end def api_version() - options = { :headers => @auth } - JSON::parse self.class.get("/version", options) + get("/version") end end end