Class: Inspec::Resource
- Inherits:
-
Object
- Object
- Inspec::Resource
- Defined in:
- lib/inspec/resource.rb
Instance Attribute Summary collapse
-
#resource_exception_message ⇒ Object
readonly
Returns the value of attribute resource_exception_message.
Class Method Summary collapse
-
.__register(name, resource_klass) ⇒ Object
Infrastructure / Bookkeeping.
-
.__resource_registry ⇒ Object
__register.
- .__resource_registry=(o) ⇒ Object
- .backfill_supports! ⇒ Object
- .default_registry ⇒ Object
- .desc(value = nil) ⇒ Object
- .example(value = nil) ⇒ Object
-
.method_missing(method_name, *arguments, &block) ⇒ Object
Support for Resource DSL plugins.
- .name(value = nil) ⇒ Object
- .new_registry ⇒ Object
-
.registry ⇒ Object
TODO: these are keyed off of strings.
-
.support_registry ⇒ Object
TODO: these are keyed off of symbols.
- .supports(criteria = nil) ⇒ Object
- .toggle_inspect ⇒ Object
Instance Method Summary collapse
- #check_supported! ⇒ Object
- #check_supports ⇒ Object
- #fail_resource(message) ⇒ Object
- #inspec ⇒ Object
- #resource_failed? ⇒ Boolean
- #resource_skipped? ⇒ Boolean
- #skip_resource(message) ⇒ Object
-
#supersuper_initialize(backend, name) ⇒ Object
TODO: this is pretty terrible and is only here to work around the idea that we’ve trained resource authors to make initialize methods w/o calling super.
- #to_s ⇒ Object
Instance Attribute Details
#resource_exception_message ⇒ Object (readonly)
Returns the value of attribute resource_exception_message.
203 204 205 |
# File 'lib/inspec/resource.rb', line 203 def @resource_exception_message end |
Class Method Details
.__register(name, resource_klass) ⇒ Object
Infrastructure / Bookkeeping
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/inspec/resource.rb', line 110 def self.__register(name, resource_klass) cl = Class.new(resource_klass) do # TODO: remove # As best I can figure out, this anonymous class only exists # because we're trying to avoid having resources with # initialize methods from having to call super, which is, # quite frankly, dumb. Avoidable even with some simple # documentation. def initialize(backend, name, *args) supersuper_initialize(backend, name) do super(*args) end end end reg = __resource_registry rescue nil reg = self.__resource_registry = Inspec::Resource.registry unless reg # Warn if a resource pack is overwriting a core resource. # Suppress warning if the resource is an AWS resource, see #3822 if reg.key?(name) && !name.start_with?("aws_") Inspec::Log.warn("Overwriting resource #{name}. To reference a specific version of #{name} use the resource() method") end reg[name] = cl end |
.__resource_registry ⇒ Object
__register
137 138 139 140 141 |
# File 'lib/inspec/resource.rb', line 137 def self.__resource_registry # rubocop:disable Style/AndOr find_class_instance_variable(:@__resource_registry) or raise("__resource_registry not set!") end |
.__resource_registry=(o) ⇒ Object
143 144 145 |
# File 'lib/inspec/resource.rb', line 143 def self.__resource_registry=(o) @__resource_registry = o end |
.backfill_supports! ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/inspec/resource.rb', line 165 def self.backfill_supports! reg = registry.keys.map(&:to_sym).sort sup = supports.keys.map(&:to_sym).sort missings = reg - sup supports[:platform] = [{ platform: "os" }] # patch the circular dep missings.each do |missing| klass = registry[missing.to_s].superclass sklas = klass.superclass.name&.to_sym # might be resource = no name klass = klass.name.to_sym case when klass != missing # an alias supports[missing] = supports[klass] when sklas supports[klass] = supports[sklas] end end end |
.default_registry ⇒ Object
147 148 149 |
# File 'lib/inspec/resource.rb', line 147 def self.default_registry @default_registry ||= {} end |
.desc(value = nil) ⇒ Object
15 16 17 18 19 |
# File 'lib/inspec/resource.rb', line 15 def self.desc(value = nil) return find_class_instance_variable(:@description) unless value @description = value end |
.example(value = nil) ⇒ Object
21 22 23 24 25 |
# File 'lib/inspec/resource.rb', line 21 def self.example(value = nil) return find_class_instance_variable(:@example) unless value @example = value end |
.method_missing(method_name, *arguments, &block) ⇒ Object
Support for Resource DSL plugins. This is called when an unknown method is encountered within a resource class definition. Even tho this is defined as an instance method, it gets added to the class via ‘extend`, so this is actually a class defintion.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/inspec/resource.rb', line 84 def self.method_missing(method_name, *arguments, &block) require "inspec/plugin/v2" # Check to see if there is a resource_dsl plugin activator hook with the method name registry = Inspec::Plugin::V2::Registry.instance hook = registry.find_activators(plugin_type: :resource_dsl, activator_name: method_name).first if hook # OK, load the hook if it hasn't been already. We'll then know a module, # which we can then inject into the resource hook.activate # Inject the module's methods into the resource as class methods. # implementation_class is the field name, but this is actually a module. extend(hook.implementation_class) # Now that the module is loaded, it defined one or more methods # (presumably the one we were looking for.) # We still haven't called it, so do so now. send(method_name, *arguments, &block) else # If we couldn't find a plugin to match, maybe something up above has it, # or maybe it is just a unknown method error. super end end |
.name(value = nil) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/inspec/resource.rb', line 7 def self.name(value = nil) return @name unless value @name = value __register(value, self) end |
.new_registry ⇒ Object
161 162 163 |
# File 'lib/inspec/resource.rb', line 161 def self.new_registry default_registry.dup end |
.registry ⇒ Object
TODO: these are keyed off of strings
152 153 154 |
# File 'lib/inspec/resource.rb', line 152 def self.registry @registry ||= default_registry end |
.support_registry ⇒ Object
TODO: these are keyed off of symbols
157 158 159 |
# File 'lib/inspec/resource.rb', line 157 def self.support_registry @support_registry ||= {} end |
.supports(criteria = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/inspec/resource.rb', line 27 def self.supports(criteria = nil) return if criteria.nil? key = @name.to_sym # HACK: this is broken!!! this is global where the rest are localized to registry Inspec::Resource.support_registry[key] ||= [] Inspec::Resource.support_registry[key].push(criteria) end |
.toggle_inspect ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/inspec/resource.rb', line 192 def self.toggle_inspect has_inspect = instance_method(:inspect).source_location unless has_inspect define_method :inspect do to_s end else undef_method :inspect end end |
Instance Method Details
#check_supported! ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/inspec/resource.rb', line 65 def check_supported! backend = @__backend_runner__ name = @__resource_name__ supported = @supports ? check_supports : true # check_supports has side effects! test_backend = defined?(Train::Transports::Mock::Connection) && backend.backend.class == Train::Transports::Mock::Connection # raise unless we are supported or in test unless supported || test_backend msg = "Unsupported resource/backend combination: %s / %s. Exiting." % [name, backend.platform.name] raise ArgumentError, msg end end |
#check_supports ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/inspec/resource.rb', line 223 def check_supports require "inspec/resources/platform" status = inspec.platform.supported?(@supports) fail_msg = "Resource `#{@__resource_name__}` is not supported on platform #{inspec.platform.name}/#{inspec.platform.release}." fail_resource(fail_msg) unless status status end |
#fail_resource(message) ⇒ Object
214 215 216 217 |
# File 'lib/inspec/resource.rb', line 214 def fail_resource() @resource_failed = true @resource_exception_message = end |
#inspec ⇒ Object
231 232 233 |
# File 'lib/inspec/resource.rb', line 231 def inspec @__backend_runner__ end |
#resource_failed? ⇒ Boolean
219 220 221 |
# File 'lib/inspec/resource.rb', line 219 def resource_failed? @resource_failed end |
#resource_skipped? ⇒ Boolean
210 211 212 |
# File 'lib/inspec/resource.rb', line 210 def resource_skipped? @resource_skipped end |
#skip_resource(message) ⇒ Object
205 206 207 208 |
# File 'lib/inspec/resource.rb', line 205 def skip_resource() @resource_skipped = true @resource_exception_message = end |
#supersuper_initialize(backend, name) ⇒ Object
TODO: this is pretty terrible and is only here to work around the idea that we’ve trained resource authors to make initialize methods w/o calling super.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/inspec/resource.rb', line 41 def supersuper_initialize(backend, name) @resource_skipped = false @resource_failed = false # TODO remove this (or the support_registry) (again?) @supports = Inspec::Resource.support_registry[name.to_sym] @resource_exception_message = nil # attach the backend to this instance @__backend_runner__ = backend @__resource_name__ = name check_supported! yield rescue Inspec::Exceptions::ResourceSkipped => e skip_resource(e.) rescue Inspec::Exceptions::ResourceFailed => e fail_resource(e.) rescue NotImplementedError => e fail_resource(e.) unless @resource_failed rescue NoMethodError => e skip_resource(e.) unless @resource_failed end |
#to_s ⇒ Object
188 189 190 |
# File 'lib/inspec/resource.rb', line 188 def to_s @__resource_name__ end |