require 'httparty' require 'json' module GTAPI class GaptoolServer include HTTParty default_timeout 600 DEFAULT_AWS_AZ = 'us-west-2c' def initialize(user, apikey, uri, default_aws_az=DEFAULT_AWS_AZ) @auth = { 'X-GAPTOOL-USER' => user, 'X-GAPTOOL-KEY' => apikey} default_aws_az = default_aws_az || DEFAULT_AWS_AZ @default_aws_az = default_aws_az @default_aws_region = default_aws_az[0..-2] GaptoolServer.base_uri uri end def rehash() options = {:body => '', :headers => @auth} JSON::parse self.class.post("/rehash", options) end def getonenode(id) options = {:headers => @auth} JSON::parse self.class.get("/instance/#{id}", options) end def getenvroles(role, environment) options = {:headers => @auth} JSON::parse self.class.get("/hosts/#{role}/#{environment}", options) end def addnode(zone, itype, role, environment, mirror=0, security_group=nil) @body = { 'zone' => zone || @default_aws_az, 'itype' => itype, 'role' => role, 'environment' => environment } unless mirror == 0 or mirror.nil? @body['mirror'] = mirror end unless security_group.nil? @body['security_group'] = security_group end @body = @body.to_json options = { :body => @body, :headers => @auth} JSON::parse self.class.post("/init", options) 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) end def getappnodes(app, environment) options = {:headers => @auth} role = JSON::parse(self.class.get("/apps", options))["app:#{app}"]['role'] JSON::parse self.class.get("/hosts/#{role}/#{environment}", options) end def ssh(role, environment, id) options = { :headers => @auth} JSON::parse self.class.get("/ssh/#{role}/#{environment}/#{id}", options) end def getrolenodes(role) options = { :headers => @auth} JSON::parse self.class.get("/hosts/#{role}", options) end def getenvnodes(environment) options = { :headers => @auth} JSON::parse self.class.get("/hosts/ALL/#{environment}", options) end def getallnodes() options = { :headers => @auth} JSON::parse self.class.get("/hosts", options) end end end