Class: Longleaf::StorageLocationValidator

Inherits:
ConfigurationValidator show all
Defined in:
lib/longleaf/services/storage_location_validator.rb

Overview

Validates application configuration of storage locations

Class Method Summary collapse

Methods inherited from ConfigurationValidator

assert

Class Method Details

.assert_path_property_valid(name, path_prop, properties, existing_paths) ⇒ Object (private)



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
58
59
# File 'lib/longleaf/services/storage_location_validator.rb', line 32

def self.assert_path_property_valid(name, path_prop, properties, existing_paths)
  path = properties[path_prop]
  begin
    StoragePathValidator::validate(path)
  rescue InvalidStoragePathError => err
    raise ConfigurationError.new(
        "Storage location '#{name}' specifies invalid '#{path_prop}' property: #{err.message}")
  end
  assert("Storage location '#{name}' must specify a '#{path_prop}' property", !path.nil? && !path.empty?)
  assert("Storage location '#{name}' must specify an absolute path for property '#{path_prop}'",
      Pathname.new(path).absolute? && !path.include?('/..'))
  assert("Storage location '#{name}' specifies a '#{path_prop}' directory which does not exist", Dir.exist?(path))

  # Ensure paths have trailing slash to avoid matching on partial directory names
  path += '/' unless path.end_with?('/')
  # Verify that the (metadata_)path property's value is not inside of another storage location or vice versa
  existing_paths.each do |existing|
    if existing.start_with?(path) || path.start_with?(existing)
      msg = "Location '#{name}' defines property #{path_prop} with value '#{path}'" \
            " which overlaps with another configured path '#{existing}'." \
            " Storage locations must not define #{AF::LOCATION_PATH} or #{AF::METADATA_PATH}" \
            " properties which are contained by another location property"
      raise ConfigurationError.new(msg)
    end
  end

  existing_paths << path
end

.validate_config(config) ⇒ Object

Validates configuration to ensure that it is syntactically correct and does not violate schema and uniqueness requirements.

Parameters:

  • config (Hash)

    hash containing the application configuration



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/longleaf/services/storage_location_validator.rb', line 16

def self.validate_config(config)
  assert("Configuration must be a hash, but a #{config.class} was provided", config.class == Hash)
  assert("Configuration must contain a root '#{AF::LOCATIONS}' key", config.key?(AF::LOCATIONS))
  locations = config[AF::LOCATIONS]
  assert("'#{AF::LOCATIONS}' must be a hash of locations", locations.class == Hash)

  existing_paths = Array.new
  locations.each do |name, properties|
    assert("Name of storage location must be a string, but was of type #{name.class}", name.instance_of?(String))
    assert("Storage location '#{name}' must be a hash, but a #{properties.class} was provided", properties.is_a?(Hash))

    assert_path_property_valid(name, AF::LOCATION_PATH, properties, existing_paths)
    assert_path_property_valid(name, AF::METADATA_PATH, properties, existing_paths)
  end
end