Class: Bolt::PAL::YamlPlan::Step
- Inherits:
-
Object
- Object
- Bolt::PAL::YamlPlan::Step
- Defined in:
- lib/bolt/pal/yaml_plan/step.rb,
lib/bolt/pal/yaml_plan/step/eval.rb,
lib/bolt/pal/yaml_plan/step/plan.rb,
lib/bolt/pal/yaml_plan/step/task.rb,
lib/bolt/pal/yaml_plan/step/script.rb,
lib/bolt/pal/yaml_plan/step/upload.rb,
lib/bolt/pal/yaml_plan/step/command.rb,
lib/bolt/pal/yaml_plan/step/resources.rb
Defined Under Namespace
Classes: Command, Eval, Plan, Resources, Script, Task, Upload
Constant Summary collapse
- STEP_KEYS =
%w[command script task plan source destination eval resources].freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .allowed_keys ⇒ Object
- .create(step_body, step_number) ⇒ Object
-
.parse_code_string(code, quote = false) ⇒ Object
Parses the an evaluable string, optionally quote it before parsing.
- .step_error(message, name, step_number) ⇒ Object
- .validate(body, step_number) ⇒ Object
-
.validate_puppet_code(step_key, value) ⇒ Object
Recursively ensure all puppet code can be parsed.
- .validate_step_keys(body, step_number) ⇒ Object
Instance Method Summary collapse
- #function_call(function, args) ⇒ Object
-
#initialize(step_body) ⇒ Step
constructor
A new instance of Step.
- #transpile ⇒ Object
Constructor Details
#initialize(step_body) ⇒ Step
Returns a new instance of Step.
37 38 39 40 41 42 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 37 def initialize(step_body) @name = step_body['name'] @description = step_body['description'] @targets = step_body['targets'] || step_body['target'] @body = step_body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 9 def body @body end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 9 def name @name end |
#targets ⇒ Object (readonly)
Returns the value of attribute targets.
9 10 11 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 9 def targets @targets end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 9 def type @type end |
Class Method Details
.allowed_keys ⇒ Object
11 12 13 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 11 def self.allowed_keys Set['name', 'description', 'target', 'targets'] end |
.create(step_body, step_number) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 17 def self.create(step_body, step_number) type_keys = (STEP_KEYS & step_body.keys) case type_keys.length when 0 raise step_error("No valid action detected", step_body['name'], step_number) when 1 type = type_keys.first else if type_keys.to_set == Set['source', 'destination'] type = 'upload' else raise step_error("Multiple action keys detected: #{type_keys.inspect}", step_body['name'], step_number) end end step_class = const_get("Bolt::PAL::YamlPlan::Step::#{type.capitalize}") step_class.validate(step_body, step_number) step_class.new(step_body) end |
.parse_code_string(code, quote = false) ⇒ Object
Parses the an evaluable string, optionally quote it before parsing
134 135 136 137 138 139 140 141 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 134 def self.parse_code_string(code, quote = false) if quote quoted = Puppet::Pops::Parser::EvaluatingParser.quote(code) Puppet::Pops::Parser::EvaluatingParser.new.parse_string(quoted) else Puppet::Pops::Parser::EvaluatingParser.new.parse_string(code) end end |
.step_error(message, name, step_number) ⇒ Object
127 128 129 130 131 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 127 def self.step_error(, name, step_number) identifier = name ? name.inspect : "number #{step_number}" error = "Parse error in step #{identifier}: \n #{}" Bolt::Error.new(error, 'bolt/invalid-plan') end |
.validate(body, step_number) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 48 def self.validate(body, step_number) validate_step_keys(body, step_number) begin body.each { |k, v| validate_puppet_code(k, v) } rescue Bolt::Error => e raise step_error(e.msg, body['name'], step_number) end unless body.fetch('parameters', {}).is_a?(Hash) msg = "Parameters key must be a hash" raise step_error(msg, body['name'], step_number) end if body.key?('name') name = body['name'] unless name.is_a?(String) && name.match?(Bolt::PAL::YamlPlan::VAR_NAME_PATTERN) = "Invalid step name: #{name.inspect}" raise step_error(, body['name'], step_number) end end end |
.validate_puppet_code(step_key, value) ⇒ Object
Recursively ensure all puppet code can be parsed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 100 def self.validate_puppet_code(step_key, value) case value when Array value.map { |element| validate_puppet_code(step_key, element) } when Hash value.each_with_object({}) do |(k, v), o| key = k.is_a?(Bolt::PAL::YamlPlan::EvaluableString) ? k.value : k o[key] = validate_puppet_code(key, v) end # CodeLiterals can be parsed directly when Bolt::PAL::YamlPlan::CodeLiteral parse_code_string(value.value) # BareString is parsed directly if it starts with '$' when Bolt::PAL::YamlPlan::BareString if value.value.start_with?('$') parse_code_string(value.value) else parse_code_string(value.value, true) end when Bolt::PAL::YamlPlan::EvaluableString # Must quote parsed strings to evaluate them parse_code_string(value.value, true) end rescue Puppet::Error => e raise Bolt::Error.new("Error parsing #{step_key.inspect}: #{e.}", "bolt/invalid-plan") end |
.validate_step_keys(body, step_number) ⇒ Object
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 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 71 def self.validate_step_keys(body, step_number) step_type = name.split('::').last.downcase # For validated step action, ensure only valid keys illegal_keys = body.keys.to_set - allowed_keys if illegal_keys.any? = "The #{step_type.inspect} step does not support: #{illegal_keys.to_a.inspect} key(s)" err = step_error(, body['name'], step_number) raise Bolt::Error.new(err, "bolt/invalid-plan") end # Ensure all required keys are present missing_keys = required_keys - body.keys # Handle cases where steps with a required 'targets' key are using the deprecated # 'target' key instead. # TODO: Remove this when 'target' is removed if body.include?('target') missing_keys -= ['targets'] end if missing_keys.any? = "The #{step_type.inspect} step requires: #{missing_keys.to_a.inspect} key(s)" err = step_error(, body['name'], step_number) raise Bolt::Error.new(err, "bolt/invalid-plan") end end |
Instance Method Details
#function_call(function, args) ⇒ Object
143 144 145 146 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 143 def function_call(function, args) code_args = args.map { |arg| Bolt::Util.to_code(arg) } "#{function}(#{code_args.join(', ')})" end |
#transpile ⇒ Object
44 45 46 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 44 def transpile raise NotImplementedError, "Step #{@name} does not supported conversion to Puppet plan language" end |