Class: Backend

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mkit/app/model/backend.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(yaml) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mkit/app/model/backend.rb', line 8

def self.create(yaml)
  validate(yaml)
  backend = Backend.new
  backend.name = yaml["name"]
  backend.port = yaml["bind"]["port"] if yaml["bind"]["port"]
  backend.mode = yaml["bind"]["mode"] if yaml["bind"]["mode"]
  backend.options = yaml["options"] if yaml["options"]
  backend.load_bal = yaml["balance"] if yaml["balance"]
  backend.bind_options = yaml["bind"]["options"] if yaml["bind"]["options"]
  backend
end

.validate(yaml) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/mkit/app/model/backend.rb', line 20

def self.validate(yaml)
  raise_bad_configuration "name is mandatory" unless yaml["name"]
  raise_bad_configuration "bind is mandatory" unless yaml["bind"]
  raise_bad_configuration "mode is mandatory" unless yaml["bind"]["mode"]
  raise_bad_configuration "mode must match tcp|http" unless yaml["bind"]["mode"] =~ /^(tcp|http)$/
  if yaml["balance"]
    raise_bad_configuration "balance must match round_robin|least_conn" unless yaml["balance"] =~ /^(round_robin|least_conn)$/
  end
end

Instance Method Details

#load_balanceObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/mkit/app/model/backend.rb', line 30

def load_balance
  case self.load_bal
  when /^round_robin$/
    "roundrobin"
  when /^least_conn$/
    "leastconn"
  else
    "roundrobin"
  end
end

#to_h(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mkit/app/model/backend.rb', line 41

def to_h(options = {})
  hash = {
    name: self.name,
    balance: self.load_bal,
    options: self.options,
    bind: {
      port: self.port,
      mode: self.mode,
      options: self.bind_options
    }
  }

  if self.port.nil? || self.port.empty?
    hash[:bind].delete(:port)
  end
  hash.remove_symbols_from_keys
end