From 33b550fc76bad8fe51f78cc7280586df4d34aea9 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 9 Mar 2016 00:53:33 -0500 Subject: [PATCH] paging for amazon modules this adds a decorator for use in amazon modules so retrieval functions can page using the 'marker' that most boto functions return --- lib/ansible/module_utils/ec2.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py index 4fa7631f008..52b085eb786 100644 --- a/lib/ansible/module_utils/ec2.py +++ b/lib/ansible/module_utils/ec2.py @@ -26,6 +26,7 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import os +from time import sleep try: import boto3 @@ -237,3 +238,27 @@ def ec2_connect(module): module.fail_json(msg="Either region or ec2_url must be specified") return ec2 + +def paging(pause=0): + """ Adds paging to boto retrieval functions that support 'marker' """ + def wrapper(f): + def page(*args, **kwargs): + results = [] + marker = None + while True: + try: + new = f(*args, marker=marker, **kwargs) + marker = new.next_marker + results.extend(new) + if not marker: + break + elif pause: + sleep(pause) + except TypeError: + # Older version of boto do not allow for marker param, just run normally + results = f(*args, **kwargs) + break + return results + return page + return wrapper +