mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
* Extending modules with resource_id
* Added documentation
* Revert previous PR
* Added filter for active vm's
* Added changelog fragment
* Update changelogs/fragments/720-cloudforms_inventory.yml
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit ac95ff5b45
)
Co-authored-by: phospi <phoffmann@spirit21.com>
This commit is contained in:
parent
720de141b5
commit
0b59a71ae7
2 changed files with 41 additions and 10 deletions
2
changelogs/fragments/720-cloudforms_inventory.yml
Normal file
2
changelogs/fragments/720-cloudforms_inventory.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- cloudforms inventory - fixed issue that non-existing (archived) VMs were synced (https://github.com/ansible-collections/community.general/pull/720).
|
|
@ -216,9 +216,9 @@ class CloudFormsInventory(object):
|
|||
parser.add_argument('--debug', action='store_true', default=False, help='Show debug output while running (default: False)')
|
||||
self.args = parser.parse_args()
|
||||
|
||||
def _get_json(self, url):
|
||||
def _http_request(self, url):
|
||||
"""
|
||||
Make a request and return the JSON
|
||||
Make a request and return the result converted from JSON
|
||||
"""
|
||||
results = []
|
||||
|
||||
|
@ -231,7 +231,8 @@ class CloudFormsInventory(object):
|
|||
try:
|
||||
results = json.loads(ret.text)
|
||||
except ValueError:
|
||||
warnings.warn("Unexpected response from {0} ({1}): {2}".format(self.cloudforms_url, ret.status_code, ret.reason))
|
||||
warnings.warn(
|
||||
"Unexpected response from {0} ({1}): {2}".format(self.cloudforms_url, ret.status_code, ret.reason))
|
||||
results = {}
|
||||
|
||||
if self.args.debug:
|
||||
|
@ -245,11 +246,18 @@ class CloudFormsInventory(object):
|
|||
|
||||
return results
|
||||
|
||||
def _get_hosts(self):
|
||||
def _get_json(self, endpoint, url_suffix):
|
||||
"""
|
||||
Get all hosts by paging through the results
|
||||
Make a request by given url, split request by configured limit,
|
||||
go through all sub-requests and return the aggregated data received
|
||||
by cloudforms
|
||||
|
||||
:param endpoint: api endpoint to access
|
||||
:param url_suffix: additional api parameters
|
||||
|
||||
"""
|
||||
limit = self.cloudforms_limit
|
||||
|
||||
limit = int(self.cloudforms_limit)
|
||||
|
||||
page = 0
|
||||
last_page = False
|
||||
|
@ -258,14 +266,35 @@ class CloudFormsInventory(object):
|
|||
|
||||
while not last_page:
|
||||
offset = page * limit
|
||||
ret = self._get_json("%s/api/vms?offset=%s&limit=%s&expand=resources,tags,hosts,&attributes=ipaddresses" % (self.cloudforms_url, offset, limit))
|
||||
results += ret['resources']
|
||||
url = "%s%s?offset=%s&limit=%s%s" % (
|
||||
self.cloudforms_url, endpoint, offset, limit, url_suffix)
|
||||
|
||||
if self.args.debug:
|
||||
print("Connecting to url '%s'" % url)
|
||||
|
||||
ret = self._http_request(url)
|
||||
results += [ret]
|
||||
|
||||
if 'subcount' in ret:
|
||||
if ret['subcount'] < limit:
|
||||
last_page = True
|
||||
page += 1
|
||||
else:
|
||||
last_page = True
|
||||
|
||||
return results
|
||||
|
||||
def _get_hosts(self):
|
||||
"""
|
||||
Get all hosts
|
||||
"""
|
||||
endpoint = "/api/vms"
|
||||
url_suffix = "&expand=resources,tags,hosts,&attributes=active,ipaddresses&filter[]=active=true"
|
||||
results = self._get_json(endpoint, url_suffix)
|
||||
resources = [item for sublist in results for item in sublist['resources']]
|
||||
|
||||
return resources
|
||||
|
||||
def update_cache(self):
|
||||
"""
|
||||
Make calls to cloudforms and save the output in a cache
|
||||
|
|
Loading…
Reference in a new issue