Class: VcoWorkflows::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/vcoworkflows/workflow.rb

Overview

Class to represent a Workflow as presented by vCenter Orchestrator.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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`).

Parameters:

  • name (String) (defaults to: nil)

    Name of the requested workflow

  • options (Hash) (defaults to: {})

    Hash of options:

    • id: (String) GUID for the Workflow

    • url: (String) vCO REST API URL

    • username: (String) User to authenticate as

    • password: (String) Password for username

    • verify_ssl: (Boolean) Perform TLS/SSL certificate validation

    • service: (VcoWorkflows::WorkflowService) WorkflowService to use for communicating to vCO

    • config: (VcoWorkflows::Config) Configuration object to use for this workflow’s session

    • config_file: (String) Path to load configuration file from for this workflow’s session



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 = {})
  @options = {
    id: nil,
    url: nil,
    username: nil,
    password: nil,
    verify_ssl: true,
    service: nil,
    config: nil,
    config_file: nil
  }.merge(options)

  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 options[:service]
    @service = options[: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

#descriptionString (readonly)

Workflow description

Returns:

  • (String)

    workflow description



30
31
32
# File 'lib/vcoworkflows/workflow.rb', line 30

def description
  @description
end

#execution_idString (readonly)

Workflow execution ID

Returns:

  • (String)


46
47
48
# File 'lib/vcoworkflows/workflow.rb', line 46

def execution_id
  @execution_id
end

#idString (readonly)

Workflow GUID

Returns:

  • (String)

    workflow GUID



18
19
20
# File 'lib/vcoworkflows/workflow.rb', line 18

def id
  @id
end

#input_parametersHash<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

#nameString (readonly)

Workflow name

Returns:

  • (String)

    workflow name



22
23
24
# File 'lib/vcoworkflows/workflow.rb', line 22

def name
  @name
end

#output_parametersHash<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

#serviceVcoWorkflows::WorkflowService

Workflow Service in use by this Workflow



42
43
44
# File 'lib/vcoworkflows/workflow.rb', line 42

def service
  @service
end

#source_jsonString (readonly)

Workflow source JSON

Returns:

  • (String)


50
51
52
# File 'lib/vcoworkflows/workflow.rb', line 50

def source_json
  @source_json
end

#versionString (readonly)

Workflow version

Returns:

  • (String)

    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!

Parameters:

  • error (Exception)


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.message
  $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

Parameters:

  • parameter_data (Array<Hash>) (defaults to: [])

    Array of parameter data hashes

Returns:

  • (Hash)


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

Parameters:

Returns:

  • (String)

    Workflow Execution ID



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

#executionsHash

Get a list of all the executions of this workflow. Wrapper for VcoWorkflows::WorkflowService#get_execution_list

Returns:

  • (Hash)


350
351
352
# File 'lib/vcoworkflows/workflow.rb', line 350

def executions
  @service.get_execution_list(@id)
end

#get_parameter(parameter_name) ⇒ Object

Deprecated.

Use #parameter to retrieve the VcoWorkflows::WorkflowParameter object, instead

Get the value for an input parameter

Parameters:

  • parameter_name (String)

    Name of the input parameter whose value to get

Returns:

  • (Object)


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

Parameters:

  • execution_id (String) (defaults to: nil)

    optional execution id to get logs for

Returns:



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

Parameters:

  • parameter_name (String)

    Name of the parameter to get

  • parameter_value (Object, nil) (defaults to: nil)

    Optional value for parameter.

Returns:



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

Parameters:



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

Parameters:

  • parameter_name (String)

    Name of the parameter to check

Returns:

  • (Boolean)


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

Parameters:

  • parameter_hash (Hash)

    input parameter values keyed by input_parameter name



292
293
294
# File 'lib/vcoworkflows/workflow.rb', line 292

def parameters=(parameter_hash)
  parameter_hash.each { |name, value| parameter(name, value) }
end

#passwordString

vCO password used when creating this workflow object

Returns:

  • (String)


180
181
182
# File 'lib/vcoworkflows/workflow.rb', line 180

def password
  options[:password]
end

#required_parametersHash

Get an array of the names of all the required input parameters

Returns:

  • (Hash)

    Hash of WorkflowParameter input parameters which are required for this workflow



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

Deprecated.

Use #parameter instead

Set a parameter to a value.

Parameters:

  • parameter_name (String)

    name of the parameter to set

  • value (Object)

    value to set

Returns:



303
304
305
# File 'lib/vcoworkflows/workflow.rb', line 303

def set_parameter(parameter_name, value)
  parameter(parameter_name, value)
end

#to_sString

Stringify the workflow

Returns:

  • (String)


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

Parameters:

  • execution_id (String) (defaults to: nil)

    optional execution id to get logs for

Returns:



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

#urlString

vCO API URL used when creating this workflow

Returns:

  • (String)


168
169
170
# File 'lib/vcoworkflows/workflow.rb', line 168

def url
  options[:url]
end

#usernameString

vCO user name used when creating this workflow object

Returns:

  • (String)


174
175
176
# File 'lib/vcoworkflows/workflow.rb', line 174

def username
  options[:username]
end

#verify_ssl?Boolean

Verify SSL?

Returns:

  • (Boolean)


186
187
188
# File 'lib/vcoworkflows/workflow.rb', line 186

def verify_ssl?
  options[:verify_ssl]
end