add Repositories resource, fix authorized_user attr name, some wb fixes
[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 :superuser, :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   end
19
20   def ping(o)
21     raise "must have :service_host and :ping_secret" unless o[:service_host] and o[:ping_secret]
22
23     if o[:ping_secret] != self.ping_secret
24       logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.ping_secret}\""
25       return nil
26     end
27
28     @bypass_arvados_authorization = true
29     self.update_attributes(o.select { |k,v|
30                              [:service_host,
31                               :service_port,
32                               :service_ssl_flag,
33                               :bytes_total,
34                               :bytes_free,
35                               :is_readable,
36                               :is_writable,
37                               :last_read_at,
38                               :last_write_at
39                              ].collect(&:to_s).index k
40                            }.merge(last_ping_at: Time.now))
41   end
42
43   protected
44
45   def ensure_ping_secret
46     self.ping_secret ||= rand(2**256).to_s(36)
47   end
48
49   def permission_to_update
50     @bypass_arvados_authorization or super
51   end
52
53   def permission_to_create
54     current_user and current_user.is_admin
55   end
56 end