Class: Moose::Inventory::InventoryContext

Inherits:
Object
  • Object
show all
Defined in:
lib/moose_inventory/inventory_context.rb

Overview

Thin facade over an explicitly supplied DB implementation.

This gives new operation/service objects a small inventory-facing seam without forcing the legacy CLI to stop using the DB singleton all at once.

Constant Summary collapse

AUTOMATIC_GROUP =
'ungrouped'

Instance Method Summary collapse

Constructor Details

#initialize(db:) ⇒ InventoryContext

Returns a new instance of InventoryContext.



15
16
17
# File 'lib/moose_inventory/inventory_context.rb', line 15

def initialize(db:)
  @db = db
end

Instance Method Details

#all_groupsObject



83
84
85
# File 'lib/moose_inventory/inventory_context.rb', line 83

def all_groups
  db.models[:group].all
end

#all_hostsObject



79
80
81
# File 'lib/moose_inventory/inventory_context.rb', line 79

def all_hosts
  db.models[:host].all
end

#audit_events(limit: 20) ⇒ Object



99
100
101
# File 'lib/moose_inventory/inventory_context.rb', line 99

def audit_events(limit: 20)
  db.models[:audit_event].reverse_order(:id).limit(limit).all
end

#automatic_groupObject



67
68
69
# File 'lib/moose_inventory/inventory_context.rb', line 67

def automatic_group
  find_or_create_group(AUTOMATIC_GROUP)
end

#create_group(name) ⇒ Object



35
36
37
# File 'lib/moose_inventory/inventory_context.rb', line 35

def create_group(name)
  db.models[:group].create(name: name)
end

#create_host(name) ⇒ Object



27
28
29
# File 'lib/moose_inventory/inventory_context.rb', line 27

def create_host(name)
  db.models[:host].create(name: name)
end

#create_variable(entity_type, name:, value:) ⇒ Object



75
76
77
# File 'lib/moose_inventory/inventory_context.rb', line 75

def create_variable(entity_type, name:, value:)
  db.models[variable_model_key(entity_type)].create(name: name, value: value)
end

#db_dataset(table_name) ⇒ Object



63
64
65
# File 'lib/moose_inventory/inventory_context.rb', line 63

def db_dataset(table_name)
  db.db[table_name]
end

#find_group(name) ⇒ Object



31
32
33
# File 'lib/moose_inventory/inventory_context.rb', line 31

def find_group(name)
  db.models[:group].find(name: name)
end

#find_host(name) ⇒ Object



23
24
25
# File 'lib/moose_inventory/inventory_context.rb', line 23

def find_host(name)
  db.models[:host].find(name: name)
end

#find_or_create_group(name) ⇒ Object



39
40
41
# File 'lib/moose_inventory/inventory_context.rb', line 39

def find_or_create_group(name)
  db.models[:group].find_or_create(name: name)
end

#find_or_create_tag(name) ⇒ Object



47
48
49
# File 'lib/moose_inventory/inventory_context.rb', line 47

def find_or_create_tag(name)
  db.models[:tag].find_or_create(name: normalize_tag_name(name))
end

#find_tag(name) ⇒ Object



43
44
45
# File 'lib/moose_inventory/inventory_context.rb', line 43

def find_tag(name)
  db.models[:tag].find(name: normalize_tag_name(name))
end

#find_variable(entity_type, id) ⇒ Object



71
72
73
# File 'lib/moose_inventory/inventory_context.rb', line 71

def find_variable(entity_type, id)
  db.models[variable_model_key(entity_type)].find(id: id)
end

#hosts_datasetObject



59
60
61
# File 'lib/moose_inventory/inventory_context.rb', line 59

def hosts_dataset
  db.models[:host].dataset
end

#moose_exception_classObject



103
104
105
# File 'lib/moose_inventory/inventory_context.rb', line 103

def moose_exception_class
  db.exceptions[:moose]
end

#normalize_tag_name(name) ⇒ Object



51
52
53
# File 'lib/moose_inventory/inventory_context.rb', line 51

def normalize_tag_name(name)
  name.to_s.downcase.strip
end

#normalize_tag_names(names) ⇒ Object



55
56
57
# File 'lib/moose_inventory/inventory_context.rb', line 55

def normalize_tag_names(names)
  names.map { |name| normalize_tag_name(name) }.reject(&:empty?).uniq
end

#record_audit_event(attributes) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/moose_inventory/inventory_context.rb', line 87

def record_audit_event(attributes)
  db.models[:audit_event].create(
    created_at: Time.now.utc.iso8601,
    actor: attributes[:actor],
    command: attributes.fetch(:command),
    action: attributes.fetch(:action),
    entity_type: attributes[:entity_type],
    entity_name: attributes[:entity_name],
    details: attributes[:details]
  )
end

#transactionObject



19
20
21
# File 'lib/moose_inventory/inventory_context.rb', line 19

def transaction(&)
  db.transaction(&)
end