|
1 | 1 | # Provides accessor methods for the type being provided |
2 | | -class Puppet::ResourceApi::TypeDefinition |
3 | | - attr_reader :definition |
| 2 | +module Puppet::ResourceApi |
| 3 | + # pre-declare class |
| 4 | + class BaseTypeDefinition; end |
| 5 | + |
| 6 | + # RSAPI Resource Type |
| 7 | + class TypeDefinition < BaseTypeDefinition |
| 8 | + def initialize(definition) |
| 9 | + super(definition, :attributes) |
| 10 | + end |
4 | 11 |
|
5 | | - def initialize(definition) |
6 | | - @data_type_cache = {} |
7 | | - validate_schema(definition) |
8 | | - end |
| 12 | + def ensurable? |
| 13 | + attributes.key?(:ensure) |
| 14 | + end |
9 | 15 |
|
10 | | - def name |
11 | | - @definition[:name] |
12 | | - end |
| 16 | + # rubocop complains when this is named has_feature? |
| 17 | + def feature?(feature) |
| 18 | + supported = (definition[:features] && definition[:features].include?(feature)) |
| 19 | + if supported |
| 20 | + Puppet.debug("#{definition[:name]} supports `#{feature}`") |
| 21 | + else |
| 22 | + Puppet.debug("#{definition[:name]} does not support `#{feature}`") |
| 23 | + end |
| 24 | + supported |
| 25 | + end |
13 | 26 |
|
14 | | - def attributes |
15 | | - @definition[:attributes] |
16 | | - end |
| 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 |
| 31 | + end |
| 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 } |
| 34 | + end |
17 | 35 |
|
18 | | - def ensurable? |
19 | | - @definition[:attributes].key?(:ensure) |
20 | | - end |
| 36 | + Puppet::ResourceApi::DataTypeHandling.validate_ensure(definition) |
21 | 37 |
|
22 | | - def namevars |
23 | | - @namevars ||= @definition[:attributes].select { |_name, options| |
24 | | - options.key?(:behaviour) && options[:behaviour] == :namevar |
25 | | - }.keys |
26 | | - end |
| 38 | + definition[:features] ||= [] |
| 39 | + supported_features = %w[supports_noop canonicalize remote_resource simple_get_filter].freeze |
| 40 | + unknown_features = definition[:features] - supported_features |
| 41 | + Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty? |
27 | 42 |
|
28 | | - # rubocop complains when this is named has_feature? |
29 | | - def feature?(feature) |
30 | | - supported = (definition[:features] && definition[:features].include?(feature)) |
31 | | - if supported |
32 | | - Puppet.debug("#{definition[:name]} supports `#{feature}`") |
33 | | - else |
34 | | - Puppet.debug("#{definition[:name]} does not support `#{feature}`") |
| 43 | + # store the validated definition |
| 44 | + @definition = definition |
35 | 45 | end |
36 | | - supported |
37 | 46 | end |
38 | 47 |
|
39 | | - def validate_schema(definition) |
40 | | - raise Puppet::DevError, 'Type definition must be a Hash, not `%{other_type}`' % { other_type: definition.class } unless definition.is_a?(Hash) |
41 | | - raise Puppet::DevError, 'Type definition must have a name' unless definition.key? :name |
42 | | - raise Puppet::DevError, 'Type definition must have `:attributes`' unless definition.key? :attributes |
43 | | - unless definition[:attributes].is_a?(Hash) |
44 | | - raise Puppet::DevError, '`%{name}.attributes` must be a hash, not `%{other_type}`' % { |
45 | | - name: definition[:name], other_type: definition[:attributes].class |
46 | | - } |
47 | | - end |
48 | | - [:title, :provider, :alias, :audit, :before, :consume, :export, :loglevel, :noop, :notify, :require, :schedule, :stage, :subscribe, :tag].each do |name| |
49 | | - raise Puppet::DevError, 'must not define an attribute called `%{name}`' % { name: name.inspect } if definition[:attributes].key? name |
50 | | - end |
51 | | - if definition.key?(:title_patterns) && !definition[:title_patterns].is_a?(Array) |
52 | | - raise Puppet::DevError, '`:title_patterns` must be an array, not `%{other_type}`' % { other_type: definition[:title_patterns].class } |
| 48 | + # RSAPI Transport schema |
| 49 | + class TransportSchemaDef < BaseTypeDefinition |
| 50 | + def initialize(definition) |
| 51 | + super(definition, :connection_info) |
53 | 52 | end |
| 53 | + end |
54 | 54 |
|
55 | | - Puppet::ResourceApi::DataTypeHandling.validate_ensure(definition) |
| 55 | + # Base RSAPI schema Object |
| 56 | + class BaseTypeDefinition |
| 57 | + attr_reader :definition, :attributes |
| 58 | + |
| 59 | + def initialize(definition, attr_key) |
| 60 | + @data_type_cache = {} |
| 61 | + validate_schema(definition, attr_key) |
| 62 | + end |
56 | 63 |
|
57 | | - definition[:features] ||= [] |
58 | | - supported_features = %w[supports_noop canonicalize remote_resource simple_get_filter].freeze |
59 | | - unknown_features = definition[:features] - supported_features |
60 | | - Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty? |
| 64 | + def name |
| 65 | + @definition[:name] |
| 66 | + end |
61 | 67 |
|
62 | | - definition[:attributes].each do |key, attr| |
63 | | - raise Puppet::DevError, "`#{definition[:name]}.#{key}` must be a Hash, not a #{attr.class}" unless attr.is_a? Hash |
64 | | - raise Puppet::DevError, "`#{definition[:name]}.#{key}` has no type" unless attr.key? :type |
65 | | - Puppet.warning("`#{definition[:name]}.#{key}` has no docs") unless attr.key? :desc |
| 68 | + def namevars |
| 69 | + @namevars ||= attributes.select { |_name, options| |
| 70 | + options.key?(:behaviour) && options[:behaviour] == :namevar |
| 71 | + }.keys |
| 72 | + end |
66 | 73 |
|
67 | | - # validate the type by attempting to parse into a puppet type |
68 | | - @data_type_cache[definition[:attributes][key][:type]] ||= |
69 | | - Puppet::ResourceApi::DataTypeHandling.parse_puppet_type( |
70 | | - key, |
71 | | - definition[:attributes][key][:type], |
72 | | - ) |
| 74 | + def validate_schema(definition, attr_key) |
| 75 | + 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) |
| 76 | + @attributes = definition[attr_key] |
| 77 | + raise Puppet::DevError, '%{type_class} must have a name' % { type_class: self.class.name } unless definition.key? :name |
| 78 | + raise Puppet::DevError, '%{type_class} must have `%{attr_key}`' % { type_class: self.class.name, attrs: attr_key } unless definition.key? attr_key |
| 79 | + unless attributes.is_a?(Hash) |
| 80 | + raise Puppet::DevError, '`%{name}.%{attrs}` must be a hash, not `%{other_type}`' % { |
| 81 | + name: definition[:name], attrs: attr_key, other_type: attributes.class |
| 82 | + } |
| 83 | + end |
73 | 84 |
|
74 | | - # fixup any weird behavior ;-) |
75 | | - next unless attr[:behavior] |
76 | | - if attr[:behaviour] |
77 | | - raise Puppet::DevError, "the '#{key}' attribute has both a `behavior` and a `behaviour`, only use one" |
| 85 | + attributes.each do |key, attr| |
| 86 | + raise Puppet::DevError, "`#{definition[:name]}.#{key}` must be a Hash, not a #{attr.class}" unless attr.is_a? Hash |
| 87 | + raise Puppet::DevError, "`#{definition[:name]}.#{key}` has no type" unless attr.key? :type |
| 88 | + Puppet.warning("`#{definition[:name]}.#{key}` has no docs") unless attr.key? :desc |
| 89 | + |
| 90 | + # validate the type by attempting to parse into a puppet type |
| 91 | + @data_type_cache[attributes[key][:type]] ||= |
| 92 | + Puppet::ResourceApi::DataTypeHandling.parse_puppet_type( |
| 93 | + key, |
| 94 | + attributes[key][:type], |
| 95 | + ) |
| 96 | + |
| 97 | + # fixup any weird behavior ;-) |
| 98 | + next unless attr[:behavior] |
| 99 | + if attr[:behaviour] |
| 100 | + raise Puppet::DevError, "the '#{key}' attribute has both a `behavior` and a `behaviour`, only use one" |
| 101 | + end |
| 102 | + attr[:behaviour] = attr[:behavior] |
| 103 | + attr.delete(:behavior) |
78 | 104 | end |
79 | | - attr[:behaviour] = attr[:behavior] |
80 | | - attr.delete(:behavior) |
| 105 | + # store the validated definition |
| 106 | + @definition = definition |
81 | 107 | end |
82 | | - # store the validated definition |
83 | | - @definition = definition |
84 | | - end |
85 | 108 |
|
86 | | - # validates a resource hash against its type schema |
87 | | - def check_schema(resource) |
88 | | - namevars.each do |namevar| |
89 | | - if resource[namevar].nil? |
90 | | - raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar}` namevar attribute" |
| 109 | + # validates a resource hash against its type schema |
| 110 | + def check_schema(resource) |
| 111 | + namevars.each do |namevar| |
| 112 | + if resource[namevar].nil? |
| 113 | + raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar}` namevar attribute" |
| 114 | + end |
91 | 115 | end |
92 | | - end |
93 | 116 |
|
94 | | - message = "Provider returned data that does not match the Type Schema for `#{name}[#{resource[namevars.first]}]`" |
| 117 | + message = "Provider returned data that does not match the Type Schema for `#{name}[#{resource[namevars.first]}]`" |
95 | 118 |
|
96 | | - rejected_keys = check_schema_keys(resource) # removes bad keys |
97 | | - bad_values = check_schema_values(resource) |
| 119 | + rejected_keys = check_schema_keys(resource) # removes bad keys |
| 120 | + bad_values = check_schema_values(resource) |
98 | 121 |
|
99 | | - unless rejected_keys.empty? |
100 | | - message += "\n Unknown attribute:\n" |
101 | | - rejected_keys.each { |key, _value| message += " * #{key}\n" } |
102 | | - end |
103 | | - unless bad_values.empty? |
104 | | - message += "\n Value type mismatch:\n" |
105 | | - bad_values.each { |key, value| message += " * #{key}: #{value}\n" } |
106 | | - end |
| 122 | + unless rejected_keys.empty? |
| 123 | + message += "\n Unknown attribute:\n" |
| 124 | + rejected_keys.each { |key, _value| message += " * #{key}\n" } |
| 125 | + end |
| 126 | + unless bad_values.empty? |
| 127 | + message += "\n Value type mismatch:\n" |
| 128 | + bad_values.each { |key, value| message += " * #{key}: #{value}\n" } |
| 129 | + end |
107 | 130 |
|
108 | | - return if rejected_keys.empty? && bad_values.empty? |
| 131 | + return if rejected_keys.empty? && bad_values.empty? |
109 | 132 |
|
110 | | - if Puppet.settings[:strict] == :off |
111 | | - Puppet.debug(message) |
112 | | - elsif Puppet.settings[:strict] == :warning |
113 | | - Puppet::ResourceApi.warning_count += 1 |
114 | | - Puppet.warning(message) if Puppet::ResourceApi.warning_count <= 100 # maximum number of schema warnings to display in a run |
115 | | - elsif Puppet.settings[:strict] == :error |
116 | | - raise Puppet::DevError, message |
| 133 | + if Puppet.settings[:strict] == :off |
| 134 | + Puppet.debug(message) |
| 135 | + elsif Puppet.settings[:strict] == :warning |
| 136 | + Puppet::ResourceApi.warning_count += 1 |
| 137 | + Puppet.warning(message) if Puppet::ResourceApi.warning_count <= 100 # maximum number of schema warnings to display in a run |
| 138 | + elsif Puppet.settings[:strict] == :error |
| 139 | + raise Puppet::DevError, message |
| 140 | + end |
117 | 141 | end |
118 | | - end |
119 | 142 |
|
120 | | - # Returns an array of keys that where not found in the type schema |
121 | | - # Modifies the resource passed in, leaving only valid attributes |
122 | | - def check_schema_keys(resource) |
123 | | - rejected = [] |
124 | | - resource.reject! { |key| rejected << key if key != :title && attributes.key?(key) == false } |
125 | | - rejected |
126 | | - end |
| 143 | + # Returns an array of keys that where not found in the type schema |
| 144 | + # Modifies the resource passed in, leaving only valid attributes |
| 145 | + def check_schema_keys(resource) |
| 146 | + rejected = [] |
| 147 | + resource.reject! { |key| rejected << key if key != :title && attributes.key?(key) == false } |
| 148 | + rejected |
| 149 | + end |
127 | 150 |
|
128 | | - # Returns a hash of keys and values that are not valid |
129 | | - # does not modify the resource passed in |
130 | | - def check_schema_values(resource) |
131 | | - bad_vals = {} |
132 | | - resource.each do |key, value| |
133 | | - next unless attributes[key] |
134 | | - type = @data_type_cache[attributes[key][:type]] |
135 | | - error_message = Puppet::ResourceApi::DataTypeHandling.try_validate( |
136 | | - type, |
137 | | - value, |
138 | | - '', |
139 | | - ) |
140 | | - bad_vals[key] = value unless error_message.nil? |
| 151 | + # Returns a hash of keys and values that are not valid |
| 152 | + # does not modify the resource passed in |
| 153 | + def check_schema_values(resource) |
| 154 | + bad_vals = {} |
| 155 | + resource.each do |key, value| |
| 156 | + next unless attributes[key] |
| 157 | + type = @data_type_cache[attributes[key][:type]] |
| 158 | + error_message = Puppet::ResourceApi::DataTypeHandling.try_validate( |
| 159 | + type, |
| 160 | + value, |
| 161 | + '', |
| 162 | + ) |
| 163 | + bad_vals[key] = value unless error_message.nil? |
| 164 | + end |
| 165 | + bad_vals |
141 | 166 | end |
142 | | - bad_vals |
143 | 167 | end |
144 | 168 | end |
0 commit comments