Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Add gce_img module for utilizing GCE image resources #229

Merged
merged 3 commits into from Feb 2, 2015
Merged

Add gce_img module for utilizing GCE image resources #229

merged 3 commits into from Feb 2, 2015

Conversation

tanpeter
Copy link
Contributor

No description provided.


import sys

# import module snippets
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These module snippets should actually appear as the line directly before the call to main()

@sivel
Copy link
Member

sivel commented Jan 28, 2015

cc @erjohnso


DOCUMENTATION = '''
---
module: gce_img
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add version_added: "1.9" here and remove it from the options below.

@erjohnso
Copy link
Contributor

Thanks for contributing this @tanpeter! And thanks for the quick review @sivel.

@tanpeter
Copy link
Contributor Author

Ok, I got your idea now. My only concern is when gce.ex_create_image() returns successfully, I can't effectively know is it a new image or an existing image, then how do I flip the changed flag?

@erjohnso
Copy link
Contributor

I usually set changed = False early on and then only flip it to true if something has changed. In this case, when you call gce.ex_create_image() and the call succeeds, you can set changed = True. If the call errors out because the resource already exists, you can leave changed = False, but not throw a module error. That gives us the idempotency that ansible strives to follow.

@tanpeter
Copy link
Contributor Author

This looks like a libcloud bug: when I pass in string parameter to gce.ex_delete_image() and the image doesn't exist, I got:

File ".ansible_module_generated", line 151, in delete_image return gce.ex_delete_image("fedora1")
File "/usr/local/lib/python2.7/dist-packages/libcloud/compute/drivers/gce.py", line 2172, in ex_delete_image
    request = '/global/images/%s' % (image.name)
AttributeError: 'NoneType' object has no attribute 'name'

And libcloud does the following when the parameter is not a GCENodeImage:

if not hasattr(image, 'name'):
            image = self.ex_get_image(image)

So there will be an ex_get_image() call in either cases.

@erjohnso
Copy link
Contributor

Hmm... what version of libcloud are you using? It looks like trunk handles string and object OK unless I'm missing something: https://github.com/apache/libcloud/blob/trunk/libcloud/compute/drivers/gce.py#L3294

FWIW, the plan is to release libcloud 0.17.0 this weekend, so if there is a bug I can get it merged in today.

@tanpeter
Copy link
Contributor Author

Please look at line 3307, if the image does not exist, self.ex_get_image() would return None; then referencing None.name will crash.

@erjohnso
Copy link
Contributor

Ah, gotcha! Thanks, I'll wrap that with a try/except NotFound error. I'll /cc you on the PR.

Then you'll be able to do something like,

def delete_image(gce, image, module):
  """Delete a specific image resource."""
  try:
     image = gce.ex_get_image(image)
     image.delete()
  except ResourceNotFoundError:
     pass
  except GoogleBaseError, e:
     module.fail_json(msg=str(e), changed=False)

@erjohnso
Copy link
Contributor

@tanpeter - ok, merged apache/libcloud#441

…e.ex_delete_image(), instead catching the ResourceExistsError or ResourceNotFoundError respectively.
@erjohnso
Copy link
Contributor

erjohnso commented Feb 2, 2015

@bcoca - Hi Brian, new GCE module ready for merge (unless you want to review). With the recent org change, I wanted an ACK before just hitting the merge button. Are you (in general) OK with me helping maintain GCE modules?

@bcoca
Copy link
Member

bcoca commented Feb 2, 2015

looks good to me. It is fine if you continue in charge of the GCE modules as you have up to now.

@erjohnso
Copy link
Contributor

erjohnso commented Feb 2, 2015

Thanks Brian.

erjohnso added a commit that referenced this pull request Feb 2, 2015
Add gce_img module for utilizing GCE image resources
@erjohnso erjohnso merged commit ed35fc6 into ansible:devel Feb 2, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants