Class: Puppet::Bindings

Inherits:
Object show all
Extended by:
Enumerable
Defined in:
lib/puppet/bindings.rb

Overview

This class allows registration of named bindings that are later contributed to a layer via a binding scheme.

The intended use is for a .rb file to be placed in confdir’s or module’s ‘lib/bindings` directory structure, with a name corresponding to the symbolic bindings name.

Here are two equivalent examples, the first using chained methods (which is compact for simple cases), and the second which uses a block.

If access is needed to the scope, this can be declared as a block parameter. If late evaluation is wanted, this can be achieved by binding a puppet expression. It is allowed to define methods in the block given to ‘newbindings`, these can be used when producing bindings. (Care should naturally be taken to not override any of the already defined methods). For all details see Pops::Binder::BindingsFactory, which is used behind the scenes.

Examples:

MyModule’s lib/bindings/mymodule/default.rb

Puppet::Bindings.newbindings('mymodule::default') do
  bind.integer.named('meaning of life').to(42)
end

Using blocks

Puppet::Bindings.newbindings('mymodule::default') do
  bind do
    integer
    name 'meaning of life'
    to 42
  end
end

MyModule’s lib/bindings/mymodule/default.rb with scope

Puppet::Bindings.newbindings('mymodule::default') do |scope|
  bind.integer.named('meaning of life').to("#{scope['::fqdn']} also think it is 42")
end

binding a puppet expression

Puppet::Bindings.newbindings('mymodule::default') do |scope|
  bind.integer.named('meaning of life').to(puppet_string("${::fqdn} also think it is 42")
end

defining method to be used while creating bindings

Puppet::Bindings.newbindings('mymodule::default') do
  def square(x)
    x * x
  end
  bind.integer.named('meaning of life squared').to(square(42))
end

Defined Under Namespace

Classes: NamedBindingsAdapter

Constant Summary collapse

Environment =
Puppet::Node::Environment

Class Method Summary collapse

Methods included from Enumerable

uniq

Class Method Details

.[](name) ⇒ Object



109
110
111
# File 'lib/puppet/bindings.rb', line 109

def self.[](name)
  get(name)
end

.eachObject

Supports Enumerable iteration (k,v) over the named bindings hash.



114
115
116
117
# File 'lib/puppet/bindings.rb', line 114

def self.each
  adapter = NamedBindingsAdapter.adapt(Puppet.lookup(:current_environment))
  adapter.each_pair {|k,v| yield k,v }
end

.get(name) ⇒ Proc, Puppet::Pops::Binder::Bindings::NamedBindings

Returns the named bindings with the given name, or nil if no such bindings have been registered.

Parameters:

  • name (String)

    The fully qualified name of a binding to get

Returns:

  • (Proc, Puppet::Pops::Binder::Bindings::NamedBindings)

    a Proc producing named bindings, or a named bindings directly



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

def self.get(name)
  adapter = NamedBindingsAdapter.adapt(Puppet.lookup(:current_environment))
  adapter[name]
end

.newbindings(name, &block) ⇒ Object

Constructs and registers a NamedBindings that later can be contributed to a bindings layer in a bindings configuration via a URI. The name is symbolic, fully qualified with module name, and at least one more qualifying name (where the name ‘default` is used in the default bindings configuration.

The given block is called with a ‘self` bound to an instance of Pops::Binder::BindingsFactory::BindingsContainerBuilder which most notably has a `#bind` method which it turn calls a block bound to an instance of Pops::Binder::BindingsFactory::BindingsBuilder. Depending on the use-case a direct chaining method calls or nested blocks may be used.

The block form is more suitable for longer, more complex forms of bindings.

Examples:

simple bindings

Puppet::Bindings.newbindings('mymodule::default') do
  bind.name('meaning of life').to(42)
  bind.integer.named('port').to(8080)
  bind.integer.named('apache::port').to(8080)
end


72
73
74
# File 'lib/puppet/bindings.rb', line 72

def self.newbindings(name, &block)
  register_proc(name, block)
end

.register(named_bindings) ⇒ Object

Registers a named_binding under its name

Parameters:

  • named_bindings (Puppet::Pops::Binder::Bindings::NamedBindings)

    The named bindings to register.



85
86
87
88
# File 'lib/puppet/bindings.rb', line 85

def self.register(named_bindings)
  adapter = NamedBindingsAdapter.adapt(Puppet.lookup(:current_environment))
  adapter[named_bindings.name] = named_bindings
end

.register_proc(name, block) ⇒ Object



76
77
78
79
# File 'lib/puppet/bindings.rb', line 76

def self.register_proc(name, block)
  adapter = NamedBindingsAdapter.adapt(Puppet.lookup(:current_environment))
  adapter[name] = block
end

.resolve(scope, name) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/puppet/bindings.rb', line 90

def self.resolve(scope, name)
  entry = get(name)
  return entry unless entry.is_a?(Proc)
  named_bindings = Puppet::Pops::Binder::BindingsFactory.safe_named_bindings(name, scope, &entry).model
  adapter = NamedBindingsAdapter.adapt(Puppet.lookup(:current_environment))
  adapter[named_bindings.name] = named_bindings
  named_bindings
end