Class: Chef::EncryptedAttribute::RemoteNode

Inherits:
Object
  • Object
show all
Includes:
SearchHelper, Mixin::ParamsValidate
Defined in:
lib/chef/encrypted_attribute/remote_node.rb

Overview

Remote Node object to read and save node attributes remotely.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchHelper

#assert_normal_search_response, #assert_partial_search_response, #assert_search_keys, #catch_search_exceptions, #empty_search?, #escape, #escape_query, #filter_normal_search_response, #filter_partial_search_response, #generate_partial_search_keys, #normal_search, #parse_normal_search_response, #parse_normal_search_row_attribute, #parse_partial_search_response, #partial_search, #query, #search, #search_by_name, #valid_search_keys?, #valid_search_keys_key?, #valid_search_keys_value?

Constructor Details

#initialize(name) ⇒ RemoteNode

Remote Node object constructor.

Parameters:

  • name (String)

    node name.



34
35
36
# File 'lib/chef/encrypted_attribute/remote_node.rb', line 34

def initialize(name)
  name(name)
end

Class Method Details

.cacheCacheLru

Remote node attribute values cache.

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

Chef::EncryptedAttribute::RemoteNode.cache.max_size(1024)

Returns:

  • (CacheLru)

    node attributes LRU cache.



47
48
49
50
# File 'lib/chef/encrypted_attribute/remote_node.rb', line 47

def self.cache
  # disabled by default
  @@cache ||= Chef::EncryptedAttribute::CacheLru.new(0)
end

Instance Method Details

#delete_attribute(attr_ary) ⇒ Boolean

Deletes a remote node attribute.

Parameters:

  • attr_ary (Array<String>)

    node attribute path as Array.

Returns:

  • (Boolean)

    whether the node attribute has been found and successfully deleted.

Raises:

  • (ArgumentError)

    if the attribute path format is wrong.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Mixed

Loads a remote node attribute.

Parameters:

  • attr_ary (Array<String>)

    node attribute path as Array.

  • rows (Integer) (defaults to: 1000)

    maximum number of rows to return in searches.

  • partial_search (Boolean) (defaults to: true)

    whether to use partial search.

Returns:

  • (Mixed)

    node attribute value, nil if not found.

Raises:

  • (ArgumentError)

    if the attribute path format is wrong.

  • (SearchFailure)

    if there is a Chef search error.

  • (SearchFatalError)

    if the Chef search response is wrong.

  • (InvalidSearchKeys)

    if search keys structure is wrong.



75
76
77
78
79
80
81
82
# 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) ⇒ String

Read or set node name.

Parameters:

  • arg (String) (defaults to: nil)

    node name.

Returns:

  • (String)

    node name.



56
57
58
59
60
61
62
63
# 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) ⇒ Mixed

Saves a remote node attribute.

Parameters:

  • attr_ary (Array<String>)

    node attribute path as Array.

  • value (Mixed)

    node attribute value to set.

Returns:

  • (Mixed)

    node attribute value.

Raises:

  • (ArgumentError)

    if the attribute path format is wrong.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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