Class: Puppet::ResourceApi::TypeDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/resource_api/type_definition.rb

Overview

Provides accessor methods for the type being provided

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ TypeDefinition

Returns a new instance of TypeDefinition.

Raises:

  • (Puppet::DevError)


5
6
7
8
# File 'lib/puppet/resource_api/type_definition.rb', line 5

def initialize(definition)
  raise Puppet::DevError, 'TypeDefinition requires definition to be a Hash' unless definition.is_a?(Hash)
  @definition = definition
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



3
4
5
# File 'lib/puppet/resource_api/type_definition.rb', line 3

def definition
  @definition
end

Instance Method Details

#attributesObject



14
15
16
# File 'lib/puppet/resource_api/type_definition.rb', line 14

def attributes
  @definition[:attributes]
end

#check_schema(resource) ⇒ Object

validates a resource hash against its type schema



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
65
66
67
68
69
70
71
# File 'lib/puppet/resource_api/type_definition.rb', line 40

def check_schema(resource)
  namevars.each do |namevar|
    if resource[namevar].nil?
      raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar}` namevar attribute"
    end
  end

  message = "Provider returned data that does not match the Type Schema for `#{name}[#{resource[namevars.first]}]`"

  rejected_keys = check_schema_keys(resource) # removes bad keys
  bad_values = check_schema_values(resource)

  unless rejected_keys.empty?
    message += "\n Unknown attribute:\n"
    rejected_keys.each { |key, _value| message += "    * #{key}\n" }
  end
  unless bad_values.empty?
    message += "\n Value type mismatch:\n"
    bad_values.each { |key, value| message += "    * #{key}: #{value}\n" }
  end

  return if rejected_keys.empty? && bad_values.empty?

  if Puppet.settings[:strict] == :off
    Puppet.debug(message)
  elsif Puppet.settings[:strict] == :warning
    Puppet::ResourceApi.warning_count += 1
    Puppet.warning(message) if Puppet::ResourceApi.warning_count <= 100 # maximum number of schema warnings to display in a run
  elsif Puppet.settings[:strict] == :error
    raise Puppet::DevError, message
  end
end

#check_schema_keys(resource) ⇒ Object

Returns an array of keys that where not found in the type schema Modifies the resource passed in, leaving only valid attributes



75
76
77
78
79
# File 'lib/puppet/resource_api/type_definition.rb', line 75

def check_schema_keys(resource)
  rejected = []
  resource.reject! { |key| rejected << key if key != :title && attributes.key?(key) == false }
  rejected
end

#check_schema_values(resource) ⇒ Object

Returns a hash of keys and values that are not valid does not modify the resource passed in



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/resource_api/type_definition.rb', line 83

def check_schema_values(resource)
  bad_vals = {}
  resource.each do |key, value|
    next unless attributes[key]
    type = Puppet::ResourceApi::DataTypeHandling.parse_puppet_type(
      key,
      attributes[key][:type],
    )
    error_message = Puppet::ResourceApi::DataTypeHandling.try_validate(
      type,
      value,
      '',
    )
    bad_vals[key] = value unless error_message.nil?
  end
  bad_vals
end

#ensurable?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/puppet/resource_api/type_definition.rb', line 18

def ensurable?
  @definition[:attributes].key?(:ensure)
end

#feature?(feature) ⇒ Boolean

rubocop complains when this is named has_feature?

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'lib/puppet/resource_api/type_definition.rb', line 29

def feature?(feature)
  supported = (definition[:features] && definition[:features].include?(feature))
  if supported
    Puppet.debug("#{definition[:name]} supports `#{feature}`")
  else
    Puppet.debug("#{definition[:name]} does not support `#{feature}`")
  end
  supported
end

#nameObject



10
11
12
# File 'lib/puppet/resource_api/type_definition.rb', line 10

def name
  @definition[:name]
end

#namevarsObject



22
23
24
25
26
# File 'lib/puppet/resource_api/type_definition.rb', line 22

def namevars
  @namevars ||= @definition[:attributes].select { |_name, options|
    options.key?(:behaviour) && options[:behaviour] == :namevar
  }.keys
end