Class: Puppet::ResourceApi::TypeDefinition
- Inherits:
-
Object
- Object
- Puppet::ResourceApi::TypeDefinition
- Defined in:
- lib/puppet/resource_api/type_definition.rb
Overview
Provides accessor methods for the type being provided
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#check_schema(resource) ⇒ Object
validates a resource hash against its type schema.
-
#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.
-
#check_schema_values(resource) ⇒ Object
Returns a hash of keys and values that are not valid does not modify the resource passed in.
- #ensurable? ⇒ Boolean
-
#feature?(feature) ⇒ Boolean
rubocop complains when this is named has_feature?.
-
#initialize(definition) ⇒ TypeDefinition
constructor
A new instance of TypeDefinition.
- #name ⇒ Object
- #namevars ⇒ Object
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) raise Puppet::DevError, 'TypeDefinition requires definition to be a Hash' unless definition.is_a?(Hash) @definition = definition end |
Instance Attribute Details
#definition ⇒ Object (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
#attributes ⇒ Object
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 = "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? += "\n Unknown attribute:\n" rejected_keys.each { |key, _value| += " * #{key}\n" } end unless bad_values.empty? += "\n Value type mismatch:\n" bad_values.each { |key, value| += " * #{key}: #{value}\n" } end return if rejected_keys.empty? && bad_values.empty? if Puppet.settings[:strict] == :off Puppet.debug() elsif Puppet.settings[:strict] == :warning Puppet::ResourceApi.warning_count += 1 Puppet.warning() if Puppet::ResourceApi.warning_count <= 100 # maximum number of schema warnings to display in a run elsif Puppet.settings[:strict] == :error raise Puppet::DevError, 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], ) = Puppet::ResourceApi::DataTypeHandling.try_validate( type, value, '', ) bad_vals[key] = value unless .nil? end bad_vals end |
#ensurable? ⇒ 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?
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 |
#name ⇒ Object
10 11 12 |
# File 'lib/puppet/resource_api/type_definition.rb', line 10 def name @definition[:name] end |
#namevars ⇒ Object
22 23 24 25 26 |
# File 'lib/puppet/resource_api/type_definition.rb', line 22 def namevars @namevars ||= @definition[:attributes].select { |_name, | .key?(:behaviour) && [:behaviour] == :namevar }.keys end |