Class: Aspera::Cli::Plugins::Console

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

Constant Summary collapse

STANDARD_PATH =
'/aspera/console'
ACTIONS =
%i[transfer health].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) ⇒ Console

Returns a new instance of Console.



57
58
59
60
61
62
63
# File 'lib/aspera/cli/plugins/console.rb', line 57

def initialize(**env)
  super
  time_now = Time.now
  options.declare(:filter_from, 'Only after date', values: :date, default: Manager.time_to_string(time_now - DEFAULT_FILTER_AGE_SECONDS))
  options.declare(:filter_to, 'Only before date', values: :date, default: Manager.time_to_string(time_now))
  options.parse_options!
end

Class Method Details

.detect(address_or_url) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aspera/cli/plugins/console.rb', line 12

def detect(address_or_url)
  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}#{STANDARD_PATH}") unless address_or_url.end_with?(STANDARD_PATH)
  error = nil
  urls.each do |base_url|
    next unless base_url.start_with?('https://')
    api = Rest.new(base_url: base_url, redirect_max: 2)
    test_endpoint = 'login'
    test_page = api.call(
      operation: 'GET',
      subpath:   test_endpoint,
      query:     {local: true})
    next unless test_page[:http].body.include?('Aspera Console')
    version = 'unknown'
    if (m = test_page[:http].body.match(/\(v([1-9]\..*)\)/))
      version = m[1]
    end
    url = test_page[:http].uri.to_s
    return {
      version: version,
      url:     url[0..url.index(test_endpoint) - 2]
    }
  rescue StandardError => e
    error = e
    Log.log.debug{"detect error: #{e}"}
  end
  raise error if error
  return nil
end

.wizard(object:, private_key_path: nil, pub_key_pem: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aspera/cli/plugins/console.rb', line 43

def wizard(object:, private_key_path: nil, pub_key_pem: nil)
  options = object.options
  return {
    preset_value: {
      url:      options.get_option(:url, mandatory: true),
      username: options.get_option(:username, mandatory: true),
      password: options.get_option(:password, mandatory: true)
    },
    test_args:    'transfer list'
  }
end

Instance Method Details

#execute_actionObject



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aspera/cli/plugins/console.rb', line 67

def execute_action
  api_console = basic_auth_api('api')
  command = options.get_next_command(ACTIONS)
  case command
  when :health
    nagios = Nagios.new
    begin
      api_console.read('ssh_keys')
      nagios.add_ok('console api', 'accessible')
    rescue StandardError => e
      nagios.add_critical('console api', e.to_s)
    end
    return nagios.result
  when :transfer
    command = options.get_next_command(%i[current smart])
    case command
    when :smart
      command = options.get_next_command(%i[list submit])
      case command
      when :list
        return {type: :object_list, data: api_console.read('smart_transfers')[:data]}
      when :submit
        smart_id = options.get_next_argument('smart_id')
        params = options.get_next_argument('transfer parameters')
        return {type: :object_list, data: api_console.create("smart_transfers/#{smart_id}", params)[:data]}
      end
    when :current
      command = options.get_next_command([:list])
      case command
      when :list
        return {
          type:   :object_list,
          data:   api_console.read('transfers', {
            'from' => options.get_option(:filter_from, mandatory: true),
            'to'   => options.get_option(:filter_to, mandatory: true)
          })[:data],
          fields: %w[id contact name status]}
      end
    end
  end
end