Class: Puppet::Parameter::Path

Inherits:
Puppet::Parameter show all
Defined in:
lib/puppet/parameter/path.rb

Overview

This specialized Puppet::Parameter handles validation and munging of paths. By default, a single path is accepted, and by calling Path.accept_arrays it is possible to allow an array of paths.

Constant Summary

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

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

Attributes inherited from Puppet::Parameter

#name, #parent, #resource, #sensitive

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Puppet::Parameter

aliasvalue, defaultto, desc, doc, #file, #format, format_value_for_display, #initialize, initvars, isnamevar, #isnamevar?, isnamevar?, isrequired, #line, #log, #metaparam?, munge, #munge, newvalues, nodefault, #noop, #path, #pathbuilder, #provider, proxymethods, #remove, #required?, required?, sensitive, #tags, #to_s, unmunge, #unmunge, #unsafe_unmunge, validate, #validate, #value, #value=, #version

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

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?

Methods included from Util::Logging

#clear_deprecation_warnings, #debug, #deprecation_warning, #format_backtrace, #format_exception, #get_deprecation_offender, #log_and_raise, #log_deprecations_to_file, #log_exception, #puppet_deprecation_warning, #send_log, setup_facter_logging!, #warn_once

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail

Constructor Details

This class inherits a constructor from Puppet::Parameter

Class Method Details

.accept_arrays(bool = true) ⇒ Object

Specifies whether multiple paths are accepted or not.



13
14
15
# File 'lib/puppet/parameter/path.rb', line 13

def self.accept_arrays(bool = true)
  @accept_arrays = !!bool
end

.arrays?Boolean

Returns:



17
18
19
# File 'lib/puppet/parameter/path.rb', line 17

def self.arrays?
  @accept_arrays
end

Instance Method Details

#unsafe_munge(paths) ⇒ String+

This is the default implementation of ‘munge`. If the concrete parameter defines a `munge` method, this default implementation will be overridden. This default implementation does not perform any munging, it just checks the one/many paths constraints. A derived implementation can perform this check as: `paths.is_a?(Array) and ! self.class.arrays?` and raise a Error.

Parameters:

  • paths (String, Array<String>)

    one of multiple paths

Returns:

  • (String, Array<String>)

    the given paths

Raises:

  • (Puppet::Error)

    if the given paths does not comply with the on/many paths rule.



54
55
56
57
58
59
60
# File 'lib/puppet/parameter/path.rb', line 54

def unsafe_munge(paths)
  if paths.is_a?(Array) and !self.class.arrays? then
    fail _("%{name} only accepts a single path, not an array of paths") % { name: name }
  end

  paths
end

#unsafe_validate(paths) ⇒ void

This method returns an undefined value.

This is the default implementation of the ‘validate` method. It will be overridden if the validate option is used when defining the parameter.



42
43
44
# File 'lib/puppet/parameter/path.rb', line 42

def unsafe_validate(paths)
  validate_path(paths)
end

#validate_path(paths) ⇒ Array<String>

Performs validation of the given paths. If the concrete parameter defines a validation method, it may call this method to perform path validation.

Returns:

  • (Array<String>)

    the given paths

Raises:

  • (Puppet::Error)

    if this property is configured for single paths and an array is given

  • (Puppet::Error)

    if a path is not an absolute path



28
29
30
31
32
33
34
35
36
# File 'lib/puppet/parameter/path.rb', line 28

def validate_path(paths)
  if paths.is_a?(Array) and !self.class.arrays? then
    fail _("%{name} only accepts a single path, not an array of paths") % { name: name }
  end

  fail(_("%{name} must be a fully qualified path") % { name: name }) unless Array(paths).all? { |path| absolute_path?(path) }

  paths
end