class Lamby::SsmParameterStore

Constants

MAX_RESULTS
Param

Attributes

params[R]
path[R]

Public Class Methods

dotenv(path) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 14
def dotenv(path)
  new(path).get!.to_dotenv
end
get!(path) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 18
def get!(path)
  parts = path.from(1).split('/')
  env = parts.pop
  path = "/#{parts.join('/')}"
  new(path).get!.params.detect do |p|
    p.env == env
  end.try(:value)
end
new(path, options = {}) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 29
def initialize(path, options = {})
  @path = path
  @params = []
  @options = options
end

Public Instance Methods

client() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 55
def client
  @client ||= begin
    options = @options[:client_options] || {}
    Aws::SSM::Client.new(options)
  end
end
get!() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 45
def get!
  get_all!
  get_history! if label.present?
  self
end
label() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 51
def label
  ENV['LAMBY_SSM_PARAMS_LABEL'] || @options[:label]
end
to_dotenv() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 41
def to_dotenv
  File.open(dotenv_file, 'w') { |f| f.write(dotenv_contents) }
end
to_env(overwrite: true) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 35
def to_env(overwrite: true)
  params.each do |param|
    overwrite ? ENV[param.env] = param.value : ENV[param.env] ||= param.value
  end
end

Private Instance Methods

dotenv_contents() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 69
def dotenv_contents
  params.each_with_object('') do |param, contents|
    line = "export #{param.env}=#{param.value}\n"
    contents << line
  end
end
dotenv_file() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 65
def dotenv_file
  @options[:dotenv_file] || ENV['LAMBY_SSM_PARAMS_FILE'] || Rails.root.join(".env.#{Rails.env}")
end
get_all() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 91
def get_all
  @all_response = client.get_parameters_by_path(get_all_options)
end
get_all!() click to toggle source

Path

# File lib/lamby/ssm_parameter_store.rb, line 78
def get_all!
  return params if @got_all
  get_parse_all
  while @all_response.next_token do get_parse_all end
  @got_all = true
  params
end
get_all_options() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 95
def get_all_options
  { path: path,
    recursive: true,
    with_decryption: true,
    max_results: MAX_RESULTS
  }.tap { |options|
    token = @all_response.try(:next_token)
    options[:next_token] = token if token
  }
end
get_history(name) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 131
def get_history(name)
  @hist_response = client.get_parameter_history(get_history_options(name))
end
get_history!() click to toggle source

History

# File lib/lamby/ssm_parameter_store.rb, line 115
def get_history!
  return params if @got_history
  params.each do |param|
    name = param.name
    get_parse_history(name)
    while @hist_response.next_token do get_parse_history(name) end
  end
  @got_history = true
  params
end
get_history_options(name) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 135
def get_history_options(name)
  { name: name,
    with_decryption: true,
    max_results: MAX_RESULTS
  }.tap { |options|
    token = @hist_response.try(:next_token)
    options[:next_token] = token if token
  }
end
get_parse_all() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 86
def get_parse_all
  get_all
  parse_all
end
get_parse_history(name) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 126
def get_parse_history(name)
  get_history(name)
  parse_history(name)
end
parse_all() click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 106
def parse_all
  @all_response.parameters.each do |p|
    env = p.name.split('/').last
    params << Param.new(p.name, env, p.value)
  end
end
parse_history(name) click to toggle source
# File lib/lamby/ssm_parameter_store.rb, line 145
def parse_history(name)
  @hist_response.parameters.each do |p|
    next unless p.labels.include? label
    param = params.detect { |param| param.name == name }
    param.value = p.value
  end
end