Class: Puppet::Indirector::Terminus

Inherits:
Object
  • Object
show all
Extended by:
Util::Docs, Util::InstanceLoader
Defined in:
lib/puppet/indirector/terminus.rb

Overview

A simple class that can function as the base class for indirected types.

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::PUPPET_STACK_INSERTION_FRAME, Util::RFC_3986_URI_REGEX

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

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Class Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::InstanceLoader

instance_hash, instance_load, instance_loader, instance_loading?, loaded_instance, loaded_instances

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, 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, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, 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

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::Docs

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

Constructor Details

#initializeTerminus

Returns a new instance of Terminus.

Raises:



134
135
136
# File 'lib/puppet/indirector/terminus.rb', line 134

def initialize
  raise Puppet::DevError, _("Cannot create instances of abstract terminus types") if self.class.abstract_terminus?
end

Class Attribute Details

.abstract_terminusObject (readonly)

Returns the value of attribute abstract_terminus.



15
16
17
# File 'lib/puppet/indirector/terminus.rb', line 15

def abstract_terminus
  @abstract_terminus
end

.indirectionObject

Returns the value of attribute indirection.



15
16
17
# File 'lib/puppet/indirector/terminus.rb', line 15

def indirection
  @indirection
end

.nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/puppet/indirector/terminus.rb', line 14

def name
  @name
end

.terminus_typeObject

Returns the value of attribute terminus_type.



14
15
16
# File 'lib/puppet/indirector/terminus.rb', line 14

def terminus_type
  @terminus_type
end

Class Method Details

.abstract_terminus?Boolean

Are we an abstract terminus type, rather than an instance with an associated indirection?

Returns:

  • (Boolean)


19
20
21
# File 'lib/puppet/indirector/terminus.rb', line 19

def abstract_terminus?
  abstract_terminus
end

.const2name(const) ⇒ Object

Convert a constant to a short name.



24
25
26
# File 'lib/puppet/indirector/terminus.rb', line 24

def const2name(const)
  const.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_#{i.downcase}" }.intern
end

.indirection_nameObject



42
43
44
# File 'lib/puppet/indirector/terminus.rb', line 42

def indirection_name
  @indirection.name
end

.inherited(subclass) ⇒ Object

Register our subclass with the appropriate indirection. This follows the convention that our terminus is named after the indirection.



49
50
51
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
82
83
84
85
86
87
# File 'lib/puppet/indirector/terminus.rb', line 49

def inherited(subclass)
  longname = subclass.to_s
  if longname =~ /#<Class/
    raise Puppet::DevError, _("Terminus subclasses must have associated constants")
  end
  names = longname.split("::")

  # Convert everything to a lower-case symbol, converting camelcase to underscore word separation.
  name = names.pop.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_#{i.downcase}" }.intern

  subclass.name = name

  # Short-circuit the abstract types, which are those that directly subclass
  # the Terminus class.
  if self == Puppet::Indirector::Terminus
    subclass.mark_as_abstract_terminus
    return
  end

  # Set the terminus type to be the name of the abstract terminus type.
  # Yay, class/instance confusion.
  subclass.terminus_type = self.name

  # This subclass is specifically associated with an indirection.
  raise("Invalid name #{longname}") unless names.length > 0
  processed_name = names.pop.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_#{i.downcase}" }

  if processed_name.empty?
    raise Puppet::DevError, _("Could not discern indirection model from class constant")
  end

  # This will throw an exception if the indirection instance cannot be found.
  # Do this last, because it also registers the terminus type with the indirection,
  # which needs the above information.
  subclass.indirection = processed_name.intern

  # And add this instance to the instance hash.
  Puppet::Indirector::Terminus.register_terminus_class(subclass)
end

.mark_as_abstract_terminusObject

Mark that this instance is abstract.



90
91
92
# File 'lib/puppet/indirector/terminus.rb', line 90

def mark_as_abstract_terminus
  @abstract_terminus = true
end

.modelObject



94
95
96
# File 'lib/puppet/indirector/terminus.rb', line 94

def model
  indirection.model
end

.name2const(name) ⇒ Object

Convert a short name to a constant.



99
100
101
# File 'lib/puppet/indirector/terminus.rb', line 99

def name2const(name)
  name.to_s.capitalize.sub(/_(.)/) { |i| $1.upcase }
end

.register_terminus_class(klass) ⇒ Object

Register a class, probably autoloaded.



104
105
106
107
# File 'lib/puppet/indirector/terminus.rb', line 104

def register_terminus_class(klass)
  setup_instance_loading klass.indirection_name
  instance_hash(klass.indirection_name)[klass.name] = klass
end

.terminus_class(indirection_name, terminus_type) ⇒ Object

Return a terminus by name, using the autoloader.



110
111
112
113
# File 'lib/puppet/indirector/terminus.rb', line 110

def terminus_class(indirection_name, terminus_type)
  setup_instance_loading indirection_name
  loaded_instance(indirection_name, terminus_type)
end

.terminus_classes(indirection_name) ⇒ Object

Return all terminus classes for a given indirection.



116
117
118
119
120
121
# File 'lib/puppet/indirector/terminus.rb', line 116

def terminus_classes(indirection_name)
  setup_instance_loading indirection_name
  instance_loader(indirection_name).files_to_load(Puppet.lookup(:current_environment)).map do |file|
    File.basename(file).chomp(".rb").intern
  end
end

Instance Method Details

#allow_remote_requests?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/puppet/indirector/terminus.rb', line 146

def allow_remote_requests?
  true
end

#indirectionObject



130
131
132
# File 'lib/puppet/indirector/terminus.rb', line 130

def indirection
  self.class.indirection
end

#modelObject



138
139
140
# File 'lib/puppet/indirector/terminus.rb', line 138

def model
  self.class.model
end

#nameObject



142
143
144
# File 'lib/puppet/indirector/terminus.rb', line 142

def name
  self.class.name
end

#terminus_typeObject



150
151
152
# File 'lib/puppet/indirector/terminus.rb', line 150

def terminus_type
  self.class.terminus_type
end

#validate(request) ⇒ Object



154
155
156
157
158
159
# File 'lib/puppet/indirector/terminus.rb', line 154

def validate(request)
  if request.instance
    validate_model(request)
    validate_key(request)
  end
end

#validate_key(request) ⇒ Object



161
162
163
164
165
# File 'lib/puppet/indirector/terminus.rb', line 161

def validate_key(request)
  unless request.key == request.instance.name
    raise Puppet::Indirector::ValidationError, _("Instance name %{name} does not match requested key %{key}") % { name: request.instance.name.inspect, key: request.key.inspect }
  end
end

#validate_model(request) ⇒ Object



167
168
169
170
171
# File 'lib/puppet/indirector/terminus.rb', line 167

def validate_model(request)
  unless model === request.instance
    raise Puppet::Indirector::ValidationError, _("Invalid instance type %{klass}, expected %{model_type}") % { klass: request.instance.class.inspect, model_type: model.inspect }
  end
end