lib/inspec/dsl.rb in inspec-4.16.0 vs lib/inspec/dsl.rb in inspec-4.17.7

- old
+ new

@@ -1,10 +1,12 @@ # copyright: 2015, Dominik Richter require "inspec/log" require "inspec/plugin/v2" module Inspec::DSL + attr_accessor :backend + def require_controls(id, &block) opts = { profile_id: id, include_all: false, backend: @backend, conf: @conf, dependencies: @dependencies } ::Inspec::DSL.load_spec_files_for_profile(self, opts, &block) end @@ -23,10 +25,27 @@ target_name = options[:as] || options[:resource] res = resource_class(from_profile, options[:resource]) add_resource(target_name, res) end + ## + # Try to load and instantiate a missing resource or raise LoadError + # if unable. Requiring the resource registers it and generates a + # method for it so you should only hit this once per missing + # resource. + + def self.method_missing_resource(backend, id, *arguments) + begin + require "inspec/resources/#{id}" + rescue LoadError + require "resources/aws/#{id}" + end + + klass = Inspec::Resource.registry[id.to_s] + klass.new(backend, id, *arguments) + end + # Support for Outer Profile DSL plugins # This is called when an unknown method is encountered # "bare" in a control file - outside of a control or describe block. def method_missing(method_name, *arguments, &block) # Check to see if there is a outer_profile_dsl plugin activator hook with the method name @@ -42,10 +61,14 @@ # 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 - super + begin + Inspec::DSL.method_missing_resource(backend, method_name, *arguments) + rescue LoadError + super + end end end def self.load_spec_files_for_profile(bind_context, opts, &block) dependencies = opts[:dependencies]