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.
- #insyncable_attributes ⇒ Object
- #name ⇒ Object
- #namevars ⇒ Object
- #notify_schema_errors(message) ⇒ Object
- #validate_schema(definition, attr_key) ⇒ Object
Constructor Details
#initialize(definition, attr_key) ⇒ BaseTypeDefinition
118 119 120 121 122 123 |
# File 'lib/puppet/resource_api/type_definition.rb', line 118 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.
116 117 118 |
# File 'lib/puppet/resource_api/type_definition.rb', line 116 def attributes @attributes end |
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
116 117 118 |
# File 'lib/puppet/resource_api/type_definition.rb', line 116 def definition @definition end |
Instance Method Details
#check_schema(resource, message_prefix = nil) ⇒ Object
validates a resource hash against its type schema
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/puppet/resource_api/type_definition.rb', line 184 def check_schema(resource, = nil) namevars.each do |namevar| raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar}` namevar attribute" if resource[namevar].nil? end = 'Provider returned data that does not match the Type Schema' if .nil? = "#{message_prefix} 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? notify_schema_errors() 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
223 224 225 226 227 |
# File 'lib/puppet/resource_api/type_definition.rb', line 223 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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/puppet/resource_api/type_definition.rb', line 231 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 >> #{error_message}" unless .nil? else bad_vals[key] = "#{value} (#{error_message})" unless .nil? end end bad_vals end |
#insyncable_attributes ⇒ Object
135 136 137 138 139 140 |
# File 'lib/puppet/resource_api/type_definition.rb', line 135 def insyncable_attributes @insyncable_attributes ||= attributes.reject do |_name, | # Only attributes without any behavior are normal Puppet Properties and get insynced .key?(:behaviour) end.keys end |
#name ⇒ Object
125 126 127 |
# File 'lib/puppet/resource_api/type_definition.rb', line 125 def name definition[:name] end |
#namevars ⇒ Object
129 130 131 132 133 |
# File 'lib/puppet/resource_api/type_definition.rb', line 129 def namevars @namevars ||= attributes.select do |_name, | .key?(:behaviour) && [:behaviour] == :namevar end.keys end |
#notify_schema_errors(message) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/puppet/resource_api/type_definition.rb', line 209 def notify_schema_errors() case Puppet.settings[:strict] when :off Puppet.debug() when :warning Puppet::ResourceApi.warning_count += 1 Puppet.warning() if Puppet::ResourceApi.warning_count <= 100 # maximum number of schema warnings to display in a run when :error raise Puppet::DevError, end end |
#validate_schema(definition, attr_key) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/puppet/resource_api/type_definition.rb', line 142 def validate_schema(definition, attr_key) raise Puppet::DevError, format('%<type_class>s must be a Hash, not `%<other_type>s`', type_class: self.class.name, other_type: definition.class) unless definition.is_a?(Hash) @attributes = definition[attr_key] raise Puppet::DevError, format('%<type_class>s must have a name', type_class: self.class.name) unless definition.key? :name raise Puppet::DevError, format('%<type_class>s must have `%<attr_key>s`', type_class: self.class.name, attrs: attr_key) unless definition.key? attr_key raise Puppet::DevError, format('`%<name>s.%<attrs>s` must be a hash, not `%<other_type>s`', name: definition[:name], attrs: attr_key, other_type: attributes.class) unless attributes.is_a?(Hash) # fixup desc/docs backwards compatibility if definition.key? :docs raise Puppet::DevError, format('`%<name>s` has both `desc` and `docs`, prefer using `desc`', name: definition[:name]) if definition[:desc] definition[:desc] = definition[:docs] definition.delete(:docs) end Puppet.warning(format('`%<name>s` has no documentation, add it using a `desc` key', name: definition[:name])) unless definition.key? :desc attributes.each do |key, attr| raise Puppet::DevError, '`rsapi_custom_insync_trigger` cannot be specified as an attribute; it is reserved for propertyless types with the custom_insync feature' if key == :rsapi_custom_insync_trigger # rubocop:disable Layout/LineLength 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(format('`%<name>s.%<key>s` has no documentation, add it using a `desc` key', name: definition[:name], key: key)) 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] raise Puppet::DevError, "the '#{key}' attribute has both a `behavior` and a `behaviour`, only use one" if attr[:behaviour] attr[:behaviour] = attr[:behavior] attr.delete(:behavior) end end |