Fix up keep_disks/ping and add tests. refs #1581
[arvados.git] / services / api / app / controllers / arvados / v1 / nodes_controller.rb
1 class Arvados::V1::NodesController < ApplicationController
2   skip_before_filter :require_auth_scope_all, :only => :ping
3
4   def create
5     @object = Node.new
6     @object.save!
7     @object.start!(lambda { |h| arvados_v1_ping_node_url(h) })
8     show
9   end
10
11   def self._ping_requires_parameters
12     { ping_secret: true }
13   end
14   def ping
15     @object.ping({ ip: params[:local_ipv4] || request.env['REMOTE_ADDR'],
16                    ping_secret: params[:ping_secret],
17                    ec2_instance_id: params[:instance_id] })
18     if @object.info[:ping_secret] == params[:ping_secret]
19       render json: @object.as_api_response(:superuser)
20     else
21       raise "Invalid ping_secret after ping"
22     end
23   end
24
25   def find_objects_for_index
26     if current_user.andand.is_admin || !current_user.andand.is_active
27       super
28     else
29       # active non-admin users can list nodes that are (or were
30       # recently) working
31       @objects = model_class.where('last_ping_at >= ?', Time.now - 1.hours)
32     end
33   end
34 end