Class: GTAPI::GaptoolServer

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/gaptool-api.rb

Constant Summary collapse

DEFAULT_AWS_AZ =
'us-west-2c'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, apikey, uri, default_aws_az = DEFAULT_AWS_AZ) ⇒ GaptoolServer

Returns a new instance of GaptoolServer.



11
12
13
14
15
16
17
18
19
# File 'lib/gaptool-api.rb', line 11

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_region = default_aws_az[0..-2]
  @version = File.read(File.realpath(File.join(File.dirname(__FILE__),
                                               '..', 'VERSION'))).strip
  GaptoolServer.base_uri uri
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/gaptool-api.rb', line 9

def version
  @version
end

Instance Method Details

#addnode(zone, itype, role, environment, _mirror = nil, security_group = nil, ami = nil, chef_repo = nil, chef_branch = nil, chef_runlist = nil, no_terminate = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gaptool-api.rb', line 65

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 = {
    'zone' => zone || @default_aws_az,
    'itype' => itype,
    'role' => role,
    'environment' => environment,
    'ami' => ami,
    'chef_repo' => chef_repo,
    'chef_branch' => chef_branch
  }
  body['security_group'] = security_group unless security_group.nil?
  unless chef_runlist.nil? || chef_runlist.empty?
    body['chef_runlist'] = [*chef_runlist]
  end
  body['terminate'] = false if no_terminate
  post('/init', body: body.to_json)
end

#api_versionObject



110
111
112
# File 'lib/gaptool-api.rb', line 110

def api_version
  get('/version')
end

#call(verb, url, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gaptool-api.rb', line 21

def call(verb, url, opts = {})
  options = opts || {}
  options[:headers] ||= {}
  options[:headers].merge!(@auth)
  options[:headers]['Accept'] = 'application/json'
  options[:headers]['Content-Type'] = 'application/json'
  options[:headers]['X-GAPTOOL-VERSION'] = @version
  case verb
  when :post
    options[:body] ||= ''
  end
  JSON.parse(self.class.send(verb, url, options).body)
end

#get(url, opts = {}) ⇒ Object



35
36
37
# File 'lib/gaptool-api.rb', line 35

def get(url, opts = {})
  call(:get, url, opts)
end

#getallnodes(params = {}) ⇒ Object



106
107
108
# File 'lib/gaptool-api.rb', line 106

def getallnodes(params = {})
  get('/hosts', query: params)
end

#getappnodes(app, environment, params = {}) ⇒ Object



94
95
96
# File 'lib/gaptool-api.rb', line 94

def getappnodes(app, environment, params = {})
  get("/app/#{app}/#{environment}/hosts", query: params)
end

#getenvnodes(environment, params = {}) ⇒ Object



102
103
104
# File 'lib/gaptool-api.rb', line 102

def getenvnodes(environment, params = {})
  get("/hosts/ALL/#{environment}", query: params)
end

#getenvroles(role, environment, params = {}) ⇒ Object



61
62
63
# File 'lib/gaptool-api.rb', line 61

def getenvroles(role, environment, params = {})
  get("/hosts/#{role}/#{environment}", query: params)
end

#getnodeattrs(id, attrs = {}) ⇒ Object



55
56
57
58
59
# File 'lib/gaptool-api.rb', line 55

def getnodeattrs(id, attrs = {})
  attrs ||= {}
  get("/instance/#{id}/attrs",
      body: attrs.to_json)
end

#getonenode(id) ⇒ Object



51
52
53
# File 'lib/gaptool-api.rb', line 51

def getonenode(id)
  get("/instance/#{id}")
end

#getrolenodes(role, params = {}) ⇒ Object



98
99
100
# File 'lib/gaptool-api.rb', line 98

def getrolenodes(role, params = {})
  get("/hosts/#{role}", query: params)
end

#patch(url, opts = {}) ⇒ Object



43
44
45
# File 'lib/gaptool-api.rb', line 43

def patch(url, opts = {})
  call(:patch, url, opts)
end

#post(url, opts = {}) ⇒ Object



39
40
41
# File 'lib/gaptool-api.rb', line 39

def post(url, opts = {})
  call(:post, url, opts)
end

#rehashObject



47
48
49
# File 'lib/gaptool-api.rb', line 47

def rehash
  post('/rehash')
end

#setparameters(instance, params = {}) ⇒ Object



90
91
92
# File 'lib/gaptool-api.rb', line 90

def setparameters(instance, params = {})
  patch("/instance/#{instance}", body: params.to_json)
end

#terminatenode(id, zone) ⇒ Object



85
86
87
88
# File 'lib/gaptool-api.rb', line 85

def terminatenode(id, zone)
  body = { 'id' => id, 'zone' => zone || @default_aws_region }
  post('/terminate', body: body.to_json)
end