File: mistral_workflow_requester.rb

package info (click to toggle)
puppet-module-mistral 25.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,004 kB
  • sloc: ruby: 2,093; python: 38; makefile: 11; sh: 10
file content (64 lines) | stat: -rw-r--r-- 2,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'csv'
require 'puppet'
require 'timeout'

class Puppet::Provider::MistralWorkflowRequester < Puppet::Provider::Openstack
  # This class only overrides the request method of the Openstack provider
  # because Mistral behaves differently when creating workflows.
  # The mistral client allows the creation of multiple workflows with a single
  # definition file so the creation call returns a list of workflows instead of
  # a single value.
  # Consequently the shell output format is not available and the csv formatter
  # must be used instead.

  # Returns an array of hashes, where the keys are the downcased CSV headers
  # with underscores instead of spaces
  #
  # @param options [Hash] Other options
  # @options :no_retry_exception_msgs [Array<Regexp>,Regexp] exception without retries
  def self.request(service, action, properties, credentials=nil, options={})
    env = credentials ? credentials.to_env : {}

    # We only need to override the create action
    if action != 'create'
      return super
    end

    Puppet::Util.withenv(env) do
      rv = nil
      begin
        # shell output is:
        # ID,Name,Description,Enabled
        response = openstack(service, action, '--quiet', '--format', 'csv', properties)
        response = parse_csv(response)
        keys = response.delete_at(0)

        if response.collect.length > 1
          definition_file = properties[-1]
          Puppet.warning("#{definition_file} creates more than one workflow, only the first one will be returned after the request.")
        end
        rv = response.collect do |line|
          hash = {}
          keys.each_index do |index|
            key = keys[index].downcase.gsub(/ /, '_').to_sym
            hash[key] = line[index]
          end
          hash
        end
      rescue Puppet::ExecutionFailure => exception
        raise Puppet::Error::OpenstackUnauthorizedError, 'Could not authenticate' if exception.message =~ /HTTP 40[13]/
        raise
      end
    end
    return rv
  end

  private

  def self.parse_csv(text)
    # Ignore warnings - assume legitimate output starts with a double quoted
    # string.  Errors will be caught and raised prior to this
    text = text.split("\n").drop_while { |line| line !~ /^\".*\"/ }.join("\n")
    return CSV.parse(text + "\n")
  end
end