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::INIT_PARAMS, 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

declare_generic_options, #do_bulk_operation, #entity_action, #entity_command, #init_params, #instance_identifier, #query_option, #query_read_delete, #value_create_modify

Constructor Details

#initialize(**env) ⇒ Faspio

Returns a new instance of Faspio.



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

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.read('ping')
  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

Instance Method Details

#execute_actionObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aspera/cli/plugins/faspio.rb', line 39

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')[:data]
      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