puppet-resource_api 1.9.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,12 +25,12 @@ module Puppet::ResourceApi
25
25
  end
26
26
 
27
27
  def validate_schema(definition, attr_key)
28
- super(definition, attr_key)
29
- [:title, :provider, :alias, :audit, :before, :consume, :export, :loglevel, :noop, :notify, :require, :schedule, :stage, :subscribe, :tag].each do |name|
30
- raise Puppet::DevError, 'must not define an attribute called `%{name}`' % { name: name.inspect } if definition[attr_key].key? name
28
+ super
29
+ %i[title provider alias audit before consume export loglevel noop notify require schedule stage subscribe tag].each do |name|
30
+ raise Puppet::DevError, format('must not define an attribute called `%<name>s`', name: name.inspect) if definition[attr_key].key? name
31
31
  end
32
32
  if definition.key?(:title_patterns) && !definition[:title_patterns].is_a?(Array)
33
- raise Puppet::DevError, '`:title_patterns` must be an array, not `%{other_type}`' % { other_type: definition[:title_patterns].class }
33
+ raise Puppet::DevError, format('`:title_patterns` must be an array, not `%<other_type>s`', other_type: definition[:title_patterns].class)
34
34
  end
35
35
 
36
36
  Puppet::ResourceApi::DataTypeHandling.validate_ensure(definition)
@@ -54,9 +54,7 @@ module Puppet::ResourceApi
54
54
  # options: The hash of attribute options, including type, desc, default, and behaviour
55
55
  def create_attribute_in(type, attribute_name, param_or_property, parent, options)
56
56
  type.send(param_or_property, attribute_name.to_sym, parent: parent) do
57
- if options[:desc]
58
- desc "#{options[:desc]} (a #{options[:type]})"
59
- end
57
+ desc "#{options[:desc]} (a #{options[:type]})" if options[:desc]
60
58
 
61
59
  # The initialize method is called when puppet core starts building up
62
60
  # type objects. The core passes in a hash of shape { resource:
@@ -70,9 +68,9 @@ module Puppet::ResourceApi
70
68
 
71
69
  # get pops data type object for this parameter or property
72
70
  define_singleton_method(:data_type) do
73
- @rsapi_data_type ||= Puppet::ResourceApi::DataTypeHandling.parse_puppet_type(
71
+ @rsapi_data_type ||= Puppet::ResourceApi::DataTypeHandling.parse_puppet_type( # rubocop:disable Naming/MemoizedInstanceVariableName
74
72
  attribute_name,
75
- options[:type],
73
+ options[:type]
76
74
  )
77
75
  end
78
76
 
@@ -82,7 +80,7 @@ module Puppet::ResourceApi
82
80
  self,
83
81
  data_type,
84
82
  param_or_property,
85
- options,
83
+ options
86
84
  )
87
85
  end
88
86
  end
@@ -101,12 +99,10 @@ module Puppet::ResourceApi
101
99
  attributes.each do |name, _options|
102
100
  type = @data_type_cache[attributes[name][:type]]
103
101
 
104
- if resource[name].nil? && !(type.instance_of? Puppet::Pops::Types::POptionalType)
105
- missing_attrs << name
106
- end
102
+ missing_attrs << name if resource[name].nil? && !(type.instance_of? Puppet::Pops::Types::POptionalType)
107
103
  end
108
104
 
109
- error_msg = "The following mandatory attributes were not provided:\n * " + missing_attrs.join(", \n * ")
105
+ error_msg = "The following mandatory attributes were not provided:\n * #{missing_attrs.join(", \n * ")}"
110
106
  raise Puppet::ResourceError, error_msg if missing_attrs.any?
111
107
  end
112
108
 
@@ -131,57 +127,54 @@ module Puppet::ResourceApi
131
127
  end
132
128
 
133
129
  def namevars
134
- @namevars ||= attributes.select { |_name, options|
130
+ @namevars ||= attributes.select do |_name, options|
135
131
  options.key?(:behaviour) && options[:behaviour] == :namevar
136
- }.keys
132
+ end.keys
137
133
  end
