Class: Lamby::SsmParameterStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lamby/ssm_parameter_store.rb

Defined Under Namespace

Classes: Param

Constant Summary collapse

MAX_RESULTS =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ SsmParameterStore

Returns a new instance of SsmParameterStore.



30
31
32
33
34
# File 'lib/lamby/ssm_parameter_store.rb', line 30

def initialize(path, options = {})
  @path = path
  @params = []
  @options = options
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/lamby/ssm_parameter_store.rb', line 10

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/lamby/ssm_parameter_store.rb', line 10

def path
  @path
end

Class Method Details

.dotenv(path) ⇒ Object



14
15
16
# File 'lib/lamby/ssm_parameter_store.rb', line 14

def dotenv(path)
  new(path).get!.to_dotenv
end

.get!(path) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/lamby/ssm_parameter_store.rb', line 18

def get!(path)
  parts = path[1..-1].split('/')
  env = parts.pop
  path = "/#{parts.join('/')}"
  param = new(path).get!.params.detect do |p|
    p.env == env
  end
  param&.value
end

Instance Method Details

#clientObject



56
57
58
59
60
61
# File 'lib/lamby/ssm_parameter_store.rb', line 56

def client
  @client ||= begin
    options = @options[:client_options] || {}
    Aws::SSM::Client.new(options)
  end
end

#get!Object



46
47
48
49
50
# File 'lib/lamby/ssm_parameter_store.rb', line 46

def get!
  get_all!
  get_history! unless label.to_s.empty?
  self
end

#labelObject



52
53
54
# File 'lib/lamby/ssm_parameter_store.rb', line 52

def label
  ENV['LAMBY_SSM_PARAMS_LABEL'] || @options[:label]
end

#to_dotenvObject



42
43
44
# File 'lib/lamby/ssm_parameter_store.rb', line 42

def to_dotenv
  File.open(dotenv_file, 'a') { |f| f.write(dotenv_contents) }
end

#to_env(overwrite: true) ⇒ Object



36
37
38
39
40
# File 'lib/lamby/ssm_parameter_store.rb', line 36

def to_env(overwrite: true)
  params.each do |param|
    overwrite ? ENV[param.env] = param.value : ENV[param.env] ||= param.value
  end
end