Skip to content

Commit

Permalink
Read paginated response for gallery image versions (#1073)
Browse files Browse the repository at this point in the history
* Read paginated response for gallery image versions

The query returns a pagineated response with a "nextLink" key in the response object. If the response has such a key, we launch another request to collect the next batch, etc.

* Fix indentation
  • Loading branch information
Autom3 authored Feb 8, 2023
1 parent 45d7f40 commit 48c2c25
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions plugins/modules/azure_rm_galleryimageversion_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def get(self):

def listbygalleryimage(self):
response = None
results = {}
results = dict(
response=[]
)
# prepare url
self.url = ('/subscriptions' +
'/{{ subscription_id }}' +
Expand All @@ -230,20 +232,37 @@ def listbygalleryimage(self):
self.url = self.url.replace('{{ image_name }}', self.gallery_image_name)

try:
response = self.mgmt_client.query(self.url,
'GET',
self.query_parameters,
self.header_parameters,
None,
self.status_code,
600,
30)
results = json.loads(response.text)
skiptoken = None

while True:
if skiptoken:
self.query_parameters['skiptoken'] = skiptoken

response = self.mgmt_client.query(self.url,
'GET',
self.query_parameters,
self.header_parameters,
None,
[200, 404],
0,
0)
try:
response = json.loads(response.text)
if isinstance(response, dict):
if response.get('value'):
results['response'] = results['response'] + response['value']
skiptoken = response.get('nextLink')
else:
results['response'] = results['response'] + [response]
except Exception as e:
self.fail('Failed to parse response: ' + str(e))
if not skiptoken:
break
# self.log('Response : {0}'.format(response))
except CloudError as e:
self.log('Could not get info for @(Model.ModuleOperationNameUpper).')

return [self.format_item(x) for x in results['value']] if results['value'] else []
return [self.format_item(x) for x in results['response']] if results['response'] else []

def format_item(self, item):
d = {
Expand Down

0 comments on commit 48c2c25

Please sign in to comment.