Class: Inspec::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resource.rb

Class Method Summary collapse

Class Method Details

.backfill_supports!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/inspec/resource.rb', line 27

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

.create_dsl(profile_context) ⇒ ResourcesDSL

Creates the inner DSL which includes all resources for creating tests. It is always connected to one target, which is specified via the backend argument.

Parameters:

  • backend (BackendRunner)

    exposing the target to resources

Returns:

  • (ResourcesDSL)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/inspec/resource.rb', line 56

def self.create_dsl(profile_context)
  backend = profile_context.backend
  my_registry = profile_context.resource_registry

  Module.new do
    define_method :resource_class do |profile_name, resource_name|
      inner_context = if profile_name == profile_context.profile_id
                        profile_context
                      else
                        profile_context.subcontext_by_name(profile_name)
                      end

      raise ProfileNotFound, "Cannot find profile named: #{profile_name}" if inner_context.nil?

      inner_context.resource_registry[resource_name]
    end

    my_registry.each do |id, r|
      define_method id.to_sym do |*args|
        r.new(backend, id.to_s, *args)
      end

      # confirm backend custom resources have access to other custom resources
      next if backend.respond_to?(id)

      backend.class.send(:define_method, id.to_sym) do |*args|
        r.new(backend, id.to_s, *args)
      end
    end

    # attach backend so we have access to all resources and
    # the train connection object
    define_method :inspec do
      backend
    end
  end
end

.default_registryObject



9
10
11
# File 'lib/inspec/resource.rb', line 9

def self.default_registry
  @default_registry ||= {}
end

.new_registryObject



23
24
25
# File 'lib/inspec/resource.rb', line 23

def self.new_registry
  default_registry.dup
end

.registryObject

TODO: these are keyed off of strings



14
15
16
# File 'lib/inspec/resource.rb', line 14

def self.registry
  @registry ||= default_registry
end

.supportsObject

TODO: these are keyed off of symbols



19
20
21
# File 'lib/inspec/resource.rb', line 19

def self.supports
  @supports ||= {}
end