Class: Percolate::Adapter::ChefDataBagAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/percolate/adapter/chef_data_bag_adapter.rb

Overview

An adapter for loading from Chef data bags.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#configure_facet, #create_facet, #method_missing

Constructor Details

#initialize(data_source) ⇒ ChefDataBagAdapter

Returns a new instance of ChefDataBagAdapter.



28
29
30
31
32
# File 'lib/percolate/adapter/chef_data_bag_adapter.rb', line 28

def initialize(data_source)
  super

  @entities_data_bag = "entities"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Percolate::Adapter::BaseAdapter

Instance Attribute Details

#entities_data_bag=(value) ⇒ Object (writeonly)

Sets the attribute entities_data_bag

Parameters:

  • value

    the value to set the attribute entities_data_bag to.



26
27
28
# File 'lib/percolate/adapter/chef_data_bag_adapter.rb', line 26

def entities_data_bag=(value)
  @entities_data_bag = value
end

Instance Method Details

#load_entitiesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/percolate/adapter/chef_data_bag_adapter.rb', line 34

def load_entities
  begin
    @data_source.data_bag(@entities_data_bag).reduce({}) do |current, item_name|
      Percolate::Util.merge_attributes(
          current,
          @data_source.data_bag_item(@entities_data_bag, item_name).raw_data["entities"] || {}
      )
    end
  rescue Net::HTTPServerException => e
    # Reraise the exception if the status code isn't 404 Not Found.
    if e.response.code != "404"
      raise
    end

    nil
  end
end

#load_facet(context, name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/percolate/adapter/chef_data_bag_adapter.rb', line 52

def load_facet(context, name)
  name = name.to_s

  facets = begin
    @data_source.data_bag(context).map do |item_name|
      facets_hash = @data_source.data_bag_item(context, item_name).raw_data["facets"]

      facet_hash = facets_hash[name] || {}
      facet_type = facet_hash.fetch("type", name)
      facet_attrs = facet_hash.fetch("attrs", {})

      configure_facet(create_facet(facet_type), facet_attrs)
    end
  rescue Net::HTTPServerException => e
    # Reraise the exception if the status code isn't 404 Not Found.
    if e.response.code != "404"
      raise
    end

    []
  end

  if facets.size > 0
    facets[1...facets.size].reduce(facets[0]) do |current, other|
      current.merge(other)
    end
  else
    nil
  end
end