Add API pagination support to Scaleway inventory (#46117)

* Add Scaleway API pagination to server inventory call

* Move Link parsing to helper module

* Correct some PEP8 errors

* Replace AnsibleError with ScalewayException in module_utils since the former doesn't work

* Simplify the regexes to match the intended purpose

* Cleanup helper to conform to review

* Cleanup Scaleway inventory to conform to review

* Flatten the conditional branches structure

* fix a regexp typo
This commit is contained in:
Johann Queuniet 2018-10-24 18:53:46 +02:00 committed by John R Barker
parent 5959158612
commit 74ce8ce935
2 changed files with 51 additions and 15 deletions

View file

@ -1,4 +1,5 @@
import json
import re
import sys
from ansible.module_utils.basic import env_fallback
@ -29,6 +30,29 @@ class ScalewayException(Exception):
self.message = message
# Specify a complete Link header, for validation purposes
R_LINK_HEADER = r'''<[^>]+>;\srel="(first|previous|next|last)"
(,<[^>]+>;\srel="(first|previous|next|last)")*'''
# Specify a single relation, for iteration and string extraction purposes
R_RELATION = r'<(?P<target_IRI>[^>]+)>; rel="(?P<relation>first|previous|next|last)"'
def parse_pagination_link(header):
if not re.match(R_LINK_HEADER, header, re.VERBOSE):
raise ScalewayException('Scaleway API answered with an invalid Link pagination header')
else:
relations = header.split(',')
parsed_relations = {}
rc_relation = re.compile(R_RELATION)
for relation in relations:
match = rc_relation.match(relation)
if not match:
raise ScalewayException('Scaleway API answered with an invalid relation in the Link pagination header')
data = match.groupdict()
parsed_relations[data['relation']] = data['target_IRI']
return parsed_relations
class Response(object):
def __init__(self, resp, info):