From d39d4d70261cf822991507a968704baffa61c2c6 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Wed, 20 Dec 2023 14:48:43 -0800 Subject: [PATCH 1/3] Added pagination support to ssm_parameter --- lib/awspec/helper/finder/ssm_parameter.rb | 26 +++++++++++++--------- lib/awspec/stub/ssm_parameter.rb | 27 ++++++++++++++++------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/awspec/helper/finder/ssm_parameter.rb b/lib/awspec/helper/finder/ssm_parameter.rb index a54db831..8ac3ae7d 100644 --- a/lib/awspec/helper/finder/ssm_parameter.rb +++ b/lib/awspec/helper/finder/ssm_parameter.rb @@ -4,16 +4,22 @@ module Awspec::Helper module Finder module SsmParameter def find_ssm_parameter(name) - ssm_client.describe_parameters( - { - filters: [ - { - key: 'Name', - values: [name] - } - ] - } - ).parameters[0] + req = { + filters: [ + { + key: 'Name', + values: [name] + } + ] + } + loop do + res = ssm_client.describe_parameters(req) + if res.parameters.size >= 1 + return res.parameters.first + end + break if res.next_token.nil? + req[:next_token] = res.next_token + end end def find_parameter_tag(id, tag_key) diff --git a/lib/awspec/stub/ssm_parameter.rb b/lib/awspec/stub/ssm_parameter.rb index 3d52fa2f..17efac29 100644 --- a/lib/awspec/stub/ssm_parameter.rb +++ b/lib/awspec/stub/ssm_parameter.rb @@ -1,18 +1,29 @@ # frozen_string_literal: true +next_token = 'eyJOZXh0VG9rZW4iOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAxfQ==' + Aws.config[:ssm] = { stub_responses: { - describe_parameters: { - parameters: [ + describe_parameters: -> (context) { + if context.params[:next_token] == next_token { - name: 'my-parameter', - type: 'SecureString', - key_id: 'alias/aws/ssm', - description: 'Some description', - version: 1, + parameters: [ + { + name: 'my-parameter', + type: 'SecureString', + key_id: 'alias/aws/ssm', + description: 'Some description', + version: 1, + } + ], next_token: nil } - ] + else + { + parameters: [], + next_token: next_token + } + end } } } From 3469d083efbb03d94f0d459588ebbd64ccfd6084 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Wed, 20 Dec 2023 15:01:07 -0800 Subject: [PATCH 2/3] Fixed lint issues --- lib/awspec/helper/finder/ssm_parameter.rb | 1 + lib/awspec/stub/ssm_parameter.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/awspec/helper/finder/ssm_parameter.rb b/lib/awspec/helper/finder/ssm_parameter.rb index 8ac3ae7d..bdf03988 100644 --- a/lib/awspec/helper/finder/ssm_parameter.rb +++ b/lib/awspec/helper/finder/ssm_parameter.rb @@ -18,6 +18,7 @@ def find_ssm_parameter(name) return res.parameters.first end break if res.next_token.nil? + req[:next_token] = res.next_token end end diff --git a/lib/awspec/stub/ssm_parameter.rb b/lib/awspec/stub/ssm_parameter.rb index 17efac29..e4e43fb8 100644 --- a/lib/awspec/stub/ssm_parameter.rb +++ b/lib/awspec/stub/ssm_parameter.rb @@ -4,7 +4,7 @@ Aws.config[:ssm] = { stub_responses: { - describe_parameters: -> (context) { + describe_parameters: lambda { |context| if context.params[:next_token] == next_token { parameters: [ @@ -13,7 +13,7 @@ type: 'SecureString', key_id: 'alias/aws/ssm', description: 'Some description', - version: 1, + version: 1 } ], next_token: nil From 8c7193d540108458938c22e4dad587071b48ca67 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Wed, 20 Dec 2023 16:33:40 -0800 Subject: [PATCH 3/3] lint/test fix --- lib/awspec/stub/ssm_parameter.rb | 45 ++++++++++++++++---------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/awspec/stub/ssm_parameter.rb b/lib/awspec/stub/ssm_parameter.rb index e4e43fb8..9bebd687 100644 --- a/lib/awspec/stub/ssm_parameter.rb +++ b/lib/awspec/stub/ssm_parameter.rb @@ -1,29 +1,30 @@ # frozen_string_literal: true -next_token = 'eyJOZXh0VG9rZW4iOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAxfQ==' - -Aws.config[:ssm] = { - stub_responses: { - describe_parameters: lambda { |context| - if context.params[:next_token] == next_token - { - parameters: [ - { - name: 'my-parameter', - type: 'SecureString', - key_id: 'alias/aws/ssm', - description: 'Some description', - version: 1 - } - ], - next_token: nil - } - else +def describe_parameters_response(context) + next_token = 'eyJOZXh0VG9rZW4iOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAxfQ==' + if context.params[:next_token] == next_token + { + parameters: [ { - parameters: [], - next_token: next_token + name: 'my-parameter', + type: 'SecureString', + key_id: 'alias/aws/ssm', + description: 'Some description', + version: 1 } - end + ], + next_token: nil } + else + { + parameters: [], + next_token: next_token + } + end +end + +Aws.config[:ssm] = { + stub_responses: { + describe_parameters: ->(context) { describe_parameters_response(context) } } }