Merge branch 'master' into 8703-job-components
[arvados.git] / services / api / app / models / keep_disk.rb
1 class KeepDisk < ArvadosModel
2   include HasUuid
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 :bytes_total
11     t.add :bytes_free
12     t.add :is_readable
13     t.add :is_writable
14     t.add :last_read_at
15     t.add :last_write_at
16     t.add :last_ping_at
17     t.add :service_host
18     t.add :service_port
19     t.add :service_ssl_flag
20     t.add :keep_service_uuid
21   end
22   api_accessible :superuser, :extend => :user do |t|
23     t.add :ping_secret
24   end
25
26   def foreign_key_attributes
27     super.reject { |a| a == "filesystem_uuid" }
28   end
29
30   def ping(o)
31     raise "must have :service_host and :ping_secret" unless o[:service_host] and o[:ping_secret]
32
33     if o[:ping_secret] != self.ping_secret
34       logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.ping_secret}\""
35       return nil
36     end
37
38     @bypass_arvados_authorization = true
39     self.update_attributes!(o.select { |k,v|
40                              [:bytes_total,
41                               :bytes_free,
42                               :is_readable,
43                               :is_writable,
44                               :last_read_at,
45                               :last_write_at
46                              ].collect(&:to_s).index k
47                            }.merge(last_ping_at: db_current_time))
48   end
49
50   def service_host
51     KeepService.find_by_uuid(self.keep_service_uuid).andand.service_host
52   end
53
54   def service_port
55     KeepService.find_by_uuid(self.keep_service_uuid).andand.service_port
56   end
57
58   def service_ssl_flag
59     KeepService.find_by_uuid(self.keep_service_uuid).andand.service_ssl_flag
60   end
61
62   protected
63
64   def ensure_ping_secret
65     self.ping_secret ||= rand(2**256).to_s(36)
66   end
67
68   def permission_to_update
69     @bypass_arvados_authorization or super
70   end
71
72   def permission_to_create
73     current_user and current_user.is_admin
74   end
75 end