Class: Aspera::Cli::Plugins::Faspio

Inherits:
BasicAuthPlugin show all
Defined in:
lib/aspera/cli/plugins/faspio.rb

Constant Summary collapse

ACTIONS =
%i[health bridges].freeze

Constants inherited from Aspera::Cli::Plugin

Aspera::Cli::Plugin::ALL_OPS, Aspera::Cli::Plugin::GLOBAL_OPS, Aspera::Cli::Plugin::INSTANCE_OPS, Aspera::Cli::Plugin::MAX_ITEMS, Aspera::Cli::Plugin::MAX_PAGES, Aspera::Cli::Plugin::REGEX_LOOKUP_ID_BY_FIELD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicAuthPlugin

#basic_auth_api, #basic_auth_params, declare_options

Methods inherited from Aspera::Cli::Plugin

#add_manual_header, #config, declare_generic_options, #do_bulk_operation, #entity_action, #entity_command, #formatter, #init_params, #instance_identifier, #options, #persistency, #query_read_delete, #transfer, #value_create_modify

Constructor Details

#initialize(**env) ⇒ Faspio

Returns a new instance of Faspio.



42
43
44
45
46
47
48
49
# File 'lib/aspera/cli/plugins/faspio.rb', line 42

def initialize(**env)
  super
  options.declare(:auth, 'OAuth type of authentication', values: %i[jwt basic])
  options.declare(:client_id, 'OAuth client identifier')
  options.declare(:private_key, 'OAuth JWT RSA private key PEM value (prefix file path with @file:)')
  options.declare(:passphrase, 'OAuth JWT RSA private key passphrase')
  options.parse_options!
end

Class Method Details

.application_nameObject



12
13
14
# File 'lib/aspera/cli/plugins/faspio.rb', line 12

def application_name
  'faspio Gateway'
end

.detect(base_url) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aspera/cli/plugins/faspio.rb', line 16

def detect(base_url)
  api = Rest.new(base_url: base_url)
  ping_result = api.call(operation: 'GET', subpath: 'ping', headers: {'Accept' => Rest::MIME_JSON})
  server_type = ping_result[:http]['Server']
  return nil unless ping_result[:data].is_a?(Hash) && ping_result[:data].empty?
  return nil unless server_type.is_a?(String) && server_type.include?('faspio')
  return {
    version: server_type.gsub(%r{^.*/}, ''),
    url:     base_url
  }
end

.wizard(object:) ⇒ Hash

Returns :preset_value, :test_args.

Parameters:

  • object (Plugin)

    An instance of this class

Returns:

  • (Hash)

    :preset_value, :test_args



30
31
32
33
34
35
36
37
38
# File 'lib/aspera/cli/plugins/faspio.rb', line 30

def wizard(object:)
  options = object.options
  return {
    preset_value: {
      url: options.get_option(:url, mandatory: true)
    },
    test_args:    'info'
  }
end

Instance Method Details

#execute_actionObject



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
92
93
# File 'lib/aspera/cli/plugins/faspio.rb', line 51

def execute_action
  base_url = options.get_option(:url, mandatory: true)
  api =
    case options.get_option(:auth, mandatory: true)
    when :basic
      basic_auth_api
    when :jwt
      app_client_id = options.get_option(:client_id, mandatory: true)
      Rest.new(
        base_url: base_url,
        auth:     {
          type:            :oauth2,
          grant_method:    :jwt,
          base_url:        "#{base_url}/auth",
          client_id:       app_client_id,
          use_query:       true,
          payload:         {
            iss: app_client_id, # issuer
            sub: app_client_id  # subject
          },
          private_key_obj: OpenSSL::PKey::RSA.new(options.get_option(:private_key, mandatory: true), options.get_option(:passphrase)),
          headers:         {typ: 'JWT'}
        })
    end
  command = options.get_next_command(ACTIONS)
  case command
  when :health
    nagios = Nagios.new
    begin
      result = api.read('ping')
      if result.is_a?(Hash) && result.empty?
        nagios.add_ok('api', 'answered ok')
      else
        nagios.add_critical('api', 'not expected answer')
      end
    rescue StandardError => e
      nagios.add_critical('api', e.to_s)
    end
    return nagios.result
  when :bridges
    return entity_action(api, 'bridges')
  end
end