Module: CloudConfig

Includes:
Cache, Providers
Defined in:
lib/cloud-config.rb,
lib/cloud-config/cache.rb,
lib/cloud-config/error.rb,
lib/cloud-config/version.rb,
lib/cloud-config/providers.rb,
lib/cloud-config/cache/redis.rb,
lib/cloud-config/cache/in_memory.rb,
lib/cloud-config/provider_config.rb,
lib/cloud-config/provider_options.rb,
lib/cloud-config/providers/in_memory.rb,
lib/cloud-config/providers/yaml_file.rb,
lib/cloud-config/providers/aws_parameter_store.rb

Overview

CloudConfig handles fetching key-value configuration settings from multiple providers. Configure CloudConfig to handle providers and the configuration keys.

Examples:

CloudConfig.configure do
  provider :aws_parameter_store, preload: { async: true } do
    setting :db_url, cache: 60
    setting :api_url
  end
end

Defined Under Namespace

Modules: Cache, Providers Classes: Error, MissingKey, ProviderConfig, ProviderOptions

Constant Summary collapse

VERSION =

Version of CloudConfig

'0.1.1'

Class Method Summary collapse

Methods included from Cache

included

Methods included from Providers

included

Class Method Details

.configure { ... } ⇒ Object

Configure CloudConfig with providers and their configuration keys

Yields:

  • Evaluate itself.



31
32
33
# File 'lib/cloud-config.rb', line 31

def configure(&)
  instance_eval(&)
end

.get(key) ⇒ Object

Fetch the value of a key using the appropriate provider.

Parameters:

  • key (String, Symbol)

    Key to lookup

Returns:

  • (Object)

    Value of the key

Raises:



40
41
42
43
44
45
46
# File 'lib/cloud-config.rb', line 40

def get(key)
  provider_config = providers.values.find { |provider| provider.settings.key?(key) }

  raise MissingKey, 'Key not found' if provider_config.nil?

  load_key(provider_config, key)
end

.load_key(provider_config, key) ⇒ Object

Fetch a key with the provider configuration. If caching is configured, the key will be fetched from the cache.

Parameters:

Returns:

  • (Object)

    Value of the key



89
90
91
92
93
# File 'lib/cloud-config.rb', line 89

def load_key(provider_config, key)
  with_cache(key, expire_in: provider_config.settings[key][:cache]) do
    provider_config.provider.get(key)
  end
end

.preloadObject

Fetch all keys that are configured for preloading. This will automatically cache the corresponding keys.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cloud-config.rb', line 64

def preload
  return if cache.nil?

  providers.each_value do |provider_config|
    next unless provider_config.provider_options.preload

    if provider_config.provider_options.async_preload?
      Parallel.each(provider_config.settings.keys) do |key|
        load_key(provider_config, key)
      end
    else
      provider_config.settings.each_key do |key|
        load_key(provider_config, key)
      end
    end
  end
end

.reset!Object

Reset the CloudConfig configuration



96
97
98
99
# File 'lib/cloud-config.rb', line 96

def reset!
  @cache = nil
  @provider = nil
end

.set(key, value) ⇒ Object

Set the value of a key with the configured provider.

Parameters:

  • key (String, Symobl)

    Key to update

  • value (Object)

    Value of key

Raises:



52
53
54
55
56
57
58
59
60
# File 'lib/cloud-config.rb', line 52

def set(key, value)
  provider_config = providers.values.find { |provider| provider.settings.key?(key) }

  raise MissingKey, 'Key not found' if provider_config.nil?

  provider_config.provider.set(key, value)

  cache&.set(key, value, expire_in: provider_config.settings[key][:cache])
end