From 6b5f86a4aae0425985b270b8b10e7dae58b0d1a7 Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Mon, 13 Aug 2018 09:08:12 -0700 Subject: [PATCH] Bug fixes for gcp_compute_image (#42814) --- .../modules/cloud/google/gcp_compute_image.py | 43 +++++++++--------- .../targets/gcp_compute_image/tasks/main.yml | 44 +++++++++---------- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gcp_compute_image.py b/lib/ansible/modules/cloud/google/gcp_compute_image.py index a35b2a39fc5..be56da41a46 100644 --- a/lib/ansible/modules/cloud/google/gcp_compute_image.py +++ b/lib/ansible/modules/cloud/google/gcp_compute_image.py @@ -146,7 +146,8 @@ options: required: false source_disk: description: - - A reference to Disk resource. + - Refers to a gcompute_disk object You must provide either this property or the rawDisk.source + property but not both to create an image. required: false source_disk_encryption_key: description: @@ -182,25 +183,21 @@ extends_documentation_fragment: gcp EXAMPLES = ''' - name: create a disk gcp_compute_disk: - name: 'disk-image' + name: "disk-image" zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: disk - name: create a image gcp_compute_image: - name: testObject + name: "test_object" source_disk: "{{ disk }}" - project: testProject - auth_kind: service_account - service_account_file: /tmp/auth.pem - scopes: - - https://www.googleapis.com/auth/compute + project: "test_project" + auth_kind: "service_account" + service_account_file: "/tmp/auth.pem" state: present ''' @@ -365,7 +362,8 @@ RETURN = ''' type: str source_disk: description: - - A reference to Disk resource. + - Refers to a gcompute_disk object You must provide either this property or the rawDisk.source + property but not both to create an image. returned: success type: dict source_disk_encryption_key: @@ -448,6 +446,9 @@ def main(): ) ) + if not module.params['scopes']: + module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] + state = module.params['state'] kind = 'compute#image' @@ -496,13 +497,13 @@ def resource_to_request(module): u'description': module.params.get('description'), u'diskSizeGb': module.params.get('disk_size_gb'), u'family': module.params.get('family'), - u'guestOsFeatures': ImageGuestOsFeatuArray(module.params.get('guest_os_features', []), module).to_request(), - u'imageEncryptionKey': ImageImageEncryKey(module.params.get('image_encryption_key', {}), module).to_request(), + 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'licenses': module.params.get('licenses'), u'name': module.params.get('name'), u'rawDisk': ImageRawDisk(module.params.get('raw_disk', {}), module).to_request(), u'sourceDisk': replace_resource_dict(module.params.get(u'source_disk', {}), 'selfLink'), - u'sourceDiskEncryptionKey': ImagSourDiskEncrKey(module.params.get('source_disk_encryption_key', {}), module).to_request(), + u'sourceDiskEncryptionKey': ImageSourceDiskEncryptionKey(module.params.get('source_disk_encryption_key', {}), module).to_request(), u'sourceDiskId': module.params.get('source_disk_id'), u'sourceType': module.params.get('source_type') } @@ -578,14 +579,14 @@ def response_to_hash(module, response): u'description': response.get(u'description'), u'diskSizeGb': response.get(u'diskSizeGb'), u'family': response.get(u'family'), - u'guestOsFeatures': ImageGuestOsFeatuArray(response.get(u'guestOsFeatures', []), module).from_response(), + u'guestOsFeatures': ImageGuestOsFeaturesArray(response.get(u'guestOsFeatures', []), module).from_response(), u'id': response.get(u'id'), - u'imageEncryptionKey': ImageImageEncryKey(response.get(u'imageEncryptionKey', {}), module).from_response(), + u'imageEncryptionKey': ImageImageEncryptionKey(response.get(u'imageEncryptionKey', {}), module).from_response(), u'licenses': response.get(u'licenses'), u'name': response.get(u'name'), u'rawDisk': ImageRawDisk(response.get(u'rawDisk', {}), module).from_response(), u'sourceDisk': response.get(u'sourceDisk'), - u'sourceDiskEncryptionKey': ImagSourDiskEncrKey(response.get(u'sourceDiskEncryptionKey', {}), module).from_response(), + u'sourceDiskEncryptionKey': ImageSourceDiskEncryptionKey(response.get(u'sourceDiskEncryptionKey', {}), module).from_response(), u'sourceDiskId': response.get(u'sourceDiskId'), u'sourceType': response.get(u'sourceType') } @@ -603,7 +604,7 @@ def async_op_url(module, extra_data=None): def wait_for_operation(module, response): op_result = return_if_object(module, response, 'compute#operation') if op_result is None: - return None + return {} status = navigate_hash(op_result, ['status']) wait_done = wait_for_completion(status, op_result, module) return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#image') @@ -655,7 +656,7 @@ class ImageDeprecated(object): }) -class ImageGuestOsFeatuArray(object): +class ImageGuestOsFeaturesArray(object): def __init__(self, request, module): self.module = module if request: @@ -686,7 +687,7 @@ class ImageGuestOsFeatuArray(object): }) -class ImageImageEncryKey(object): +class ImageImageEncryptionKey(object): def __init__(self, request, module): self.module = module if request: @@ -730,7 +731,7 @@ class ImageRawDisk(object): }) -class ImagSourDiskEncrKey(object): +class ImageSourceDiskEncryptionKey(object): def __init__(self, request, module): self.module = module if request: diff --git a/test/integration/targets/gcp_compute_image/tasks/main.yml b/test/integration/targets/gcp_compute_image/tasks/main.yml index 16fd0f1f903..924ef0f6353 100644 --- a/test/integration/targets/gcp_compute_image/tasks/main.yml +++ b/test/integration/targets/gcp_compute_image/tasks/main.yml @@ -15,13 +15,11 @@ # Pre-test setup - name: create a disk gcp_compute_disk: - name: 'disk-image' + name: "disk-image" zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: disk - name: delete a image @@ -31,8 +29,6 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent #---------------------------------------------------------- - name: create a image @@ -42,8 +38,6 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: result - name: assert changed is true @@ -52,13 +46,19 @@ - result.changed == true - "result.kind == 'compute#image'" - name: verify that image was created - shell: | - gcloud compute images describe --project="{{ gcp_project}}" "{{ resource_name }}" + gcp_compute_image_facts: + filters: + - name = {{ resource_name }} + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" + scopes: + - https://www.googleapis.com/auth/compute register: results - name: verify that command succeeded assert: that: - - results.rc == 0 + - results['items'] | length == 1 # ---------------------------------------------------------------------------- - name: create a image that already exists gcp_compute_image: @@ -67,8 +67,6 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: present register: result - name: assert changed is false @@ -84,8 +82,6 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: result - name: assert changed is true @@ -94,15 +90,19 @@ - result.changed == true - result.has_key('kind') == False - name: verify that image was deleted - shell: | - gcloud compute images describe --project="{{ gcp_project}}" "{{ resource_name }}" + gcp_compute_image_facts: + filters: + - name = {{ resource_name }} + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file }}" + scopes: + - https://www.googleapis.com/auth/compute register: results - failed_when: results.rc == 0 - name: verify that command succeeded assert: that: - - results.rc == 1 - - "\"'projects/{{ gcp_project }}/global/images/{{ resource_name }}' was not found\" in results.stderr" + - results['items'] | length == 0 # ---------------------------------------------------------------------------- - name: delete a image that does not exist gcp_compute_image: @@ -111,8 +111,6 @@ project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: result - name: assert changed is false @@ -124,12 +122,10 @@ # Post-test teardown - name: delete a disk gcp_compute_disk: - name: 'disk-image' + name: "disk-image" zone: us-central1-a project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" - scopes: - - https://www.googleapis.com/auth/compute state: absent register: disk