better metadata formatting on gcp_compute (#57778)

This commit is contained in:
Alex Stephen 2019-06-17 13:10:28 -07:00 committed by ansibot
parent 6e89fbe58a
commit 9364b4c933

View file

@ -236,8 +236,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
if navigate_hash(result, ['error', 'errors']): if navigate_hash(result, ['error', 'errors']):
module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
if result['kind'] != 'compute#instanceAggregatedList' and result['kind'] != 'compute#zoneList':
module.fail_json(msg="Incorrect result: {kind}".format(**result))
return result return result
@ -260,6 +258,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
if 'subnetwork' in network: if 'subnetwork' in network:
network['subnetwork'] = self._format_network_info(network['subnetwork']) network['subnetwork'] = self._format_network_info(network['subnetwork'])
if 'metadata' in host:
# If no metadata, 'items' will be blank.
# We want the metadata hash overriden anyways for consistency.
host['metadata'] = self._format_metadata(host['metadata'].get('items', {}))
host['project'] = host['selfLink'].split('/')[6] host['project'] = host['selfLink'].split('/')[6]
host['image'] = self._get_image(host, project_disks) host['image'] = self._get_image(host, project_disks)
return items return items
@ -300,6 +303,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
'selfLink': address 'selfLink': address
} }
def _format_metadata(self, metadata):
'''
:param metadata: A list of dicts where each dict has keys "key" and "value"
:return a dict with key/value pairs for each in list.
'''
new_metadata = {}
print(metadata)
for pair in metadata:
new_metadata[pair["key"]] = pair["value"]
return new_metadata
def _get_hostname(self, item): def _get_hostname(self, item):
''' '''
:param item: A host response from GCP :param item: A host response from GCP