Class: VcoWorkflows::Workflow
- Inherits:
-
Object
- Object
- VcoWorkflows::Workflow
- Defined in:
- lib/vcoworkflows/workflow.rb
Overview
Class to represent a Workflow as presented by vCenter Orchestrator.
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
Workflow description.
-
#execution_id ⇒ String
readonly
Workflow execution ID.
-
#id ⇒ String
readonly
Workflow GUID.
-
#input_parameters ⇒ Hash<VcoWorkflows::WorkflowParameter>
readonly
Workflow Input Parameters: Hash of WorkflowParameters, keyed by name.
-
#name ⇒ String
readonly
Workflow name.
-
#output_parameters ⇒ Hash<VcoWorkflows::WorkflowParameter>
readonly
Workflow Output Parameters: Hash of WorkflowParameters, keyed by name.
-
#service ⇒ VcoWorkflows::WorkflowService
Workflow Service in use by this Workflow.
-
#source_json ⇒ String
readonly
Workflow source JSON.
-
#version ⇒ String
readonly
Workflow version.
Class Method Summary collapse
-
.parse_failure(error, wfparam, parameter) ⇒ Object
Process exceptions raised in parse_parameters by bravely ignoring them and forging ahead blindly!.
-
.parse_parameters(parameter_data = []) ⇒ Hash
Parse json parameters and return a nice hash by vCO.
Instance Method Summary collapse
-
#execute(workflow_service = nil) ⇒ String
Execute this workflow.
-
#executions ⇒ Hash
Get a list of all the executions of this workflow.
-
#get_parameter(parameter_name) ⇒ Object
deprecated
Deprecated.
Use #parameter to retrieve the WorkflowParameter object, instead
-
#initialize(name = nil, options = {}) ⇒ VcoWorkflows::Workflow
constructor
Create a Workflow object given vCenter Orchestrator’s JSON description.
-
#log(execution_id = nil) ⇒ VcoWorkflows::WorkflowExecutionLog
Return logs for the given execution.
-
#parameter(parameter_name, parameter_value = nil) ⇒ VcoWorkflows::WorkflowParameter
Get the parameter object named.
-
#parameter=(wfparameter) ⇒ Object
Set a parameter with a WorkflowParameter object.
-
#parameter?(parameter_name) ⇒ Boolean
Determine whether a parameter has been set.
-
#parameters=(parameter_hash) ⇒ Object
Set all input parameters using the given hash.
-
#password ⇒ String
vCO password used when creating this workflow object.
-
#required_parameters ⇒ Hash
Get an array of the names of all the required input parameters.
-
#set_parameter(parameter_name, value) ⇒ VcoWorkflows::WorkflowParameter
deprecated
Deprecated.
Use #parameter instead
-
#to_s ⇒ String
Stringify the workflow.
-
#token(execution_id = nil) ⇒ VcoWorkflows::WorkflowToken
Return a WorkflowToken.
-
#url ⇒ String
vCO API URL used when creating this workflow.
-
#username ⇒ String
vCO user name used when creating this workflow object.
-
#verify_ssl? ⇒ Boolean
Verify SSL?.
Constructor Details
#initialize(name = nil, options = {}) ⇒ VcoWorkflows::Workflow
Create a Workflow object given vCenter Orchestrator’s JSON description
When passed ‘url`, `username` and `password` the necessary session and service objects will be created behind the scenes. Alternatively you can pass in a Config or a WorkflowService object if you have constructed them yourself. You may also pass in the path to a configuration file (`config_file`).
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 |
# File 'lib/vcoworkflows/workflow.rb', line 71 def initialize(name = nil, = {}) @options = { id: nil, url: nil, username: nil, password: nil, verify_ssl: true, service: nil, config: nil, config_file: nil }.merge() config = nil @service = nil @execution_id = nil # ------------------------------------------------------------- # Figure out how to get a workflow service. If I can't, I die. # (DUN dun dun...) if [:service] @service = [:service] else # If we were given a configuration object, use it # If we were given a config file path, use it # If we have a url, username and password, use them # If all we have is a URL, try anyway, maybe we'll get username and # password from ENV values (hey, it might work...) if @options[:config] config = @options[:config] elsif @options[:config_file] config = VcoWorkflows::Config.new(config_file: @options[:config_file]) elsif @options[:url] && @options[:username] && @options[:password] config = VcoWorkflows::Config.new(url: @options[:url], username: @options[:username], password: @options[:password], verify_ssl: @options[:verify_ssl]) elsif @options[:url] config = VcoWorkflows::Config.new(url: @options[:url], verify_ssl: @options[:verify_ssl]) end # If we got a config object above, great. If it's still nil, VcoSession # will accept that and try to load the default config file. session = VcoWorkflows::VcoSession.new(config: config) @service = VcoWorkflows::WorkflowService.new(session) end raise(IOError, 'Unable to create/use a WorkflowService!') if @service.nil? # ------------------------------------------------------------- # Retrieve the workflow and parse it into a data structure # If we're given both a name and ID, prefer the id workflow_json = if @options[:id] @service.get_workflow_for_id(@options[:id]) else @service.get_workflow_for_name(name) end workflow_data = JSON.parse(workflow_json) # Set up the attributes if they exist in the data json, # otherwise nil them @id = workflow_data.key?('id') ? workflow_data['id'] : nil @name = workflow_data.key?('name') ? workflow_data['name'] : nil @version = workflow_data.key?('version') ? workflow_data['version'] : nil @description = workflow_data.key?('description') ? workflow_data['description'] : nil # Process the input parameters if workflow_data.key?('input-parameters') @input_parameters = Workflow.parse_parameters(workflow_data['input-parameters']) else @input_parameters = {} end # Identify required input_parameters wfpres = VcoWorkflows::WorkflowPresentation.new(@service, @id) wfpres.required.each do |req_param| @input_parameters[req_param].required(true) end # Process the output parameters if workflow_data.key?('output-parameters') @output_parameters = Workflow.parse_parameters(workflow_data['output-parameters']) else @output_parameters = {} end end |
Instance Attribute Details
#description ⇒ String (readonly)
Workflow description
28 29 30 |
# File 'lib/vcoworkflows/workflow.rb', line 28 def description @description end |
#execution_id ⇒ String (readonly)
Workflow execution ID
44 45 46 |
# File 'lib/vcoworkflows/workflow.rb', line 44 def execution_id @execution_id end |
#id ⇒ String (readonly)
Workflow GUID
16 17 18 |
# File 'lib/vcoworkflows/workflow.rb', line 16 def id @id end |
#input_parameters ⇒ Hash<VcoWorkflows::WorkflowParameter> (readonly)
Workflow Input Parameters: Hash of WorkflowParameters, keyed by name
32 33 34 |
# File 'lib/vcoworkflows/workflow.rb', line 32 def input_parameters @input_parameters end |
#name ⇒ String (readonly)
Workflow name
20 21 22 |
# File 'lib/vcoworkflows/workflow.rb', line 20 def name @name end |
#output_parameters ⇒ Hash<VcoWorkflows::WorkflowParameter> (readonly)
Workflow Output Parameters: Hash of WorkflowParameters, keyed by name
36 37 38 |
# File 'lib/vcoworkflows/workflow.rb', line 36 def output_parameters @output_parameters end |
#service ⇒ VcoWorkflows::WorkflowService
Workflow Service in use by this Workflow
40 41 42 |
# File 'lib/vcoworkflows/workflow.rb', line 40 def service @service end |
#source_json ⇒ String (readonly)
Workflow source JSON
48 49 50 |
# File 'lib/vcoworkflows/workflow.rb', line 48 def source_json @source_json end |
#version ⇒ String (readonly)
Workflow version
24 25 26 |
# File 'lib/vcoworkflows/workflow.rb', line 24 def version @version end |
Class Method Details
.parse_failure(error, wfparam, parameter) ⇒ Object
Process exceptions raised in parse_parameters by bravely ignoring them
and forging ahead blindly!
223 224 225 226 227 228 229 |
# File 'lib/vcoworkflows/workflow.rb', line 223 def self.parse_failure(error, wfparam, parameter) $stderr.puts "\nWhoops!" $stderr.puts "Ran into a problem parsing parameter #{wfparam.name} (#{wfparam.type})!" $stderr.puts "Source data: #{JSON.pretty_generate(parameter)}\n" $stderr.puts error. $stderr.puts "\nBravely forging on and ignoring parameter #{wfparam.name}!" end |
.parse_parameters(parameter_data = []) ⇒ Hash
Parse json parameters and return a nice hash by vCO
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/vcoworkflows/workflow.rb', line 190 def self.parse_parameters(parameter_data = []) wfparams = {} parameter_data.each do |parameter| wfparam = VcoWorkflows::WorkflowParameter.new(parameter['name'], parameter['type']) if parameter['value'] if wfparam.type.eql?('Array') value = [] begin parameter['value'][wfparam.type.downcase]['elements'].each do |element| value << element[element.keys.first]['value'] end rescue StandardError => error parse_failure(error, wfparam, parameter) end else begin value = parameter['value'][parameter['value'].keys.first]['value'] rescue StandardError => error parse_failure(error, wfparam, parameter) end end value = nil if value.eql?('null') wfparam.set(value) end wfparams[parameter['name']] = wfparam end wfparams end |
Instance Method Details
#execute(workflow_service = nil) ⇒ String
Execute this workflow
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/vcoworkflows/workflow.rb', line 308 def execute(workflow_service = nil) # If we're not given an explicit workflow service for this execution # request, use the one defined when we were created. workflow_service = @service if workflow_service.nil? # If we still have a nil workflow_service, go home. raise(IOError, ERR[:no_workflow_service_defined]) if workflow_service.nil? # Make sure we didn't forget any required parameters verify_parameters # Let's get this thing running! @execution_id = workflow_service.execute_workflow(@id, input_parameter_json) end |
#executions ⇒ Hash
Get a list of all the executions of this workflow. Wrapper for VcoWorkflows::WorkflowService#get_execution_list
323 324 325 |
# File 'lib/vcoworkflows/workflow.rb', line 323 def executions @service.get_execution_list(@id) end |
#get_parameter(parameter_name) ⇒ Object
Use #parameter to retrieve the VcoWorkflows::WorkflowParameter object, instead
Get the value for an input parameter
301 302 303 |
# File 'lib/vcoworkflows/workflow.rb', line 301 def get_parameter(parameter_name) parameter(parameter_name).value end |
#log(execution_id = nil) ⇒ VcoWorkflows::WorkflowExecutionLog
Return logs for the given execution
338 339 340 341 342 |
# File 'lib/vcoworkflows/workflow.rb', line 338 def log(execution_id = nil) execution_id = @execution_id if execution_id.nil? log_json = @service.get_log(@id, execution_id) VcoWorkflows::WorkflowExecutionLog.new(log_json) end |
#parameter(parameter_name, parameter_value = nil) ⇒ VcoWorkflows::WorkflowParameter
Get the parameter object named. If a value is provided, set the value and return the parameter object.
To get a parameter value, use parameter(parameter_name).value
rubocop:disable Metrics/MethodLength
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/vcoworkflows/workflow.rb', line 250 def parameter(parameter_name, parameter_value = nil) unless parameter_value.nil? if @input_parameters.key?(parameter_name) @input_parameters[parameter_name].set parameter_value else $stderr.puts "\nAttempted to set a value for a non-existent WorkflowParameter!" $stderr.puts "It appears that there is no parameter \"#{parameter}\"." $stderr.puts "Valid parameter names are: #{@input_parameters.keys.join(', ')}" $stderr.puts '' raise(IOError, ERR[:no_such_parameter]) end end @input_parameters[parameter_name] end |
#parameter=(wfparameter) ⇒ Object
Set a parameter with a WorkflowParameter object
268 269 270 |
# File 'lib/vcoworkflows/workflow.rb', line 268 def parameter=(wfparameter) @input_parameters[wfparameter.name] = wfparameter end |
#parameter?(parameter_name) ⇒ Boolean
Determine whether a parameter has been set
275 276 277 |
# File 'lib/vcoworkflows/workflow.rb', line 275 def parameter?(parameter_name) parameter(parameter_name).set? end |
#parameters=(parameter_hash) ⇒ Object
Set all input parameters using the given hash
282 283 284 |
# File 'lib/vcoworkflows/workflow.rb', line 282 def parameters=(parameter_hash) parameter_hash.each { |name, value| parameter(name, value) } end |
#password ⇒ String
vCO password used when creating this workflow object
174 175 176 |
# File 'lib/vcoworkflows/workflow.rb', line 174 def password [:password] end |
#required_parameters ⇒ Hash
Get an array of the names of all the required input parameters
234 235 236 237 238 |
# File 'lib/vcoworkflows/workflow.rb', line 234 def required_parameters required = {} @input_parameters.each_value { |v| required[v.name] = v if v.required? } required end |
#set_parameter(parameter_name, value) ⇒ VcoWorkflows::WorkflowParameter
Use #parameter instead
Set a parameter to a value.
291 292 293 |
# File 'lib/vcoworkflows/workflow.rb', line 291 def set_parameter(parameter_name, value) parameter(parameter_name, value) end |
#to_s ⇒ String
Stringify the workflow
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/vcoworkflows/workflow.rb', line 348 def to_s string = "Workflow: #{@name}\n" string << "ID: #{@id}\n" string << "Description: #{@description}\n" string << "Version: #{@version}\n" string << "\nInput Parameters:\n" unless @input_parameters.empty? @input_parameters.each_value { |wf_param| string << " #{wf_param}" } end string << "\nOutput Parameters:" << "\n" unless @output_parameters.empty? @output_parameters.each_value { |wf_param| string << " #{wf_param}" } end # Assert string end |
#token(execution_id = nil) ⇒ VcoWorkflows::WorkflowToken
Return a WorkflowToken
330 331 332 333 |
# File 'lib/vcoworkflows/workflow.rb', line 330 def token(execution_id = nil) execution_id = @execution_id if execution_id.nil? VcoWorkflows::WorkflowToken.new(@service, @id, execution_id) end |
#url ⇒ String
vCO API URL used when creating this workflow
162 163 164 |
# File 'lib/vcoworkflows/workflow.rb', line 162 def url [:url] end |
#username ⇒ String
vCO user name used when creating this workflow object
168 169 170 |
# File 'lib/vcoworkflows/workflow.rb', line 168 def username [:username] end |
#verify_ssl? ⇒ Boolean
Verify SSL?
180 181 182 |
# File 'lib/vcoworkflows/workflow.rb', line 180 def verify_ssl? [:verify_ssl] end |