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) ⇒ 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`).
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 158 159 160 161 162 163 |
# File 'lib/vcoworkflows/workflow.rb', line 75 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 fail(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 if @options[:id] workflow_json = @service.get_workflow_for_id(@options[:id]) else workflow_json = @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 # rubocop:disable SpaceAroundOperators @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 # rubocop:enable SpaceAroundOperators # 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
30 31 32 |
# File 'lib/vcoworkflows/workflow.rb', line 30 def description @description end |
#execution_id ⇒ String (readonly)
Workflow execution ID
46 47 48 |
# File 'lib/vcoworkflows/workflow.rb', line 46 def execution_id @execution_id end |
#id ⇒ String (readonly)
Workflow GUID
18 19 20 |
# File 'lib/vcoworkflows/workflow.rb', line 18 def id @id end |
#input_parameters ⇒ Hash<VcoWorkflows::WorkflowParameter> (readonly)
Workflow Input Parameters: Hash of WorkflowParameters, keyed by name
34 35 36 |
# File 'lib/vcoworkflows/workflow.rb', line 34 def input_parameters @input_parameters end |
#name ⇒ String (readonly)
Workflow name
22 23 24 |
# File 'lib/vcoworkflows/workflow.rb', line 22 def name @name end |
#output_parameters ⇒ Hash<VcoWorkflows::WorkflowParameter> (readonly)
Workflow Output Parameters: Hash of WorkflowParameters, keyed by name
38 39 40 |
# File 'lib/vcoworkflows/workflow.rb', line 38 def output_parameters @output_parameters end |
#service ⇒ VcoWorkflows::WorkflowService
Workflow Service in use by this Workflow
42 43 44 |
# File 'lib/vcoworkflows/workflow.rb', line 42 def service @service end |
#source_json ⇒ String (readonly)
Workflow source JSON
50 51 52 |
# File 'lib/vcoworkflows/workflow.rb', line 50 def source_json @source_json end |
#version ⇒ String (readonly)
Workflow version
26 27 28 |
# File 'lib/vcoworkflows/workflow.rb', line 26 def version @version end |
Class Method Details
.parse_failure(error) ⇒ Object
Process exceptions raised in parse_parameters by bravely ignoring them
and forging ahead blindly!
231 232 233 234 235 236 237 |
# File 'lib/vcoworkflows/workflow.rb', line 231 def self.parse_failure(error) $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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/vcoworkflows/workflow.rb', line 196 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) end else begin value = parameter['value'][parameter['value'].keys.first]['value'] rescue StandardError => error parse_failure(error) 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
334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/vcoworkflows/workflow.rb', line 334 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. fail(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
350 351 352 |
# File 'lib/vcoworkflows/workflow.rb', line 350 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
313 314 315 |
# File 'lib/vcoworkflows/workflow.rb', line 313 def get_parameter(parameter_name) parameter(parameter_name).value end |
#log(execution_id = nil) ⇒ VcoWorkflows::WorkflowExecutionLog
Return logs for the given execution
365 366 367 368 369 |
# File 'lib/vcoworkflows/workflow.rb', line 365 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
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/vcoworkflows/workflow.rb', line 262 def parameter(parameter_name, 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 '' fail(IOError, ERR[:no_such_parameter]) end unless parameter_value.nil? @input_parameters[parameter_name] end |
#parameter=(wfparameter) ⇒ Object
Set a parameter with a WorkflowParameter object
278 279 280 |
# File 'lib/vcoworkflows/workflow.rb', line 278 def parameter=(wfparameter) @input_parameters[wfparameter.name] = wfparameter end |
#parameter?(parameter_name) ⇒ Boolean
Determine whether a parameter has been set
285 286 287 |
# File 'lib/vcoworkflows/workflow.rb', line 285 def parameter?(parameter_name) parameter(parameter_name).set? end |
#parameters=(parameter_hash) ⇒ Object
Set all input parameters using the given hash
292 293 294 |
# File 'lib/vcoworkflows/workflow.rb', line 292 def parameters=(parameter_hash) parameter_hash.each { |name, value| parameter(name, value) } end |
#password ⇒ String
vCO password used when creating this workflow object
180 181 182 |
# File 'lib/vcoworkflows/workflow.rb', line 180 def password [:password] end |
#required_parameters ⇒ Hash
Get an array of the names of all the required input parameters
245 246 247 248 249 |
# File 'lib/vcoworkflows/workflow.rb', line 245 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.
303 304 305 |
# File 'lib/vcoworkflows/workflow.rb', line 303 def set_parameter(parameter_name, value) parameter(parameter_name, value) end |
#to_s ⇒ String
Stringify the workflow
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/vcoworkflows/workflow.rb', line 375 def to_s string = "Workflow: #{@name}\n" string << "ID: #{@id}\n" string << "Description: #{@description}\n" string << "Version: #{@version}\n" string << "\nInput Parameters:\n" if @input_parameters.size > 0 @input_parameters.each_value { |wf_param| string << " #{wf_param}" } end string << "\nOutput Parameters:" << "\n" if @output_parameters.size > 0 @output_parameters.each_value { |wf_param| string << " #{wf_param}" } end # Assert string end |
#token(execution_id = nil) ⇒ VcoWorkflows::WorkflowToken
Return a WorkflowToken
357 358 359 360 |
# File 'lib/vcoworkflows/workflow.rb', line 357 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
168 169 170 |
# File 'lib/vcoworkflows/workflow.rb', line 168 def url [:url] end |
#username ⇒ String
vCO user name used when creating this workflow object
174 175 176 |
# File 'lib/vcoworkflows/workflow.rb', line 174 def username [:username] end |
#verify_ssl? ⇒ Boolean
Verify SSL?
186 187 188 |
# File 'lib/vcoworkflows/workflow.rb', line 186 def verify_ssl? [:verify_ssl] end |