Class: Noah::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/noah/app.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) DELETE '/a/:appname/?'



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/noah/app.rb', line 217

delete '/a/:appname/?' do |appname|
  app = ::Application.find(:name => appname).first
  if app.nil?
    halt 404
  else
    configurations = []
    ::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
    app.delete
    r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
    r.to_json
  end
end

- (Object) DELETE '/c/:appname/:element?'



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/noah/app.rb', line 289

delete '/c/:appname/:element?' do |appname, element|
  app = ::Application.find(:name => appname).first
  if app
    config = ::Configuration.find(:name=> element, :application_id => app.id).first
    if config
      config.delete
      r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"}
      r.to_json
    else
      halt 404
    end
  else
    halt 404
  end
end

- (Object) DELETE '/h/:hostname/?'



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/noah/app.rb', line 105

delete '/h/:hostname/?' do |hostname|
  host = ::Host.find(:name => hostname).first
  if host
    services = []
    Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0
    host.delete
    r = {"result" => "success", "id" => "#{host.id}", "name" => "#{hostname}", "service_count" => "#{services.size}"}
    r.to_json
  else
    halt 404
  end
end

- (Object) DELETE '/s/:servicename/:hostname/?'



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/noah/app.rb', line 168

delete '/s/:servicename/:hostname/?' do |servicename, hostname|
  host = ::Host.find(:name => hostname).first || (halt 404)
  service = ::Service.find(:name => servicename, :host_id => host.id).first || (halt 404)
  if host && service
    service.delete
    r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename}
    r.to_json
  else
    halt 404
  end  
end

- (Object) GET '/'

Displays an overview page of all registered objects



45
46
47
48
49
# File 'lib/noah/app.rb', line 45

get '/' do
  content_type "text/html"

  haml :index, :format => :html5, :locals => {:redis_url => settings.redis_url, :noah_version => Noah::VERSION}
end

- (Object) GET '/a/?'



230
231
232
233
234
235
236
237
238
# File 'lib/noah/app.rb', line 230

get '/a/?' do
  apps = []
  ::Application.all.sort.each {|a| apps << a.to_hash}
  if apps.empty?
    halt 404
  else  
    apps.to_json
  end  
end

- (Object) GET '/a/:appname/?'



190
191
192
193
194
195
196
197
# File 'lib/noah/app.rb', line 190

get '/a/:appname/?' do |appname|
  app = ::Application.find(:name => appname).first
  if app.nil?
    halt 404
  else
    app.to_json
  end
end

- (Object) GET '/a/:appname/:config/?'



180
181
182
183
184
185
186
187
188
# File 'lib/noah/app.rb', line 180

get '/a/:appname/:config/?' do |appname, config|
  app = ::Application.find(:name => appname).first
  if app.nil?
    halt 404
  else  
    c = ::Configuration.find(:name => config, :application_id => app.id).first
    c.to_json
  end  
end

- (Object) GET '/c/?'



262
263
264
265
266
267
268
269
270
# File 'lib/noah/app.rb', line 262

get '/c/?' do
  configs = []
  ::Configuration.all.sort.each {|c| configs << c.to_hash}
  if configs.empty?
    halt 404
  else  
    configs.to_json
  end  
end

- (Object) GET '/c/:appname/?'



251
252
253
254
255
256
257
258
259
260
# File 'lib/noah/app.rb', line 251

get '/c/:appname/?' do |appname|
  config = []
  a = ::Application.find(:name => appname).first
  if a.nil?
    halt 404
  else  
    ::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
    config.to_json
  end  
end

- (Object) GET '/c/:appname/:element/?'



240
241
242
243
244
245
246
247
248
249
# File 'lib/noah/app.rb', line 240

get '/c/:appname/:element/?' do |appname, element|
  a = ::Application.find(:name => appname).first
  if a.nil?
    halt 404
  else  
    c = ::Configuration.find(:name => element, :application_id => a.id).first
    content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
    c.body
  end  
end

- (Object) GET '/h/?'

GET all Hosts



84
85
86
87
88
89
90
91
# File 'lib/noah/app.rb', line 84

get '/h/?' do
  hosts.map {|h| h.to_hash}
  if hosts.size == 0
    halt 404
  else
    hosts.to_json
  end
