Class: Eso::Workflow

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

Overview

The following classes have mixed casing (snake and camel) to accommodate for the API. I guess a TODO would be to write a helper to automatically convert them.

Direct Known Subclasses

History

Defined Under Namespace

Modules: State Classes: History, StateHistory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, name:, steps: [], time_created: (Time.now.strftime('%s').to_i * 1000)) ⇒ Workflow

Constructor for the workflow

Parameters:

  • id (String) (defaults to: nil)

    ID of the workflow.

  • name (String)

    Name of the workflow.

  • steps (Array) (defaults to: [])

    Array of the steps that this workflow takes.

  • time_created (Fixnum) (defaults to: (Time.now.strftime('%s').to_i * 1000))

    The time the workflow was created in millis since epoch



25
26
27
28
29
30
# File 'lib/eso/workflow.rb', line 25

def initialize(id: nil, name:, steps: [], time_created: (Time.now.strftime('%s').to_i * 1000))
  @id = id
  @name = name
  @steps = steps
  @timeCreated = time_created
end

Instance Attribute Details

#idObject

The id of the workflow. This will be created upon saving to the server upon creation.



7
8
9
# File 'lib/eso/workflow.rb', line 7

def id
  @id
end

#nameObject

The name of the workflow. This is required.



10
11
12
# File 'lib/eso/workflow.rb', line 10

def name
  @name
end

#stepsObject

An array of the steps this workflow takes action on.



13
14
15
# File 'lib/eso/workflow.rb', line 13

def steps
  @steps
end

#timeCreatedObject

The time the workflow was created in milliseconds since epoch



16
17
18
# File 'lib/eso/workflow.rb', line 16

def timeCreated
  @timeCreated
end

Class Method Details

.load(conductor, id) ⇒ Workflow

Load an existing workflow from the API.

Parameters:

  • conductor (Conductor)

    The Conductor object governing the workflows

  • id (String)

    ID of the workflow to load

Returns:

  • (Workflow)

    Workflow object that was loaded.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eso/workflow.rb', line 38

def self.load(conductor, id)
  uri = "#{conductor.url}workflows/#{id}"
  resp = conductor.get(url: uri)
  workflow = self.new(id: resp[:id], name: resp[:name])
  steps = resp[:steps]
  steps.each do |step|
    workflow_step = Step.new(uuid: step[:uuid],
                             service_name: step[:serviceName],
                             workflow: workflow,
                             type_name: step[:stepConfiguration][:typeName],
                             previous_type_name: step[:stepConfiguration][:previousTypeName],
                             configuration_params: step[:stepConfiguration][:configurationParams])
    workflow.steps << workflow_step
  end
  workflow
end

Instance Method Details

#get_step(type_name) ⇒ Step

Return the relevant step based on the given service name. For example, if you want the step related to the scan service you would pass ‘nexpose-scan-service’.

Parameters:

  • service_name (String)

    Service name to be returned.

Returns:

  • (Step)

    Step object corresponding to the given service.



61
62
63
64
65
# File 'lib/eso/workflow.rb', line 61

def get_step(type_name)
  @steps.find do |step|
    step.type_name == type_name
  end
end

#to_hashHash{}

Return this object as a hash. The corresponding steps will still be objects.

Returns:

  • (Hash{})

    Hash interpretation of this workflow.



97
98
99
100
101
# File 'lib/eso/workflow.rb', line 97

def to_hash
  hash = {}
  instance_variables.each { |var| hash[var.to_s.delete('@')] = instance_variable_get(var) }
  hash
end

#to_jsonString

Return this object and the associated steps in a digestible JSON format.

Returns:

  • (String)

    JSON interpretation of this workflow.



84
85
86
87
88
89
90
91
# File 'lib/eso/workflow.rb', line 84

def to_json
  hash = self.to_hash
  steps = hash['steps']
  hashified_steps = []
  steps.each { |step| hashified_steps << step.to_hash }
  hash['steps'] = hashified_steps
  hash.to_json
end

#triggerStep

Return the trigger step of a workflow. The trigger step is defined as a step that monitors for events that will cause the action to fire.

Currently triggers do not have a previous-action so that is what this is returning. This behavior could change in ESO’s future.

Returns:

  • (Step)

    Step object representation of the trigger step.



74
75
76
77
78
# File 'lib/eso/workflow.rb', line 74

def trigger
  @steps.find do |step|
    step.stepConfiguration.previousTypeName.nil?
  end
end