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.



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

def initialize(definition)
  @data_type_cache = {}
  validate_schema(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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puppet/resource_api/type_definition.rb', line 87

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



122
123
124
125
126
# File 'lib/puppet/resource_api/type_definition.rb', line 122

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/puppet/resource_api/type_definition.rb', line 130

def check_schema_values(resource)
  bad_vals = {}
  resource.each do |key, value|
    next unless attributes[key]
    type = @data_type_cache[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

#validate_schema(definition) ⇒ Object

Raises:

  • (Puppet::DevError)


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet/resource_api/type_definition.rb', line 39

def validate_schema(definition)
  raise Puppet::DevError, 'Type definition must be a Hash, not `%{other_type}`' % { other_type: definition.class } unless definition.is_a?(Hash)
  raise Puppet::DevError, 'Type definition must have a name' unless definition.key? :name
  raise Puppet::DevError, 'Type definition must have `:attributes`' unless definition.key? :attributes
  unless definition[:attributes].is_a?(Hash)
    raise Puppet::DevError, '`%{name}.attributes` must be a hash, not `%{other_type}`' % {
      name: definition[:name], other_type: definition[:attributes].class
    }
  end
  [:title, :provider, :alias, :audit, :before, :consume, :export, :loglevel, :noop, :notify, :require, :schedule, :stage, :subscribe, :tag].each do |name|
    raise Puppet::DevError, 'must not define an attribute called `%{name}`' % { name: name.inspect } if definition[:attributes].key? name
  end
  if definition.key?(:title_patterns) && !definition[:title_patterns].is_a?(Array)
    raise Puppet::DevError, '`:title_patterns` must be an array, not `%{other_type}`' % { other_type: definition[:title_patterns].class }
  end

  Puppet::ResourceApi::DataTypeHandling.validate_ensure(definition)

  definition[:features] ||= []
  supported_features = %w[supports_noop canonicalize remote_resource simple_get_filter].freeze
  unknown_features = definition[:features] - supported_features
  Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty?

  definition[:attributes].each do |key, attr|
    raise Puppet::DevError, "`#{definition[:name]}.#{key}` must be a Hash, not a #{attr.class}" unless attr.is_a? Hash
    raise Puppet::DevError, "`#{definition[:name]}.#{key}` has no type" unless attr.key? :type
    Puppet.warning("`#{definition[:name]}.#{key}` has no docs") unless attr.key? :desc

    # validate the type by attempting to parse into a puppet type
    @data_type_cache[definition[:attributes][key][:type]] ||=
      Puppet::ResourceApi::DataTypeHandling.parse_puppet_type(
        key,
        definition[:attributes][key][:type],
      )

    # fixup any weird behavior  ;-)
    next unless attr[:behavior]
    if attr[:behaviour]
      raise Puppet::DevError, "the '#{key}' attribute has both a `behavior` and a `behaviour`, only use one"
    end
    attr[:behaviour] = attr[:behavior]
    attr.delete(:behavior)
  end
  # store the validated definition
  @definition = definition
end