Class: Bolt::PAL::YamlPlan::Step::Resources

Inherits:
Bolt::PAL::YamlPlan::Step show all
Defined in:
lib/bolt/pal/yaml_plan/step/resources.rb

Constant Summary

Constants inherited from Bolt::PAL::YamlPlan::Step

STEP_KEYS

Instance Attribute Summary

Attributes inherited from Bolt::PAL::YamlPlan::Step

#body

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Bolt::PAL::YamlPlan::Step

allowed_keys, create, parse_code_string, validate_puppet_code, validate_step_keys

Constructor Details

#initialize(body) ⇒ Resources

Returns a new instance of Resources.


16
17
18
19
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 16

def initialize(body)
  super
  @body['resources'] = normalize_resources(@body['resources'])
end

Class Method Details

.option_keysObject


8
9
10
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 8

def self.option_keys
  Set['catch_errors', 'description', 'noop', 'run_as']
end

.required_keysObject


12
13
14
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 12

def self.required_keys
  Set['resources', 'targets']
end

.validate(body, step_number) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 85

def self.validate(body, step_number)
  super

  body['resources'].each do |resource|
    if resource['type'] || resource['title']
      if !resource['type']
        err = "Resource declaration must include type key if title key is set"
        raise StepError.new(err, body['name'], step_number)
      elsif !resource['title']
        err = "Resource declaration must include title key if type key is set"
        raise StepError.new(err, body['name'], step_number)
      end
    else
      type_keys = (resource.keys - ['parameters'])
      if type_keys.empty?
        err = "Resource declaration is missing a type"
        raise StepError.new(err, body['name'], step_number)
      elsif type_keys.length > 1
        err = "Resource declaration has ambiguous type: could be #{type_keys.join(' or ')}"
        raise StepError.new(err, body['name'], step_number)
      end
    end
  end
end

Instance Method Details

#evaluate(scope, evaluator) ⇒ Object


32
33
34
35
36
37
38
39
40
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 32

def evaluate(scope, evaluator)
  evaluated = evaluator.evaluate_code_blocks(scope, body)

  scope.call_function('apply_prep', evaluated['targets'])

  apply_args = format_args(evaluated)
  manifest   = generate_manifest(evaluated['resources'])
  apply_manifest(scope, apply_args, manifest)
end

#transpileObject


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 127

def transpile
  code = StringIO.new

  code.print "  "
  code << function_call('apply_prep', [body['targets']])
  code.print "\n"

  code.print "  "
  code.print "$#{body['name']} = " if body['name']

  code << function_call('apply', format_args(body))

  code.print " {\n"

  declarations = body['resources'].map do |resource|
    type = resource['type'].is_a?(EvaluableString) ? resource['type'].value : resource['type']
    title = Bolt::Util.to_code(resource['title'])
    parameters = resource['parameters'].transform_values do |val|
      Bolt::Util.to_code(val)
    end

    resource_str = StringIO.new
    if parameters.empty?
      resource_str.puts "    #{type} { #{title}: }"
    else
      resource_str.puts "    #{type} { #{title}:"
      parameters.each do |key, val|
        resource_str.puts "      #{key} => #{val},"
      end
      resource_str.puts "    }"
    end
    resource_str.string
  end

  code.puts declarations.join("    ->\n")

  code.puts "  }\n"
  code.string
end