Merge pull request #2665 from Timalkemade/devel

Change the GCE module to only fetch image names when necessary
reviewable/pr18780/r1
Brian Coca 9 years ago
commit 04cde60d14

@ -355,7 +355,6 @@ def create_instances(module, gce, instance_names):
new_instances = [] new_instances = []
changed = False changed = False
lc_image = gce.ex_get_image(image)
lc_disks = [] lc_disks = []
disk_modes = [] disk_modes = []
for i, disk in enumerate(disks or []): for i, disk in enumerate(disks or []):
@ -397,6 +396,7 @@ def create_instances(module, gce, instance_names):
else: else:
metadata = md metadata = md
lc_image = LazyDiskImage(module, gce, image, lc_disks)
ex_sa_perms = [] ex_sa_perms = []
bad_perms = [] bad_perms = []
if service_account_permissions: if service_account_permissions:
@ -409,7 +409,7 @@ def create_instances(module, gce, instance_names):
ex_sa_perms[0]['scopes'] = service_account_permissions ex_sa_perms[0]['scopes'] = service_account_permissions
# These variables all have default values but check just in case # These variables all have default values but check just in case
if not lc_image or not lc_network or not lc_machine_type or not lc_zone: if not lc_network or not lc_machine_type or not lc_zone:
module.fail_json(msg='Missing required create instance variable', module.fail_json(msg='Missing required create instance variable',
changed=False) changed=False)
@ -419,21 +419,21 @@ def create_instances(module, gce, instance_names):
pd = lc_disks[0] pd = lc_disks[0]
elif persistent_boot_disk: elif persistent_boot_disk:
try: try:
pd = gce.create_volume(None, "%s" % name, image=lc_image)
except ResourceExistsError:
pd = gce.ex_get_volume("%s" % name, lc_zone) pd = gce.ex_get_volume("%s" % name, lc_zone)
except ResourceNotFoundError:
pd = gce.create_volume(None, "%s" % name, image=lc_image())
inst = None inst = None
try: try:
inst = gce.ex_get_node(name, lc_zone)
except ResourceNotFoundError:
inst = gce.create_node( inst = gce.create_node(
name, lc_machine_type, lc_image, location=lc_zone, name, lc_machine_type, lc_image(), location=lc_zone,
ex_network=network, ex_tags=tags, ex_metadata=metadata, ex_network=network, ex_tags=tags, ex_metadata=metadata,
ex_boot_disk=pd, ex_can_ip_forward=ip_forward, ex_boot_disk=pd, ex_can_ip_forward=ip_forward,
external_ip=instance_external_ip, ex_disk_auto_delete=disk_auto_delete, external_ip=instance_external_ip, ex_disk_auto_delete=disk_auto_delete,
ex_service_accounts=ex_sa_perms ex_service_accounts=ex_sa_perms
) )
changed = True changed = True
except ResourceExistsError:
inst = gce.ex_get_node(name, lc_zone)
except GoogleBaseError as e: except GoogleBaseError as e:
module.fail_json(msg='Unexpected error attempting to create ' + module.fail_json(msg='Unexpected error attempting to create ' +
'instance %s, error: %s' % (name, e.value)) 'instance %s, error: %s' % (name, e.value))
@ -587,6 +587,30 @@ def main():
json_output['changed'] = changed json_output['changed'] = changed
module.exit_json(**json_output) module.exit_json(**json_output)
class LazyDiskImage:
"""
Object for lazy instantiation of disk image
gce.ex_get_image is a very expensive call, so we want to avoid calling it as much as possible.
"""
def __init__(self, module, gce, name, has_pd):
self.image = None
self.was_called = False
self.gce = gce
self.name = name
self.has_pd = has_pd
self.module = module
def __call__(self):
if not self.was_called:
self.was_called = True
if not self.has_pd:
self.image = self.gce.ex_get_image(self.name)
if not self.image:
self.module.fail_json(msg='image or disks missing for create instance', changed=False)
return self.image
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.gce import * from ansible.module_utils.gce import *

Loading…
Cancel
Save