diff options
author | Brandon Weeks <weeks@squareup.com> | 2016-02-17 13:19:43 -0800 |
---|---|---|
committer | Brandon Weeks <weeks@squareup.com> | 2016-02-17 13:19:43 -0800 |
commit | 18423747f8c6e94b1f33f08f90ae623a6340b12a (patch) | |
tree | 66991c28988f82415c1777077962f379c7e55b1b | |
parent | 07d7d5f42ad84047f0512a28bc0136c4eed53ff8 (diff) |
add support for pagination
-rwxr-xr-x | foreman_ansible_inventory.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py index bb2f6d4..ea39ebe 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): """ |