Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Initial addition of generated Image to Terraform.
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored and modular-magician committed Dec 29, 2018
1 parent 8aff6fc commit c11f76f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
53 changes: 48 additions & 5 deletions lib/ansible/modules/cloud/google/gcp_compute_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
- The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied encryption
key that protects this resource.
required: false
labels:
description:
- Labels to apply to this Image.
required: false
version_added: 2.8
licenses:
description:
- Any applicable license URI.
Expand Down Expand Up @@ -147,7 +152,7 @@
description:
- The full Google Cloud Storage URL where disk storage is stored You must
provide either this property or the sourceDisk property but not both.
required: false
required: true
source_disk:
description:
- Refers to a gcompute_disk object You must provide either this property or the
Expand Down Expand Up @@ -187,6 +192,9 @@
choices:
- RAW
extends_documentation_fragment: gcp
notes:
- 'API Reference: U(https://cloud.google.com/compute/docs/reference/latest/images)'
- 'Official Documentation: U(https://cloud.google.com/compute/docs/images)'
'''

EXAMPLES = '''
Expand Down Expand Up @@ -329,6 +337,17 @@
key that protects this resource.
returned: success
type: str
labels:
description:
- Labels to apply to this Image.
returned: success
type: dict
labelFingerprint:
description:
- The fingerprint used for optimistic locking of this resource. Used internally
during updates.
returned: success
type: str
licenses:
description:
- Any applicable license URI.
Expand Down Expand Up @@ -438,12 +457,13 @@ def main():
raw_key=dict(type='str'),
sha256=dict(type='str')
)),
labels=dict(type='dict'),
licenses=dict(type='list', elements='str'),
name=dict(required=True, type='str'),
raw_disk=dict(type='dict', options=dict(
container_type=dict(type='str', choices=['TAR']),
sha1_checksum=dict(type='str'),
source=dict(type='str')
source=dict(required=True, type='str')
)),
source_disk=dict(),
source_disk_encryption_key=dict(type='dict', options=dict(
Expand All @@ -467,7 +487,7 @@ def main():
if fetch:
if state == 'present':
if is_different(module, fetch):
update(module, self_link(module), kind)
update(module, self_link(module), kind, fetch)
fetch = fetch_resource(module, self_link(module), kind)
changed = True
else:
Expand All @@ -491,9 +511,29 @@ def create(module, link, kind):
return wait_for_operation(module, auth.post(link, resource_to_request(module)))


def update(module, link, kind):
def update(module, link, kind, fetch):
update_fields(module, resource_to_request(module),
response_to_hash(module, fetch))
return fetch_resource(module, self_link(module), kind)


def update_fields(module, request, response):
if response.get('labels') != request.get('labels'):
labels_update(module, request, response)


def labels_update(module, request, response):
auth = GcpSession(module, 'compute')
return wait_for_operation(module, auth.put(link, resource_to_request(module)))
auth.post(
''.join([
"https://www.googleapis.com/compute/v1/",
"projects/{project}/global/images/{name}/setLabels"
]).format(**module.params),
{
u'labels': module.params.get('labels'),
u'labelFingerprint': response.get('labelFingerprint')
}
)


def delete(module, link, kind):
Expand All @@ -509,6 +549,7 @@ def resource_to_request(module):
u'family': module.params.get('family'),
u'guestOsFeatures': ImageGuestosfeaturesArray(module.params.get('guest_os_features', []), module).to_request(),
u'imageEncryptionKey': ImageImageencryptionkey(module.params.get('image_encryption_key', {}), module).to_request(),
u'labels': module.params.get('labels'),
u'licenses': module.params.get('licenses'),
u'name': module.params.get('name'),
u'rawDisk': ImageRawdisk(module.params.get('raw_disk', {}), module).to_request(),
Expand Down Expand Up @@ -590,6 +631,8 @@ def response_to_hash(module, response):
u'guestOsFeatures': ImageGuestosfeaturesArray(response.get(u'guestOsFeatures', []), module).from_response(),
u'id': response.get(u'id'),
u'imageEncryptionKey': ImageImageencryptionkey(response.get(u'imageEncryptionKey', {}), module).from_response(),
u'labels': response.get(u'labels'),
u'labelFingerprint': response.get(u'labelFingerprint'),
u'licenses': response.get(u'licenses'),
u'name': response.get(u'name'),
u'rawDisk': ImageRawdisk(response.get(u'rawDisk', {}), module).from_response(),
Expand Down
11 changes: 11 additions & 0 deletions lib/ansible/modules/cloud/google/gcp_compute_image_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@
key that protects this resource.
returned: success
type: str
labels:
description:
- Labels to apply to this Image.
returned: success
type: dict
labelFingerprint:
description:
- The fingerprint used for optimistic locking of this resource. Used internally
during updates.
returned: success
type: str
licenses:
description:
- Any applicable license URI.
Expand Down

0 comments on commit c11f76f

Please sign in to comment.