Class: Bosh::Template::EvaluationContext

Inherits:
Object
  • Object
show all
Includes:
PropertyHelper
Defined in:
lib/bosh/template/evaluation_context.rb

Overview

Helper class to evaluate templates. Used by Director, CLI and Agent.

Defined Under Namespace

Classes: ActiveElseBlock, BackCompatOpenStruct, InactiveElseBlock

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PropertyHelper

#copy_property, #lookup_property, #set_property, #sort_property, #validate_properties_format

Constructor Details

#initialize(spec, dns_encoder) ⇒ EvaluationContext

Returns a new instance of EvaluationContext.

Parameters:

  • spec (Hash)

    Template spec



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bosh/template/evaluation_context.rb', line 32

def initialize(spec, dns_encoder)
  unless spec.is_a?(Hash)
    raise EvaluationFailed,
          'Invalid spec provided for template evaluation context, ' +
              "Hash expected, #{spec.class} given"
  end

  if spec['job'].is_a?(Hash)
    @name = spec['job']['name']
  else
    @name = nil
  end

  @index = spec['index']
  @spec = openstruct(spec, BackCompatOpenStruct)
  @raw_properties = spec['properties'] || {}
  @properties = openstruct(@raw_properties)
  @dns_encoder = dns_encoder

  @links = spec['links'] || {}
end

Instance Attribute Details

#indexInteger (readonly)

Returns Template instance index.

Returns:

  • (Integer)

    Template instance index



20
21
22
# File 'lib/bosh/template/evaluation_context.rb', line 20

def index
  @index
end

#nameString (readonly)

Returns Template name.

Returns:

  • (String)

    Template name



17
18
19
# File 'lib/bosh/template/evaluation_context.rb', line 17

def name
  @name
end

#propertiesHash (readonly)

Returns Template properties.

Returns:

  • (Hash)

    Template properties



23
24
25
# File 'lib/bosh/template/evaluation_context.rb', line 23

def properties
  @properties
end

#raw_propertiesHash (readonly)

Returns Raw template properties (no openstruct).

Returns:

  • (Hash)

    Raw template properties (no openstruct)



26
27
28
# File 'lib/bosh/template/evaluation_context.rb', line 26

def raw_properties
  @raw_properties
end

#specHash (readonly)

Returns Template spec.

Returns:

  • (Hash)

    Template spec



29
30
31
# File 'lib/bosh/template/evaluation_context.rb', line 29

def spec
  @spec
end

Instance Method Details

#get_bindingBinding

Returns Template binding.

Returns:

  • (Binding)

    Template binding



55
56
57
# File 'lib/bosh/template/evaluation_context.rb', line 55

def get_binding
  binding.taint
end

Run a block of code if the link given exists

Parameters:

  • name (String)

    of the link

Yields:

  • (Object)

    link, which is an array of instances



126
127
128
129
130
131
132
133
134
# File 'lib/bosh/template/evaluation_context.rb', line 126

def if_link(name)
  link_spec = lookup_property(@links, name)
  if link_spec.nil? || !link_spec.has_key?('instances')
    return ActiveElseBlock.new(self)
  else
    yield create_evaluation_link(link_spec)
    InactiveElseBlock.new
  end
end

#if_p(*names) {|Object| ... } ⇒ Object

Run a block of code if all given properties are defined

Parameters:

  • names (Array<String>)

    Property names

Yields:

  • (Object)

    property values



101
102
103
104
105
106
107
108
109
110
# File 'lib/bosh/template/evaluation_context.rb', line 101

def if_p(*names)
  values = names.map do |name|
    value = lookup_property(@raw_properties, name)
    return ActiveElseBlock.new(self) if value.nil?
    value
  end

  yield *values
  InactiveElseBlock.new
end

Raises:



112
113
114
115
116
117
118
119
120
121
# File 'lib/bosh/template/evaluation_context.rb', line 112

def link(name)
  link_spec = lookup_property(@links, name)
  raise UnknownLink.new(name) if link_spec.nil?

  if link_spec.has_key?('instances')
    return create_evaluation_link(link_spec)
  end

  raise UnknownLink.new(name)
end

#p(name, default_value) ⇒ Object #p(names, default_value) ⇒ Object #p(names) ⇒ Object #p(name) ⇒ Object

Property lookup helper

Overloads:

  • #p(name, default_value) ⇒ Object

    Returns property value or default value if property not set

    Parameters:

    • name (String)

      Property name

    • default_value (Object)

      Default value

    Returns:

    • (Object)

      Property value

  • #p(names, default_value) ⇒ Object

    Returns first property from the list that is set or default value if none of them are set

    Parameters:

    • names (Array<String>)

      Property names

    • default_value (Object)

      Default value

    Returns:

    • (Object)

      Property value

  • #p(names) ⇒ Object

    Looks up first property from the list that is set, raises an error if none of them are set.

    Parameters:

    • names (Array<String>)

      Property names

    Returns:

    • (Object)

      Property value

    Raises:

    • (Bosh::Common::UnknownProperty)
  • #p(name) ⇒ Object

    Looks up property and raises an error if it’s not set

    Parameters:

    • name (String)

      Property name

    Returns:

    • (Object)

      Property value

    Raises:

    • (Bosh::Common::UnknownProperty)

Raises:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bosh/template/evaluation_context.rb', line 86

def p(*args)
  names = Array(args[0])

  names.each do |name|
    result = lookup_property(@raw_properties, name)
    return result unless result.nil?
  end

  return args[1] if args.length == 2
  raise UnknownProperty.new(names)
end