From d95c8b1b0feab72583ba7b4984fb387a62cd6df3 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 7 May 2018 11:19:35 -0400 Subject: [PATCH] Handle pagination The spec on this is pretty subpar. This is hacked in to resolve a production issue! Failed to realize that the Ruby SDK limits parameter responses to 10 because the CLI does not. --- lib/psenv/retriever.rb | 13 ++++++++++--- spec/retriever_spec.rb | 30 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/psenv/retriever.rb b/lib/psenv/retriever.rb index 731c60c..420809d 100644 --- a/lib/psenv/retriever.rb +++ b/lib/psenv/retriever.rb @@ -38,9 +38,16 @@ module Psenv end def parameters - ssm. - get_parameters_by_path(path: @path, with_decryption: true). - parameters + parameters = [] + response = ssm.get_parameters_by_path(path: @path, with_decryption: true) + parameters << response.parameters + + while response.next_page? + response = response.next_page + parameters << response.parameters + end + + parameters.flatten rescue StandardError => error raise RetrieveError, error end diff --git a/spec/retriever_spec.rb b/spec/retriever_spec.rb index 87eff01..97090a9 100644 --- a/spec/retriever_spec.rb +++ b/spec/retriever_spec.rb @@ -41,7 +41,7 @@ RSpec.describe Psenv::Retriever do subject { Psenv::Retriever.new("/psenv/test").call } - context "with a successful request" do + context "with a single page request" do before(:each) do allow(ssm).to receive(:get_parameters_by_path) { OpenStruct.new( @@ -60,6 +60,34 @@ RSpec.describe Psenv::Retriever do end end + context "with multiple pages" do + before(:each) do + allow(ssm).to receive(:get_parameters_by_path) { + OpenStruct.new( + parameters: [{ + name: "/psenv/test/API_KEY", + value: "value", + type: "String", + version: 1, + }], + next_page?: true, + next_page: OpenStruct.new( + parameters: [{ + name: "/psenv/test/CLIENT_KEY", + value: "value", + type: "String", + version: 1, + }], + ), + ) + } + end + + it "returns both parameters" do + expect(subject).to eq("API_KEY" => "value", "CLIENT_KEY" => "value") + end + end + context "when the request fails" do before(:each) { allow(ssm).to receive(:get_parameters_by_path).and_raise }