aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2016-02-26 18:24:31 +0100
committerGuido Günther <agx@sigxcpu.org>2016-02-26 18:24:31 +0100
commit12cff5111229280c8c264d93cce86509de83d8a9 (patch)
tree56f20454c92b0632b356f1dde58c57c51c1f286d
parent1501b84daaa0a3f80762dc5ebf09f0686caf25cf (diff)
parent8688673f8bf419762643dd263dd23433b0d2b201 (diff)
Merge pull request #6 from brandonweeks/pagination
Pagination logic improvements
-rwxr-xr-xforeman_ansible_inventory.py30
1 files changed, 17 insertions, 13 deletions
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py
index bdf1107..5bb16b6 100755
--- a/foreman_ansible_inventory.py
+++ b/foreman_ansible_inventory.py
@@ -125,19 +125,23 @@ class ForemanInventory(object):
help='Force refresh of cache by making API requests to foreman (default: False - use cache files)')
self.args = parser.parse_args()
- def _get_json(self, url, page = 1, results = []):
- ret = requests.get(url,
- auth=HTTPBasicAuth(self.foreman_user, self.foreman_pw),
- verify=self.foreman_ssl_verify,
- params={'page': page})
- ret.raise_for_status()
- if not ret.json().has_key('results'):
- return ret.json()
- results = results + ret.json()['results']
- if len(results) >= ret.json()['total']:
- return results
- else:
- return self._get_json(url, page=page+1, results=results)
+ def _get_json(self, url):
+ page = 1
+ results = []
+ while True:
+ ret = requests.get(url,
+ auth=HTTPBasicAuth(self.foreman_user, self.foreman_pw),
+ verify=self.foreman_ssl_verify,
+ params={'page': page, 'per_page': 250})
+ ret.raise_for_status()
+ json = ret.json()
+ if not json.has_key('results'):
+ return json
+ results = results + json['results']
+ if len(results) >= json['total']:
+ break
+ page += 1
+ return results
def _get_hosts(self):
return self._get_json("%s/api/v2/hosts" % self.foreman_url)