Sha256: d5b234ab9ba8a4a84aeeba06c6e366714fa6e329ec419f8ccb46f2f07fdc5cbe

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require "aws-sdk-ssm"

module Chambermaid
  class ParameterStore
    def initialize(path:)
      @path = path
    end

    def load!
      fetch_ssm_params!
    end

    def reload!
      clear_params!
      fetch_ssm_params!
    end

    def loaded?
      !@params_list.empty?
    end

    def self.load!(path:)
      store = new(path: path)
      store.load!
      store
    end

    def params
      @params ||= @param_list.map { |p|
        [p.name.split("/").last.upcase, p.value]
      }.to_h
    end

    alias :to_h :params

    private

    def client
      @client ||= Aws::SSM::Client.new
    end

    def fetch_ssm_params!
      Chambermaid.logger.debug("fetching AWS SSM parameters from `#{@path}`")
      @param_list = []
      response = nil
      loop do
        response = fetch_ssm_param_batch!(response&.next_token)
        @param_list.concat(response.parameters)

        if response.next_token
          Chambermaid.logger.debug("response.next_token found, continuing fetch")
        else
          break
        end
      end
      Chambermaid.logger.debug("fetched #{@param_list.size} parameters from `#{@path}`")
    end

    def fetch_ssm_param_batch!(next_token = nil)
      client.get_parameters_by_path(
        path: @path,
        with_decryption: true,
        recursive: true,
        next_token: next_token
      )
    end

    def clear_params!
      @params = nil
      @params_list = []
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chambermaid-0.5.4 lib/chambermaid/parameter_store.rb
chambermaid-0.4.1 lib/chambermaid/parameter_store.rb
chambermaid-0.4.0 lib/chambermaid/parameter_store.rb