Skip to content

Commit

Permalink
Merge pull request #12 from CDLUC3/add-support-for-paging
Browse files Browse the repository at this point in the history
Update to get `parameters_for_path` to return ALL SSM parameters for the given path
  • Loading branch information
briri authored Jun 15, 2021
2 parents 74b5d8d + 974d234 commit 5af8578
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
17 changes: 15 additions & 2 deletions lib/uc3-ssm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def parameters_for_path(**options)
path_list.each do |root_path|
begin
options[:path] = root_path
resp = @client.get_parameters_by_path(options)
param_list += resp.parameters if !resp.nil? && resp.parameters.any?
param_list += fetch_param_list(options)
rescue Aws::SSM::Errors::ParameterNotFound
@logger.debug "ParameterNotFound for path '#{root_path}' in parameters_by_path"
next
Expand Down Expand Up @@ -239,6 +238,20 @@ def retrieve_ssm_value(key)
rescue StandardError => e
raise ConfigResolverError, "Cannot read SSM parameter #{key} - #{e.message}"
end

# Recursively gather the parameters from SSM
def fetch_param_list(**options)
param_list = []
resp = @client.get_parameters_by_path(options)
return param_list unless resp.present? && resp.parameters.any?

param_list += resp.parameters
options[:next_token] = resp.next_token

param_list += fetch_param_list(options) if options[:next_token].present?
param_list
end

end
# rubocop:enable Metrics/ClassLength
end
Expand Down
2 changes: 1 addition & 1 deletion lib/uc3-ssm/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Uc3Ssm
VERSION = '0.3.0'
VERSION = '0.3.1'
end
11 changes: 10 additions & 1 deletion spec/test/resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def mock_ssm_not_found(name)
it 'throws ConfigResolverError if no ssm_root_path and path is not fully qualified' do
expect do
@resolver.parameters_for_path(path: 'badpath')
end.to raise_exception(Uc3Ssm::ConfigResolverError)
end.to raise_exception(Uc3Ssm::ConfigResolverError)
end
it 'searches over ssm_root_path when options[path] not specified' do
allow_any_instance_of(Aws::SSM::Client).to receive(:get_parameters_by_path).with(path: '/root/path/')
Expand Down Expand Up @@ -122,6 +122,15 @@ def mock_ssm_not_found(name)
.with(path: '/other/path/foo').and_return(resp2)
expect(@resolver_prefix_list.parameters_for_path(path: 'foo')).to eql(%w[c d])
end
it 'returns more than 10 params' do
resp1 = OpenStruct.new(parameters: %w[0 1 2 3 4 5 6 7 8 9], next_token: "foo")
resp2 = OpenStruct.new(parameters: %w[a])
allow_any_instance_of(Aws::SSM::Client).to receive(:fetch_param_list)
.with(path: '/root/path/foo').and_return(resp1)
allow_any_instance_of(Aws::SSM::Client).to receive(:get_parameters_by_path)
.with(path: '/other/path/foo', next_token: "foo").and_return(resp2)
expect(@resolver_prefix_list.parameters_for_path(path: 'foo')).to eql(%w[0 1 2 3 4 5 6 7 8 9 a])
end
end

describe '#parameter_for_key(key)' do
Expand Down

0 comments on commit 5af8578

Please sign in to comment.