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

COMMON_STEP_KEYS, STEP_KEYS

Instance Attribute Summary

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

#name, #target, #type

Class Method Summary collapse

Instance Method Summary collapse

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

create, #function_call, parse_code_string, step_error, validate_puppet_code, validate_step_keys

Constructor Details

#initialize(step_body) ⇒ Resources

Returns a new instance of Resources.



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

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

Class Method Details

.allowed_keysObject



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

def self.allowed_keys
  super + Set['resources']
end

.required_keysObject



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

def self.required_keys
  Set['target']
end

.validate(body, step_number) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 22

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

Instance Method Details

#bodyObject



62
63
64
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 62

def body
  @body.merge('resources' => @normalized_resources)
end

#normalize_resources(resources) ⇒ Object

What if this comes from a code block?



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 48

def normalize_resources(resources)
  resources.map do |resource|
    if resource['type'] && resource['title']
      type = resource['type']
      title = resource['title']
    else
      type, = (resource.keys - ['parameters'])
      title = resource[type]
    end

    { 'type' => type, 'title' => title, 'parameters' => resource['parameters'] || {} }
  end
end

#transpileObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bolt/pal/yaml_plan/step/resources.rb', line 66

def transpile
  code = StringIO.new

  code.print "  "
  fn = 'apply_prep'
  args = [@target]
  code << function_call(fn, args)
  code.print "\n"

  code.print "  "
  code.print "$#{@name} = " if @name

  code.puts "apply(#{Bolt::Util.to_code(@target)}) {"

  declarations = @normalized_resources.map do |resource|
    type = resource['type'].is_a?(EvaluableString) ? resource['type'].value : resource['type']
    title = Bolt::Util.to_code(resource['title'])
    parameters = Bolt::Util.map_vals(resource['parameters']) 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