Module: GClouder::Resources::Validate::Local

Includes:
Logging
Included in:
Global, Region
Defined in:
lib/gclouder/resources/validate/local.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#add, #bad, #change, #debug, #error, #fatal, #good, #info, log, loggers, #remove, report, #resource_state, setup, #warn, #warning

Class Method Details

.included(klass) ⇒ Object



9
10
11
# File 'lib/gclouder/resources/validate/local.rb', line 9

def self.included(klass)
  klass.extend Local
end

Instance Method Details

#has_required_keys?(instance, required_keys, ignore_keys, indent: 3) ⇒ Boolean

FIXME: this should probably recurse

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gclouder/resources/validate/local.rb', line 14

def has_required_keys?(instance, required_keys, ignore_keys, indent: 3)
  success = true

  required_keys["name"] = {
    "type" => "String",
    "required" => true
  }

  required_keys.each do |key, data|
    next if ignore_keys.include?(key)

    if !instance.key?(key)
      bad "#{key} is missing", indent: indent
      success = false
    end
  end

  success
end

#has_unknown_keys?(instance, permitted_keys, ignore_keys, indent: 0) ⇒ Boolean

Returns:



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
61
62
63
64
# File 'lib/gclouder/resources/validate/local.rb', line 34

def has_unknown_keys?(instance, permitted_keys, ignore_keys, indent: 0)
  success = false

  # a name is required for every resources
  permitted_keys["name"] = {
    "type" => "String",
    "required" => true
  }

  instance.each do |key, value|
    next if ignore_keys.include?(key)

    if !permitted_keys.key?(key)
      bad "#{key} is an invalid key", indent: 4 + indent
      success = true
      next
    end

    required_type = Object.const_get(permitted_keys[key]["type"])

    if !value.is_a?(required_type)
      bad "#{key} invalid type: #{value.class} (should be: #{required_type})", indent: 4 + indent
      success = true
      next
    end

    good "#{key} is a #{required_type} (#{value})", indent: 4 + indent
  end

  success
end