diff --git a/lib/ansible/modules/cloud/google/gcp_compute_image.py b/lib/ansible/modules/cloud/google/gcp_compute_image.py index 6f7434b347639f..277a73cfe91f65 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_image.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_image.py @@ -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. @@ -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 @@ -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 = ''' @@ -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. @@ -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( @@ -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: @@ -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): @@ -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(), @@ -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(), diff --git a/lib/ansible/modules/cloud/google/gcp_compute_image_facts.py b/lib/ansible/modules/cloud/google/gcp_compute_image_facts.py index 0e77c403254d9f..bb9fe4420973ee 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_image_facts.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_image_facts.py @@ -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.