138
134
 
139
135
  def insyncable_attributes
140
- @insyncable_attributes ||= attributes.reject { |_name, options|
136
+ @insyncable_attributes ||= attributes.reject do |_name, options|
141
137
  # Only attributes without any behavior are normal Puppet Properties and get insynced
142
138
  options.key?(:behaviour)
143
- }.keys
139
+ end.keys
144
140
  end
145
141
 
146
142
  def validate_schema(definition, attr_key)
147
- 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)
143
+ 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)
144
+
148
145
  @attributes = definition[attr_key]
149
- raise Puppet::DevError, '%{type_class} must have a name' % { type_class: self.class.name } unless definition.key? :name
150
- raise Puppet::DevError, '%{type_class} must have `%{attr_key}`' % { type_class: self.class.name, attrs: attr_key } unless definition.key? attr_key
151
- unless attributes.is_a?(Hash)
152
- raise Puppet::DevError, '`%{name}.%{attrs}` must be a hash, not `%{other_type}`' % {
153
- name: definition[:name], attrs: attr_key, other_type: attributes.class
154
- }
155
- end
146
+ raise Puppet::DevError, format('%<type_class>s must have a name', type_class: self.class.name) unless definition.key? :name
147
+ 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
148
+
149
+ 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)
156
150
 
157
151
  # fixup desc/docs backwards compatibility
158
152
  if definition.key? :docs
159
- if definition[:desc]
160
- raise Puppet::DevError, '`%{name}` has both `desc` and `docs`, prefer using `desc`' % { name: definition[:name] }
161
- end
153
+ raise Puppet::DevError, format('`%<name>s` has both `desc` and `docs`, prefer using `desc`', name: definition[:name]) if definition[:desc]
154
+
162
155
  definition[:desc] = definition[:docs]
163
156
  definition.delete(:docs)
164
157
  end
165
- Puppet.warning('`%{name}` has no documentation, add it using a `desc` key' % { name: definition[:name] }) unless definition.key? :desc
158
+ Puppet.warning(format('`%<name>s` has no documentation, add it using a `desc` key', name: definition[:name])) unless definition.key? :desc
166
159
 
167
160
  attributes.each do |key, attr|
168
- 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 Metrics/LineLength
161
+ 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
169
162
  raise Puppet::DevError, "`#{definition[:name]}.#{key}` must be a Hash, not a #{attr.class}" unless attr.is_a? Hash
170
163
  raise Puppet::DevError, "`#{definition[:name]}.#{key}` has no type" unless attr.key? :type
171
- Puppet.warning('`%{name}.%{key}` has no documentation, add it using a `desc` key' % { name: definition[:name], key: key }) unless attr.key? :desc
164
+
165
+ 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
172
166
 
173
167
  # validate the type by attempting to parse into a puppet type
174
168
  @data_type_cache[attributes[key][:type]] ||=
175
169
  Puppet::ResourceApi::DataTypeHandling.parse_puppet_type(
176
170
  key,
177
- attributes[key][:type],
171
+ attributes[key][:type]
178
172
  )
179
173
 
180
174
  # fixup any weird behavior ;-)
181
175
  next unless attr[:behavior]
182
- if attr[:behaviour]
183
- raise Puppet::DevError, "the '#{key}' attribute has both a `behavior` and a `behaviour`, only use one"
184
- end
176
+ raise Puppet::DevError, "the '#{key}' attribute has both a `behavior` and a `behaviour`, only use one" if attr[:behaviour]
177
+
185
178
  attr[:behaviour] = attr[:behavior]
186
179
  attr.delete(:behavior)
187
180
  end
@@ -190,9 +183,7 @@ module Puppet::ResourceApi
190
183
  # validates a resource hash against its type schema
191
184
  def check_schema(resource, message_prefix = nil)
192
185
  namevars.each do |namevar|
