Adding multiple project support for GCE (#39473)

* Adding multiple project support for GCP inventory.

* Adding some documentation on the .ini file
This commit is contained in:
John Roach 2018-05-11 11:38:13 -04:00 committed by ansibot
parent 21004d86f9
commit 22456a57de
2 changed files with 24 additions and 15 deletions

View file

@ -29,6 +29,7 @@
# (See ansible/test/gce_tests.py comments for full install instructions) # (See ansible/test/gce_tests.py comments for full install instructions)
# #
# Author: Eric Johnson <erjohnso@google.com> # Author: Eric Johnson <erjohnso@google.com>
# Contributors: John Roach <johnroach1985@gmail.com>
[gce] [gce]
# GCE Service Account configuration information can be stored in the # GCE Service Account configuration information can be stored in the
@ -41,6 +42,8 @@ libcloud_secrets =
# If you are not going to use a 'secrets.py' file, you can set the necessary # If you are not going to use a 'secrets.py' file, you can set the necessary
# authorization parameters here. # authorization parameters here.
# You can add multiple gce projects to by using a comma separated list. Make
# sure that the service account used has permissions on said projects.
gce_service_account_email_address = gce_service_account_email_address =
gce_service_account_pem_file_path = gce_service_account_pem_file_path =
gce_project_id = gce_project_id =

View file

@ -70,8 +70,9 @@ Examples:
$ contrib/inventory/gce.py --host my_instance $ contrib/inventory/gce.py --host my_instance
Author: Eric Johnson <erjohnso@google.com> Author: Eric Johnson <erjohnso@google.com>
Contributors: Matt Hite <mhite@hotmail.com>, Tom Melendez <supertom@google.com> Contributors: Matt Hite <mhite@hotmail.com>, Tom Melendez <supertom@google.com>,
Version: 0.0.3 John Roach <johnroach1985@gmail.com>
Version: 0.0.4
''' '''
try: try:
@ -167,7 +168,7 @@ class GceInventory(object):
# Read settings and parse CLI arguments # Read settings and parse CLI arguments
self.parse_cli_args() self.parse_cli_args()
self.config = self.get_config() self.config = self.get_config()
self.driver = self.get_gce_driver() self.drivers = self.get_gce_drivers()
self.ip_type = self.get_inventory_options() self.ip_type = self.get_inventory_options()
if self.ip_type: if self.ip_type:
self.ip_type = self.ip_type.lower() self.ip_type = self.ip_type.lower()
@ -277,9 +278,9 @@ class GceInventory(object):
ip_type = os.environ.get('INVENTORY_IP_TYPE', ip_type) ip_type = os.environ.get('INVENTORY_IP_TYPE', ip_type)
return ip_type return ip_type
def get_gce_driver(self): def get_gce_drivers(self):
"""Determine the GCE authorization settings and return a """Determine the GCE authorization settings and return a list of
libcloud driver. libcloud drivers.
""" """
# Attempt to get GCE params from a configuration file, if one # Attempt to get GCE params from a configuration file, if one
# exists. # exists.
@ -325,12 +326,16 @@ class GceInventory(object):
kwargs['project'] = os.environ.get('GCE_PROJECT', kwargs['project']) kwargs['project'] = os.environ.get('GCE_PROJECT', kwargs['project'])
kwargs['datacenter'] = os.environ.get('GCE_ZONE', kwargs['datacenter']) kwargs['datacenter'] = os.environ.get('GCE_ZONE', kwargs['datacenter'])
# Retrieve and return the GCE driver. gce_drivers = []
gce = get_driver(Provider.GCE)(*args, **kwargs) projects = kwargs['project'].split(',')
gce.connection.user_agent_append( for project in projects:
'%s/%s' % (USER_AGENT_PRODUCT, USER_AGENT_VERSION), kwargs['project'] = project
) gce = get_driver(Provider.GCE)(*args, **kwargs)
return gce gce.connection.user_agent_append(
'%s/%s' % (USER_AGENT_PRODUCT, USER_AGENT_VERSION),
)
gce_drivers.append(gce)
return gce_drivers
def parse_env_zones(self): def parse_env_zones(self):
'''returns a list of comma separated zones parsed from the GCE_ZONE environment variable. '''returns a list of comma separated zones parsed from the GCE_ZONE environment variable.
@ -420,9 +425,10 @@ class GceInventory(object):
all_nodes = [] all_nodes = []
params, more_results = {'maxResults': 500}, True params, more_results = {'maxResults': 500}, True
while more_results: while more_results:
self.driver.connection.gce_params = params for driver in self.drivers:
all_nodes.extend(self.driver.list_nodes()) driver.connection.gce_params = params
more_results = 'pageToken' in params all_nodes.extend(driver.list_nodes())
more_results = 'pageToken' in params
return all_nodes return all_nodes
def group_instances(self, zones=None): def group_instances(self, zones=None):