Class: Aspera::Plugins::Faspex5::Commands

Inherits:
Cli::Plugins::Base show all
Includes:
Cli::Plugins::Oauth
Defined in:
lib/aspera/plugins/faspex5/commands.rb

Constant Summary collapse

API_SCHEMA =

Key of the Faspex 5 API in the schemas of the bundle (manifest)

'faspex'
POSTPROC_OPTIONS =

Options of the Faspex 4 post-processing servlet, in the schemas of the bundle

'faspex5_opts:components.schemas.Faspex4PostProcOptions'
QUERY_SCHEMA_COMMANDS =

Commands that may carry a query_schema annotation

%i[list delete].freeze
WORKGROUP_TYPES =
%w{workgroup shared_inbox}.freeze

Constants included from Cli::Plugins::Oauth

Cli::Plugins::Oauth::AUTH_OPTIONS, Cli::Plugins::Oauth::AUTH_TYPES

Constants inherited from Cli::Plugins::Base

Cli::Plugins::Base::FILTER_ARGS

Instance Attribute Summary

Attributes inherited from Cli::Plugins::Base

#context, #help_path

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cli::Plugins::Oauth

included, kwargs_from_options

Methods included from Cli::Option::Declarator

#declare_options, #option, #option_specs, #register_option_spec

Methods included from Cli::Plugins::BasicAuth

#basic_auth_api, #basic_auth_params, included

Methods inherited from Cli::Plugins::Base

#action_for, application_name, #bulk_result, #check_argument_access, command, command_registry, commands_under, #config, #crud_api, crud_commands, declare_options, define_action_method, #dispatch_child, #dispatch_from_registry, #dispatch_leaf, #dispatch_mount, #entity_create, #entity_delete, #entity_list, #entity_modify, entity_noun, #entity_res_path, #entity_show, #execute_action, #execute_leaf, file_matcher, #formatter, #generate_help, #http_config, include_mounts, #initialize, #invoke_action, operation_description, option_sources, option_specs, #options, #persistency, #poll_result, #presets, #progress_bar, #query_read_delete, register_option_spec, #resolve_argument, #resolve_arguments, #transfer, use_options, used_option_sources, #validate_registry

Constructor Details

This class inherits a constructor from Aspera::Cli::Plugins::Base

Class Method Details

.detect(address_or_url) ⇒ Hash, NilClass

Returns:



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
# File 'lib/aspera/plugins/faspex5/commands.rb', line 46

