Class: Aspera::Cli::Plugins::Aoc
- Defined in:
- lib/aspera/cli/plugins/aoc.rb
Constant Summary collapse
- FILES_COMMANDS =
(Node::COMMANDS_GEN4 + %i[transfer]).freeze
- ADMIN_ACTIONS =
%i[bearer_token application ats usage_reports analytics subscription auth_providers].concat(ADMIN_OBJECTS).freeze
- ACTIONS =
must be public
%i[reminder servers bearer_token organization tier_restrictions user packages files admin automation gateway].freeze
Constants inherited from Oauth
Oauth::AUTH_OPTIONS, Oauth::AUTH_TYPES
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
- .application_name ⇒ Object
- .detect(base_url) ⇒ Hash, NilClass
-
.next_available_folder(base, always: false) ⇒ String
Get folder path that does not exist.
-
.unique_folder(package_info, destination_folder, fld: nil, seq: false, opt: false) ⇒ Object
Get folder path that does not exist If it exists, an extension is added or a sequential number if extension == :seq.
Instance Method Summary collapse
-
#aoc_api ⇒ Api::AoC
AoC Rest object.
-
#api_from_options(base_path) ⇒ Api::AoC
Create an API object with the options from CLI, but with a different subpath.
-
#change_api_scope(new_scope) ⇒ Object
Change API scope for subsequent calls, re-instantiate API object.
- #execute_action ⇒ Object
- #execute_application_action ⇒ Object
-
#execute_nodegen4_command(command_repo, node_id, file_id: nil, scope: nil) ⇒ Object
Execute a node gen4 command starting at given node and file IDs.
-
#execute_resource_action(resource_type) ⇒ Object
Execute an action on admin resources.
-
#get_resource_id_from_args(resource_class_path) ⇒ Object
Get resource identifier from command line, either directly specifying the ‘id` or from `name` (percent selector).
-
#get_resource_path_from_args(resource_class_path) ⇒ Object
Get resource path from command line.
-
#initialize(**_) ⇒ Aoc
constructor
A new instance of Aoc.
-
#list_all_packages_with_query ⇒ Hash
List all packages according to ‘query` option.
-
#package_persistency ⇒ Object
Persistency object if option ‘once_only` is used.
- #reject_packages_from_persistency(all_packages, skip_ids_persistency) ⇒ Object
-
#resolve_dropbox_name_default_ws_id(query) ⇒ Object
Translates ‘dropbox_name` to `dropbox_id` and fills current workspace_id.
-
#result_list(resource_class_path, fields: nil, base_query: {}, default_query: {}) {|query| ... } ⇒ Object
List all entities, given additional, default and user’s queries.
-
#short_link_command(**shared_data, &perm_block) ⇒ Object
Create a short link for the given entity: Shared folder or Shared Inbox Short link entity: ‘short_links` have: - a numerical id, e.g.
-
#wizard(wizard, app_url) ⇒ Hash
:preset_value, :test_args.
-
#workspace_id_hash(hash = nil, string: false, name: false) ⇒ Hash{Symbol, String => String}
Generate or update Hash with workspace id and name (option), if not already set.
Methods inherited from Oauth
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, #options, #persistency, #query_read_delete, #transfer, #value_create_modify
Constructor Details
#initialize(**_) ⇒ Aoc
Returns a new instance of Aoc.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 202 def initialize(**_) super @cache_workspace_info = nil @cache_home_node_file = nil @cache_api_aoc = nil @scope = Api::AoC::Scope::USER .declare(:workspace, 'Name of workspace', allowed: [String, NilClass], default: Api::AoC::DEFAULT_WORKSPACE) .declare(:new_user_option, 'New user creation option for unknown package recipients', allowed: Hash) .declare(:validate_metadata, 'Validate shared inbox metadata', allowed: Allowed::TYPES_BOOLEAN, default: true) .declare(:package_folder, 'Handling of reception of packages in folders', allowed: Hash, default: {}) . # add node plugin options (for manual) Node.() end |
Class Method Details
.application_name ⇒ Object
58 59 60 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 58 def application_name 'Aspera on Cloud' end |
.detect(base_url) ⇒ Hash, NilClass
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 63 def detect(base_url) # no protocol ? base_url = "https://#{base_url}" unless base_url.match?(%r{^[a-z]{1,6}://}) # only org provided ? base_url = "#{base_url}.#{Api::AoC::SAAS_DOMAIN_PROD}" unless base_url.include?('.') # AoC is only https return unless base_url.start_with?('https://') location = Rest.new(base_url: base_url, redirect_max: 0).call(operation: 'GET', subpath: 'auth/ping', exception: false, ret: :resp)['Location'] return if location.nil? redirect_uri = URI.parse(location) od = Api::AoC.split_org_domain(URI.parse(base_url)) return unless redirect_uri.path.end_with?("oauth2/#{od[:organization]}/login") # either in standard domain, or product name in page return { version: Api::AoC.saas_url?(base_url) ? 'SaaS' : 'Self-managed', url: base_url } end |
.next_available_folder(base, always: false) ⇒ String
Get folder path that does not exist
86 87 88 89 90 91 92 93 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 86 def next_available_folder(base, always: false) counter = always ? 1 : 0 loop do result = counter.zero? ? base : "#{base}.#{counter}" return result unless Dir.exist?(result) counter += 1 end end |
.unique_folder(package_info, destination_folder, fld: nil, seq: false, opt: false) ⇒ Object
Get folder path that does not exist If it exists, an extension is added or a sequential number if extension == :seq
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 101 def unique_folder(package_info, destination_folder, fld: nil, seq: false, opt: false) Aspera.assert_array_all(fld, String, type: BadArgument){'fld'} Aspera.assert([1, 2].include?(fld.length)){'fld must have 1 or 2 elements'} folder = Environment.instance.sanitized_filename(package_info[fld[0]]) if seq folder = next_available_folder(folder, always: !opt) elsif fld[1] && (Dir.exist?(folder) || !opt) # NOTE: it might already exist folder = "#{folder}.#{Environment.instance.sanitized_filename(fld[1])}" end puts("sub= #{folder}") File.join(destination_folder, folder) end |
Instance Method Details
#aoc_api ⇒ Api::AoC
AoC Rest object
254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 254 def aoc_api if @cache_api_aoc.nil? @cache_api_aoc = (Api::AoC::API_V1) transfer.httpgw_url_cb = lambda do organization = @cache_api_aoc.read('organization') # @cache_api_aoc.current_user_info['connect_disabled'] organization['http_gateway_server_url'] if organization['http_gateway_enabled'] && organization['http_gateway_server_url'] end end return @cache_api_aoc end |
#api_from_options(base_path) ⇒ Api::AoC
Create an API object with the options from CLI, but with a different subpath
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 229 def (base_path) # Get all existing OAuth kwargs from `options`. api = Api::AoC.new( scope: @scope, subpath: base_path, secret_finder: config, **Oauth.() ) # User set a workspace ? # @type [String, nil] workspace = .get_option(:workspace) if !workspace.nil? && (m = Manager.percent_selector(workspace)) case m[:field] when 'name' then api.ws_ids[:name] = m[:value] when 'id' then api.ws_ids[:id] = m[:value] else Aspera.error_unexpected_value(m[:field]){'workspace selector: only `name` or `id`'} end else api.ws_ids[:name] = workspace end api end |
#change_api_scope(new_scope) ⇒ Object
Change API scope for subsequent calls, re-instantiate API object
219 220 221 222 223 224 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 219 def change_api_scope(new_scope) # Discard cache @cache_api_aoc = nil @scope = new_scope nil end |
#execute_action ⇒ Object
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 975 def execute_action command = .get_next_command(ACTIONS) if %i[files packages].include?(command) formatter.display_status("Workspace: #{aoc_api.workspace_info[:name].to_s.red}#{' (default)' if aoc_api.default_workspace?}") if !aoc_api.private_link.nil? folder_name = aoc_api.node_api_from(node_id: aoc_api.home[:node_id]).read("files/#{aoc_api.home[:file_id]}")['name'] formatter.display_status("Private Folder: #{folder_name}") end end case command when :reminder # send an email reminder with list of orgs user_email = .get_option(:username, mandatory: true) Rest.new(base_url: "#{Api::AoC.api_base_url}/#{Api::AoC::API_V1}").create('organization_reminders', {email: user_email}) return Main.result_status("List of organizations user is member of, has been sent by e-mail to #{user_email}") when :servers return Main.result_object_list(Rest.new(base_url: "#{Api::AoC.api_base_url}/#{Api::AoC::API_V1}").read('servers')) when :bearer_token return Main.result_text(aoc_api.oauth.) when :organization return Main.result_single_object(aoc_api.read('organization')) when :tier_restrictions return Main.result_single_object(aoc_api.read('tier_restrictions')) when :user user_cmd = .get_next_command(%i[workspaces profile preferences notifications contacts]) case user_cmd when :contacts return execute_resource_action(:contact) # when :settings # return Main.result_object_list(aoc_api.read('client_settings/')) when :workspaces case .get_next_command(%i[list current]) when :list return result_list('workspaces', fields: %w[id name]) when :current return Main.result_single_object(aoc_api.workspace_info) end when :profile case .get_next_command(%i[show modify]) when :show return Main.result_single_object(aoc_api.current_user_info(exception: true)) when :modify aoc_api.update("users/#{aoc_api.current_user_info(exception: true)['id']}", .get_next_argument('properties', validation: Hash)) return Main.result_status('modified') end when :preferences, :notifications user_preferences_res = "users/#{aoc_api.current_user_info(exception: true)['id']}/#{user_cmd.eql?(:preferences) ? 'user_interaction_preferences' : 'notification_preferences'}" case .get_next_command(%i[show modify]) when :show return Main.result_single_object(aoc_api.read(user_preferences_res)) when :modify aoc_api.update(user_preferences_res, .get_next_argument('properties', validation: Hash)) return Main.result_status('modified') end end when :packages package_command = .get_next_command(%i[shared_inboxes send receive list show delete modify].concat(Node::NODE4_READ_ACTIONS), aliases: {recv: :receive}) case package_command when :shared_inboxes case .get_next_command(%i[list show short_link]) when :list default_query = {'embed[]' => 'dropbox', 'aggregate_permissions_by_dropbox' => true, 'sort' => 'dropbox_name'} workspace_id_hash(default_query, string: true) return result_list('dropbox_memberships', fields: %w[dropbox_id dropbox.name], default_query: default_query) when :show return Main.result_single_object(aoc_api.read(get_resource_path_from_args('dropboxes'))) when :short_link # TODO: check name return short_link_command(dropbox_id: get_resource_id_from_args('dropboxes'), name: '') end when :send package_data = value_create_modify(command: package_command) new_user_option = .get_option(:new_user_option) option_validate = .get_option(:validate_metadata) # Works for both normal user auth and link auth. workspace_id_hash(package_data, string: true) unless package_data.key?('workspace_id') if !aoc_api.public_link.nil? aoc_api.assert_public_link_types(%w[send_package_to_user send_package_to_dropbox]) box_type = aoc_api.public_link['purpose'].split('_').last package_data['recipients'] = [{'id' => aoc_api.public_link['data']["#{box_type}_id"], 'type' => box_type}] # enforce workspace id from link (should be already ok, but in case user wanted to override) package_data['workspace_id'] = aoc_api.public_link['data']['workspace_id'] end package_data['encryption_at_rest'] = true if transfer.user_transfer_spec['content_protection'].eql?('encrypt') # transfer may raise an error created_package = aoc_api.create_package_simple(package_data, option_validate, new_user_option) Main.result_transfer(transfer.start(created_package[:spec], rest_token: created_package[:node])) # return all info on package (especially package id) return Main.result_single_object(created_package[:info]) when :receive ids_to_download = nil if !aoc_api.public_link.nil? aoc_api.assert_public_link_types(['view_received_package']) # Set the package id from link ids_to_download = aoc_api.public_link['data']['package_id'] end # Get from command line unless it was a public link ids_to_download ||= .instance_identifier skip_ids_persistency = package_persistency case ids_to_download when SpecialValues::INIT all_packages = list_all_packages_with_query[:items] Aspera.assert(skip_ids_persistency){'INIT requires option once_only'} skip_ids_persistency.data.clear.concat(all_packages.map{ |e| e['id']}) skip_ids_persistency.save return Main.result_status("Initialized skip for #{skip_ids_persistency.data.count} package(s)") when SpecialValues::ALL all_packages = list_all_packages_with_query[:items] # remove from list the ones already downloaded reject_packages_from_persistency(all_packages, skip_ids_persistency) ids_to_download = all_packages.map{ |e| e['id']} formatter.display_status("Found #{ids_to_download.length} package(s).") else # single id to array ids_to_download = [ids_to_download] unless ids_to_download.is_a?(Array) end # download all files, or specified list only ts_paths = transfer.ts_source_paths(default: ['.']) per_package_def = .get_option(:package_folder).symbolize_keys = per_package_def.delete(:inf) # get value outside of loop destination_folder = transfer.destination_folder(Transfer::Spec::DIRECTION_RECEIVE) result_transfer = [] ids_to_download.each do |package_id| package_info = aoc_api.read("packages/#{package_id}") package_node_api = aoc_api.node_api_from( node_id: package_info['node_id'], package_info: package_info, **workspace_id_hash(name: true) ) transfer_spec = package_node_api.transfer_spec_gen4( package_info['contents_file_id'], Transfer::Spec::DIRECTION_RECEIVE, {'paths'=> ts_paths} ) transfer.user_transfer_spec['destination_root'] = self.class.unique_folder(package_info, destination_folder, **per_package_def) unless per_package_def.empty? dest_folder = transfer.user_transfer_spec['destination_root'] || destination_folder formatter.display_status(%Q{Downloading package: [#{package_info['id']}] "#{package_info['name']}" to [#{dest_folder}]}) statuses = transfer.start( transfer_spec, rest_token: package_node_api ) File.write(File.join(dest_folder, "#{package_id}.info.json"), package_info.to_json) if result_transfer.push({'package' => package_id, Main::STATUS_FIELD => statuses}) # update skip list only if all transfer sessions completed if skip_ids_persistency && TransferAgent.session_status(statuses).eql?(:success) skip_ids_persistency.data.push(package_id) skip_ids_persistency.save end end return Main.result_transfer_multiple(result_transfer) when :show package_id = .instance_identifier package_info = aoc_api.read("packages/#{package_id}") return Main.result_single_object(package_info) when :list result = list_all_packages_with_query skip_ids_persistency = package_persistency reject_packages_from_persistency(result[:items], skip_ids_persistency) display_fields = PACKAGE_LIST_DEFAULT_FIELDS display_fields += ['workspace_id'] if aoc_api.workspace_info[:id].nil? return Main.result_object_list(result[:items], fields: display_fields, total: result[:total]) when :delete return do_bulk_operation(command: package_command, values: .instance_identifier) do |package_id| Aspera.assert_type(package_id, String, Integer){'identifier'} aoc_api.delete("packages/#{package_id}") end when :modify package_id = .instance_identifier package_data = value_create_modify(command: package_command) aoc_api.update("packages/#{package_id}", package_data) return Main.result_status('modified') when *Node::NODE4_READ_ACTIONS package_id = .instance_identifier package_info = aoc_api.read("packages/#{package_id}") return execute_nodegen4_command(package_command, package_info['node_id'], file_id: package_info['contents_file_id'], scope: Api::Node::Scope::USER) end when :files command_repo = .get_next_command([:short_link].concat(FILES_COMMANDS)) case command_repo when *FILES_COMMANDS return execute_nodegen4_command(command_repo, aoc_api.home[:node_id], file_id: aoc_api.home[:file_id], scope: Api::Node::Scope::USER) when :short_link folder_dest = .get_next_argument('path', validation: String) home_node_api = aoc_api.node_api_from( node_id: aoc_api.home[:node_id], **workspace_id_hash(name: true) ) shared_apifid = home_node_api.resolve_api_fid(aoc_api.home[:file_id], folder_dest) return short_link_command( node_id: shared_apifid.node_api.app_info.node_info['id'], file_id: shared_apifid.file_id ) do |op, id, access_levels| case op when :create # `id` is the resource id perm_data = { 'file_id' => shared_apifid.file_id, 'access_id' => id, 'access_type' => 'user', 'access_levels' => Api::AoC.(access_levels), 'tags' => { 'url_token' => true, 'folder_name' => File.basename(folder_dest), 'created_by_name' => aoc_api.current_user_info['name'], 'created_by_email' => aoc_api.current_user_info['email'], 'access_key' => shared_apifid.node_api.app_info.node_info['access_key'], 'node' => shared_apifid.node_api.app_info.node_info['name'], **workspace_id_hash(string: true, name: true) } } created_data = shared_apifid.node_api.create('permissions', perm_data) aoc_api.(event_data: created_data, app_info: shared_apifid.node_api.app_info) when :update # `id` is the permission_id found = shared_apifid.node_api.read('permissions', {file_id: shared_apifid.file_id, inherited: false, access_type: 'user', access_id: id}).find{ |i| i['access_id'].eql?(id)} raise Error, "Short link not found: #{id}" if found.nil? shared_apifid.node_api.update("permissions/#{found['id']}", {access_levels: Api::AoC.(access_levels)}) when :delete # `id` is the resource id, i.e. `access_id` found = shared_apifid.node_api.read('permissions', {file_id: shared_apifid.file_id, inherited: false, access_type: 'user', access_id: id}).first raise Error, "Short link not found: #{id}" if found.nil? shared_apifid.node_api.delete("permissions/#{found['id']}") else Aspera.error_unexpected_value(op) end end end when :automation change_api_scope(Api::AoC::Scope::ADMIN_USER) Log.log.warn('BETA: work under progress') # automation api is not in the same place automation_api = Rest.new(**aoc_api.params, base_url: aoc_api.base_url.gsub('/api/', '/automation/')) command_automation = .get_next_command(%i[workflows instances]) case command_automation when :instances return entity_execute(api: aoc_api, entity: 'workflow_instances') when :workflows wf_command = .get_next_command(%i[action launch].concat(Operations::ALL)) case wf_command when *Operations::ALL return entity_execute( api: automation_api, entity: 'workflows', command: wf_command ) when :launch wf_id = .instance_identifier data = automation_api.create("workflows/#{wf_id}/launch", {}) return Main.result_single_object(data) when :action # TODO: not complete wf_id = .instance_identifier wf_action_cmd = .get_next_command(%i[list create show]) Log.log.warn{"Not implemented: #{wf_action_cmd}"} step = automation_api.create('steps', {'workflow_id' => wf_id}) automation_api.update("workflows/#{wf_id}", {'step_order' => [step['id']]}) action = automation_api.create('actions', {'step_id' => step['id'], 'type' => 'manual'}) automation_api.update("steps/#{step['id']}", {'action_order' => [action['id']]}) wf = automation_api.read("workflows/#{wf_id}") return Main.result_single_object(wf) end end when :admin return execute_admin_action when :gateway require 'aspera/faspex_gw' parameters = value_create_modify(command: command, default: {}).symbolize_keys uri = URI.parse(parameters.delete(:url){WebServerSimple::DEFAULT_URL}) server = WebServerSimple.new(uri, **parameters.slice(*WebServerSimple::PARAMS)) Aspera.assert(parameters.except(*WebServerSimple::PARAMS).empty?) server.mount(uri.path, Faspex4GWServlet, aoc_api, aoc_api.workspace_info[:id]) server.start return Main.result_status('Gateway terminated') else Aspera.error_unreachable_line end Aspera.error_unreachable_line end |
#execute_application_action ⇒ Object
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 555 def execute_application_action apps_info = aoc_api.read('admin/apps') all_app_types = apps_info.map{ |i| i['app_type'].to_sym} command_apps = .get_next_command(%i[types settings instance membership]) case command_apps when :types return Main.result_object_list(apps_info) when :settings app_type = .get_next_command(all_app_types) cmd_path = "/apps/#{app_type}/settings" command_app_settings = .get_next_command(Operations::SINGLETON) case command_app_settings when :show return Main.result_single_object(aoc_api.read(cmd_path)) when :modify aoc_api.update(cmd_path, .get_next_argument('properties', validation: Hash)) return Main.result_status('modified') end when :instance list_default_query = {workspace_id: aoc_api.workspace_info[:id]} list_default_fields = %w[id app_type available workspace_id] command_app_instances = .get_next_command(%i[list] + Operations::SINGLETON) resource_path = 'admin/apps_new' if Operations::SINGLETON.include?(command_app_instances) app_type = .get_next_command(all_app_types) resource_path = "#{resource_path}/#{app_type}/#{.instance_identifier(description: "#{app_type} identifier")}" end case command_app_instances when :list return result_list(resource_path, fields: list_default_fields, default_query: list_default_query) when :show return Main.result_single_object(aoc_api.read(resource_path, query_read_delete)) when :modify aoc_api.update(resource_path, .get_next_argument('properties', validation: Hash)) return Main.result_status('modified') end when :membership resource_path = 'apps/app_memberships' command_app_member = .get_next_command(%i[create list show delete]) resource_path = "#{resource_path}/#{.instance_identifier(description: 'membership id')}" unless Operations::GLOBAL.include?(command_app_member) case command_app_member when :list return result_list(resource_path) when :delete aoc_api.delete("#{resource_path}}") return Main.result_status('deleted') when :show return Main.result_single_object(aoc_api.read(resource_path, query_read_delete)) when :create aoc_api.update(resource_path, .get_next_argument('membership properties', validation: Hash)) return Main.result_status('modified') end end end |
#execute_nodegen4_command(command_repo, node_id, file_id: nil, scope: nil) ⇒ Object
Execute a node gen4 command starting at given node and file IDs
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 349 def execute_nodegen4_command(command_repo, node_id, file_id: nil, scope: nil) top_node_api = aoc_api.node_api_from( node_id: node_id, scope: scope, **workspace_id_hash(name: true) ) file_id = top_node_api.read("access_keys/#{top_node_api.app_info.node_info['access_key']}")['root_file_id'] if file_id.nil? node_plugin = Node.new(context: context, api: top_node_api) case command_repo when *Node::COMMANDS_GEN4 return node_plugin.execute_command_gen4(command_repo, file_id) when :transfer # client side is agent # server side is transfer server # in same workspace push_pull = .get_next_argument('direction', accept_list: %i[push pull]) source_folder = .get_next_argument('folder or source files', validation: String) case push_pull when :push client_direction = Transfer::Spec::DIRECTION_SEND client_folder = source_folder server_folder = transfer.destination_folder(client_direction) when :pull client_direction = Transfer::Spec::DIRECTION_RECEIVE client_folder = transfer.destination_folder(client_direction) server_folder = source_folder else Aspera.error_unreachable_line end client_apifid = top_node_api.resolve_api_fid(file_id, client_folder) server_apifid = top_node_api.resolve_api_fid(file_id, server_folder) # force node as transfer agent transfer.agent_instance = Agent::Node.new( url: client_apifid.node_api.base_url, username: client_apifid.node_api.app_info.node_info['access_key'], password: client_apifid.node_api.oauth., root_id: client_apifid.file_id ) # additional node to node TS info add_ts = { 'remote_access_key' => server_apifid.node_api.app_info.node_info['access_key'], 'destination_root_id' => server_apifid.file_id, 'source_root_id' => client_apifid.file_id } return Main.result_transfer(transfer.start(server_apifid.node_api.transfer_spec_gen4( server_apifid.file_id, client_direction, add_ts ))) else Aspera.error_unexpected_value(command_repo){'command'} end Aspera.error_unreachable_line end |
#execute_resource_action(resource_type) ⇒ Object
Execute an action on admin resources
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 404 def execute_resource_action(resource_type) require_workspace_id = false list_default_fields = %w[id name] list_default_query = {} supported_operations = Operations::ALL resource_class_path = "#{resource_type}s" case resource_type when :client supported_operations += %i[set_pub_key] when :client_access_key resource_class_path = "admin/#{resource_type}s" when :client_registration_token resource_class_path = "admin/#{resource_type}s" list_default_fields = %w[id value data.client_subject_scopes created_at] when :contact list_default_fields = %w[source_type source_id name email] # list_default_query = {'include_only_user_personal_contacts' => true} if @scope == Api::AoC::Scope::USER when :dropbox require_workspace_id = true resource_class_path = "#{resource_type}es" when :group_membership list_default_fields = %w[id group_id member_type member_id] when :kms_profile resource_class_path = "integrations/#{resource_type}s" when :node list_default_fields = %w[id name host access_key] supported_operations += %i[do bearer_token] when :operation list_default_fields = nil when :organization, :self supported_operations = Operations::SINGLETON resource_instance_path = resource_class_path = resource_type when :short_link list_default_fields = %w[id short_url data.url_token_data.purpose password_enabled password_protected updated_by_user_id updated_at] when :user list_default_fields = %w[id name email] supported_operations += %i[preferences notifications] when :workspace supported_operations += %i[shared_folder dropbox] when :workspace_membership list_default_fields = %w[id workspace_id member_type member_id] end command = .get_next_command(supported_operations) # Require identifier for non global commands if (supported_operations != Operations::SINGLETON) && !Operations::GLOBAL.include?(command) res_id = get_resource_id_from_args(resource_class_path) resource_instance_path = "#{resource_class_path}/#{res_id}" end case command when :create id_result = 'id' id_result = 'token' if resource_class_path.eql?('admin/client_registration_tokens') # TODO: report inconsistency: creation url is !=, and does not return id. resource_class_path = 'admin/client_registration/token' if resource_class_path.eql?('admin/client_registration_tokens') workspace_id = aoc_api.workspace_info[:id] if require_workspace_id return do_bulk_operation(command: command, descr: 'creation data', id_result: id_result) do |params| params['workspace_id'] = workspace_id if require_workspace_id && workspace_id && !params.key?('workspace_id') aoc_api.create(resource_class_path, params) end when :list return result_list(resource_class_path, fields: list_default_fields, default_query: list_default_query) when :show object = aoc_api.read(resource_instance_path, query_read_delete) return Main.result_single_object(object, fields: Formatter.all_but('certificate')) when :modify changes = .get_next_argument('properties', validation: Hash) return do_bulk_operation(command: command, values: res_id) do |one_id| aoc_api.update("#{resource_class_path}/#{one_id}", changes) {'id' => one_id} end when :delete return do_bulk_operation(command: command, values: res_id) do |one_id| aoc_api.delete("#{resource_class_path}/#{one_id}") {'id' => one_id} end when :set_pub_key # special : reads private and generate public the_private_key = .get_next_argument('private_key PEM value', validation: String) the_public_key = OpenSSL::PKey::RSA.new(the_private_key).public_key.to_s aoc_api.update(resource_instance_path, {jwt_grant_enabled: true, public_key: the_public_key}) return Main.result_success when :do command_repo = .get_next_command(FILES_COMMANDS) return execute_nodegen4_command(command_repo, res_id, scope: Api::Node::Scope::ADMIN) when :bearer_token node_api = aoc_api.node_api_from( node_id: res_id, scope: .get_next_argument('scope', default: Api::Node::Scope::ADMIN) ) return Main.result_text(node_api.oauth.) when :dropbox command_shared = .get_next_command(%i[list]) case command_shared when :list query = .get_option(:query) || {} res_data = aoc_api.read('dropboxes', query.merge({'workspace_id'=>res_id})) return Main.result_object_list(res_data, fields: %w[id name description]) end when :shared_folder query = .get_option(:query) || Api::AoC.workspace_access(res_id).merge({'admin' => true}) shared_folders = aoc_api.read_with_paging("#{resource_instance_path}/permissions", query)[:items] # inside a workspace command_shared = .get_next_command(%i[list member]) case command_shared when :list return Main.result_object_list(shared_folders, fields: %w[id node_name node_id file_id file.path tags.aspera.files.workspace.share_as]) when :member shared_folder_id = .instance_identifier shared_folder = shared_folders.find{ |i| i['id'].eql?(shared_folder_id)} Aspera.assert(shared_folder) command_shared_member = .get_next_command(%i[list]) case command_shared_member when :list node_api = aoc_api.node_api_from( node_id: shared_folder['node_id'], workspace_id: res_id, workspace_name: nil, scope: Api::Node::Scope::USER ) result = node_api.read( 'permissions', {'file_id' => shared_folder['file_id'], 'tag' => "aspera.files.workspace.id=#{res_id}"} ) result.each do |item| item['member'] = begin if Api::AoC.workspace_access?(item) {'name'=>'[Internal permission]'} else aoc_api.read("admin/#{item['access_type']}s/#{item['access_id']}") rescue {'name': 'not found'} end rescue => e {'name'=>e.to_s} end end # TODO : read users and group name and add, if query "include_members" return Main.result_object_list(result, fields: %w[access_type access_id access_level last_updated_at member.name member.email member.system_group_type member.system_group]) end end when :preferences, :notifications user_preferences_res = "#{resource_instance_path}/#{command.eql?(:preferences) ? 'user_interaction_preferences' : 'notification_preferences'}" case .get_next_command(%i[show modify]) when :show return Main.result_single_object(aoc_api.read(user_preferences_res)) when :modify aoc_api.update(user_preferences_res, .get_next_argument('properties', validation: Hash)) return Main.result_status('modified') end else Aspera.error_unexpected_value(command) end end |
#get_resource_id_from_args(resource_class_path) ⇒ Object
Get resource identifier from command line, either directly specifying the ‘id` or from `name` (percent selector).
290 291 292 293 294 295 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 290 def get_resource_id_from_args(resource_class_path) return .instance_identifier do |field, value| Aspera.assert(field.eql?('name'), type: BadArgument){'only selection by name is supported'} aoc_api.lookup_with_q(resource_class_path, value: value)['id'] end end |
#get_resource_path_from_args(resource_class_path) ⇒ Object
Get resource path from command line
298 299 300 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 298 def get_resource_path_from_args(resource_class_path) return "#{resource_class_path}/#{get_resource_id_from_args(resource_class_path)}" end |
#list_all_packages_with_query ⇒ Hash
List all packages according to ‘query` option.
334 335 336 337 338 339 340 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 334 def list_all_packages_with_query query = query_read_delete(default: {}) Aspera.assert_type(query, Hash){'query'} PACKAGE_RECEIVED_BASE_QUERY.each{ |k, v| query[k] = v unless query.key?(k)} resolve_dropbox_name_default_ws_id(query) return aoc_api.read_with_paging('packages', query.compact) end |
#package_persistency ⇒ Object
Returns persistency object if option ‘once_only` is used.
951 952 953 954 955 956 957 958 959 960 961 962 963 964 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 951 def package_persistency return unless .get_option(:once_only, mandatory: true) # TODO: add query info to id PersistencyActionOnce.new( manager: persistency, data: [], id: IdGenerator.from_list( 'aoc_recv', .get_option(:url, mandatory: true), aoc_api.workspace_info[:id], aoc_api.additional_persistence_ids ) ) end |
#reject_packages_from_persistency(all_packages, skip_ids_persistency) ⇒ Object
966 967 968 969 970 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 966 def reject_packages_from_persistency(all_packages, skip_ids_persistency) return if skip_ids_persistency.nil? skip_package = skip_ids_persistency.data.to_h{ |i| [i, true]} all_packages.reject!{ |pkg| skip_package[pkg['id']]} end |
#resolve_dropbox_name_default_ws_id(query) ⇒ Object
Translates ‘dropbox_name` to `dropbox_id` and fills current workspace_id
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 319 def resolve_dropbox_name_default_ws_id(query) if query.key?('dropbox_name') # convenience: specify name instead of id raise BadArgument, 'Use field dropbox_name or dropbox_id, not both' if query.key?('dropbox_id') # TODO : craft a query that looks for dropbox only in current workspace query['dropbox_id'] = aoc_api.lookup_with_q('dropboxes', value: query.delete('dropbox_name'))['id'] end workspace_id_hash(query, string: true) # by default show dropbox packages only for dropboxes query['exclude_dropbox_packages'] = !query.key?('dropbox_id') unless query.key?('exclude_dropbox_packages') end |
#result_list(resource_class_path, fields: nil, base_query: {}, default_query: {}) {|query| ... } ⇒ Object
List all entities, given additional, default and user’s queries
308 309 310 311 312 313 314 315 316 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 308 def result_list(resource_class_path, fields: nil, base_query: {}, default_query: {}) Aspera.assert_type(base_query, Hash) Aspera.assert_type(default_query, Hash) query = query_read_delete(default: default_query) # caller may add specific modifications or checks to query yield(query) if block_given? result = aoc_api.read_with_paging(resource_class_path, base_query.merge(query).compact) return Main.result_object_list(result[:items], fields: fields, total: result[:total]) end |
#short_link_command(**shared_data, &perm_block) ⇒ Object
Create a short link for the given entity: Shared folder or Shared Inbox Short link entity: ‘short_links` have:
-
a numerical id, e.g. ‘764412`
-
a resource type, e.g. ‘UrlToken`
-
a resource id, e.g. ‘scQ7uXPbvQ`
-
a short URL path, e.g. ‘dxyRpT9`
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 829 def short_link_command(**shared_data, &perm_block) link_type = .get_next_argument('link access (public or private)', accept_list: %i[public private]) if shared_data.keys.sort == %i[dropbox_id name] # Packages app, Shared inbox token_purpose = 'send_package_to_dropbox' short_link_purpose = link_type.eql?(:public) ? 'send_package_to_dropbox' : 'shared_folder_auth_link' elsif shared_data.keys.sort == %i[file_id node_id] # Files app, Shared folder token_purpose = 'view_shared_file' short_link_purpose = link_type.eql?(:public) ? 'token_auth_redirection' : 'shared_folder_auth_link' else Aspera.error_unexpected_value(shared_data.keys) end command = .get_next_command(%i[create delete list show] + (link_type.eql?(:public) ? %i[modify] : [])) case command when :create # Add workspace id workspace_id_hash(shared_data) create_payload = { purpose: short_link_purpose, user_selected_name: nil } case link_type when :private create_payload[:data] = shared_data when :public create_payload[:expires_at] = nil create_payload[:password_enabled] = false shared_data[:name] = '' create_payload[:data] = { aoc: true, url_token_data: { data: shared_data, purpose: token_purpose } } end custom_data = value_create_modify(command: command, default: {}) access_levels = custom_data.delete('access_levels') if (pass = custom_data.delete('password')) create_payload[:data][:url_token_data][:password] = pass create_payload[:password_enabled] = true end create_payload.deep_merge!(custom_data) result_create_short_link = aoc_api.create('short_links', create_payload) # Creation: perm_block: permission on node yield(:create, result_create_short_link['resource_id'], access_levels) if block_given? && link_type.eql?(:public) return Main.result_single_object(result_create_short_link) when :delete, :list, :show, :modify workspace_id_hash(shared_data) query = if link_type.eql?(:private) shared_data else { url_token_data: { data: shared_data, purpose: token_purpose } } end list_params = { json_query: query.to_json, purpose: short_link_purpose, edit_access: true, # embed: 'updated_by_user', sort: '-created_at' } short_list = aoc_api.read_with_paging('short_links', list_params.merge(query_read_delete(default: {})).compact) case command when :delete one_id = .instance_identifier(description: 'short link id') if link_type.eql?(:public) found = short_list[:items].find{ |item| item['id'].eql?(one_id)} raise BadIdentifier.new('Short link', one_id) if found.nil? yield(:delete, found['resource_id'], nil) end aoc_api.delete("short_links/#{one_id}", { edit_access: true, json_query: shared_data.to_json }) return Main.result_status('deleted') when :list return Main.result_object_list(short_list[:items], fields: Formatter.all_but('data'), total: short_list[:total]) when :show one_id = .instance_identifier(description: 'short link id') found = short_list[:items].find{ |item| item['id'].eql?(one_id)} raise BadIdentifier.new('Short link', one_id) if found.nil? return Main.result_single_object(found, fields: Formatter.all_but('data')) when :modify one_id = .instance_identifier(description: 'short link id') node_file = shared_data.slice(:node_id, :file_id) modify_payload = { edit_access: true, json_query: node_file } custom_data = value_create_modify(command: command) if (pass = custom_data.delete('password')) modify_payload[:password_enabled] = true modify_payload[:data] = { url_token_data: { password: pass, data: node_file } } else modify_payload[:password_enabled] = false end if custom_data.delete('access_levels') # Modification: perm_block: permission on node found = short_list[:items].find{ |item| item['id'].eql?(one_id)} raise BadIdentifier.new('Short link', one_id) if found.nil? yield(:update, found['resource_id'], access_levels) end modify_payload.deep_merge!(custom_data) aoc_api.update("short_links/#{one_id}", modify_payload) return Main.result_status('modified') end else Aspera.error_unexpected_value(command) end end |
#wizard(wizard, app_url) ⇒ Hash
Returns :preset_value, :test_args.
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 160 161 162 163 164 165 166 167 168 169 170 171 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 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 119 def wizard(wizard, app_url) pub_link_info = Api::AoC.link_info(app_url) # public link case if pub_link_info.key?(:token) pub_api = Rest.new(base_url: "https://#{URI.parse(pub_link_info[:url]).host}/api/v1") pub_info = pub_api.read('env/url_token_check', {token: pub_link_info[:token]}) preset_value = { link: app_url } preset_value[:password] = .get_option(:password, mandatory: true) if pub_info['password_protected'] return { preset_value: preset_value, test_args: 'organization' } end .declare(:use_generic_client, 'Wizard: AoC: use global or org specific jwt client id', allowed: Allowed::TYPES_BOOLEAN, default: Api::AoC.saas_url?(app_url)) . # make username mandatory for jwt, this triggers interactive input wiz_username = .get_option(:username, mandatory: true) wizard.check_email(wiz_username) # Set the pub key and jwt tag in the user's profile automatically auto_set_pub_key = false auto_set_jwt = false # use browser authentication to bootstrap use_browser_authentication = false private_key_path = wizard.ask_private_key( user: wiz_username, url: app_url, page: '👤 → Account Settings → Profile → Public Key' ) client_id = .get_option(:client_id) client_secret = .get_option(:client_secret) if client_id.nil? || client_secret.nil? if .get_option(:use_generic_client) client_id = client_secret = nil formatter.display_status('Using global client_id.') else formatter.display_status('Using organization specific client_id.') formatter.display_status('Please login to your Aspera on Cloud instance.'.red) formatter.display_status('Navigate to: 𓃑 → Admin → Integrations → API Clients') formatter.display_status('Check or create in integration:') formatter.display_status('- name: cli') formatter.display_status("- redirect uri: #{REDIRECT_LOCALHOST}") formatter.display_status('- origin: localhost') formatter.display_status('Use the generated client id and secret in the following prompts.'.red) Environment.instance.open_uri("#{app_url}/admin/integrations/api-clients") client_id = .get_option(:client_id, mandatory: true) client_secret = .get_option(:client_secret, mandatory: true) # use_browser_authentication = true end end if use_browser_authentication formatter.display_status('We will use web authentication to bootstrap.') auto_set_pub_key = true auto_set_jwt = true Aspera.error_not_implemented # aoc_api.oauth.grant_method = :web # aoc_api.oauth.scope = Api::AoC::Scope::ADMIN # aoc_api.oauth.specific_parameters[:redirect_uri] = REDIRECT_LOCALHOST end myself = aoc_api.read('self') if auto_set_pub_key Aspera.assert(myself['public_key'].empty?, type: Error){'Public key is already set in profile (use --override=yes)'} unless option_override formatter.display_status('Updating profile with the public key.') aoc_api.update("users/#{myself['id']}", {'public_key' => pub_key_pem}) end if auto_set_jwt formatter.display_status('Enabling JWT for client') aoc_api.update("clients/#{.get_option(:client_id)}", {'jwt_grant_enabled' => true, 'explicit_authorization_required' => false}) end return { preset_value: { url: app_url, username: myself['email'], auth: :jwt.to_s, private_key: "@file:#{private_key_path}", client_id: client_id, client_secret: client_secret }.compact, test_args: 'user profile show' } end |
#workspace_id_hash(hash = nil, string: false, name: false) ⇒ Hash{Symbol, String => String}
The key type (String or Symbol) depends on the string parameter.
Generate or update Hash with workspace id and name (option), if not already set
274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/aspera/cli/plugins/aoc.rb', line 274 def workspace_id_hash(hash = nil, string: false, name: false) info = aoc_api.workspace_info hash = {} if hash.nil? fields = %i[id] fields.push(:name) if name fields.each do |i| k = "workspace_#{i}" k = k.to_sym unless string hash[k] = info[i] unless info[i].nil? || hash.key?(k) end return hash end |