Class: Puppet::DataProviders::DataAdapter

Inherits:
Pops::Adaptable::Adapter show all
Defined in:
lib/puppet/data_providers/data_adapter.rb

Overview

A DataAdapter adapts an object with a Hash of data

Constant Summary

Constants inherited from Pops::Adaptable::Adapter

Pops::Adaptable::Adapter::DOUBLE_COLON, Pops::Adaptable::Adapter::USCORE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pops::Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, get, instance_var_name, self_attr_name

Constructor Details

#initialize(env) ⇒ DataAdapter

Returns a new instance of DataAdapter.



7
8
9
10
# File 'lib/puppet/data_providers/data_adapter.rb', line 7

def initialize(env)
  @env = env
  @data = {}
end

Instance Attribute Details

#dataObject



4
5
6
# File 'lib/puppet/data_providers/data_adapter.rb', line 4

def data
  @data
end

#env_providerObject



5
6
7
# File 'lib/puppet/data_providers/data_adapter.rb', line 5

def env_provider
  @env_provider
end

Class Method Details

.create_adapter(environment) ⇒ Object



40
41
42
# File 'lib/puppet/data_providers/data_adapter.rb', line 40

def self.create_adapter(environment)
  new(environment)
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
# File 'lib/puppet/data_providers/data_adapter.rb', line 12

def [](name)
  @data[name]
end

#[]=(name, value) ⇒ Object



20
21
22
23
24
25
# File 'lib/puppet/data_providers/data_adapter.rb', line 20

def []=(name, value)
  unless value.is_a?(Hash)
    raise ArgumentError, "Given value must be a Hash, got: #{value.class}."
  end
  @data[name] = value
end

#has_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/puppet/data_providers/data_adapter.rb', line 16

def has_name?(name)
  @data.has_key? name
end

#initialize_env_providerObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/puppet/data_providers/data_adapter.rb', line 67

def initialize_env_provider
  # Get the environment's configuration since we need to know which data provider
  # should be used (includes 'none' which gets a null implementation).
  #
  env_conf = Puppet.lookup(:environments).get_conf(@env.name)

  # Get the data provider and find the bound implementation
  # TODO: PUP-1640, drop the nil check when legacy env support is dropped
  provider_name = env_conf.nil? ? 'none' : env_conf.environment_data_provider
  service_type = Puppet::Plugins::DataProviders.hash_of_environment_data_providers
  service_name = Puppet::Plugins::DataProviders::ENV_DATA_PROVIDERS_KEY

  # Get the service (registry of known implementations)
  # Support running tests without an injector being configured == using a null implementation
  unless injector = Puppet.lookup(:injector) { nil }
    return Puppet::Plugins::DataProviders::EnvironmentDataProvider.new()
  end
  service = Puppet.lookup(:injector).lookup(nil, service_type, service_name)
  provider = service[provider_name]
  unless provider
    raise Puppet::Error.new("Environment '#{@env.name}', cannot find environment_data_provider '#{provider_name}'")
  end
  provider
end

#initialize_module_provider(module_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/puppet/data_providers/data_adapter.rb', line 44

def initialize_module_provider(module_name)
  # Support running tests without an injector being configured == using a null implementation
  unless injector = Puppet.lookup(:injector) { nil }
    return Puppet::Plugins::DataProviders::ModuleDataProvider.new()
  end
  # Get the registry of module to provider implementation name
  module_service_type = Puppet::Plugins::DataProviders.hash_of_per_module_data_provider
  module_service_name = Puppet::Plugins::DataProviders::PER_MODULE_DATA_PROVIDER_KEY
  module_service = Puppet.lookup(:injector).lookup(nil, module_service_type, module_service_name)
  provider_name = module_service[module_name] || 'none'

  service_type = Puppet::Plugins::DataProviders.hash_of_module_data_providers
  service_name = Puppet::Plugins::DataProviders::MODULE_DATA_PROVIDERS_KEY

  # Get the service (registry of known implementations)
  service = Puppet.lookup(:injector).lookup(nil, service_type, service_name)
  provider = service[provider_name]
  unless provider
    raise Puppet::Error.new("Environment '#{@env.name}', cannot find module_data_provider '#{provider_name}'")
  end
  provider
end

#module_provider(module_name) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/puppet/data_providers/data_adapter.rb', line 31

def module_provider(module_name)
  # Test if the key is present for the given module_name. It might be there even if the
  # value is nil (which indicates that no module provider is configured for the given name)
  unless @data.include?(module_name)
    @data[module_name] = initialize_module_provider(module_name)
  end
  @data[module_name]
end