193
- if resource[namevar].nil?
194
- raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar}` namevar attribute"
195
- end
186
+ raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar}` namevar attribute" if resource[namevar].nil?
196
187
  end
197
188
 
198
189
  message_prefix = 'Provider returned data that does not match the Type Schema' if message_prefix.nil?
@@ -216,12 +207,13 @@ module Puppet::ResourceApi
216
207
  end
217
208
 
218
209
  def notify_schema_errors(message)
219
- if Puppet.settings[:strict] == :off
210
+ case Puppet.settings[:strict]
211
+ when :off
220
212
  Puppet.debug(message)
221
- elsif Puppet.settings[:strict] == :warning
213
+ when :warning
222
214
  Puppet::ResourceApi.warning_count += 1
223
215
  Puppet.warning(message) if Puppet::ResourceApi.warning_count <= 100 # maximum number of schema warnings to display in a run
224
- elsif Puppet.settings[:strict] == :error
216
+ when :error
225
217
  raise Puppet::DevError, message
226
218
  end
227
219
  end
@@ -240,15 +232,16 @@ module Puppet::ResourceApi
240
232
  bad_vals = {}
241
233
  resource.each do |key, value|
242
234
  next unless attributes[key]
235
+
243
236
  type = @data_type_cache[attributes[key][:type]]
244
- is_sensitive = (attributes[key].key?(:sensitive) && (attributes[key][:sensitive] == true))
237
+ is_sensitive = attributes[key].key?(:sensitive) && (attributes[key][:sensitive] == true)
245
238
  error_message = Puppet::ResourceApi::DataTypeHandling.try_validate(
246
239
  type,
247
240
  value,
248
- '',
241
+ ''
249
242
  )
250
243
  if is_sensitive
251
- bad_vals[key] = '<< redacted value >> ' + error_message unless error_message.nil?
244
+ bad_vals[key] = "<< redacted value >> #{error_message}" unless error_message.nil?
252
245
  else
253
246
  bad_vals[key] = "#{value} (#{error_message})" unless error_message.nil?
254
247
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation,Style/ClassAndModuleChildren
3
+ module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation
4
4
 
5
5
  # This module is responsible for setting default and alias values for the
6
6
  # resource class.
@@ -36,14 +36,12 @@ module Puppet::ResourceApi::ValueCreator
36
36
  end
37
37
 
38
38
  # provide hints to `puppet type generate` for better parsing
39
- if data_type.instance_of? Puppet::Pops::Types::POptionalType
40
- data_type = data_type.type
41
- end
39
+ data_type = data_type.type if data_type.instance_of? Puppet::Pops::Types::POptionalType
42
40
 
43
41
  case data_type
44
42
  when Puppet::Pops::Types::PStringType
45
43
  # require any string value
46
- def_newvalues(attribute_class, param_or_property, %r{})
44
+ def_newvalues(attribute_class, param_or_property, //)
47
45
  when Puppet::Pops::Types::PBooleanType
48
46
  def_newvalues(attribute_class, param_or_property, 'true', 'false')
49
47
  attribute_class.aliasvalue true, 'true'
@@ -51,7 +49,7 @@ module Puppet::ResourceApi::ValueCreator
51
49
  attribute_class.aliasvalue :true, 'true' # rubocop:disable Lint/BooleanSymbol
52
50
  attribute_class.aliasvalue :false, 'false' # rubocop:disable Lint/BooleanSymbol
53
51
  when Puppet::Pops::Types::PIntegerType
54
- def_newvalues(attribute_class, param_or_property, %r{^-?\d+$})
52
+ def_newvalues(attribute_class, param_or_property, /^-?\d+$/)
55
53
  when Puppet::Pops::Types::PFloatType, Puppet::Pops::Types::PNumericType
56
54
  def_newvalues(attribute_class, param_or_property, Puppet::Pops::Patterns::NUMERIC)
57
55
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Puppet
4
4
  module ResourceApi
5
- VERSION = '1.9.2'
5
+ VERSION = '2.0.1'
6
6
  end
7
7
  end