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

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

Constant Summary collapse

ACTIONS =
%i[transfer health].freeze

Constants inherited from Base

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

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, #options, #persistency, #query_read_delete, #transfer, #value_create_modify

Constructor Details

#initialize(**_) ⇒ Console

Returns a new instance of Console.



63
64
65
# File 'lib/aspera/cli/plugins/console.rb', line 63

def initialize(**_)
  super
end

Class Method Details

.detect(address_or_url) ⇒ Object



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
42
43
44
45
46
# File 'lib/aspera/cli/plugins/console.rb', line 16

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
end

Instance Method Details

#execute_actionObject



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

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 Main.result_object_list(api_console.read('smart_transfers'))
      when :submit
        smart_id = options.get_next_argument('smart_id')
        params = options.get_next_argument('transfer parameters', validation: Hash)
        return Main.result_object_list(api_console.create("smart_transfers/#{smart_id}", params))
      end
    when :current
      command = options.get_next_command(%i[list show files start pause cancel resume rerun change_rate change_policy move_forwards move_back])
      case command
      when :list
        # https://developer.ibm.com/apis/catalog/aspera--aspera-console-rest-api/Developer+Guides#transfer-list
        query = query_read_delete(default: {})
        if query['from'].nil? && query['to'].nil?
          time_now = Time.now
          query['from'] = Manager.time_to_string(time_now - DEFAULT_FILTER_AGE_SECONDS)
          query['to'] = Manager.time_to_string(time_now)
        end
        if (filter = query.delete('filter'))
          parse_extended_filter(filter, query)
        end
        return Main.result_object_list(
          api_console.read('transfers', query),
          fields: %w[id contact name status]
        )
      when :show
        transfer_id = instance_identifier(description: 'transfer ID')
        return Main.result_single_object(api_console.read("transfers/#{transfer_id}"))
      when :files
        transfer_id = instance_identifier(description: 'transfer ID')
        query = query_read_delete(default: {})
        query['limit'] ||= 100
        return Main.result_object_list(api_console.read("transfers/#{transfer_id}/files", query))
      when :start, :pause, :cancel, :resume, :rerun, :change_rate, :change_policy, :move_forwards, :move_back
        transfer_id = instance_identifier(description: 'transfer ID')
        return Main.result_single_object(api_console.update("transfers/#{transfer_id}/#{command}", query_read_delete))
      end
    end
  end
end

#parse_extended_filter(filter, query) ⇒ Object

Raises:



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

def parse_extended_filter(filter, query)
  raise BadArgument, "Invalid filter syntax: #{filter}, shall be (field op val)and(field op val)..." unless filter.start_with?('(') && filter.end_with?(')')
  filter[1..-2].split(')and(').each_with_index do |expr, i|
    m = expr.match(EXPR_RE)
    raise BadArgument, "Invalid expression: #{expr}, shall be: <field> <op> <val>" unless m
    t = m.captures
    i += 1
    query["filter#{i}"] = t[0]
    query["comp#{i}"]   = t[1]
    query["val#{i}"]    = t[2]
  end
end

#wizard(wizard, app_url) ⇒ Hash

Returns :preset_value, :test_args.

Parameters:

  • wizard (Wizard)

    The wizard object

  • app_url (Wizard)

    The wizard object

Returns:

  • (Hash)

    :preset_value, :test_args



52
53
54
55
56
57
58
59
60
61
# File 'lib/aspera/cli/plugins/console.rb', line 52

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:    'transfer list'
  }
end