Address review comments from @sivel and @erjohnso
This commit is contained in:
parent
42fbeda997
commit
35a4212b5f
1 changed files with 82 additions and 59 deletions
|
@ -18,26 +18,10 @@
|
||||||
|
|
||||||
"""An Ansible module to utilize GCE image resources."""
|
"""An Ansible module to utilize GCE image resources."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
from ansible.module_utils.gce import *
|
|
||||||
|
|
||||||
try:
|
|
||||||
from libcloud.compute.types import Provider
|
|
||||||
from libcloud.compute.providers import get_driver
|
|
||||||
from libcloud.common.google import GoogleBaseError
|
|
||||||
from libcloud.common.google import ResourceNotFoundError
|
|
||||||
_ = Provider.GCE
|
|
||||||
except ImportError:
|
|
||||||
print('failed=True '
|
|
||||||
"msg='libcloud with GCE support is required for this module.'")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: gce_img
|
module: gce_img
|
||||||
|
version_added: "1.9"
|
||||||
short_description: utilize GCE image resources
|
short_description: utilize GCE image resources
|
||||||
description:
|
description:
|
||||||
- This module can create and delete GCE private images from gzipped
|
- This module can create and delete GCE private images from gzipped
|
||||||
|
@ -46,7 +30,7 @@ description:
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
- the name of the image to create
|
- the name of the image to create or delete
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
aliases: []
|
aliases: []
|
||||||
|
@ -61,7 +45,7 @@ options:
|
||||||
- desired state of the image
|
- desired state of the image
|
||||||
required: false
|
required: false
|
||||||
default: "present"
|
default: "present"
|
||||||
choices: ["active", "present", "absent", "deleted"]
|
choices: ["present", "absent"]
|
||||||
aliases: []
|
aliases: []
|
||||||
zone:
|
zone:
|
||||||
description:
|
description:
|
||||||
|
@ -70,21 +54,18 @@ options:
|
||||||
default: "us-central1-a"
|
default: "us-central1-a"
|
||||||
aliases: []
|
aliases: []
|
||||||
service_account_email:
|
service_account_email:
|
||||||
version_added: "1.6"
|
|
||||||
description:
|
description:
|
||||||
- service account email
|
- service account email
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: []
|
aliases: []
|
||||||
pem_file:
|
pem_file:
|
||||||
version_added: "1.6"
|
|
||||||
description:
|
description:
|
||||||
- path to the pem file associated with the service account email
|
- path to the pem file associated with the service account email
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: []
|
aliases: []
|
||||||
project_id:
|
project_id:
|
||||||
version_added: "1.6"
|
|
||||||
description:
|
description:
|
||||||
- your GCE project ID
|
- your GCE project ID
|
||||||
required: false
|
required: false
|
||||||
|
@ -103,48 +84,52 @@ EXAMPLES = '''
|
||||||
zone: us-central1-a
|
zone: us-central1-a
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
# Delete an image named test-image in zone us-central1-a.
|
# Create an image named test-image from a tarball in Google Cloud Storage.
|
||||||
- gce_img:
|
- gce_img:
|
||||||
name: test-image
|
name: test-image
|
||||||
zone: us-central1-a
|
source: https://storage.googleapis.com/bucket/path/to/image.tgz
|
||||||
state: deleted
|
|
||||||
|
# Alternatively use the gs scheme
|
||||||
|
- gce_img:
|
||||||
|
name: test-image
|
||||||
|
source: gs://bucket/path/to/image.tgz
|
||||||
|
|
||||||
|
# Delete an image named test-image.
|
||||||
|
- gce_img:
|
||||||
|
name: test-image
|
||||||
|
state: absent
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import sys
|
||||||
def main():
|
|
||||||
module = AnsibleModule(
|
|
||||||
argument_spec=dict(
|
|
||||||
name=dict(required=True),
|
|
||||||
source=dict(),
|
|
||||||
state=dict(default='present'),
|
|
||||||
zone=dict(default='us-central1-a'),
|
|
||||||
service_account_email=dict(),
|
|
||||||
pem_file=dict(),
|
|
||||||
project_id=dict(),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
gce = gce_connect(module)
|
|
||||||
|
|
||||||
name = module.params.get('name')
|
|
||||||
source = module.params.get('source')
|
|
||||||
state = module.params.get('state')
|
|
||||||
zone = module.params.get('zone')
|
|
||||||
changed = False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
image = gce.ex_get_image(name)
|
from libcloud.compute.types import Provider
|
||||||
except GoogleBaseError, e:
|
from libcloud.compute.providers import get_driver
|
||||||
module.fail_json(msg=str(e), changed=False)
|
from libcloud.common.google import GoogleBaseError
|
||||||
|
from libcloud.common.google import ResourceNotFoundError
|
||||||
|
_ = Provider.GCE
|
||||||
|
has_libcloud = True
|
||||||
|
except ImportError:
|
||||||
|
has_libcloud = False
|
||||||
|
|
||||||
|
|
||||||
|
GCS_URI = 'https://storage.googleapis.com/'
|
||||||
|
|
||||||
|
|
||||||
|
def create_image(gce, name, module):
|
||||||
|
"""Create an image with the specified name."""
|
||||||
|
source = module.params.get('source')
|
||||||
|
zone = module.params.get('zone')
|
||||||
|
|
||||||
# user wants to create an image.
|
|
||||||
if state in ['active', 'present'] and not image:
|
|
||||||
if not source:
|
if not source:
|
||||||
module.fail_json(msg='Must supply a source', changed=False)
|
module.fail_json(msg='Must supply a source', changed=False)
|
||||||
|
|
||||||
if source.startswith('https://storage.googleapis.com'):
|
if source.startswith(GCS_URI):
|
||||||
# source is a Google Cloud Storage URI
|
# source is a Google Cloud Storage URI
|
||||||
volume = source
|
volume = source
|
||||||
|
elif source.startswith('gs://'):
|
||||||
|
# libcloud only accepts https URI.
|
||||||
|
volume = source.replace('gs://', GCS_URI)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
volume = gce.ex_get_volume(source, zone)
|
volume = gce.ex_get_volume(source, zone)
|
||||||
|
@ -155,20 +140,58 @@ def main():
|
||||||
module.fail_json(msg=str(e), changed=False)
|
module.fail_json(msg=str(e), changed=False)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
image = gce.ex_create_image(name, volume)
|
gce.ex_create_image(name, volume)
|
||||||
changed = True
|
|
||||||
except GoogleBaseError, e:
|
except GoogleBaseError, e:
|
||||||
module.fail_json(msg=str(e), changed=False)
|
module.fail_json(msg=str(e), changed=False)
|
||||||
|
|
||||||
# user wants to delete the image.
|
|
||||||
if state in ['absent', 'deleted'] and image:
|
def delete_image(gce, image, module):
|
||||||
|
"""Delete a specific image resource."""
|
||||||
try:
|
try:
|
||||||
gce.ex_delete_image(image)
|
gce.ex_delete_image(image)
|
||||||
changed = True
|
|
||||||
except GoogleBaseError, e:
|
except GoogleBaseError, e:
|
||||||
module.fail_json(msg=str(e), changed=False)
|
module.fail_json(msg=str(e), changed=False)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module = AnsibleModule(
|
||||||
|
argument_spec=dict(
|
||||||
|
name=dict(required=True),
|
||||||
|
source=dict(),
|
||||||
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
|
zone=dict(default='us-central1-a'),
|
||||||
|
service_account_email=dict(),
|
||||||
|
pem_file=dict(),
|
||||||
|
project_id=dict(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not has_libcloud:
|
||||||
|
module.fail_json(msg='libcloud with GCE support is required.')
|
||||||
|
|
||||||
|
gce = gce_connect(module)
|
||||||
|
|
||||||
|
name = module.params.get('name')
|
||||||
|
state = module.params.get('state')
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
image = gce.ex_get_image(name)
|
||||||
|
|
||||||
|
# user wants to create an image.
|
||||||
|
if state == 'present' and not image:
|
||||||
|
create_image(gce, name, module)
|
||||||
|
changed = True
|
||||||
|
|
||||||
|
# user wants to delete the image.
|
||||||
|
if state == 'absent' and image:
|
||||||
|
delete_image(gce, image, module)
|
||||||
|
changed = True
|
||||||
|
|
||||||
module.exit_json(changed=changed, name=name)
|
module.exit_json(changed=changed, name=name)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
# import module snippets
|
||||||
|
from ansible.module_utils.basic import *
|
||||||
|
from ansible.module_utils.gce import *
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue