Class: Uc3DmpApiCore::SsmReader

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-dmp-api-core/ssm_reader.rb

Overview


SSM Parameter Store Helper

Shared helper methods for accessing SSM parameters


Constant Summary collapse

SOURCE =
'Uc3DmpApiCore::SsmReader'

Class Method Summary collapse

Class Method Details

.available_keysObject

Return all of the available keys



16
17
18
# File 'lib/uc3-dmp-api-core/ssm_reader.rb', line 16

def available_keys
  _ssm_keys.keys
end

.fetch_value(key:, logger: nil) ⇒ Object

Call SSM to get the value for the specified key



40
41
42
43
44
# File 'lib/uc3-dmp-api-core/ssm_reader.rb', line 40

def fetch_value(key:, logger: nil)
  resp = Aws::SSM::Client.new.get_parameter(name: key, with_decryption: true)
  logger&.debug(message: "Searching for SSM Key: #{key}, Found: '#{resp&.parameter&.value}'")
  resp.nil? || resp.parameter.nil? ? nil : resp.parameter.value
end

.get_ssm_value(key:, provenance_name: nil, logger: nil) ⇒ Object

Fetch the value for the specified :key


rubocop:disable Metrics/AbcSize



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uc3-dmp-api-core/ssm_reader.rb', line 23

def get_ssm_value(key:, provenance_name: nil, logger: nil)
  full_key = _ssm_keys[:"#{key.downcase}"] unless key.nil?
  logger&.debug(message: "Looking for SSM Key: #{full_key}")
  return nil if full_key.nil?

  key_vals = { env: ENV.fetch('LAMBDA_ENV', 'dev').to_s.downcase }
  # Swap in the provenance name if applicable
  key_vals[:provenance] = provenance_name unless provenance_name.nil? ||
                                                 !full_key.include?('%{provenance}')
  fetch_value(key: format(full_key, key_vals), logger:)
rescue Aws::Errors::ServiceError => e
  logger&.error(message: "Looking for SSM Key: #{key} - #{e.message}", details: e.backtrace)
  nil
end