def detect(address_or_url)
  # add scheme if missing
  address_or_url = "https://#{address_or_url}" unless address_or_url.match?(%r{^[a-z]{1,6}://})
  urls = [address_or_url]
  urls.push("#{address_or_url}#{Api::PATH_STANDARD_ROOT}") unless address_or_url.end_with?(Api::PATH_STANDARD_ROOT)
  error = nil
  urls.each do |base_url|
    # Faspex is always HTTPS
    next unless base_url.start_with?('https://')
    api = Rest::Client.new(base_url: base_url, redirect_max: 1)
    response = api.read(Api::PATH_API_DETECT, ret: :resp)
    next unless response.code.start_with?('2') && response.body.strip.empty?
    # end is at -1, and subtract 1 for "/"
    url_length = -2 - Api::PATH_API_DETECT.length
    # take redirect if any
    return {
      version: response[Api::HEADER_FASPEX_VERSION] || '5',
      url:     response.uri.to_s[0..url_length]
    }
  rescue StandardError => e
    error = e
    Log.log.debug { "detect error: #{e}" }
  end
  raise error if error
  return
end

Instance Method Details

#action_admin_accounts_reset_password(account_id:) ⇒ Object

admin > accounts > reset_password



795
796
797
798
# File 'lib/aspera/plugins/faspex5/commands.rb', line 795

def action_admin_accounts_reset_password(account_id:, **)
  api_v5.create("accounts/#{}/reset_password", {})
  Cli::Result::Status.new('password reset, user shall check email')
end

#action_admin_file_processing_next ⇒ Object

admin > file_processing > next



801
802
803
804
805
# File 'lib/aspera/plugins/faspex5/commands.rb', line 801

def action_admin_file_processing_next(**)
  args = res_exec_args(:file_processing)
  result, count = api_v5.list_entities_limit_offset_total_count(entity: args[:entity], operation: 'POST', items_key: 'files')
  Cli::Result::ObjectList.new(result, total: count)
end

#action_admin_nodes_browse(folder_path:, node_id:) ⇒ Object

admin > nodes > browse



808
809
810
# File 'lib/aspera/plugins/faspex5/commands.rb', line 808

def action_admin_nodes_browse(folder_path:, node_id:, **)
  browse_folder("nodes/#{node_id}/browse", {}, folder_path: folder_path)
end

#action_admin_smtp_test(recipient:) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
# File 'lib/aspera/plugins/faspex5/commands.rb', line 782

def action_admin_smtp_test(recipient:, **)
  recipient = {test_email_recipient: recipient} if recipient.is_a?(String)
  creation = api_v5.create('configuration/smtp/test', recipient)
  result = wait_for_job(creation['job_id'])
  begin
    result['serialized_args'] = JSON.parse(result['serialized_args'])
  rescue JSON::ParserError
    # keep as string if not valid JSON
  end
  Cli::Result::SingleObject.new(result)
end

#action_gateway(parameters: {}) ⇒ Object



992
993
994
995
996
997
998
999
1000
1001
# File 'lib/aspera/plugins/faspex5/commands.rb', line 992

def action_gateway(parameters: {}, **)
  # Loaded on use: web server
  require_relative '../faspex4/gw_servlet'
  Faspex4::GWServlet.serve(parameters.symbolize_keys, type: Cli::BadArgument) do
    # evaluated before the server listens: invalid options fail at startup, and no lazy memo is shared by request threads
    api_v5
    ->(faspex_pkg_parameters) { faspex4_send_to_faspex5(faspex_pkg_parameters) }
  end
  Cli::Result::Status.new('Gateway terminated')
end

#action_health ⇒ Object

--- handlers ---



952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'lib/aspera/plugins/faspex5/commands.rb', line 952

def action_health(**)
  nagios = Nagios.new
  begin
    data, http = Rest::Client.new(base_url: options.get_option(:url, mandatory: true))
      .read('health', ret: :both)
    data.each do |k, v|
      nagios.add_ok(k, v.to_s)
    end
    nagios.add_ok('version', http['X-IBM-Aspera']) if http['X-IBM-Aspera']
  rescue StandardError => e
    nagios.add_critical('core', e.to_s)
  end
  Cli::Result::ObjectList.new(nagios.status_list)
end

#action_invitations_create(invitation:) ⇒ Object

invitations sub-handlers



985
986
987
988
989
990
# File 'lib/aspera/plugins/faspex5/commands.rb', line 985

def action_invitations_create(invitation:, **)
  bulk_result(invitation, command: :create) do |params|
    endpoint = params.key?('recipient_name') ? 'public_invitations' : 'invitations'
    api_v5.create(endpoint, params)
  end
end

#action_packages_delete(package_id:) ⇒ Object



936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/aspera/plugins/faspex5/commands.rb', line 936

def action_packages_delete(package_id:, **)
  ids = package_id.is_a?(Array) ? package_id : [package_id]
  Aspera.assert_array_all(ids, String, type: Cli::BadArgument) { 'Package id(s)' }
  # API returns 204, empty on success
  api_v5.call(
    operation:    'DELETE',
    subpath:      'packages',
    content_type: Mime::JSON,
    body:         {ids: ids},
    headers:      {'Accept' => Mime::JSON}
  )
  Cli::Result::Status.new('Package(s) deleted')
end

#action_packages_list(filter: nil) ⇒ Object



927
928
929
930
931
932
933
934
# File 'lib/aspera/plugins/faspex5/commands.rb', line 927

def action_packages_list(filter: nil, **)
  list, max_items, total = list_packages_with_filter(filter: filter)
  list = list[0, max_items] if max_items
  fields = %w[id title status sender.name recipients.0.name release_date total_bytes total_files]
  fields.delete('recipients.0.name') if %w[inbox inbox_history].include?(options.get_option(:box))
  fields.delete('sender.name') if %w[outbox outbox_history].include?(options.get_option(:box))
  Cli::Result::ObjectList.new(list, total: total, fields: fields)
end

#action_postprocessing(parameters: {}) ⇒ Object



1028
1029
1030
1031
1032
# File 'lib/aspera/plugins/faspex5/commands.rb', line 1028

def action_postprocessing(parameters: {}, **)
  require_relative 'faspex4_post_proc_servlet'
  Faspex4PostProcServlet.serve(parameters.symbolize_keys, type: Cli::BadArgument)
  Cli::Result::Status.new('Post-processing terminated')
end

#action_shared_folders_browse(folder_path:, shared_folder_id:) ⇒ Object

Raises:



976
977
978
979
980
981
# File 'lib/aspera/plugins/faspex5/commands.rb', line 976

def action_shared_folders_browse(folder_path:, shared_folder_id:, **)
  all_shared_folders = api_v5.read('shared_folders')['shared_folders']
  node = all_shared_folders.find { |i| i['id'].eql?(shared_folder_id) }
  raise Cli::BadIdentifier.new('Shared folder', shared_folder_id) if node.nil?
  browse_folder("nodes/#{node['node_id']}/shared_folders/#{shared_folder_id}/browse", {}, folder_path: folder_path)
end

#api_v5 ⇒ Api

Faspex 5 API, built from CLI options on first use.

Returns:



911
912
913
914
915
916
917
# File 'lib/aspera/plugins/faspex5/commands.rb', line 911

def api_v5
  return @api_v5 if @api_v5
  @api_v5 = Api.new(**Cli::Plugins::Oauth.kwargs_from_options(options))
  # in case user wants to use HTTPGW tell transfer agent how to get address
  transfer.httpgw_url_cb = lambda { @api_v5.read('account')['gateway_url'] }
  @api_v5
end

#browse_folder(browse_endpoint, base_query = {}, folder_path: '/') ⇒ Object

Browse a folder

Parameters:

  • browse_endpoint (String) —

    the endpoint to browse

  • folder_path (String) (defaults to: '/') —

    starting path (default: '/')



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/aspera/plugins/faspex5/commands.rb', line 363

def browse_folder(browse_endpoint, base_query = {}, folder_path: '/')
  folders_to_process = [folder_path]
  query = base_query.merge(query_read_delete(default: {}))
  filters = query.delete('filters') { {} }
  Aspera.assert_type(filters, Hash, type: Cli::BadArgument) { 'filters' }
  filters['basenames'] ||= []
  Aspera.assert_type(filters, Hash, type: Cli::BadArgument) { 'filters' }
  max_items = query.delete(Rest::List::MAX_ITEMS)
  recursive = query.delete('recursive')
  use_paging = query.delete('paging') { true }
  if use_paging
    browse_endpoint = "#{browse_endpoint}/page"
    query['per_page'] ||= 500
  else
    query['offset'] ||= 0
    query['limit'] ||= 500
  end
  all_items = []
  total_count = nil
  until folders_to_process.empty?
    path = folders_to_process.shift
    loop do
      data, http = api_v5.call(
        operation:    'POST',
        subpath:      browse_endpoint,
        query:        query,
        content_type: Mime::JSON,
        body:         {'path' => path, 'filters' => filters},
        headers:      {'Accept' => Mime::JSON},
        ret:          :both
      )
      all_items.concat(data['items'])
      if !max_items.nil? && (all_items.count >= max_items)
        all_items = all_items.slice(0, max_items) if all_items.count > max_items
        break
      end
      folders_to_process.concat(data['items'].select { |i| i['type'].eql?('directory') }.map { |i| i['path'] }) if recursive
      if use_paging
        iteration_token = http[Api::HEADER_X_NEXT_ITER_TOKEN]
        break if iteration_token.nil? || iteration_token.empty?
        query['iteration_token'] = iteration_token
      else
        total_count = data['total_count'] if total_count.nil?
        break if data['item_count'].eql?(0)
        query['offset'] += data['item_count']
      end
      Rest::Parameters.instance.spinner_cb.call(all_items.count)
    end
    query.delete('iteration_token')
  end
  Rest::Parameters.instance.spinner_cb.call(action: :success)
  return Cli::Result::ObjectList.new(all_items, total: total_count)
end

#list_packages_with_filter(filter: nil, query: {}) ⇒ Array(Array, Integer|nil, Integer|nil)

List all packages with optional filter. The special max key is extracted before the API call and returned separately, so callers that apply a post-API filter (e.g. once_only) can enforce the limit after filtering rather than before.

Parameters:

  • filter (Proc, nil) (defaults to: nil) —

    optional filter lambda applied to each package entry

  • query (Hash) (defaults to: {}) —

    additional query parameters forwarded to the API

Returns:

  • (Array(Array, Integer|nil, Integer|nil)) —

    [filtered list, max (or nil), total count]



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/aspera/plugins/faspex5/commands.rb', line 188

def list_packages_with_filter(filter: nil, query: {})
  filter ||= ->(_x) { true }
  box = options.get_option(:box)
  # Translate box name to API prefix (with ending slash)
  entity =
    case box
    when Cli::SpecialValues::ALL then 'packages' # only admin can list all packages globally
    when *Api::API_LIST_MAILBOX_TYPES then "#{box}/packages"
    else
      group_type = options.get_option(:group_type)
      "#{group_type}/#{api_v5.lookup_entity_by_field(entity: group_type, value: box)['id']}/packages"
    end
  # Merge default query with user-provided query: user values take precedence, but defaults are preserved
  user_query = query_read_delete(schema: Schema::Registry.query_params(API_SCHEMA, 'packages'))
  merged_query = user_query.nil? ? query.dup : query.merge(user_query)
  # Extract `max` before the API call so callers can apply it after post-API filtering
  max_items = merged_query.delete(Rest::List::MAX_ITEMS)&.to_i
  list, total = api_v5.list_entities_limit_offset_total_count(entity: entity, query: merged_query)
  return list.select(&filter), max_items, total
end

#lookup_node_id(field, value) ⇒ Object

admin > nodes — lookup node id by field/value



489
490
491
# File 'lib/aspera/plugins/faspex5/commands.rb', line 489

def lookup_node_id(field, value, **)
  api_v5.lookup_entity_by_field(entity: 'nodes', field: field, value: value)['id']
end

#lookup_sf_id(field, value, sf_entity:) ⇒ Object

Lookup shared folder id by field/value within a node's shared_folders entity. sf_entity is in ctx from setup_admin_nodes_shared_folders (Phase A of parent).



819
820
821
# File 'lib/aspera/plugins/faspex5/commands.rb', line 819

def lookup_sf_id(field, value, sf_entity:, **)
  api_v5.lookup_entity_by_field(entity: sf_entity, items_key: 'shared_folders', field: field, value: value)['id']
end

#lookup_sf_user_id(field, value, user_path:) ⇒ Object

Lookup custom access user id by field/value within a shared folder's users entity. user_path is in ctx from setup_admin_nodes_shared_folders_user (Phase A of parent).



825
826
827
# File 'lib/aspera/plugins/faspex5/commands.rb', line 825

def lookup_sf_user_id(field, value, user_path:, **)
  api_v5.lookup_entity_by_field(entity: user_path, items_key: 'users', field: field, value: value)['id']
end

#lookup_shared_folder_id(field, value) ⇒ Object

Lookup a shared folder id by field/value. Called via lookup: :lookup_shared_folder_id on the shared_folders > browse command.

Raises:



969
970
971
972
973
974
# File 'lib/aspera/plugins/faspex5/commands.rb', line 969

def lookup_shared_folder_id(field, value, **)
  all = api_v5.read('shared_folders')['shared_folders']
  matches = all.select { |i| i[field].eql?(value) }
  raise Cli::BadIdentifier.new('Shared folder', value, field: field, count: matches.length) unless matches.length.eql?(1)
  matches.first['id']
end

#normalize_recipients(parameters, type) ⇒ Object

if recipient is just an email, then convert to expected API hash : name and type



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/aspera/plugins/faspex5/commands.rb', line 118

def normalize_recipients(parameters, type)
  type = type.to_s
  return unless parameters.key?(type)
  Aspera.assert_type(parameters[type], Array, type: Cli::BadArgument) { type }
  recipient_types = Api::RECIPIENT_TYPES
  if parameters.key?('recipient_types')
    recipient_types = Array(parameters.delete('recipient_types'))
    Aspera.assert_array_all(recipient_types, String) { 'recipient_types' }
  end
  parameters[type].map! do |recipient_data|
    # If just a string, make a general lookup and build expected name/type hash
    if recipient_data.is_a?(String)
      matched = api_v5.lookup_with_q('contacts', value: recipient_data, query: Rest.php_style({context: 'packages', type: recipient_types}))
      recipient_data = {
        name:           matched['name'],
        recipient_type: matched['type']
      }
    end
    # result for mapping
    recipient_data
  end
end

#package_receive(package_ids) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/aspera/plugins/faspex5/commands.rb', line 231

def package_receive(package_ids)
  # prepare persistency if needed
  skip_ids_persistency = nil
  if options.get_option(:once_only, mandatory: true)
    # read ids from persistency
    skip_ids_persistency = PersistencyActionOnce.new(
      manager: persistency,
      data:    [],
      id:      IdGenerator.from_list(
        'faspex_recv',
        options.get_option(:url, mandatory: true),
        persistency_user_id,
        options.get_option(:box, mandatory: true)
      )
    )
  end
  packages = []
  case package_ids
  when Cli::SpecialValues::INIT
    Aspera.assert(skip_ids_persistency, 'Only with option once_only', type: Cli::BadArgument)
    skip_ids_persistency.data.clear.concat(list_packages_with_filter.first.map { |p| p['id'] }) # no filter: all packages, max ignored
    skip_ids_persistency.save
    return Cli::Result::Status.new("Initialized skip for #{skip_ids_persistency.data.count} package(s)")
  when Cli::SpecialValues::ALL
    # TODO: if packages have same name, they will overwrite ?
    packages, max_items = list_packages_with_filter(query: {'status' => 'completed'}) # no filter: all completed packages
    Log.dump(:package_ids, level: :trace1) { packages.map { |p| p['id'] } }
    Log.dump(:skip_ids, skip_ids_persistency&.data, level: :trace1)
    packages.reject! { |p| skip_ids_persistency.data.include?(p['id']) } if skip_ids_persistency
    # Apply `max` after once_only filtering so we get the N first not-yet-downloaded packages
    packages = packages[0, max_items] if max_items
    Log.dump(:package_ids, level: :trace1) { packages.map { |p| p['id'] } }
  else
    # a single id was provided, or a list of ids
    package_ids = Array(package_ids)
    Aspera.assert_array_all(package_ids, String, type: Cli::BadArgument) { 'Package id(s)' }
    # packages = package_ids.map{|pkg_id|api_v5.read("packages/#{pkg_id}")}
    packages = package_ids.map { |pkg_id| {'id'=>pkg_id} }
    # The package of a public link is received only once, like with `ALL`
    packages.reject! { |p| skip_ids_persistency.data.include?(p['id']) } if skip_ids_persistency && api_v5.pub_link_context
  end
  result_transfer = []
  param_file_list = {}
  begin
    param_file_list['paths'] = transfer.ts_source_paths.map { |p| {'path' => p['source']} }
  rescue Cli::MissingArgument
    # paths is optional
  end
  box = options.get_option(:box)
  download_params = {
    type:          Api.box_type(box),
    transfer_type: Api::TRANSFER_CONNECT
  }
  # download_params[:recipient_workgroup_id] = api_v5.lookup_entity_by_field(entity: options.get_option(:group_type), value: box)['id'] if !Api::API_LIST_MAILBOX_TYPES.include?(box) && box != Cli::SpecialValues::ALL
  packages.each do |package|
    pkg_id = package['id']
    formatter.display_status("Receiving package #{pkg_id}")
    # TODO: allow from sent as well ?
    transfer_spec = api_v5.call(
      operation:    'POST',
      subpath:      "packages/#{pkg_id}/transfer_spec/download",
      query:        download_params.merge(recipient_query(pkg_id)),
      content_type: Mime::JSON,
      body:         param_file_list,
      headers:      {'Accept' => Mime::JSON}
    )
    # delete flag for Connect Client
    transfer_spec.delete('authentication')
    statuses = transfer.start(transfer_spec)
    result_transfer.push({'package' => pkg_id, Xfer::TransferAgent::STATUS_FIELD => statuses})
    # skip only if all sessions completed
    if statuses.is_a?(Aspera::Transfer::Result::Success) && skip_ids_persistency
      skip_ids_persistency.data.push(pkg_id)
      skip_ids_persistency.save
    end
  end
  return Xfer::TransferAgent.result_transfer_multiple(result_transfer)
end

#package_send(parameters) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/aspera/plugins/faspex5/commands.rb', line 310

def package_send(parameters)
  # The package is created before the transfer
  transfer.assert_no_dry_run
  # autofill recipient for public url
  if api_v5.pub_link_context&.key?('recipient_type') && !parameters.key?('recipients')
    parameters['recipients'] = [{
      name:           api_v5.pub_link_context['name'],
      recipient_type: api_v5.pub_link_context['recipient_type']
    }]
  end
  PACKAGE_RECIPIENT_TYPES.each { |type| normalize_recipients(parameters, type) }
  # User specified content prot in tspec, but faspex requires in package creation
  # `transfer_spec/upload` will set `content_protection`
  if transfer.user_transfer_spec['content_protection'] && !parameters.key?('ear_enabled')
    transfer.user_transfer_spec.delete('content_protection')
    parameters['ear_enabled'] = true
  end
  package = api_v5.create('packages', parameters)
  shared_folder = options.get_option(:shared_folder)
  if shared_folder.nil?
    # send from local files
    transfer_spec = api_v5.create(
      "packages/#{package['id']}/transfer_spec/upload",
      {paths: transfer.source_list},
      query: {transfer_type: Api::TRANSFER_CONNECT}
    )
    # well, we asked a TS for connect, but we actually want a generic one
    transfer_spec.delete('authentication')
    return Xfer::TransferAgent.result_object_transfer(package, transfer.start(transfer_spec))
  else
    # send from remote shared folder
    if (m = Cli::Parser.percent_selector(shared_folder))
      shared_folder = api_v5.lookup_entity_by_field(
        entity: 'shared_folders',
        field: m[:field],
        value: m[:value]
      )['id']
    end
    transfer_request = {shared_folder_id: shared_folder, paths: transfer.source_list}
    # start remote transfer and get first status
    result = api_v5.create("packages/#{package['id']}/remote_transfer", transfer_request)
    result['id'] = package['id']
    unless result['status'].eql?('completed')
      formatter.display_status("Package #{package['id']}")
      result = wait_package_status(package['id'])
    end
    return Cli::Result::SingleObject.new(result)
  end
end

#persistency_user_id ⇒ String

Returns user part of the persistency id of received packages: the username, or for a public link (no username), a digest of the link context (unique per link).

Returns:

  • (String) —

    user part of the persistency id of received packages: the username, or for a public link (no username), a digest of the link context (unique per link)



226
227
228
229
# File 'lib/aspera/plugins/faspex5/commands.rb', line 226

def persistency_user_id
  return options.get_option(:username, mandatory: true) if api_v5.pub_link_context.nil?
  Digest::SHA256.hexdigest(JSON.generate(api_v5.pub_link_context))[0, 16]
end

#recipient_query(package_id) ⇒ Object

Build query to get package recipients based on package info in case of shared inbox or workgroup recipient

Parameters:

  • package_id (String) —

    the package id to get info from



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/aspera/plugins/faspex5/commands.rb', line 211

def recipient_query(package_id)
  # With a public link, the recipient context is already encoded in the auth token: no extra query params needed
  return {} if api_v5.pub_link_context
  package_info = api_v5.read("packages/#{package_id}")
  base_query = {}
  first_recipient = package_info['recipients']&.first
  if first_recipient
    base_query['recipient_workgroup_id'] = first_recipient['id'] if WORKGROUP_TYPES.include?(first_recipient['recipient_type'])
    base_query['recipient_user_id'] = first_recipient['id'] if first_recipient['recipient_type'].eql?('user')
  end
  base_query
end

#res_exec_args(res_sym) ⇒ Object

Build args hash from RESOURCE_CONFIG for use with entity_list/show/create/modify/delete.



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/aspera/plugins/faspex5/commands.rb', line 472

def res_exec_args(res_sym)
  cfg = RESOURCE_CONFIG.fetch(res_sym, {})
  {
    api:             resource_config_value(cfg, :api) || api_v5,
    entity:          resource_config_value(cfg, :entity) || res_sym.to_s,
    items_key:       resource_config_value(cfg, :items_key),
    delete_style:    resource_config_value(cfg, :delete_style),
    id_as_arg:       resource_config_value(cfg, :id_as_arg) || false,
    display_fields:  resource_config_value(cfg, :display_fields),
    list_query:      resource_config_value(cfg, :list_query),
    is_singleton:    resource_config_value(cfg, :is_singleton) || false,
    query_component: resource_config_value(cfg, :query_component),
    body_component:  resource_config_value(cfg, :body_component)
  }.compact
end

#res_lookup_id(res_sym, field, value) ⇒ Object

Lookup id for a RESOURCE_CONFIG resource by field/value.



494
495
496
497
498
499
500
# File 'lib/aspera/plugins/faspex5/commands.rb', line 494

def res_lookup_id(res_sym, field, value)
  cfg = RESOURCE_CONFIG.fetch(res_sym, {})
  entity      = resource_config_value(cfg, :entity) || res_sym.to_s
  items_key   = resource_config_value(cfg, :items_key)
  res_id_query = resource_config_value(cfg, :res_id_query) || :default
  api_v5.lookup_entity_by_field(entity: entity, value: value, field: field, items_key: items_key, query: res_id_query)['id']
end

#resolve_member_user_ids(users) ⇒ Array<String>

Resolve user ids for shared_inbox/workgroup members/create, handling percent selectors.

Parameters:

  • users (Array) —

    raw user ids or percent-selector strings

Returns:

  • (Array<String>) —

    resolved user ids



848
849
850
851
852
853
854
855
856
857
858
# File 'lib/aspera/plugins/faspex5/commands.rb', line 848

def resolve_member_user_ids(users)
  users = Array(users)
  Aspera.assert_array_all(users, String, type: Cli::BadArgument) { 'users' }
  users.map do |user|
    if (m = Cli::Parser.percent_selector(user))
      api_v5.lookup_entity_by_field(entity: 'accounts', field: m[:field], value: m[:value], query: Rest.php_style({type: ACCOUNT_TYPES}))['id']
    else
      user
    end
  end
end

#resource_config_value(cfg, key) ⇒ Object

Resolve a RESOURCE_CONFIG value that may be a Proc (evaluated in instance context).



466
467
468
469
# File 'lib/aspera/plugins/faspex5/commands.rb', line 466

def resource_config_value(cfg, key)
  v = cfg[key]
  v.is_a?(Proc) ? instance_exec(&v) : v
end

#setup_admin_nodes_shared_folders(node_id:) ⇒ Object

admin > nodes > shared_folders — node_id: already in ctx via arguments: on the :shared_folders command



813
814
815
# File 'lib/aspera/plugins/faspex5/commands.rb', line 813

def setup_admin_nodes_shared_folders(node_id:, **)
  {sf_entity: "nodes/#{node_id}/shared_folders"}
end

#setup_admin_nodes_shared_folders_user(sf_entity:, shared_folder_id:) ⇒ Object

admin > nodes > shared_folders > user — shared_folder_id: already in ctx via arguments: on the :user command



830
831
832
# File 'lib/aspera/plugins/faspex5/commands.rb', line 830

def setup_admin_nodes_shared_folders_user(sf_entity:, shared_folder_id:, **)
  {user_path: "#{sf_entity}/#{shared_folder_id}/custom_access_users"}
end

#setup_package_id ⇒ Hash

Setup for package sub-commands that need an id: with a public link to a package, the id comes from the link. Leaf setup runs before argument resolution: the declared package_id argument is then not read.

Returns:

  • (Hash) —

    ctx key: package_id, or empty (package_id is read from the command line)



922
923
924
925
# File 'lib/aspera/plugins/faspex5/commands.rb', line 922

def setup_package_id(**)
  return {} unless api_v5.pub_link_context&.key?('package_id')
  {package_id: api_v5.pub_link_context['package_id']}
end

#wait_for_job(job_id) ⇒ Hash

Returns result of API call for job status.

Parameters:

  • job_id (String) —

    job identifier

Returns:

  • (Hash) —

    result of API call for job status



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/aspera/plugins/faspex5/commands.rb', line 169

def wait_for_job(job_id)
  result = nil
  loop do
    result = api_v5.read("jobs/#{job_id}", {type: :formatted})
    break unless Api::JOB_RUNNING.include?(result['status'])
    Rest::Parameters.instance.spinner_cb.call(result['status'])
    sleep(0.5)
  end
  Rest::Parameters.instance.spinner_cb.call(action: :success)
  return result
end

#wait_package_status(id, status_list: Api::PACKAGE_TERMINATED) ⇒ Object

wait for package status to be in provided list



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/aspera/plugins/faspex5/commands.rb', line 142

def wait_package_status(id, status_list: Api::PACKAGE_TERMINATED)
  total_sent = false
  loop do
    status = api_v5.read("packages/#{id}/upload_details")
    status['id'] = id
    # user asked to not follow
    return status if status_list.nil?
    if status['upload_status'].eql?('submitted')
      progress_bar&.event(:sessions_init, session_id: nil, info: status['upload_status'])
    elsif !total_sent
      progress_bar&.event(:session_start, session_id: id)
      progress_bar&.event(:session_size, session_id: id, info: status['bytes_total'].to_i)
      total_sent = true
    else
      progress_bar&.event(:transfer, session_id: id, info: status['bytes_written'].to_i)
    end
    if status_list.include?(status['upload_status'])
      progress_bar&.event(:session_end, session_id: id)
      progress_bar&.event(:end, info: !status['upload_status'].eql?('failed'))
      return status
    end
    sleep(1.0)
  end
end

#wizard(wizard, app_url) ⇒ Hash

Returns :preset_value, :test_args.

Parameters:

Returns:

  • (Hash) —

    :preset_value, :test_args



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aspera/plugins/faspex5/commands.rb', line 77

def wizard(wizard, app_url)
  client_id = options.get_option(:client_id)
  client_secret = options.get_option(:client_secret)
  if client_id.nil? || client_secret.nil?
    formatter.display_status('Ask the ascli client id and secret to your Administrator.'.red)
    formatter.display_status("Log in as an admin user at: #{app_url}")
    Environment.instance.open_uri(app_url)
    wizard.display_navigation(Environment.char('𓃑'), 'Admin', 'Configurations', 'API clients')
    formatter.display_status('Create an API client with:')
    formatter.display_status('- name: ascli')
    formatter.display_status('- JWT: enabled')
    formatter.display_status('Upon creation, the admin shall get those parameters:')
    client_id = options.get_option(:client_id, mandatory: wizard.required)
    client_secret = options.get_option(:client_secret, mandatory: wizard.required)
  end
  wiz_username = options.get_option(:username, mandatory: true)
  wizard.check_email(wiz_username)
  private_key = wizard.ask_private_key(
    user: wiz_username,
    url: app_url,
    page: [Environment.char('👤'), 'Account Settings', 'Preferences', 'Public Key in PEM']
  )
  return {
    preset_ids:   [wiz_username, client_id].compact,
    preset_value: {
      url:           app_url,
      username:      wiz_username,
      auth:          :jwt.to_s,
      private_key:   private_key,
      client_id:     client_id,
      client_secret: client_secret
    },
    test_args:    'user profile show'
  }
end