class Chef::EncryptedAttribute::RemoteNode

Remote Node object to read and save node attributes remotely.

Public Class Methods

cache() click to toggle source

Remote node attribute values cache.

It is disabled by default. You can enable it changing it's size:

“`ruby Chef::EncryptedAttribute::RemoteNode.cache.max_size(1024) “`

@return [CacheLru] node attributes LRU cache.

# File lib/chef/encrypted_attribute/remote_node.rb, line 47
def self.cache
  # disabled by default
  @@cache ||= Chef::EncryptedAttribute::CacheLru.new(0)
end
new(name) click to toggle source

Remote Node object constructor.

@param name [String] node name.

# File lib/chef/encrypted_attribute/remote_node.rb, line 34
def initialize(name)
  name(name)
end

Public Instance Methods

delete_attribute(attr_ary) click to toggle source

Deletes a remote node attribute.

@param attr_ary [Array<String>] node attribute path as Array. @return [Boolean] whether the node attribute has been found and

successfully deleted.

@raise [ArgumentError] if the attribute path format is wrong.

# File lib/chef/encrypted_attribute/remote_node.rb, line 112
def delete_attribute(attr_ary)
  assert_attribute_array(attr_ary)
  cache_key = cache_key(name, attr_ary)

  node = Chef::Node.load(name)
  last = attr_ary.pop
  node_attr = attr_ary.reduce(node.normal) do |a, k|
    a.respond_to?(:key?) && a.key?(k) ? a[k] : nil
  end
  if node_attr.respond_to?(:key?) && node_attr.key?(last)
    node_attr.delete(last)
    node.save
    self.class.cache.delete(cache_key)
    true
  else
    false
  end
end
load_attribute(attr_ary, rows = 1000, partial_search = true) click to toggle source

Loads a remote node attribute.

@param attr_ary [Array<String>] node attribute path as Array. @param rows [Integer] maximum number of rows to return in searches. @param partial_search [Boolean] whether to use partial search. @return [Mixed] node attribute value, `nil` if not found. @raise [ArgumentError] if the attribute path format is wrong. @raise [SearchFailure] if there is a Chef search error. @raise [SearchFatalError] if the Chef search response is wrong. @raise [InvalidSearchKeys] if search keys structure is wrong.

# File lib/chef/encrypted_attribute/remote_node.rb, line 75
def load_attribute(attr_ary, rows = 1000, partial_search = true)
  assert_attribute_array(attr_ary)
  cache_key = cache_key(name, attr_ary)
  return self.class.cache[cache_key] if self.class.cache.key?(cache_key)
  keys = { 'value' => attr_ary }
  res = search_by_name(:node, @name, keys, rows, partial_search)
  self.class.cache[cache_key] = parse_search_result(res)
end
name(arg = nil) click to toggle source

Read or set node name.

@param arg [String] node name. @return [String] node name.

# File lib/chef/encrypted_attribute/remote_node.rb, line 56
def name(arg = nil)
  # TODO: clean the cache when changed?
  set_or_return(
    :name,
    arg,
    kind_of: String
  )
end
save_attribute(attr_ary, value) click to toggle source

Saves a remote node attribute.

@param attr_ary [Array<String>] node attribute path as Array. @param value [Mixed] node attribute value to set. @return [Mixed] node attribute value. @raise [ArgumentError] if the attribute path format is wrong.

# File lib/chef/encrypted_attribute/remote_node.rb, line 90
def save_attribute(attr_ary, value)
  assert_attribute_array(attr_ary)
  cache_key = cache_key(name, attr_ary)

  node = Chef::Node.load(name)
  last = attr_ary.pop
  node_attr = attr_ary.reduce(node.normal) do |a, k|
    a[k] = Mash.new unless a.key?(k)
    a[k]
  end
  node_attr[last] = value

  node.save
  self.class.cache[cache_key] = value
end

Protected Instance Methods

assert_attribute_array(attr_ary) click to toggle source

Asserts that the attribute path is in the correct format.

@param attr_ary [Array<String>] node attribute path as Array. @return void @raise [ArgumentError] if the attribute path format is wrong.

# File lib/chef/encrypted_attribute/remote_node.rb, line 158
def assert_attribute_array(attr_ary)
  return if attr_ary.is_a?(Array)
  fail ArgumentError,
       "#{self.class}##{__method__} attr_ary argument must be an array "\
       "of strings. You passed #{attr_ary.inspect}."
end
cache_key(name, attr_ary) click to toggle source

Generates the cache key.

@param name [String] node name. @param attr_ary [Array<String>] node attribute path as Array. @return [String] cache key.

# File lib/chef/encrypted_attribute/remote_node.rb, line 149
def cache_key(name, attr_ary)
  "#{name}:#{attr_ary.inspect}" # TODO: OK, this can be improved
end
parse_search_result(res) click to toggle source

Parses {SearchHelper#search} result.

@param res [Array<Hash>] {SearchHelper#search} result. @return [Mixed] final search result value.

# File lib/chef/encrypted_attribute/remote_node.rb, line 137
def parse_search_result(res)
  unless res.is_a?(Array) && res[0].is_a?(Hash) && res[0].key?('value')
    return nil
  end
  res[0]['value']
end