Merge branch '7167-keep-rsync-test-setup' into 7167-keep-rsync
[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 :render_404_if_no_object, :only => :get_all_logins
4   before_filter(:admin_required,
5                 :only => [:logins, :get_all_logins])
6
7   # Get all login permissons (user uuid, login account, SSH key) for a
8   # single VM
9   def logins
10     render_logins_for VirtualMachine.where(uuid: @object.uuid)
11   end
12
13   # Get all login permissons for all VMs
14   def get_all_logins
15     render_logins_for VirtualMachine
16   end
17
18   protected
19
20   def render_logins_for vm_query
21     @response = []
22     @vms = vm_query.eager_load :login_permissions
23     @users = {}
24     User.eager_load(:authorized_keys).
25       where('users.uuid in (?)',
26             @vms.map { |vm| vm.login_permissions.map &:tail_uuid }.flatten.uniq).
27       each do |u|
28       @users[u.uuid] = u
29     end
30     @vms.each do |vm|
31       vm.login_permissions.each do |perm|
32         user_uuid = perm.tail_uuid
33         next if not @users[user_uuid]
34         next if perm.properties['username'].blank?
35         aks = @users[user_uuid].authorized_keys
36         if aks.empty?
37           # We'll emit one entry, with no public key.
38           aks = [nil]
39         end
40         aks.each do |ak|
41           @response << {
42             username: perm.properties['username'],
43             hostname: vm.hostname,
44             groups: (perm.properties['groups'].to_a rescue []),
45             public_key: ak ? ak.public_key : nil,
46             user_uuid: user_uuid,
47             virtual_machine_uuid: vm.uuid,
48             authorized_key_uuid: ak ? ak.uuid : nil,
49           }
50         end
51       end
52     end
53     send_json kind: "arvados#HashList", items: @response
54   end
55 end