Class: Aspera::Cli::Plugins::Shares

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

Overview

Plugin for Aspera Shares v1

Constant Summary collapse

NODE_API_PATH =

path for node API after base url

'node_api'
ADMIN_API_PATH =

path for node admin after base url

'api/v1'
SAML_IMPORT_MANDATORY =
%w[id name_id].freeze
SAML_IMPORT_ALLOWED =
%w[email given_name surname].concat(SAML_IMPORT_MANDATORY).freeze
ACTIONS =
%i[health files admin].freeze
USR_GRP_SETTINGS =

common to users and groups

%i[transfer_settings app_authorizations share_permissions].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) ⇒ Shares

Returns a new instance of Shares.



53
54
55
# File 'lib/aspera/cli/plugins/shares.rb', line 53

def initialize(**env)
  super
end

Class Method Details

.detect(address_or_url) ⇒ Object



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

def detect(address_or_url)
  address_or_url = "https://#{address_or_url}" unless address_or_url.match?(%r{^[a-z]{1,6}://})
  api = Rest.new(base_url: address_or_url, redirect_max: 1)
  found = false
  begin
    # shall fail: shares requires auth, but we check error message
    # TODO: use ping instead ?
    api.read("#{NODE_API_PATH}/app")
  rescue RestCallError => e
    if e.response.code.to_s.eql?('401') && e.response.body.eql?('{"error":{"user_message":"API user authentication failed"}}')
      found = true
    end
  end
  return nil unless found
  version = 'unknown'
  test_page = api.call(operation: 'GET', subpath: 'login')
  if (m = test_page[:http].body.match(/\(v(1\..*)\)/))
    version = m[1]
  end
  return {
    version: version,
    url:     address_or_url
  }
end

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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aspera/cli/plugins/shares.rb', line 40

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:    'files br /'
  }
end

Instance Method Details

#execute_actionObject



64
65
66
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/aspera/cli/plugins/shares.rb', line 64

def execute_action
  command = options.get_next_command(ACTIONS, aliases: {repository: :files})
  case command
  when :health
    nagios = Nagios.new
    begin
      res = Rest
        .new(base_url: "#{options.get_option(:url, mandatory: true)}/#{NODE_API_PATH}")
        .call(
          operation: 'GET',
          subpath: 'ping',
          headers: {'content-type': 'application/json'})
      raise 'Shares not detected' unless res[:http].body.eql?(' ')
      nagios.add_ok('shares api', 'accessible')
    rescue StandardError => e
      nagios.add_critical('API', e.to_s)
    end
    return nagios.result
  when :repository, :files
    api_shares_node = basic_auth_api(NODE_API_PATH)
    repo_command = options.get_next_command(Node::COMMANDS_SHARES)
    return Node
        .new(**init_params, api: api_shares_node)
        .execute_action(repo_command)
  when :admin
    api_shares_admin = basic_auth_api(ADMIN_API_PATH)
    admin_command = options.get_next_command(%i[node share transfer_settings user group].freeze)
    case admin_command
    when :node
      return entity_action(api_shares_admin, 'data/nodes')
    when :share
      share_command = options.get_next_command(%i[user_permissions group_permissions].concat(Plugin::ALL_OPS))
      case share_command
      when *Plugin::ALL_OPS
        return entity_command(
          share_command, api_shares_admin, 'data/shares',
          display_fields: %w[id name node_id directory percent_free])
      when :user_permissions, :group_permissions
        share_id = instance_identifier
        return entity_action(api_shares_admin, "data/shares/#{share_id}/#{share_command}")
      end
    when :transfer_settings
      xfer_settings_command = options.get_next_command(%i[show modify])
      return entity_command(xfer_settings_command, api_shares_admin, 'data/transfer_settings', is_singleton: true)
    when :user, :group
      entity_type = admin_command
      entities_location = options.get_next_command(%i[all local ldap saml])
      entities_prefix = entities_location.eql?(:all) ? '' : "#{entities_location}_"
      entities_path = "data/#{entities_prefix}#{entity_type}s"
      entity_action = nil
      case entities_location
      when :all
        entity_action = %i[list show delete]
        entity_action.concat(USR_GRP_SETTINGS)
        entity_action.push(:users) if entity_type.eql?(:group)
        entity_action.freeze
      when :local
        entity_action = %i[list show delete create modify]
        entity_action.push(:users) if entity_type.eql?(:group)
        entity_action.freeze
      when :ldap
        entity_action = %i[add].freeze
      when :saml
        entity_action = %i[import].freeze
      end
      entity_verb = options.get_next_command(entity_action)
      case entity_verb
      when *Plugin::ALL_OPS # list, show, delete, create, modify
        display_fields = entity_type.eql?(:user) ? %w[id username first_name last_name email] : nil
        display_fields.push(:directory_user) if entity_type.eql?(:user) && entities_location.eql?(:all)
        return entity_command(entity_verb, api_shares_admin, entities_path, display_fields: display_fields)
      when *USR_GRP_SETTINGS # transfer_settings, app_authorizations, share_permissions
        group_id = instance_identifier
        entities_path = "#{entities_path}/#{group_id}/#{entity_verb}"
        return entity_action(api_shares_admin, entities_path, is_singleton: !entity_verb.eql?(:share_permissions))
      when :import # saml
        return do_bulk_operation(command: entity_verb, descr: 'user information') do |entity_parameters|
          entity_parameters = entity_parameters.transform_keys{|k|k.gsub(/\s+/, '_').downcase}
          Aspera.assert_type(entity_parameters, Hash)
          SAML_IMPORT_MANDATORY.each{|p|raise "missing mandatory field: #{p}" if entity_parameters[p].nil?}
          entity_parameters.each_key do |p|
            raise "unsupported field: #{p}, use: #{SAML_IMPORT_ALLOWED.join(',')}" unless SAML_IMPORT_ALLOWED.include?(p)
          end
          api_shares_admin.create("#{entities_path}/import", entity_parameters)[:data]
        end
      when :add # ldap
        return do_bulk_operation(command: entity_verb, descr: "#{entity_type} name", values: String) do |entity_name|
          api_shares_admin.create(entities_path, {entity_type=>entity_name})[:data]
        end
      when :users # group
        return entity_action(api_shares_admin, "#{entities_path}/#{instance_identifier}/#{entities_prefix}users")
      else Aspera.error_unexpected_value(entity_verb)
      end
    end
  end
end