Class: DigitalOceanInventory::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/digital_ocean_inventory/inventory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, groups, hosts) ⇒ Inventory

Returns a new instance of Inventory.



18
19
20
21
22
# File 'lib/digital_ocean_inventory/inventory.rb', line 18

def initialize(config, groups, hosts)
  @config = config
  @groups = groups
  @hosts  = hosts
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/digital_ocean_inventory/inventory.rb', line 7

def config
  @config
end

#groupsObject (readonly)

Returns the value of attribute groups.



7
8
9
# File 'lib/digital_ocean_inventory/inventory.rb', line 7

def groups
  @groups
end

#hostsObject (readonly)

Returns the value of attribute hosts.



7
8
9
# File 'lib/digital_ocean_inventory/inventory.rb', line 7

def hosts
  @hosts
end

Class Method Details

.build(config) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/digital_ocean_inventory/inventory.rb', line 9

def self.build(config)
  all = Group.build name: "all", config: config

  groups = OpenStruct.new all: all
  hosts  = OpenStruct.new

  new config, groups, hosts
end

Instance Method Details

#add_group(name) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/digital_ocean_inventory/inventory.rb', line 24

def add_group(name)
  return groups[name] if groups[name]

  group = Group.build name: name, config: config
  @groups[name] = group

  group
end

#add_host(droplet, host_groups = []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/digital_ocean_inventory/inventory.rb', line 33

def add_host(droplet, host_groups = [])
  name = droplet.name

  if hosts[name]
    host = hosts[name]
  else
    host = Host.build droplet: droplet, config: config
    hosts[name] = host
  end

  groups.all << host

  host_groups.each do |g|
    if groups[g]
      groups[g] << host
    else
      group = add_group g
      group << host
    end
  end

  host
end