Class: Aspera::Cli::Plugins::Shares

Inherits:
BasicAuth show all
Defined in:
lib/aspera/cli/plugins/shares.rb

Overview

Plugin for Aspera Shares v1

Constant Summary collapse

NODE_API_PATH =

path for node API after base url

'node_api'
ADMIN_API_PATH =

path for node admin after base url

'api/v1'
SAML_IMPORT_MANDATORY =
%w[id name_id].freeze
SAML_IMPORT_ALLOWED =
(%w[email given_name surname] + SAML_IMPORT_MANDATORY).freeze
USR_GRP_SETTINGS =

common to users and groups

%i[transfer_settings app_authorizations share_permissions].freeze
SHARE_PERMISSIONS_OPS =

share_permissions is read-only (Rails only exposes index+show)

%i[list show].freeze
GROUP_USERS_OPS =

group users: Rails only exposes index+show+update+destroy (no create)

(Operations::ALL - %i[create]).freeze

Constants inherited from Base

Base::FILTER_ARGS

Instance Attribute Summary

Attributes inherited from Base

#context, #help_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicAuth

#basic_auth_api, #basic_auth_params

Methods inherited from Base

#action_for, #add_manual_header, application_name, #bulk_result, command, command_registry, commands_under, #config, crud_commands, declare_options, define_action_method, #dispatch_child, #dispatch_from_registry, #dispatch_leaf, #entity_create, #entity_delete, entity_display_name, #entity_list, #entity_modify, #entity_res_path, #entity_show, #execute_action, #execute_leaf, file_matcher, #formatter, #generate_help, #http_config, #invoke_action, option, #options, #persistency, #presets, #progress_bar, #query_read_delete, #resolve_argument, root_setup, #transfer, use_options, used_option_sources

Constructor Details

#initialize(**_) ⇒ Shares

Returns a new instance of Shares.



92
93
94
95
# File 'lib/aspera/cli/plugins/shares.rb', line 92

def initialize(**_)
  super
  @api_shares_admin = nil
end

Class Method Details

.detect(address_or_url) ⇒ Hash, NilClass

Returns:



67
68
69
70
71
72
73
74
75
# File 'lib/aspera/cli/plugins/shares.rb', line 67

def detect(address_or_url)
  address_or_url = "https://#{address_or_url}" unless address_or_url.match?(%r{^[a-z]{1,6}://})
  health = health_check(address_or_url)
  return unless health[:api].is_a?(String)
  return {
    version: health[:version].is_a?(String) ? health[:version] : 'unknown',
    url:     address_or_url
  }
end

.health_check(url) ⇒ Hash

Check various endpoints on Shares

Returns:

  • (Hash)

    with version, ping, api



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
# File 'lib/aspera/cli/plugins/shares.rb', line 20

def health_check(url)
  result = {}
  # Get version from main page
  result[:version] =
    begin
      version = nil
       = Rest
        .new(base_url: url, redirect_max: 2)
        .read('', headers: {'Accept'=>'text/html'})
      Aspera.assert(.include?('aspera-Shares')) { 'not Shares' }
      if (m = .match(/\(v([0-9a-f\.]+)\)/))
        version = m[1]
        if (m = .match(/Patch level ([0-9]+)/))
          version = "#{version} #{m[0]}"
        end
      end
      version.nil? ? 'no version' : version
    rescue => e
      e
    end
  result[:ping] =
    begin
      Rest
        .new(base_url: "#{url}/#{NODE_API_PATH}")
        .read('ping', headers: {'Content-Type'=>'application/json'})
      'ping ok'
    rescue => e
      e
    end
  result[:api] =
    begin
      data, resp = Rest
        .new(base_url: "#{url}/#{NODE_API_PATH}", redirect_max: 1)
        .read('info', exception: false, ret: :both)
      # shall fail: shares requires auth, but we check error message
      if resp.code.to_s.eql?('401') && data&.dig('error', 'user_message')&.include?('authentication failed')
        'available'
      else
        raise "not found (#{resp.code})"
      end
    rescue => e
      e
    end
  result
end

Instance Method Details

#action_healthObject

--- health ---



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/aspera/cli/plugins/shares.rb', line 283

def action_health
  nagios = Nagios.new
  shares_url = options.get_option(:url, mandatory: true)
  health = self.class.health_check(shares_url)
  nagios.add_ok('version', health[:version]) if health[:version].is_a?(String)
  if health[:ping].is_a?(String)
    nagios.add_ok('ping', health[:ping])
  else
    nagios.add_critical('ping', health[:ping].to_s)
  end
  if health[:api].is_a?(String)
    nagios.add_ok('API', health[:api])
  else
    nagios.add_critical('API', health[:api].to_s)
  end
  Result::ObjectList.new(nagios.status_list)
end

#lookup_share_id(field, value) ⇒ Object

Lookup a share id by field/value using the admin API.



277
278
279
# File 'lib/aspera/cli/plugins/shares.rb', line 277

def lookup_share_id(field, value, **)
  RestList.lookup_entity_generic(entity: 'share', field: field, value: value) { @api_shares_admin.read('data/shares') }['id']
end

#setup_adminHash

Build the admin REST API and store in ivar.

Returns:

  • (Hash)

    empty ctx (state stored in @api_shares_admin)



271
272
273
274
# File 'lib/aspera/cli/plugins/shares.rb', line 271

def setup_admin(**)
  @api_shares_admin = basic_auth_api(ADMIN_API_PATH)
  {}
end

#setup_shares_nodeHash

Build the Shares node API plugin and inject into ctx.

Returns:

  • (Hash)

    context hash containing :shares_node_plugin



264
265
266
267
# File 'lib/aspera/cli/plugins/shares.rb', line 264

def setup_shares_node(**)
  api_shares_node = basic_auth_api(NODE_API_PATH)
  {shares_node_plugin: Node.new(context: context, api: api_shares_node)}
end

#wizard(wizard, app_url) ⇒ Hash

Returns :preset_value, :test_args.

Parameters:

  • wizard (Wizard)

    The wizard object

  • app_url (String)

    Tested URL

Returns:

  • (Hash)

    :preset_value, :test_args



81
82
83
84
85
86
87
88
89
90
# File 'lib/aspera/cli/plugins/shares.rb', line 81

def wizard(wizard, app_url)
  return {
    preset_value: {
      url:      app_url,
      username: options.get_option(:username, mandatory: true),
      password: options.get_option(:password, mandatory: true)
    },
    test_args:    'files browse /'
  }
end