class Aspera::Cli::Plugins::Cos

Constants

ACTIONS

Public Class Methods

new(env) click to toggle source
Calls superclass method Aspera::Cli::Plugin::new
# File lib/aspera/cli/plugins/cos.rb, line 9
def initialize(env)
  super(env)
  @service_creds=nil
  self.options.add_opt_simple(:bucket,'IBM Cloud Object storage bucket')
  self.options.add_opt_simple(:endpoint,'storage endpoint url')
  self.options.add_opt_simple(:apikey,'storage API key')
  self.options.add_opt_simple(:crn,'ressource instance id')
  #self.options.add_opt_simple(:oauth,'Oauth url')
  self.options.add_opt_simple(:service_credentials,'IBM Cloud service credentials (Hash)')
  self.options.add_opt_simple(:region,'IBM Cloud Object storage region')
end

Public Instance Methods

execute_action() click to toggle source
# File lib/aspera/cli/plugins/cos.rb, line 23
def execute_action
  command=self.options.get_next_command(ACTIONS)
  case command
  when :node
    bucket_name=self.options.get_option(:bucket,:mandatory)
    # get service credentials, Hash, e.g. @json:@file:...
    service_credentials=self.options.get_option(:service_credentials,:optional)
    storage_endpoint=self.options.get_option(:endpoint,:optional)
    raise "one of: endpoint or service_credentials is required" if service_credentials.nil? and storage_endpoint.nil?
    raise "endpoint and service_credentials are mutually exclusive" unless service_credentials.nil? or storage_endpoint.nil?
    if service_credentials.nil?
      service_api_key = self.options.get_option(:apikey,:mandatory)
      instance_id = self.options.get_option(:crn,:mandatory)
    else
      # check necessary contents
      raise CliBadArgument,'service_credentials must be a Hash' unless service_credentials.is_a?(Hash)
      ['apikey','resource_instance_id','endpoints'].each do |field|
        raise CliBadArgument,"service_credentials must have a field: #{field}" unless service_credentials.has_key?(field)
      end
      Aspera::Log.dump('service_credentials',service_credentials)
      # get options
      bucket_region=self.options.get_option(:region,:mandatory)
      # get API key from service credentials
      service_api_key=service_credentials['apikey']
      instance_id=service_credentials['resource_instance_id']
      # read endpoints from service provided in service credentials
      endpoints=Aspera::Rest.new({:base_url=>service_credentials['endpoints']}).read('')[:data]
      Aspera::Log.dump('endpoints',endpoints)
      storage_endpoint=endpoints.dig('service-endpoints','regional',bucket_region,'public',bucket_region)
      raise "no such region: #{bucket_region}" if storage_endpoint.nil?
      storage_endpoint='https://'+storage_endpoint
    end
    api_node=CosNode.new(bucket_name,storage_endpoint,instance_id,service_api_key)
    #command=self.options.get_next_command(Node::ACTIONS)
    #command=self.options.get_next_command(Node::COMMON_ACTIONS)
    command=self.options.get_next_command([:upload,:download,:info,:access_key,:api_details])
    node_plugin=Node.new(@agents.merge(skip_basic_auth_options: true, node_api: api_node, add_request_param: api_node.add_ts))
    return node_plugin.execute_action(command)
  end
end