end

- (JSON) GET '/h/:hostname/?'

GET named Host

Parameters:

  • :hostname

    name of Host

Returns:

  • (JSON)

    representation of Host



74
75
76
77
78
79
80
81
# File 'lib/noah/app.rb', line 74

get '/h/:hostname/?' do |hostname|
  h = host(:name => hostname)
  if h.nil?
    halt 404
  else
    h.to_json
  end
end

- (Object) GET '/h/:hostname/:servicename/?'

GET named Service for named Host



62
63
64
65
66
67
68
69
# File 'lib/noah/app.rb', line 62

get '/h/:hostname/:servicename/?' do |hostname, servicename|
  h = host_service(hostname, servicename)
  if h.nil?
    halt 404
  else
    h.to_json
  end
end

- (Object) GET '/s/?'



140
141
142
143
144
145
146
147
# File 'lib/noah/app.rb', line 140

get '/s/?' do
  if services.empty?
    halt 404
  else
    services.map {|s| s.to_hash}
    services.to_json
  end  
end

- (Object) GET '/s/:servicename/?'



130
131
132
133
134
135
136
137
138
# File 'lib/noah/app.rb', line 130

get '/s/:servicename/?' do |servicename|
  s = services(:name => servicename)
  s.map {|x| x.to_hash}
  if s.empty?
    halt 404
  else  
    s.to_json
  end  
end

- (Object) GET '/s/:servicename/:hostname/?'

get named Service for named Host



121
122
123
124
125
126
127
128
# File 'lib/noah/app.rb', line 121

get '/s/:servicename/:hostname/?' do |servicename, hostname|
  hs = host_service(hostname, servicename)
  if hs.nil?
    halt 404
  else  
    hs.to_json
  end  
end

- (Object) NOT_FOUND



51
52
53
54
# File 'lib/noah/app.rb', line 51

not_found do
  content_type "application/json"
  erb :404'
end

- (Object) PUT '/a/:appname/?'



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/noah/app.rb', line 199

put '/a/:appname/?' do |appname|
  required_params = ["name"]
  data = JSON.parse(request.body.read)
  if data.keys.sort == required_params.sort && data['name'] == appname
    app = ::Application.find_or_create(:name => appname)
  else
    raise "Missing Parameters"
  end  
  if app.valid?
    action = app.is_new? ? "create" : "update"
    app.save
    r = {"result" => "success","id" => app.id, "action" => action, "name" => app.name }
    r.to_json
  else
    raise "#{app.errors}"
  end
end

- (Object) PUT '/c/:appname/:element?'



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/noah/app.rb', line 272

put '/c/:appname/:element?' do |appname, element|
  app = ::Application.find_or_create(:name => appname)
  config = ::Configuration.find_or_create(:name => element, :application_id => app.id)
  required_params = ["format", "body"]
  data = JSON.parse(request.body.read)
  data.keys.sort == required_params.sort  ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
  if config.valid?
    config.save
    action = config.is_new? ? "create" : "update"
    dependency_action = app.is_new? ? "created" : "updated"
    r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
    r.to_json
  else
    raise "#{config.errors}"
  end
end

- (Object) PUT '/h/:hostname/?'



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/noah/app.rb', line 93

put '/h/:hostname/?' do |hostname|
  required_params = ["name", "status"]
  data = JSON.parse(request.body.read)
  (data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = ::Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
  if host.valid?
    r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?}
    r.to_json
  else
    raise "#{host.errors}"
  end
end

- (Object) PUT '/s/:servicename/?'



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/noah/app.rb', line 149

put '/s/:servicename/?' do |servicename|
  required_params = ["status", "host", "name"]
  data = JSON.parse(request.body.read)
  if data.keys.sort == required_params.sort
    h = ::Host.find(:name => data['host']).first || (raise "Invalid Host")
    service = ::Service.find_or_create(:name => servicename, :status => data['status'], :host => h)
    if service.valid?
      action = service.is_new? ? "create" : "update"
      service.save
      r = {"action" => action, "result" => "success", "id" => service.id, "host" => h.name, "name" => service.name}
      r.to_json
    else
      raise "#{service.errors}"
    end
  else
    raise "Missing Parameters"
  end  
end