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].concat(SAML_IMPORT_MANDATORY).freeze
ACTIONS =
%i[health files admin].freeze
USR_GRP_SETTINGS =

common to users and groups

%i[transfer_settings app_authorizations share_permissions].freeze

Constants inherited from Base

Base::ALL_OPS, Base::GLOBAL_OPS, Base::INSTANCE_OPS, Base::MAX_ITEMS, Base::MAX_PAGES

Instance Attribute Summary

Attributes inherited from Base

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicAuth

#basic_auth_api, #basic_auth_params, declare_options

Methods inherited from Base

#add_manual_header, #config, declare_options, #do_bulk_operation, #entity_execute, #formatter, #instance_identifier, #list_entities_limit_offset_total_count, #lookup_entity_by_field, #lookup_entity_generic, #options, percent_selector, #persistency, #query_read_delete, #transfer, #value_create_modify

Constructor Details

#initialize(**_) ⇒ Shares

Returns a new instance of Shares.



84
85
86
# File 'lib/aspera/cli/plugins/shares.rb', line 84

def initialize(**_)
  super
end

Class Method Details

.detect(address_or_url) ⇒ Hash, NilClass

Returns:



59
60
61
62
63
64
65
66
67
# File 'lib/aspera/cli/plugins/shares.rb', line 59

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



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

def health_check(url)
  result = {}
  result[:version] =
    begin
      version = nil
       = Rest
        .new(base_url: url, redirect_max: 2)
        .read('', headers: {'Accept'=>'text/html'})
      if (m = .match(/\(v([0-9a-f\.]+)\)/))
        version = m[1]
        if (m = .match(/Patch level ([0-9]+)/))
          version = "#{result[:version]} #{m[0]}"
        end
      end
      raise 'no version' if version.nil?
      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
      resp = Rest.new(base_url: url, redirect_max: 1).read("#{NODE_API_PATH}/app", exception: false, ret: :resp)
      # shall fail: shares requires auth, but we check error message
      raise 'not found' unless resp.code.to_s.eql?('401') && resp.body.eql?('{"error":{"user_message":"API user authentication failed"}}')
      'available'
    rescue => e
      e
    end
  result
end

Instance Method Details

#execute_actionObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/aspera/cli/plugins/shares.rb', line 95

def execute_action
  command = options.get_next_command(ACTIONS, aliases: {repository: :files})
  case command
  when :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
    Main.result_object_list(nagios.status_list)
  when :files
    api_shares_node = basic_auth_api(NODE_API_PATH)
    repo_command = options.get_next_command(Node::COMMANDS_SHARES)
    return Node
        .new(context: context, api: api_shares_node)
        .execute_action(repo_command)
  when :admin
    api_shares_admin = basic_auth_api(ADMIN_API_PATH)
    admin_command = options.get_next_command(%i[node share transfer_settings user group].freeze)
    lookup_share = ->(field, value){lookup_entity_generic(entity: 'share', field: field, value: value){api_shares_admin.read('data/shares')}['id']}
    case admin_command
    when :node
      return entity_execute(api: api_shares_admin, entity: 'data/nodes')
    when :share
      share_command = options.get_next_command(%i[user_permissions group_permissions].concat(ALL_OPS))
      case share_command
      when *ALL_OPS
        return entity_execute(
          api:            api_shares_admin,
          entity:         'data/shares',
          command:        share_command,
          display_fields: %w[id name node_id directory percent_free],
          &lookup_share
        )
      when :user_permissions, :group_permissions
        share_id = instance_identifier(&lookup_share)
        return entity_execute(api: api_shares_admin, entity: "data/shares/#{share_id}/#{share_command}")
      end
    when :transfer_settings
      xfer_settings_command = options.get_next_command(%i[show modify])
      return entity_execute(
        api: api_shares_admin,
        entity: 'data/transfer_settings',
        command: xfer_settings_command,
        is_singleton: true
      )
    when :user, :group
      entity_type = admin_command
      entities_location = options.get_next_command(%i[all local ldap saml])
      entities_prefix = entities_location.eql?(:all) ? '' : "#{entities_location}_"
      entities_path = "data/#{entities_prefix}#{entity_type}s"
      entity_commands = nil
      case entities_location
      when :all
        entity_commands = %i[list show delete]
        entity_commands.concat(USR_GRP_SETTINGS)
        entity_commands.push(:users) if entity_type.eql?(:group)
        entity_commands.freeze
      when :local
        entity_commands = %i[list show delete create modify]
        entity_commands.push(:users) if entity_type.eql?(:group)
        entity_commands.freeze
      when :ldap
        entity_commands = %i[add].freeze
      when :saml
        entity_commands = %i[import].freeze
      end
      entity_verb = options.get_next_command(entity_commands)
      lookup_block = ->(field, value){lookup_entity_generic(entity: entity_type, field: field, value: value){api_shares_admin.read(entities_path)}['id']}
      case entity_verb
      when *ALL_OPS # list, show, delete, create, modify
        display_fields = entity_type.eql?(:user) ? %w[id user_id username first_name last_name email] : nil
        display_fields.push(:directory_user) if entity_type.eql?(:user) && entities_location.eql?(:all)
        return entity_execute(
          api:            api_shares_admin,
          entity:         entities_path,
          command:        entity_verb,
          display_fields: display_fields,
          &lookup_block
        )
      when *USR_GRP_SETTINGS # transfer_settings, app_authorizations, share_permissions
        group_id = instance_identifier(&lookup_block)
        entities_path = "#{entities_path}/#{group_id}/#{entity_verb}"
        return entity_execute(api: api_shares_admin, entity: entities_path, is_singleton: !entity_verb.eql?(:share_permissions), &lookup_share)
      when :import # saml
        return do_bulk_operation(command: entity_verb, descr: 'user information') do |entity_parameters|
          entity_parameters = entity_parameters.transform_keys{ |k| k.gsub(/\s+/, '_').downcase}
          Aspera.assert_type(entity_parameters, Hash)
          SAML_IMPORT_MANDATORY.each{ |p| raise "missing mandatory field: #{p}" if entity_parameters[p].nil?}
          entity_parameters.each_key do |p|
            raise "unsupported field: #{p}, use: #{SAML_IMPORT_ALLOWED.join(',')}" unless SAML_IMPORT_ALLOWED.include?(p)
          end
          api_shares_admin.create("#{entities_path}/import", entity_parameters)
        end
      when :add # ldap
        return do_bulk_operation(command: entity_verb, descr: "#{entity_type} name", values: String) do |entity_name|
          api_shares_admin.create(entities_path, {entity_type=>entity_name})
        end
      when :users # group
        return entity_execute(api: api_shares_admin, entity: "#{entities_path}/#{instance_identifier(&lookup_block)}/#{entities_prefix}users")
      else Aspera.error_unexpected_value(entity_verb)
      end
    end
  end
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



73
74
75
76
77
78
79
80
81
82
# File 'lib/aspera/cli/plugins/shares.rb', line 73

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