Class: Eso::Conductor

Inherits:
Service show all
Defined in:
lib/eso/conductor.rb

Constant Summary

Constants inherited from Service

Service::CONTENT_TYPE_JSON

Instance Attribute Summary

Attributes inherited from Service

#host, #port, #url

Instance Method Summary collapse

Methods inherited from Service

#add_nexpose_session, #delete, #get, #http, #https, #post, #put, #request

Constructor Details

#initialize(host:, port: 3780, nsc:) ⇒ Eso::Conductor

Constructor for Conductor.

Parameters:

  • host (String)

    Hostname or IP address where this conductor resides.

  • port (Integer) (defaults to: 3780)

    The TCP port to connect to this conductor on.

  • nsc (Nexpose::Connection)

    A logged-in Nexpose::Connection object with a valid session used to authenticate.



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

def initialize(host:, port: 3780, nsc:)
  super(host: host, port: port, nsc: nsc)
  @url = "https://#{@host}:#{@port}/eso/conductor-service/api/"
end

Instance Method Details

#create_workflow(name:, steps:) ⇒ Eso::Workflow

Create a new workflow on this conductor.

Parameters:

  • name (String)

    The user-facing name the workflow will be created with.

  • steps (Array)

    An array containing each of the steps that the workflow will be created with, in Eso::Step format.

Returns:



128
129
130
131
132
133
134
135
# File 'lib/eso/conductor.rb', line 128

def create_workflow(name:, steps:)
  workflow = Workflow.new(name: name, steps: steps)

  resp = post(url: "#{@url}workflows/", payload: workflow.to_json)
  created_workflow = Workflow.load(self, resp[:id])

  created_workflow
end

#delete_all_workflowsObject

Delete all current workflows on the conductor.



156
157
158
159
# File 'lib/eso/conductor.rb', line 156

def delete_all_workflows
  wfs = workflows
  wfs.each { |wf| delete_workflow(workflow_id: wf.id) }
end

#delete_workflow(workflow_id:) ⇒ Object

Delete an existing workflow from the conductor.

Parameters:

  • workflow_id (String)

    The ID of the workflow to be deleted.



150
151
152
# File 'lib/eso/conductor.rb', line 150

def delete_workflow(workflow_id:)
  delete(url: "#{@url}workflows/#{workflow_id}")
end

#get_translation_key(step_type, label) ⇒ String

Returns the metadata key for a specified translated string. The translated value needs to be in language that the user has configured.

Parameters:

  • step_type (String)

    The step type to query metadata for. Valid values defined in Eso::StepNames

  • label (String)

    The translated value of which you are requesting the key for.

Returns:

  • (String)

    The metadata key corresponding to this label.



220
221
222
223
224
225
# File 'lib/eso/conductor.rb', line 220

def get_translation_key(step_type, label)
  json_data = get(url: "#{@url}services/nexpose/metadata/#{step_type}")

  target_hash = json_data[:labels].values.find { |label_hash| label_hash.values.include?(label) }
  target_hash.key(label).to_s if target_hash
end

#get_translation_label(step_type, key) ⇒ String

Returns the translated value of the specified key for a step type (defined in Eso::StepNames). The translated value will be based on the language settings the user has configured.

Parameters:

  • step_type (String)

    The step type to query metadata for. Valid values defined in Eso::StepNames

  • key (String)

    The key value in the metadata that maps to the desired label.

Returns:

  • (String)

    The translated value of the key.



206
207
208
209
210
211
# File 'lib/eso/conductor.rb', line 206

def get_translation_label(step_type, key)
  json_data = get(url: "#{@url}services/nexpose/metadata/#{step_type}")

  target_hash = json_data[:labels].values.find { |label_hash| label_hash.has_key?(key) }
  target_hash[key] if target_hash
end

#start_all_workflowsHash

Start all workflows that exist on the conductor.

Returns:

  • (Hash)

    A hash containing the states of all existing workflows, keyed by workflow ID.



181
182
183
184
185
186
# File 'lib/eso/conductor.rb', line 181

def start_all_workflows
  wf_states = workflow_states

  wf_states.each { |wf_id, state| start_workflow(workflow_id: wf_id) if state[:workflow_state] == "STOPPED" }
  workflow_states
end

#start_workflow(workflow_id:) ⇒ Object

Start the specified workflow.

Parameters:

  • workflow_id (String)

    The ID of the workflow to be started.



165
166
167
# File 'lib/eso/conductor.rb', line 165

def start_workflow(workflow_id:)
  post(url: "#{@url}workflows/#{workflow_id}/state")
end

#stop_all_workflowsHash

Stop all workflows that exist on the conductor.

Returns:

  • (Hash)

    A hash containing the states of all existing workflows, keyed by workflow ID.



192
193
194
195
196
197
# File 'lib/eso/conductor.rb', line 192

