1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Bug fixes for gcp_compute_image (#42814)

This commit is contained in:
Alex Stephen 2018-08-13 09:08:12 -07:00 committed by Ryan Brown
parent 439716a832
commit 6b5f86a4aa
2 changed files with 42 additions and 45 deletions

View file

@ -146,7 +146,8 @@ options:
required: false required: false
source_disk: source_disk:
description: 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 required: false
source_disk_encryption_key: source_disk_encryption_key:
description: description:
@ -182,25 +183,21 @@ extends_documentation_fragment: gcp
EXAMPLES = ''' EXAMPLES = '''
- name: create a disk - name: create a disk
gcp_compute_disk: gcp_compute_disk:
name: 'disk-image' name: "disk-image"
zone: us-central1-a zone: us-central1-a
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
register: disk register: disk
- name: create a image - name: create a image
gcp_compute_image: gcp_compute_image:
name: testObject name: "test_object"
source_disk: "{{ disk }}" source_disk: "{{ disk }}"
project: testProject project: "test_project"
auth_kind: service_account auth_kind: "service_account"
service_account_file: /tmp/auth.pem service_account_file: "/tmp/auth.pem"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
''' '''
@ -365,7 +362,8 @@ RETURN = '''
type: str type: str
source_disk: source_disk:
description: 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 returned: success
type: dict type: dict
source_disk_encryption_key: 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'] state = module.params['state']
kind = 'compute#image' kind = 'compute#image'
@ -496,13 +497,13 @@ def resource_to_request(module):
u'description': module.params.get('description'), u'description': module.params.get('description'),
u'diskSizeGb': module.params.get('disk_size_gb'), u'diskSizeGb': module.params.get('disk_size_gb'),
u'family': module.params.get('family'), u'family': module.params.get('family'),
u'guestOsFeatures': ImageGuestOsFeatuArray(module.params.get('guest_os_features', []), module).to_request(), u'guestOsFeatures': ImageGuestOsFeaturesArray(module.params.get('guest_os_features', []), module).to_request(),
u'imageEncryptionKey': ImageImageEncryKey(module.params.get('image_encryption_key', {}), module).to_request(), u'imageEncryptionKey': ImageImageEncryptionKey(module.params.get('image_encryption_key', {}), module).to_request(),
u'licenses': module.params.get('licenses'), u'licenses': module.params.get('licenses'),
u'name': module.params.get('name'), u'name': module.params.get('name'),
u'rawDisk': ImageRawDisk(module.params.get('raw_disk', {}), module).to_request(), u'rawDisk': ImageRawDisk(module.params.get('raw_disk', {}), module).to_request(),
u'sourceDisk': replace_resource_dict(module.params.get(u'source_disk', {}), 'selfLink'), 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'sourceDiskId': module.params.get('source_disk_id'),
u'sourceType': module.params.get('source_type') u'sourceType': module.params.get('source_type')
} }
@ -578,14 +579,14 @@ def response_to_hash(module, response):
u'description': response.get(u'description'), u'description': response.get(u'description'),
u'diskSizeGb': response.get(u'diskSizeGb'), u'diskSizeGb': response.get(u'diskSizeGb'),
u'family': response.get(u'family'), 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'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'licenses': response.get(u'licenses'),
u'name': response.get(u'name'), u'name': response.get(u'name'),
u'rawDisk': ImageRawDisk(response.get(u'rawDisk', {}), module).from_response(), u'rawDisk': ImageRawDisk(response.get(u'rawDisk', {}), module).from_response(),
u'sourceDisk': response.get(u'sourceDisk'), 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'sourceDiskId': response.get(u'sourceDiskId'),
u'sourceType': response.get(u'sourceType') u'sourceType': response.get(u'sourceType')
} }
@ -603,7 +604,7 @@ def async_op_url(module, extra_data=None):
def wait_for_operation(module, response): def wait_for_operation(module, response):
op_result = return_if_object(module, response, 'compute#operation') op_result = return_if_object(module, response, 'compute#operation')
if op_result is None: if op_result is None:
return None return {}
status = navigate_hash(op_result, ['status']) status = navigate_hash(op_result, ['status'])
wait_done = wait_for_completion(status, op_result, module) wait_done = wait_for_completion(status, op_result, module)
return fetch_resource(module, navigate_hash(wait_done, ['targetLink']), 'compute#image') 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): def __init__(self, request, module):
self.module = module self.module = module
if request: if request:
@ -686,7 +687,7 @@ class ImageGuestOsFeatuArray(object):
}) })
class ImageImageEncryKey(object): class ImageImageEncryptionKey(object):
def __init__(self, request, module): def __init__(self, request, module):
self.module = module self.module = module
if request: if request:
@ -730,7 +731,7 @@ class ImageRawDisk(object):
}) })
class ImagSourDiskEncrKey(object): class ImageSourceDiskEncryptionKey(object):
def __init__(self, request, module): def __init__(self, request, module):
self.module = module self.module = module
if request: if request:

View file

@ -15,13 +15,11 @@
# Pre-test setup # Pre-test setup
- name: create a disk - name: create a disk
gcp_compute_disk: gcp_compute_disk:
name: 'disk-image' name: "disk-image"
zone: us-central1-a zone: us-central1-a
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
register: disk register: disk
- name: delete a image - name: delete a image
@ -31,8 +29,6 @@
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: absent state: absent
#---------------------------------------------------------- #----------------------------------------------------------
- name: create a image - name: create a image
@ -42,8 +38,6 @@
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
register: result register: result
- name: assert changed is true - name: assert changed is true
@ -52,13 +46,19 @@
- result.changed == true - result.changed == true
- "result.kind == 'compute#image'" - "result.kind == 'compute#image'"
- name: verify that image was created - name: verify that image was created
shell: | gcp_compute_image_facts:
gcloud compute images describe --project="{{ gcp_project}}" "{{ resource_name }}" 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 register: results
- name: verify that command succeeded - name: verify that command succeeded
assert: assert:
that: that:
- results.rc == 0 - results['items'] | length == 1
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
- name: create a image that already exists - name: create a image that already exists
gcp_compute_image: gcp_compute_image:
@ -67,8 +67,6 @@
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: present state: present
register: result register: result
- name: assert changed is false - name: assert changed is false
@ -84,8 +82,6 @@
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: absent state: absent
register: result register: result
- name: assert changed is true - name: assert changed is true
@ -94,15 +90,19 @@
- result.changed == true - result.changed == true
- result.has_key('kind') == False - result.has_key('kind') == False
- name: verify that image was deleted - name: verify that image was deleted
shell: | gcp_compute_image_facts:
gcloud compute images describe --project="{{ gcp_project}}" "{{ resource_name }}" 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 register: results
failed_when: results.rc == 0
- name: verify that command succeeded - name: verify that command succeeded
assert: assert:
that: that:
- results.rc == 1 - results['items'] | length == 0
- "\"'projects/{{ gcp_project }}/global/images/{{ resource_name }}' was not found\" in results.stderr"
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
- name: delete a image that does not exist - name: delete a image that does not exist
gcp_compute_image: gcp_compute_image:
@ -111,8 +111,6 @@
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: absent state: absent
register: result register: result
- name: assert changed is false - name: assert changed is false
@ -124,12 +122,10 @@
# Post-test teardown # Post-test teardown
- name: delete a disk - name: delete a disk
gcp_compute_disk: gcp_compute_disk:
name: 'disk-image' name: "disk-image"
zone: us-central1-a zone: us-central1-a
project: "{{ gcp_project }}" project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}" auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}" service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
state: absent state: absent
register: disk register: disk