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,
lib/cloud-config/providers/aws_secrets_manager.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.2.2'

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, reset_cache: false) ⇒ Object

Fetch the value of a key using the appropriate provider.

Parameters:

  • key (String, Symbol)

    Key to lookup

  • reset_cache (Boolean) (defaults to: false)

    Whether the cache for the key should be reset

Returns:

  • (Object)

    Value of the key

Raises:



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

def get(key, reset_cache: false)
  provider_config = providers_by_key[key]

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

  load_key(provider_config, key, reset_cache:)
end

.load_key(provider_config, key, reset_cache: false) ⇒ Object

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

Parameters:

  • provider_config (CloudConfig::ProviderConfig)

    provider configuration

  • key (String, Symbol)

    Key to fetch

  • reset_cache (Boolean) (defaults to: false)

    Whether the cache for the key should be reset

Returns:

  • (Object)

    Value of the key



91
92
93
94
95
# File 'lib/cloud-config.rb', line 91

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

.preloadObject

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



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

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



98
99
100
101
102
# File 'lib/cloud-config.rb', line 98

def reset!
  @cache = nil
  @providers = {}
  @providers_by_key = {}
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:



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

def set(key, value)
  provider_config = providers_by_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