-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzip_endpoint.rb
57 lines (51 loc) · 2.76 KB
/
zip_endpoint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Metadata about a zip endpoint which stores zipped archives of version directories from Moab
# objects.
class ZipEndpoint < ApplicationRecord
has_many :zipped_moab_versions, dependent: :restrict_with_exception
has_and_belongs_to_many :preservation_policies
# @note Hash values cannot be modified without migrating any associated persisted data.
# @see [enum docs] http://api.rubyonrails.org/classes/ActiveRecord/Enum.html
# TODO: switch this to use plain strings representing the class name
enum delivery_class: {
S3WestDeliveryJob => 1,
S3EastDeliveryJob => 2
}
validates :endpoint_name, presence: true, uniqueness: true
# TODO: after switching to string, validate that input resolves to class which #is_a class of the right type?
validates :delivery_class, presence: true
# for the given druid, which zip endpoints should have archive copies, as per the preservation_policy?
scope :targets, lambda { |druid|
joins(preservation_policies: [:preserved_objects]).where(preserved_objects: { druid: druid })
}
# for a given druid, which zip endpoints have an archive copy of the given version?
scope :which_have_archive_copy, lambda { |druid, version|
joins(zipped_moab_versions: [:preserved_object])
.where(
preserved_objects: { druid: druid },
zipped_moab_versions: { version: version }
)
}
# for a given version of a druid, which zip endpoints need an archive copy, based on the governing pres policy?
scope :which_need_archive_copy, lambda { |druid, version|
targets(druid).where.not(id: which_have_archive_copy(druid, version))
}
# iterates over the zip endpoints enumerated in settings, creating a ZipEndpoint for each if one doesn't
# already exist.
# @param preservation_policies [Enumerable<PreservationPolicy>] the list of preservation policies
# which the newly created zip endpoints implement.
# @return [Array<ZipEndpoint>] the ZipEndpoint list for the zip endpoints defined in the config (all
# entries, including any entries that may have been seeded already)
# @note this adds new entries from the config, and leaves existing entries alone, but won't delete anything.
# TODO: figure out deletion/update based on config?
def self.seed_zip_endpoints_from_config(preservation_policies)
return unless Settings.zip_endpoints
Settings.zip_endpoints.map do |endpoint_name, endpoint_config|
find_or_create_by!(endpoint_name: endpoint_name.to_s) do |zip_endpoint|
zip_endpoint.endpoint_node = endpoint_config.endpoint_node
zip_endpoint.storage_location = endpoint_config.storage_location
zip_endpoint.preservation_policies = preservation_policies
zip_endpoint.delivery_class = delivery_classes[endpoint_config.delivery_class.constantize]
end
end
end
end