Class: Jerakia::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(request, opts) ⇒ Schema

Arguments: request(Jerakia::Request), opts(Hash)


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
58
59
60
# File 'lib/jerakia/schema.rb', line 4

def initialize(request, opts)
  schema_datasource = datasource(opts)
  schema_request = Jerakia::Request.new(
    :metadata   => request.,
    :key        => request.key,
    :namespace  => request.namespace,
    :use_schema => false
  )

  Jerakia.log.debug("Schema lookup invoked for #{request.key} namespace: #{request.namespace}")

  begin
    schema_policy = Jerakia::Launcher.evaluate do
      policy :schema do
        lookup :schema do
          datasource *schema_datasource
        end
      end
    end
    schema_lookup = schema_policy.run(schema_request)
  rescue Jerakia::Error => e
    raise Jerakia::SchemaError, "Schema lookup for #{request.key} failed: #{e.message}"
  end

  @schema_data = schema_lookup.payload || {}

  # Validate the returned data from the schema
  raise Jerakia::SchemaError, "Schema must return a hash for key #{request.key}" unless @schema_data.is_a?(Hash)

  valid_opts = %w(alias cascade merge)
  @schema_data.keys.each do |key|
    unless valid_opts.include?(key)
      raise Jerakia::SchemaError, "Unknown schema option #{key} for key #{request.key}"
    end
  end

  Jerakia.log.debug("Schema returned #{@schema_data}")

  if salias = @schema_data['alias']
    Jerakia.log.debug("Schema alias found to #{@schema_data['alias']}")
    request.namespace = Array(salias['namespace']) if salias['namespace']
    request.key = salias['key'] if salias['key']
  end

  if @schema_data['cascade']
    Jerakia.log.debug("Overriding lookup_type from #{request.lookup_type} to :cascade")
    request.lookup_type = :cascade
  end

  if @schema_data['merge']
    if %w(array hash deep_hash).include?(@schema_data['merge'])
      request.merge = @schema_data['merge'].to_sym
    else
      raise Jerakia::SchemaError, "Invalid merge type #{@schema_data['merge']} found in schema for key #{request.key}"
    end
  end
end

Instance Method Details

#datasource(opts = {}) ⇒ Object


62
63
64
65
66
67
68
69
70
71
# File 'lib/jerakia/schema.rb', line 62

def datasource(opts = {})
  [
    :file, {
      :docroot        => opts['docroot'] || '/var/lib/jerakia/schema',
      :format         => opts['format']  || :json,
      :enable_caching => opts['enable_caching'] || true,
      :searchpath     => ['']
    }
  ]
end