From 5987f60e3fe56aba75c1b264ff924e6a04e718f0 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Fri, 1 Aug 2014 10:22:14 -0500 Subject: [PATCH] Add _meta to Azure inventory script. --- awx/plugins/inventory/windows_azure.py | 27 ++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/awx/plugins/inventory/windows_azure.py b/awx/plugins/inventory/windows_azure.py index 156072e5b2..8be899d939 100755 --- a/awx/plugins/inventory/windows_azure.py +++ b/awx/plugins/inventory/windows_azure.py @@ -92,13 +92,23 @@ class AzureInventory(object): elif self.args.list: # Display list of nodes for inventory if len(self.inventory) == 0: - data_to_print = self.get_inventory_from_cache() + data = json.loads(self.get_inventory_from_cache()) else: - data_to_print = self.json_format_dict(self.inventory, True) + data = self.inventory + + # Add the `_meta` information. + _meta = {} + for host in set(reduce(lambda x, y: x + y, + [i for i in data.values()])): + _meta[host] = self.get_host(host, jsonify=False) + data['_meta'] = _meta + + # JSONify the data. + data_to_print = self.json_format_dict(data, pretty=True) print data_to_print - def get_host(self, hostname): + def get_host(self, hostname, jsonify=True): """Return information about the given hostname, based on what the Windows Azure API provides. """ @@ -110,10 +120,13 @@ class AzureInventory(object): # Retrieve information about the host. host = self.sms.get_hosted_service_properties(hostname) hsp = host.hosted_service_properties # Because reasons. - return json.dumps({ + answer = { 'label': hsp.label, 'status': hsp.status.lower(), - }) + } + if jsonify: + return json.dumps(answer) + return answer def get_images(self): images = [] @@ -195,7 +208,9 @@ class AzureInventory(object): sys.exit(1) def add_deployments(self, cloud_service): - """Makes an Azure API call to get the list of virtual machines associated with a cloud service""" + """Makes an Azure API call to get the list of virtual machines + associated with a cloud service. + """ try: for deployment in self.sms.get_hosted_service_properties(cloud_service.service_name,embed_detail=True).deployments.deployments: if deployment.deployment_slot == "Production":