Modify API discovery document and CLI tool so resource objects can be
[arvados.git] / services / api / app / controllers / arvados / v1 / virtual_machines_controller.rb
1 class Arvados::V1::VirtualMachinesController < ApplicationController
2   skip_before_filter :find_object_by_uuid, :only => :get_all_logins
3   skip_before_filter(:require_auth_scope_all,
4                      :only => [:logins, :get_all_logins])
5   before_filter(:admin_required,
6                 :only => [:logins, :get_all_logins])
7   before_filter(:require_auth_scope_for_get_all_logins,
8                 :only => [:logins, :get_all_logins])
9
10   def logins
11     get_all_logins
12   end
13
14   def get_all_logins
15     @users = {}
16     User.includes(:authorized_keys).all.each do |u|
17       @users[u.uuid] = u
18     end
19     @response = []
20     @vms = VirtualMachine.includes(:login_permissions)
21     if @object
22       @vms = @vms.where('uuid=?', @object.uuid)
23     else
24       @vms = @vms.all
25     end
26     @vms.each do |vm|
27       vm.login_permissions.each do |perm|
28         user_uuid = perm.tail_uuid
29         @users[user_uuid].andand.authorized_keys.andand.each do |ak|
30           username = perm.properties.andand['username']
31           if username
32             @response << {
33               username: username,
34               hostname: vm.hostname,
35               public_key: ak.public_key,
36               user_uuid: user_uuid,
37               virtual_machine_uuid: vm.uuid,
38               authorized_key_uuid: ak.uuid
39             }
40           end
41         end
42       end
43     end
44     render json: { kind: "arvados#HashList", items: @response }
45   end
46
47   protected
48
49   def require_auth_scope_for_get_all_logins
50     if @object
51       # Client wants all logins for a single VM.
52       require_auth_scope(['all', arvados_v1_virtual_machine_url(@object.uuid)])
53     else
54       # ...for a non-existent VM, or all VMs.
55       require_auth_scope(['all'])
56     end
57   end
58 end