13561: Avoid old versions to be updated, with test.
[arvados.git] / services / api / app / models / keep_disk.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class KeepDisk < ArvadosModel
6   include HasUuid
7   include KindAndEtag
8   include CommonApiTemplate
9   before_validation :ensure_ping_secret
10
11   api_accessible :user, extend: :common do |t|
12     t.add :node_uuid
13     t.add :filesystem_uuid
14     t.add :bytes_total
15     t.add :bytes_free
16     t.add :is_readable
17     t.add :is_writable
18     t.add :last_read_at
19     t.add :last_write_at
20     t.add :last_ping_at
21     t.add :service_host
22     t.add :service_port
23     t.add :service_ssl_flag
24     t.add :keep_service_uuid
25   end
26   api_accessible :superuser, :extend => :user do |t|
27     t.add :ping_secret
28   end
29
30   def foreign_key_attributes
31     super.reject { |a| a == "filesystem_uuid" }
32   end
33
34   def ping(o)
35     raise "must have :service_host and :ping_secret" unless o[:service_host] and o[:ping_secret]
36
37     if o[:ping_secret] != self.ping_secret
38       logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.ping_secret}\""
39       return nil
40     end
41
42     @bypass_arvados_authorization = true
43     self.update_attributes!(o.select { |k,v|
44                              [:bytes_total,
45                               :bytes_free,
46                               :is_readable,
47                               :is_writable,
48                               :last_read_at,
49                               :last_write_at
50                              ].collect(&:to_s).index k
51                            }.merge(last_ping_at: db_current_time))
52   end
53
54   def service_host
55     KeepService.find_by_uuid(self.keep_service_uuid).andand.service_host
56   end
57
58   def service_port
59     KeepService.find_by_uuid(self.keep_service_uuid).andand.service_port
60   end
61
62   def service_ssl_flag
63     KeepService.find_by_uuid(self.keep_service_uuid).andand.service_ssl_flag
64   end
65
66   protected
67
68   def ensure_ping_secret
69     self.ping_secret ||= rand(2**256).to_s(36)
70   end
71
72   def permission_to_update
73     @bypass_arvados_authorization or super
74   end
75
76   def permission_to_create
77     current_user and current_user.is_admin
78   end
79 end