Azure inventory sync!

This commit is contained in:
Luke Sneeringer
2014-07-29 13:06:27 -05:00
parent 504b810418
commit cf96240556
4 changed files with 108 additions and 38 deletions

View File

@@ -85,7 +85,9 @@ class AzureInventory(object):
elif not self.is_cache_valid():
self.do_api_calls_update_cache()
if self.args.list_images:
if self.args.host:
data_to_print = self.get_host(self.args.host)
elif self.args.list_images:
data_to_print = self.json_format_dict(self.get_images(), True)
elif self.args.list:
# Display list of nodes for inventory
@@ -96,6 +98,23 @@ class AzureInventory(object):
print data_to_print
def get_host(self, hostname):
"""Return information about the given hostname, based on what
the Windows Azure API provides.
"""
# Strip ".cloudapp.net" off of the end of the hostname if
# it is present.
if hostname.endswith('.cloudapp.net'):
hostname = hostname.replace('.cloudapp.net', '')
# Retrieve information about the host.
host = self.sms.get_hosted_service_properties(hostname)
hsp = host.hosted_service_properties # Because reasons.
return json.dumps({
'label': hsp.label,
'status': hsp.status.lower(),
})
def get_images(self):
images = []
for image in self.sms.list_os_images():
@@ -142,13 +161,20 @@ class AzureInventory(object):
def parse_cli_args(self):
"""Command line argument processing"""
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on Azure')
parser = argparse.ArgumentParser(
description='Produce an Ansible Inventory file based on Azure',
)
parser.add_argument('--list', action='store_true', default=True,
help='List nodes (default: True)')
help='List nodes (default: True)')
parser.add_argument('--list-images', action='store',
help='Get all available images.')
parser.add_argument('--refresh-cache', action='store_true', default=False,
help='Force refresh of cache by making API requests to Azure (default: False - use cache files)')
help='Get all available images.')
parser.add_argument('--refresh-cache',
action='store_true', default=False,
help='Force refresh of thecache by making API requests to Azure '
'(default: False - use cache files)',
)
parser.add_argument('--host', action='store',
help='Get all information about an instance.')
self.args = parser.parse_args()
def do_api_calls_update_cache(self):