From 80503bb976e5d8afb2da8213a99c7dd94db8085b Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Mon, 17 Jun 2019 13:10:28 -0700 Subject: [PATCH] better metadata formatting on gcp_compute (#57778) --- plugins/inventory/gcp_compute.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/gcp_compute.py b/plugins/inventory/gcp_compute.py index 4e1310e..861ebd2 100644 --- a/plugins/inventory/gcp_compute.py +++ b/plugins/inventory/gcp_compute.py @@ -236,8 +236,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if 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 @@ -260,6 +258,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if 'subnetwork' in network: 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['image'] = self._get_image(host, project_disks) return items @@ -300,6 +303,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): '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): ''' :param item: A host response from GCP