Class: Aspera::Plugins::Ats::Commands

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

Overview

Constant Summary

Constants inherited from Cli::Plugins::Base

Cli::Plugins::Base::FILTER_ARGS

Instance Attribute Summary

Attributes inherited from Cli::Plugins::Base

#context, #help_path

Instance Method Summary collapse

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, #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

Methods included from Cli::Option::Declarator

#declare_options, #option, #option_specs, #register_option_spec

Constructor Details

#initialize(api: nil, **base_args) ⇒ Commands

Returns a new instance of Commands.



35
36
37
38
39
# File 'lib/aspera/plugins/ats/commands.rb', line 35

def initialize(api: nil, **base_args)
  super(**base_args)
  @ats_api_open = Api.new
  @ats_api_auth = api
end

Instance Method Details

#action_access_key_cluster(access_key_id:) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/aspera/plugins/ats/commands.rb', line 233

def action_access_key_cluster(access_key_id:, **)
  ats_url = ats_api.base_url
  api_ak_auth = Rest::Client.new(
    base_url: ats_url,
    auth:     {
      type:     :basic,
      username: access_key_id,
      password: context.secret_finder.lookup(url: ats_url, username: access_key_id)
    }
  )
  return Cli::Result::SingleObject.new(api_ak_auth.read('servers'))
end

#action_access_key_create(access_key: {}) ⇒ Object



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

def action_access_key_create(access_key: {}, **)
  params = access_key
  server_data = nil
  # if transfer_server_id not provided, get it from command line options
  if !params.key?('transfer_server_id')
    server_data = server_by_cloud_region
    params['transfer_server_id'] = server_data['id']
  end
  Log.log.debug { "using params: #{params}".bg(:red).white }
  if params.key?('storage')
    case params['storage']['type']
    # here we need somehow to map storage type to field to get for auth end point
    when 'ibm-s3'
      server_data2 = nil
      if server_data.nil?
        server_data2 = @ats_api_open.all_servers.find { |s| s['id'].eql?(params['transfer_server_id']) }
        raise "no such transfer server id: #{params['transfer_server_id']}" if server_data2.nil?
      else
        server_data2 = @ats_api_open.all_servers.find do |s|
          s['cloud'].eql?(server_data['cloud']) &&
            s['region'].eql?(server_data['region']) &&
            s.key?('s3_authentication_endpoint')
        end
        raise "no such transfer server id: #{params['transfer_server_id']}" if server_data2.nil?
        # specific one do not have s3 end point in id
        params['transfer_server_id'] = server_data2['id']
      end
      params['storage']['endpoint'] = server_data2['s3_authentication_endpoint'] if !params['storage'].key?('authentication_endpoint')
    end
  end
  res = ats_api.create('access_keys', params)
  return Cli::Result::SingleObject.new(res)
  # TODO : action : modify, with "PUT"
end

#action_access_key_modify(access_key:, access_key_id:) ⇒ Object



207
208
209
210
211
212
# File 'lib/aspera/plugins/ats/commands.rb', line 207

def action_access_key_modify(access_key:, access_key_id:, **)
  params = access_key
  params['id'] = access_key_id
  ats_api.update("access_keys/#{access_key_id}", params)
  return Cli::Result::Status.new('modified')
end

#action_cluster_show(server_id: nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/aspera/plugins/ats/commands.rb', line 161

def action_cluster_show(server_id: nil, **)
  if options.get_option(:cloud) || options.get_option(:region)
    server_data = server_by_cloud_region
  else
    Aspera.assert(server_id, type: Cli::MissingArgument) { 'server_id (or options cloud and region)' }
    server_data = @ats_api_open.all_servers.find { |i| i['id'].eql?(server_id) }
    raise Cli::BadIdentifier.new('server', server_id) if server_data.nil?
  end
  Cli::Result::SingleObject.new(server_data)
end

#ak_node_plugin(access_key_id:) ⇒ Array(Node, Hash)

access_key > node - mount target: Node plugin for an ATS access key. access_key_id: is already in ctx via arguments: on the :node command.

Returns:

  • (Array(Node, Hash)) —

    Node plugin and seed ctx for access_keys do



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/aspera/plugins/ats/commands.rb', line 217

def ak_node_plugin(access_key_id:, **)
  ak_data = ats_api.read("access_keys/#{access_key_id}")
  server_data = @ats_api_open.all_servers.find { |i| i['id'].start_with?(ak_data['transfer_server_id']) }
  Aspera.assert(!server_data.nil?, type: Cli::Error) { 'no such server found' }
  node_url = server_data['transfer_setup_url']
  api_node = Node::Api.new(
    base_url: node_url,
    auth:     {
      type:     :basic,
      username: access_key_id,
      password: context.secret_finder.lookup(url: node_url, username: access_key_id)
    }
  )
  [Node::Commands.new(context: context, api: api_node), {do_root_file_id: ak_data['root_file_id']}]
end

#api_key_available? ⇒ Boolean

api_key sub-tree is only available when authenticated via ATS key (not injected API)

Returns:

  • (Boolean)


117
118
119
# File 'lib/aspera/plugins/ats/commands.rb', line 117

def api_key_available?
  @ats_api_auth.nil?
end

#ats_api ⇒ Object

require api key only if needed



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/aspera/plugins/ats/commands.rb', line 131

def ats_api
  return @ats_api_auth unless @ats_api_auth.nil?
  @ats_api_auth = Rest::Client.new(
    base_url: "#{Api::SERVICE_BASE_URL}/pub/v1",
    auth:     {
      type:     :basic,
      username: options.get_option(:ats_key, mandatory: true),
      password: options.get_option(:ats_secret, mandatory: true)
    }
  )
end

#ats_api_v2_auth_ibm(rest_add_headers = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/aspera/plugins/ats/commands.rb', line 143

def ats_api_v2_auth_ibm(rest_add_headers = {})
  return Rest::Client.new(
    base_url: "#{Api::SERVICE_BASE_URL}/v2",
    headers:  rest_add_headers,
    auth:     {
      type:          :oauth2,
      grant_method:  :generic,
      base_url:      'https://iam.bluemix.net/identity',
      # does not work:  base_url:    'https://iam.cloud.ibm.com/identity',
      grant_type:    'urn:ibm:params:oauth:grant-type:apikey',
      response_type: 'cloud_iam',
      params:        {
        apikey: options.get_option(:ibm_api_key, mandatory: true)
      }
    }
  )
end

#server_by_cloud_region ⇒ Object

--- helpers ---



123
124
125
126
127
128
# File 'lib/aspera/plugins/ats/commands.rb', line 123

def server_by_cloud_region
  # TODO: provide list ?
  cloud = options.get_option(:cloud, mandatory: true).upcase
  region = options.get_option(:region, mandatory: true)
  return @ats_api_open.read("servers/#{cloud}/#{region}")
end