Class: Puppet::Confine::Variable

Inherits:
Puppet::Confine show all
Defined in:
lib/puppet/confine/variable.rb

Overview

Require a specific value for a variable, either a Puppet setting or a Facter value. This class is a bit weird because the name is set explicitly by the ConfineCollection class – from this class, it’s not obvious how the name would ever get set.

Constant Summary

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Attribute Summary collapse

Attributes inherited from Puppet::Confine

#for_binary, #label, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Puppet::Confine

#for_binary?, inherited, #result, test

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(values) ⇒ Variable

Returns a new instance of Variable.



25
26
27
28
# File 'lib/puppet/confine/variable.rb', line 25

def initialize(values)
  super
  @values = @values.collect { |v| v.to_s.downcase }
end

Instance Attribute Details

#nameObject

This is set by ConfineCollection.



18
19
20
# File 'lib/puppet/confine/variable.rb', line 18

def name
  @name
end

Class Method Details

.summarize(confines) ⇒ Object

Provide a hash summary of failing confines – the key of the hash is the name of the confine, and the value is the missing yet required values. Only returns failed values, not all required values.



12
13
14
15
# File 'lib/puppet/confine/variable.rb', line 12

def self.summarize(confines)
  result = Hash.new { |hash, key| hash[key] = [] }
  confines.inject(result) { |total, confine| total[confine.name] += confine.values unless confine.valid?; total }
end

Instance Method Details

#facter_valueObject

Retrieve the value from facter



21
22
23
# File 'lib/puppet/confine/variable.rb', line 21

def facter_value
  @facter_value ||= Puppet.runtime[:facter].value(name).to_s.downcase
end

#message(value) ⇒ Object



30
31
32
# File 'lib/puppet/confine/variable.rb', line 30

def message(value)
  "facter value '#{test_value}' for '#{self.name}' not in required list '#{values.join(",")}'"
end

#pass?(value) ⇒ Boolean

Compare the passed-in value to the retrieved value.

Returns:



35
36
37
# File 'lib/puppet/confine/variable.rb', line 35

def pass?(value)
  test_value.downcase.to_s == value.to_s.downcase
end

#resetObject



39
40
41
42
43
# File 'lib/puppet/confine/variable.rb', line 39

def reset
  # Reset the cache.  We want to cache it during a given
  # run, but not across runs.
  @facter_value = nil
end

#valid?Boolean

Returns:



45
46
47
48
49
# File 'lib/puppet/confine/variable.rb', line 45

def valid?
  @values.include?(test_value.to_s.downcase)
ensure
  reset
end