X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/20f1e7eaf6053d52210002720e2b0a0c26e5169e..91dc5f1d7f5ad9eb2640f6089e2d0476cbf87c8e:/sdk/cli/bin/arv-tag diff --git a/sdk/cli/bin/arv-tag b/sdk/cli/bin/arv-tag index 67def8412d..816c1e9320 100755 --- a/sdk/cli/bin/arv-tag +++ b/sdk/cli/bin/arv-tag @@ -5,56 +5,89 @@ # arv tag remove tag1 [tag2 ...] --object obj_uuid1 [--object obj_uuid2 ...] # arv tag remove tag1 [tag2 ...] --all -def usage - abort "Usage:\n" + - "arv tag add tag1 [tag2 ...] --objects object_uuid1 [object_uuid2...]\n" + - "arv tag remove tag1 [tag2 ...] --objects object_uuid1 [object_uuid2...]\n" + +def usage_string + return "\nUsage:\n" + + "arv tag add tag1 [tag2 ...] --object object_uuid1 [object_uuid2...]\n" + + "arv tag remove tag1 [tag2 ...] --object object_uuid1 [object_uuid2...]\n" + "arv tag remove --all\n" end -def tag_add(tag, obj_uuid) - request_body = { - :api_token => ENV['ARVADOS_API_TOKEN'], - :link => { - :name => tag, - :link_class => :tag, - :head_uuid => obj_uuid, - } - } - - result = $client.execute(:api_method => $arvados.links.create, - :body => request_body, - :authenticated => false) +def usage + abort usage_string +end + +def api_call(method, parameters:{}, request_body:{}) + result = $client.execute(:api_method => method, + :parameters => parameters, + :body_object => request_body, + :authenticated => false, + :headers => { + authorization: "OAuth2 #{ENV['ARVADOS_API_TOKEN']}", + }) begin results = JSON.parse result.body rescue JSON::ParserError => e - $stderr.puts "Failed to parse server response:\n" + e.to_s - return nil + abort "Failed to parse server response:\n" + e.to_s end - if results["errors"] then - $stderr.puts "Error: #{results["errors"][0]}" - return nil + if results["errors"] + abort "Error: #{results["errors"][0]}" end return results end -def tag_remove(tag, obj_uuid=nil) - request_body = { - :api_token => ENV['ARVADOS_API_TOKEN'], - :link => { - :name => tag, - :link_class => :tag, - :head_uuid => obj_uuid, - } - } +def tag_add(tag, obj_uuid) + return api_call($arvados.links.create, + request_body: { + :link => { + :name => tag, + :link_class => :tag, + :head_uuid => obj_uuid, + } + }) +end + +def tag_remove(tag, obj_uuids=nil) + # If we got a list of objects to untag, look up the uuids for the + # links that need to be deleted. + link_uuids = [] + if obj_uuids + obj_uuids.each do |uuid| + link = api_call($arvados.links.list, + request_body: { + :where => { + :link_class => :tag, + :name => tag, + :head_uuid => uuid, + } + }) + if link['items_available'] > 0 + link_uuids.push link['items'][0]['uuid'] + end + end + else + all_tag_links = api_call($arvados.links.list, + request_body: { + :where => { + :link_class => :tag, + :name => tag, + } + }) + link_uuids = all_tag_links['items'].map { |obj| obj['uuid'] } + end - return $client.execute(:api_method => $arvados.links.destroy, - :parameters => params, - :body => request_body, - :authenticated => false) + results = [] + if link_uuids + link_uuids.each do |uuid| + results.push api_call($arvados.links.delete, parameters:{ :uuid => uuid }) + end + else + $stderr.puts "no tags found to remove" + end + + return results end if RUBY_VERSION < '1.9.3' then @@ -68,13 +101,15 @@ $arvados_api_host = ENV['ARVADOS_API_HOST'] or abort "#{$0}: fatal: ARVADOS_API_HOST environment variable not set." $arvados_api_token = ENV['ARVADOS_API_TOKEN'] or abort "#{$0}: fatal: ARVADOS_API_TOKEN environment variable not set." -$arvados_api_host_insecure = ENV['ARVADOS_API_HOST_INSECURE'] == 'yes' +$arvados_api_host_insecure = %w(1 true yes). + include?((ENV['ARVADOS_API_HOST_INSECURE'] || "").downcase) begin require 'rubygems' require 'google/api_client' require 'json' require 'pp' + require 'oj' require 'trollop' rescue LoadError abort <<-EOS @@ -119,7 +154,8 @@ class Google::APIClient end global_opts = Trollop::options do - banner "arvados cli client" + banner usage_string + banner "" opt :dry_run, "Don't actually do anything", :short => "-n" opt :verbose, "Print some things on stderr", :short => "-v" opt :uuid, "Return the UUIDs of the objects in the response, one per line (default)", :short => nil @@ -129,7 +165,7 @@ global_opts = Trollop::options do opt :yaml, "Return the response received from the API server, in YAML format", :short => "-y" stop_on ['add', 'remove'] end - + p = Trollop::Parser.new do opt(:all, "Remove this tag from all objects under your ownership. Only valid with `tag remove'.", @@ -159,6 +195,11 @@ $arvados = $client.discovered_api('arvados', $arvados_api_version) results = [] cmd = ARGV.shift + +if ARGV.empty? + usage +end + case cmd when 'add' ARGV.each do |tag| @@ -169,11 +210,9 @@ when 'add' when 'remove' ARGV.each do |tag| if $options[:all] then - results.push(tag_remove(tag)) + results.concat tag_remove(tag) else - $options[:object].each do |obj| - results.push(tag_remove(tag, obj)) - end + results.concat tag_remove(tag, $options[:object]) end end else @@ -188,10 +227,7 @@ elsif global_opts[:json] then puts Oj.dump(results) else results.each do |r| - next if r == nil - if r["items"] and r["kind"].match /list$/i - r['items'].each do |i| puts i['uuid'] end - elsif r['uuid'].nil? + if r['uuid'].nil? abort("Response did not include a uuid:\n" + Oj.dump(r, :indent => 1) + "\n")