Class: Puppet::ResourceApi::BaseTypeDefinition
- Inherits:
-
Object
- Object
- Puppet::ResourceApi::BaseTypeDefinition
- Defined in:
- lib/puppet/resource_api/type_definition.rb,
lib/puppet/resource_api/type_definition.rb
Overview
Base RSAPI schema Object
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Instance Method Summary collapse
-
#check_schema(resource, message_prefix = nil) ⇒ 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 No longer modifies the resource passed in.
-
#check_schema_values(resource) ⇒ Object
Returns a hash of keys and values that are not valid does not modify the resource passed in.
-
#initialize(definition, attr_key) ⇒ BaseTypeDefinition
constructor
A new instance of BaseTypeDefinition.
- #name ⇒ Object
- #namevars ⇒ Object
- #validate_schema(definition, attr_key) ⇒ Object
Constructor Details
#initialize(definition, attr_key) ⇒ BaseTypeDefinition
Returns a new instance of BaseTypeDefinition.
72 73 74 75 76 77 |
# File 'lib/puppet/resource_api/type_definition.rb', line 72 def initialize(definition, attr_key) @data_type_cache = {} validate_schema(definition, attr_key) # store the validated definition @definition = definition end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
70 71 72 |
# File 'lib/puppet/resource_api/type_definition.rb', line 70 def attributes @attributes end |
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
70 71 72 |
# File 'lib/puppet/resource_api/type_definition.rb', line 70 def definition @definition end |
Instance Method Details
#check_schema(resource, message_prefix = nil) ⇒ Object
validates a resource hash against its type schema
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/puppet/resource_api/type_definition.rb', line 123 def check_schema(resource, = nil) 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' if .nil? = "#{} for `#{name}[#{resource[namevars.first]}]`" rejected_keys = check_schema_keys(resource) 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 No longer modifies the resource passed in
159 160 161 162 163 |
# File 'lib/puppet/resource_api/type_definition.rb', line 159 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/puppet/resource_api/type_definition.rb', line 167 def check_schema_values(resource) bad_vals = {} resource.each do |key, value| next unless attributes[key] type = @data_type_cache[attributes[key][:type]] is_sensitive = (attributes[key].key?(:sensitive) && (attributes[key][:sensitive] == true)) = Puppet::ResourceApi::DataTypeHandling.try_validate( type, value, '', ) if is_sensitive bad_vals[key] = '<< redacted value >> ' + unless .nil? else bad_vals[key] = value unless .nil? end end bad_vals end |
#name ⇒ Object
79 80 81 |
# File 'lib/puppet/resource_api/type_definition.rb', line 79 def name definition[:name] end |
#namevars ⇒ Object
83 84 85 86 87 |
# File 'lib/puppet/resource_api/type_definition.rb', line 83 def namevars @namevars ||= attributes.select { |_name, | .key?(:behaviour) && [:behaviour] == :namevar }.keys end |
#validate_schema(definition, attr_key) ⇒ Object
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 119 120 |
# File 'lib/puppet/resource_api/type_definition.rb', line 89 def validate_schema(definition, attr_key) raise Puppet::DevError, '%{type_class} must be a Hash, not `%{other_type}`' % { type_class: self.class.name, other_type: definition.class } unless definition.is_a?(Hash) @attributes = definition[attr_key] raise Puppet::DevError, '%{type_class} must have a name' % { type_class: self.class.name } unless definition.key? :name raise Puppet::DevError, '%{type_class} must have `%{attr_key}`' % { type_class: self.class.name, attrs: attr_key } unless definition.key? attr_key unless attributes.is_a?(Hash) raise Puppet::DevError, '`%{name}.%{attrs}` must be a hash, not `%{other_type}`' % { name: definition[:name], attrs: attr_key, other_type: attributes.class } end 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[attributes[key][:type]] ||= Puppet::ResourceApi::DataTypeHandling.parse_puppet_type( key, 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 end |