Module: Noah::SinatraBaseHelpers

Defined in:
lib/noah/helpers/base_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_config_to_app(appname, config_hash) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/noah/helpers/base_helpers.rb', line 95

def add_config_to_app(appname, config_hash)
  begin
    config = Noah::Configuration.find_or_create(config_hash)
    if config.valid?
      dep_action = config.is_new? ? "create" : "update"
    else
      raise "#{format_errors(config)}"
    end
    app = Noah::Application.find_or_create(:name => appname)
    if app.valid?
      action = app.is_new? ? "create" : "update"
      app.configurations << config
      r = {"result" => "success", "id" => "#{app.id}", "name" => "#{app.name}", "action" => action, "configuration" => {"action" => dep_action, "id" => "#{config.id}", "item" => "#{config.name}"}}
      r.to_json
    else
      raise "#{format_errors(app)}"
    end
  rescue Exception => e
    e.message
  ensure
    # Clean up partial objects
    app.delete if app.valid? == false
    config.delete if config.valid? == false
  end
end

#application(opts = {}) ⇒ Object



121
122
123
# File 'lib/noah/helpers/base_helpers.rb', line 121

def application(opts = {})
  Noah::Application.find(opts).first
end

#applications(opts = {}) ⇒ Object



125
126
127
# File 'lib/noah/helpers/base_helpers.rb', line 125

def applications(opts = {})
  Noah::Applications.all(opts)
end

#configuration(opts = {}) ⇒ Object



129
130
131
# File 'lib/noah/helpers/base_helpers.rb', line 129

def configuration(opts = {})
  Noah::Configuration.find(opts).first
end

#configurations(opts = {}) ⇒ Object



133
134
135
# File 'lib/noah/helpers/base_helpers.rb', line 133

def configurations(opts = {})
  Noah::Configurations.all(opts)
end

#delete_service_from_host(servicename, hostname) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/noah/helpers/base_helpers.rb', line 75

def delete_service_from_host(servicename, hostname)
  host = Noah::Host.find(:name => hostname).first
  (halt 404) if host.nil?
  service = Noah::Service.find(:name => servicename, :host_id => host.id).first
  (halt 404) if service.nil?
  service.delete
  r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename}
  r.to_json
end

#find_hosts_by_service(servicename) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/noah/helpers/base_helpers.rb', line 64

def find_hosts_by_service(servicename)
  affected_hosts = []
  s = Noah::Service.find(:name => servicename)
  if s.nil?
    affected_hosts
  else
    Noah::Host.all.each {|x| affected_hosts << x if (x.services.to_a & s.to_a).length > 0}
    affected_hosts
  end
end


85
86
87
88
89
90
91
92
93
# File 'lib/noah/helpers/base_helpers.rb', line 85

def find_named_link(path)
  link = Noah::Link.find(:path => "/"+path).first
  if link.nil?
    # try finding it wihtout the slash
    link = Noah::Link.find(:path => path).first
    (halt 404) if link.nil?
  end
  return link
end

#format_errors(model) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/noah/helpers/base_helpers.rb', line 4

def format_errors(model)
  error_messages = model.errors.present do |e|
    # Missing attributes
    e.on [:name, :not_present], "Name attribute missing"
    e.on [:status, :not_present], "Status attribute missing"
    e.on [:format, :not_present], "Format attribute missing"
    e.on [:body, :not_present], "Body attribute missing"
    e.on [:application_id, :not_present], "Application attribute missing"
    e.on [:path, :not_present], "Path attribute missing"
    e.on [:pattern, :not_present], "Pattern attribute missing"
    e.on [:endpoint, :not_present], "Endpoint attribute missing"
    # Invalid option
    e.on [:status, :not_member], "Status must be up, down or pending"
    # Duplicate keys
    e.on [[:name, :application_id], :not_unique], "Record already exists"
    e.on [[:name, :host_id], :not_unique], "Record already exists"
    e.on [[:endpoint, :pattern], :not_unique], "Record already exists"
    e.on [:path, :not_unique], "Record already exists"
    # Custom exceptions
    e.on [:pattern, :already_provided], "Pattern is already provided"
    e.on [:pattern, :replaces_existing], "Pattern would overwrite existing"
    e.on [:path, :reserved_path], "Path is reserved"
  end
  error_messages.first
end

#host(opts = {}) ⇒ Object



30
31
32
# File 'lib/noah/helpers/base_helpers.rb', line 30

def host(opts = {})
  Noah::Host.find(opts).first
end

#host_service(hostname, servicename) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/noah/helpers/base_helpers.rb', line 46

def host_service(hostname, servicename)
  h = Noah::Host.find(:name => hostname).first
  if h.nil?
    nil
  else  
    Noah::Service.find(:host_id => h.id, :name => servicename).first
  end  
end

#host_services(hostname) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/noah/helpers/base_helpers.rb', line 55

def host_services(hostname)
  h = Noah::Host.find(:name => hostname).first
  if h.nil?
    nil
  else  
    Noah::Services.all(:host_id => id)
  end  
end

#hosts(opts = {}) ⇒ Object



34
35
36
# File 'lib/noah/helpers/base_helpers.rb', line 34

def hosts(opts = {})
  Noah::Hosts.all(opts)
end

#service(opts = {}) ⇒ Object



38
39
40
# File 'lib/noah/helpers/base_helpers.rb', line 38

def service(opts = {})
  Noah::Service.find(options)
end

#services(opts = {}) ⇒ Object



42
43
44
# File 'lib/noah/helpers/base_helpers.rb', line 42

def services(opts = {})
  Noah::Services.all(opts)
end