Merge branch '1578-api-server-in-docker'
[arvados.git] / services / api / app / models / keep_disk.rb
1 class KeepDisk < ArvadosModel
2   include AssignUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   before_validation :ensure_ping_secret
6
7   api_accessible :user, extend: :common do |t|
8     t.add :node_uuid
9     t.add :filesystem_uuid
10     t.add :ping_secret
11     t.add :bytes_total
12     t.add :bytes_free
13     t.add :is_readable
14     t.add :is_writable
15     t.add :last_read_at
16     t.add :last_write_at
17     t.add :last_ping_at
18     t.add :service_host
19     t.add :service_port
20     t.add :service_ssl_flag
21   end
22
23   def ping(o)
24     raise "must have :service_host and :ping_secret" unless o[:service_host] and o[:ping_secret]
25
26     if o[:ping_secret] != self.ping_secret
27       logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.ping_secret}\""
28       return nil
29     end
30
31     @bypass_arvados_authorization = true
32     self.update_attributes(o.select { |k,v|
33                              [:service_host,
34                               :service_port,
35                               :service_ssl_flag,
36                               :bytes_total,
37                               :bytes_free,
38                               :is_readable,
39                               :is_writable,
40                               :last_read_at,
41                               :last_write_at
42                              ].collect(&:to_s).index k
43                            }.merge(last_ping_at: Time.now))
44   end
45
46   protected
47
48   def ensure_ping_secret
49     self.ping_secret ||= rand(2**256).to_s(36)
50   end
51
52   def permission_to_update
53     @bypass_arvados_authorization or super
54   end
55
56   def permission_to_create
57     current_user and current_user.is_admin
58   end
59 end