def stop_all_workflows
  wf_states = workflow_states

  wf_states.each { |wf_id, state| stop_workflow(workflow_id: wf_id) if state[:workflow_state] == "RUNNING" }
  workflow_states
end

#stop_workflow(workflow_id:) ⇒ Object

Stop the specified workflow.

Parameters:

  • workflow_id (String)

    The ID of the workflow to be stopped.



173
174
175
# File 'lib/eso/conductor.rb', line 173

def stop_workflow(workflow_id:)
  delete(url: "#{@url}workflows/#{workflow_id}/state")
end

#update_workflow(updated_workflow:) ⇒ Object

Update an existing workflow on the conductor to have the configuration of the workflow object passed into this method.

Parameters:

  • updated_workflow (Eso::Workflow)

    A workflow object that has already had all required changes made to it. This workflow must have an ID set.



141
142
143
144
# File 'lib/eso/conductor.rb', line 141

def update_workflow(updated_workflow:)
  payload = updated_workflow.to_json
  put(url: "#{@url}workflows/#{updated_workflow.id}", payload: payload)
end

#workflow_histories(starttime, endtime) ⇒ Array[Eso::Workflow::History]

Return the workflow histories with only the state histories for the given date range.

Parameters:

  • starttime (Fixnum)

    The time in milliseconds since epoch for which you want the workflow histories

  • endtime (Fixnum)

    The time in milliseconds since epoch for which you want the workflow histories

Returns:

  • (Array[Eso::Workflow::History])

    An array containing all of the workflow histories from the Conductor, which has StateHistory objects containing startTime’s within the specified time range. Only the StateHistories within that range are returned in the WorkflowHistory object. Returns an empty array if none are present.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eso/conductor.rb', line 49

def workflow_histories(starttime, endtime)
  histories = []
  json_data = get(url: "#{@url}workflows/history/#{starttime}/#{endtime}")
  json_data.each do |wf|
    # Initialize WorkflowHistory elements
    workflow_steps = []
    state_histories = []

    # Create a new WorkflowHistory with the details we already know
    workflow_history = Eso::Workflow::History.new(id: wf[:id],
                                                  name: wf[:name],
                                                  timeCreated: wf[:timeCreated],
                                                  state: wf[:state],
                                                  message: wf[:message],
                                                  steps: workflow_steps,
                                                  history: state_histories
    )

    # Parse the steps out of the response to be returned with the WorkflowHistory
    wf[:steps].each do |step|
      workflow_steps << Step.new(uuid: step[:uuid],
                               service_name: step[:serviceName],
                               workflow: workflow_history,
                               type_name: step[:stepConfiguration][:typeName],
                               previous_type_name: step[:stepConfiguration][:previousTypeName],
                               configuration_params: step[:stepConfiguration][:configurationParams])
    end
    workflow_history.steps = workflow_steps

    # Parse the histories out of the response, to be returned with the WorkflowHistory. For some reason.
    # this failed with named parameters. For now I returned it to positional.
    wf[:history].each do |history|
      state_histories << Eso::Workflow::StateHistory.new(history[:message],
                                       history[:state],
                                       history[:startTime])
    end
    workflow_history.state_histories = state_histories

    # Add the Workflow History we just built to the list to be returned.
    histories << workflow_history
  end
  histories
end

#workflow_state(workflow_id:) ⇒ String

Get the state of the specified workflow.

Parameters:

  • workflow_id (String)

    The ID of the workflow to retrieve the state of.

Returns:

  • (String)

    The current state of the workflow.



98
99
100
# File 'lib/eso/conductor.rb', line 98

def workflow_state(workflow_id:)
  get(url: "#{@url}workflows/#{workflow_id}/state")
end

#workflow_statesHash

Retrieve the states for all of the workflows created on the conductor.

Returns:

  • (Hash)

    A hash containing the states of all existing workflows, keyed by workflow ID.



115
116
117
118
119
120
# File 'lib/eso/conductor.rb', line 115

def workflow_states
  wfs = workflows
  states = {}
  wfs.each { |wf| states[wf.id] = workflow_state(workflow_id: wf.id) }
  states
end

#workflowsArray

Return all of the workflows that currently exist on this conductor.

Returns:

  • (Array)

    An array containing all of the current workflows on the conductor in Eso::Workflow object format. Returns an empty array if no workflows are present.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eso/conductor.rb', line 22

def workflows
  rv = []
  json_data = get(url: "#{@url}workflows/")
  json_data.each do |wf|
    workflow = Workflow.new(id: wf[:id], name: wf[:name])
    steps = wf[: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
    rv << workflow
  end
  rv
end

#workflows_state_count(state) ⇒ Integer

Get the count of items in a state of the specified workflow.

Parameters:

Returns:

  • (Integer)

    The number of workflows in the requested state.



107
108
109
# File 'lib/eso/conductor.rb', line 107

def workflows_state_count(state)
  get(url: "#{@url}workflows/count/#{state}")
end