Class: BjnInventory::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/bjn_inventory/inventory.rb,
lib/inventory.rb,
lib/bjn_inventory/inventory/source.rb

Overview

{ “model”: “device.json”,

"context": "data/context",
"sources": [
  { "file": "data/device42.json",
    "map": "maps/device42.rb" },
  { "file": "data/aws.json",
    "map": "maps/aws.rb" }
]

}

Defined Under Namespace

Classes: Source

Instance Method Summary collapse

Constructor Details

#initialize(manifest = {}) ⇒ Inventory

Create a new Inventory according to the specified manifest (optional).

Parameters:

  • manifest (defaults to: {})

    :model Optionally, a device model to use when listing entries (passed to BjnInventory::Device.use_model)

  • manifest (defaults to: {})

    :context A directory or file containing context data to use when evaluating mapping rules.

  • manifest (defaults to: {})

    :sources An array of source specifiers, each source specifier is used to create a new BjnInventory::Inventory::Source



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

def initialize(manifest={})
    @manifest = manifest
end

Instance Method Details

#by(field) ⇒ Object

Return devices in the inventory as a hash, where the keys of the hash are the specified field values. For example, if the devices look like this: “‘

[
  { 'name' => 'testnode0', 'cost' => 15 }
  { 'name' => 'testnode1', 'cost' => 10 }
]

“‘ then the result of inventory.by(’name’) would be: “‘

{
  'testnode0' => { 'name' => 'testnode0', 'cost' => 15 },
  'testnode1' => { 'name' => 'testnode1', 'cost' => 10 }
}

“‘

Parameters:

  • field

    The field value to use as the keys of the hash



81
82
83
# File 'lib/bjn_inventory/inventory.rb', line 81

def by(field)
    BjnInventory::ByKey[devices_by(field).map { |key, device| [key, device.to_hash] }]
end

#devicesObject

Return all of the devices in the inventory in a flat array, model-standardized by the inventory source (with rules applied, etc.)



47
48
49
50
51
# File 'lib/bjn_inventory/inventory.rb', line 47

def devices()
    BjnInventory::List.new(@sources.map do |source|
        source.devices
    end.flatten)
end

#devices_by(field) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bjn_inventory/inventory.rb', line 85

def devices_by(field)
    field = field.intern
    devices.inject(BjnInventory::ByKey.new) do |hash, device|
        key = device.send field
        if key and !key.empty?
            if hash.has_key? key
                hash[key] =
                    hash[key].merge(device)
            else
                hash[key] = device
            end
        else
            @logger.warn "Skipping device with empty or null #{field.to_s}: ``#{device.inspect}''"
        end
        hash
    end
end

#make_model(model_data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/bjn_inventory/inventory.rb', line 53

def make_model(model_data)
    if model_data.nil?
        nil
    elsif model_data.respond_to? :to_hash
        model_data
    else
        # Must be a filename
        JSON.parse(File.read(model_data))
    end
end