Sha256: bfc4e63a15c3b37e4eb7ec60c4348210cab2e61cb13f9b0bd9b815609896b266

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module ParamStore
  module Adapters
    class SSM
      attr_reader :default_path

      def initialize(default_path: nil)
        @default_path = default_path
      end

      def fetch(key, *args, path: nil, &block)
        key = prepend_path(path, key)
        tmp = {}
        if string = get_parameter(key)
          tmp[key] = string
        end
        tmp.fetch(key, *args, &block)
      end

      def fetch_all(*keys, path: nil)
        keys = keys.flatten
        keys = keys.map { |key| prepend_path(path, key) } if path
        ParamStore.ssm_client.get_parameters(names: keys, with_decryption: true).parameters.each_with_object({}) do |param, result|
          result[param.name.gsub(path.to_s, '')] = param.value
        end
      end

      private

      def get_parameter(key)
        ParamStore.ssm_client.get_parameter(name: key, with_decryption: true).parameter.value
      rescue Aws::SSM::Errors::ParameterNotFound
        # let the tmp.fetch below deal with key not found and defaults
      end

      def prepend_path(path, key)
        "#{path || default_path}#{key}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
param_store-1.0.1 lib/param_store/adapters/ssm.rb
param_store-1.0.0 lib/param_store/adapters/ssm.rb