aboutsummaryrefslogtreecommitdiff
path: root/foreman_ansible_inventory.py
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2016-02-18 08:18:57 +0100
committerGuido Günther <agx@sigxcpu.org>2016-02-18 08:18:57 +0100
commit5c0132cee387eb43664eb1c44f1aafaf642eb7b9 (patch)
treecc1c5d75ed6b2426bd182cc1842cba3bf63d20ed /foreman_ansible_inventory.py
parent16516c79cf8710f55c0aed673d3ba66f8c3f3a29 (diff)
parent18423747f8c6e94b1f33f08f90ae623a6340b12a (diff)
Merge pull request #4 from brandonweeks/pagination
add support for pagination
Diffstat (limited to 'foreman_ansible_inventory.py')
-rwxr-xr-xforeman_ansible_inventory.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py
index aec58c4..dc4fcef 100755
--- a/foreman_ansible_inventory.py
+++ b/foreman_ansible_inventory.py
@@ -124,15 +124,22 @@ 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):
+ 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)
+ verify=self.foreman_ssl_verify,
+ params={'page': page})
ret.raise_for_status()
- return ret.json()
+ 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_hosts(self):
- return self._get_json("%s/api/v2/hosts" % self.foreman_url)['results']
+ return self._get_json("%s/api/v2/hosts" % self.foreman_url)
def _get_hostgroup_by_id(self, hid):
if hid not in self.hostgroups:
@@ -142,7 +149,7 @@ class ForemanInventory(object):
def _get_params_by_id(self, hid):
url = "%s/api/v2/hosts/%s/parameters" % (self.foreman_url, hid)
- return self._get_json(url)['results']
+ return self._get_json(url)
def _resolve_params(self, host):
"""