Class: Konzertmeister::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/konzertmeister/host.rb

Constant Summary collapse

FLAVORS =
%w[
  2x_small
  extra_small
  small
  medium
  large
  extra_large
  2x_large
]
POSTGRES_KIND =
'postgres'
CODA_KIND =
'coda'
UTIL_KIND =
'util'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Host

Returns a new instance of Host.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/konzertmeister/host.rb', line 31

def initialize(data)
  @data = data
  data.each do |k,value|
    next if %w[backend].include?(k)
    if m = k.match(/^host_(.*)$/)
      key = m[1]
    else
      key = k
    end

    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#backend_ipObject (readonly)

Returns the value of attribute backend_ip.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def backend_ip
  @backend_ip
end

#created_atObject (readonly)

Returns the value of attribute created_at.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def created_at
  @created_at
end

#flavorObject (readonly)

Returns the value of attribute flavor.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def flavor
  @flavor
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def hostname
  @hostname
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def id
  @id
end

#kindObject (readonly)

Returns the value of attribute kind.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def kind
  @kind
end

#management_ipObject (readonly)

Returns the value of attribute management_ip.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def management_ip
  @management_ip
end

#monthly_costObject (readonly)

Returns the value of attribute monthly_cost.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def monthly_cost
  @monthly_cost
end

#statusObject (readonly)

Returns the value of attribute status.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def status
  @status
end

#tokenObject (readonly)

Returns the value of attribute token.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def token
  @token
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



16
17
18
# File 'lib/konzertmeister/host.rb', line 16

def updated_at
  @updated_at
end

Class Method Details

.allObject



18
19
20
21
22
23
24
25
# File 'lib/konzertmeister/host.rb', line 18

def self.all
  response = Konzertmeister.session.get('/hosts')
  if response
    response.map do |data|
      Konzertmeister::Host.new(data)
    end.sort_by(&:hostname)
  end
end

.find_by(attr, value) ⇒ Object



27
28
29
# File 'lib/konzertmeister/host.rb', line 27

def self.find_by(attr, value)
  Host.new(Konzertmeister.session.get("/hosts/#{attr}/#{value}"))
end

Instance Method Details

#backendObject



45
46
47
48
49
50
51
# File 'lib/konzertmeister/host.rb', line 45

def backend
  @backend ||= if @data.key?('backend')
      Konzertmeister::Backend.new(@data.fetch('backend'))
    else
      Host.find_by('id', self.id).backend
  end
end

#nameObject



53
54
55
# File 'lib/konzertmeister/host.rb', line 53

def name
  "#{kind}-#{token}"
end

#to_sObject



57
58
59
60
61
62
63
64
# File 'lib/konzertmeister/host.rb', line 57

def to_s
  if status == "active"
    cost_string = "$%1.2f/month" % [monthly_cost]
    "%-14s %s" % [cost_string, name]
  else
    "%-14s %s" % ["(inactive)", name]
  end
end