Module: ConfigurationService::Factory

Defined in:
lib/configuration_service/factory.rb,
lib/configuration_service/factory/context.rb,
lib/configuration_service/factory/yaml_file_context.rb,
lib/configuration_service/factory/environment_context.rb,
lib/configuration_service/factory/context/symbolic_access_wrapper.rb,
lib/configuration_service/factory/environment_context_backward_compatibility.rb

Overview

A factory for creating a configuration service client

Examples:

Requesting configuration from the Vault service provider


# With the following in the environment:
#
# CFGSRV_IDENTIFIER="acme"
# CFGSRV_TOKEN="0b2a80f4-54ce-45f4-8267-f6558fee64af"
# CFGSRV_PROVIDER="vault"
# CFGSRV_PROVIDER_ADDRESS="http://127.0.0.1:8200"

# And the following in Gemfile:
#
# source 'https://rubygems.org'
#
# gem 'configuration_service-provider-vault'
# gem 'acme_application'

# Now main.rb (or config.ru or whatever) is decoupled from provider
# selection and service configuration:

require 'bundler'
Bundler.require(:default) # Registers the vault provider

configuration_service = ConfigurationService::Factory.create_client
configuraton = configuration_service.request_configuration
AcmeApplication.new(configuration.data).run

Creating a multi-identifier client with static bootstrap configuration


admin_client = ConfigurationService::Factory.create_client(
  "token" => "c3935418-f621-40de-ada3-cc8169f1348a",
  # Note: no identifier provided
  "provider_id" => "vault",
  "provider_config" => {
    "address" => "http://127.0.0.1:8200"
  }
)
acme_config = admin_client.request_configuration(identifier: "acme")
wile_config = admin_client.request_configuration(identifier: "wile")

Defined Under Namespace

Modules: EnvironmentContextBackwardCompatibility Classes: Context, EnvironmentContext, YamlFileContext

Class Method Summary collapse

Class Method Details

.create_admin_client(context = EnvironmentContext.new) ⇒ Object

Deprecated.

use create_client()

@ see create_client()



82
83
84
# File 'lib/configuration_service/factory.rb', line 82

def self.create_admin_client(context = EnvironmentContext.new)
  create_client(context)
end

.create_client(context = EnvironmentContext.new) ⇒ ConfigurationService::Client

Create a configuration service client

When the context is a Hash, it is wrapped in a Context, which does not scrub sources after the configuration service client is created. For this reason, the process ENV should never be given as the context; rather give no context, so that the default EnvironmentContext will be used to safely scrub the process ENV and (on JRuby) system properties after the configuration service client is created.

If the context does not provide an identifier property, the instance methods on the client object will require an identifier argument.

Parameters:

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/configuration_service/factory.rb', line 68

def self.create_client(context = EnvironmentContext.new)
  context = Context.new(context) if context.is_a?(Hash)
  decorators = get_decorators(context.decorators)
  provider = decorators.any? ? decorate(decorators << create_provider(context)) : create_provider(context)
  identifier = context.identifier? ? context.identifier : nil
  ConfigurationService::Client.new(credentials: context.token, provider: provider, identifier: identifier)
ensure
  context.scrub!
end