aboutsummaryrefslogtreecommitdiff
path: root/foreman_ansible_inventory.py
diff options
context:
space:
mode:
authorBrandon Weeks <weeks@squareup.com>2016-02-25 14:00:00 -0800
committerBrandon Weeks <weeks@squareup.com>2016-02-25 14:04:53 -0800
commit8688673f8bf419762643dd263dd23433b0d2b201 (patch)
tree7f7692fdbb6efdf3c5f4c443229748fc410aee49 /foreman_ansible_inventory.py
parent18423747f8c6e94b1f33f08f90ae623a6340b12a (diff)
Pagination logic improvements
* Rewrite _get_json() to not use recursion * Increase per_page limit to 250
Diffstat (limited to 'foreman_ansible_inventory.py')
-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 ea39ebe..eaae95f 100755
--- a/foreman_ansible_inventory.py
+++ b/foreman_ansible_inventory.py
@@ -124,19 +124,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)