mirror of
https://github.com/ansible-collections/google.cloud.git
synced 2025-09-30 13:33:30 -07:00
Merge pull request #711 from thekad/bug/attach-disk-to-instance
Attach disk to instance
This commit is contained in:
commit
7a6688d602
4 changed files with 151 additions and 11 deletions
|
@ -1216,8 +1216,7 @@ def main():
|
||||||
if fetch:
|
if fetch:
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if is_different(module, fetch):
|
if is_different(module, fetch):
|
||||||
update(module, self_link(module), kind, fetch)
|
fetch = update(module, self_link(module), kind, fetch)
|
||||||
fetch = fetch_resource(module, self_link(module), kind)
|
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
delete(module, self_link(module), kind)
|
delete(module, self_link(module), kind)
|
||||||
|
@ -1259,6 +1258,35 @@ def update_fields(module, request, response):
|
||||||
machine_type_update(module, request, response)
|
machine_type_update(module, request, response)
|
||||||
if response.get('shieldedInstanceConfig') != request.get('shieldedInstanceConfig'):
|
if response.get('shieldedInstanceConfig') != request.get('shieldedInstanceConfig'):
|
||||||
shielded_instance_config_update(module, request, response)
|
shielded_instance_config_update(module, request, response)
|
||||||
|
if response.get('disks') != request.get('disks'):
|
||||||
|
extra_disks_update(module, request, response)
|
||||||
|
|
||||||
|
|
||||||
|
def extra_disks_update(module, request, response):
|
||||||
|
auth = GcpSession(module, 'compute')
|
||||||
|
# fetch all non-boot (i.e. additional) disks to attach
|
||||||
|
# but discard local disks (if defined) because they can
|
||||||
|
# only be attached to instances at creation time anyway
|
||||||
|
req_disks = set()
|
||||||
|
for d in request.get('disks', []):
|
||||||
|
if not d.get('boot'):
|
||||||
|
if d.get('source'):
|
||||||
|
req_disks.add(d['source'])
|
||||||
|
else:
|
||||||
|
module.warn(
|
||||||
|
'Non-persistent disks can only be attached at creation time, '
|
||||||
|
'changed status might be incorrect.')
|
||||||
|
rsp_disks = set()
|
||||||
|
for d in response.get('disks', []):
|
||||||
|
if d.get('source') and not d.get('boot'):
|
||||||
|
rsp_disks.add(d['source'])
|
||||||
|
for d in req_disks.difference(rsp_disks):
|
||||||
|
auth.post(
|
||||||
|
''.join([
|
||||||
|
"https://compute.googleapis.com/compute/v1/",
|
||||||
|
"projects/{project}/zones/{zone}/instances/{name}/attachDisk"]).format(**module.params),
|
||||||
|
{u'source': d},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def label_fingerprint_update(module, request, response):
|
def label_fingerprint_update(module, request, response):
|
||||||
|
@ -1375,7 +1403,7 @@ def response_to_hash(module, response):
|
||||||
u'cpuPlatform': response.get(u'cpuPlatform'),
|
u'cpuPlatform': response.get(u'cpuPlatform'),
|
||||||
u'creationTimestamp': response.get(u'creationTimestamp'),
|
u'creationTimestamp': response.get(u'creationTimestamp'),
|
||||||
u'deletionProtection': response.get(u'deletionProtection'),
|
u'deletionProtection': response.get(u'deletionProtection'),
|
||||||
u'disks': InstanceDisksArray(module.params.get('disks', []), module).to_request(),
|
u'disks': InstanceDisksArray(response.get('disks', []), module).from_response(),
|
||||||
u'guestAccelerators': InstanceGuestacceleratorsArray(response.get(u'guestAccelerators', []), module).from_response(),
|
u'guestAccelerators': InstanceGuestacceleratorsArray(response.get(u'guestAccelerators', []), module).from_response(),
|
||||||
u'hostname': response.get(u'hostname'),
|
u'hostname': response.get(u'hostname'),
|
||||||
u'id': response.get(u'id'),
|
u'id': response.get(u'id'),
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
---
|
||||||
|
- block:
|
||||||
|
- name: Create instance
|
||||||
|
google.cloud.gcp_compute_instance:
|
||||||
|
name: "{{ resource_name }}-attach-vm"
|
||||||
|
machine_type: n1-standard-1
|
||||||
|
state: present
|
||||||
|
disks:
|
||||||
|
- auto_delete: true
|
||||||
|
boot: true
|
||||||
|
initialize_params:
|
||||||
|
source_image: "{{ gcp_disk_image }}"
|
||||||
|
disk_type: pd-standard
|
||||||
|
network_interfaces:
|
||||||
|
- network: "{{ _network }}"
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
register: _result1
|
||||||
|
|
||||||
|
- name: Verify instance info post-create
|
||||||
|
google.cloud.gcp_compute_instance_info:
|
||||||
|
filters:
|
||||||
|
- name = {{ _result1.name }}
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
scopes:
|
||||||
|
- https://www.googleapis.com/auth/compute
|
||||||
|
register: _info1
|
||||||
|
|
||||||
|
- name: Create extra disk
|
||||||
|
google.cloud.gcp_compute_disk:
|
||||||
|
name: "{{ resource_name }}-extra"
|
||||||
|
state: present
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
size_gb: 20
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
register: _disk
|
||||||
|
|
||||||
|
- name: Attach extra disk to instance
|
||||||
|
google.cloud.gcp_compute_instance:
|
||||||
|
name: "{{ resource_name }}-attach-vm"
|
||||||
|
machine_type: n1-standard-1
|
||||||
|
state: present
|
||||||
|
disks:
|
||||||
|
- auto_delete: true
|
||||||
|
boot: true
|
||||||
|
initialize_params:
|
||||||
|
source_image: "{{ gcp_disk_image }}"
|
||||||
|
disk_type: pd-standard
|
||||||
|
- source: "{{ _disk }}"
|
||||||
|
network_interfaces:
|
||||||
|
- network: "{{ _network }}"
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
register: _result2
|
||||||
|
|
||||||
|
- name: Verify instance info post-change
|
||||||
|
google.cloud.gcp_compute_instance_info:
|
||||||
|
filters:
|
||||||
|
- name = {{ _result2.name }}
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
scopes:
|
||||||
|
- https://www.googleapis.com/auth/compute
|
||||||
|
register: _info2
|
||||||
|
|
||||||
|
- name: Run assertions
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- _info1.resources | length > 0
|
||||||
|
- _info1.resources[0].disks | length == 1
|
||||||
|
- _info2.resources | length > 0
|
||||||
|
- _info2.resources[0].disks | length == 2
|
||||||
|
|
||||||
|
always:
|
||||||
|
# teardown
|
||||||
|
- name: Destroy instance
|
||||||
|
google.cloud.gcp_compute_instance:
|
||||||
|
name: "{{ resource_name }}-attach-vm"
|
||||||
|
state: absent
|
||||||
|
machine_type: n1-standard-1
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
|
||||||
|
- name: Destroy extra disk
|
||||||
|
google.cloud.gcp_compute_disk:
|
||||||
|
name: "{{ resource_name }}-extra"
|
||||||
|
state: absent
|
||||||
|
zone: "{{ gcp_zone }}"
|
||||||
|
project: "{{ gcp_project }}"
|
||||||
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
|
@ -9,8 +9,8 @@
|
||||||
google.cloud.gcp_compute_disk:
|
google.cloud.gcp_compute_disk:
|
||||||
name: "{{ resource_prefix }}-{{ item.key }}"
|
name: "{{ resource_prefix }}-{{ item.key }}"
|
||||||
size_gb: 50
|
size_gb: 50
|
||||||
source_image: projects/rhel-cloud/global/images/rhel-9-v20250513
|
source_image: "{{ gcp_disk_image }}"
|
||||||
zone: us-central1-a
|
zone: "{{ gcp_zone }}"
|
||||||
project: "{{ gcp_project }}"
|
project: "{{ gcp_project }}"
|
||||||
auth_kind: "{{ gcp_cred_kind }}"
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
@ -28,7 +28,7 @@
|
||||||
network_interfaces:
|
network_interfaces:
|
||||||
- network: "{{ _network }}"
|
- network: "{{ _network }}"
|
||||||
nic_type: "{{ item.value if item.value != 'default' else omit }}"
|
nic_type: "{{ item.value if item.value != 'default' else omit }}"
|
||||||
zone: us-central1-a
|
zone: "{{ gcp_zone }}"
|
||||||
project: "{{ gcp_project }}"
|
project: "{{ gcp_project }}"
|
||||||
auth_kind: "{{ gcp_cred_kind }}"
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
@ -39,7 +39,7 @@
|
||||||
google.cloud.gcp_compute_instance_info:
|
google.cloud.gcp_compute_instance_info:
|
||||||
filters:
|
filters:
|
||||||
- name = {{ resource_name }}-{{ item.key }}
|
- name = {{ resource_name }}-{{ item.key }}
|
||||||
zone: us-central1-a
|
zone: "{{ gcp_zone }}"
|
||||||
project: "{{ gcp_project }}"
|
project: "{{ gcp_project }}"
|
||||||
auth_kind: "{{ gcp_cred_kind }}"
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
@ -60,7 +60,7 @@
|
||||||
google.cloud.gcp_compute_instance:
|
google.cloud.gcp_compute_instance:
|
||||||
name: "{{ resource_name }}-{{ item.key }}"
|
name: "{{ resource_name }}-{{ item.key }}"
|
||||||
machine_type: n1-standard-1
|
machine_type: n1-standard-1
|
||||||
zone: us-central1-a
|
zone: "{{ gcp_zone }}"
|
||||||
project: "{{ gcp_project }}"
|
project: "{{ gcp_project }}"
|
||||||
auth_kind: "{{ gcp_cred_kind }}"
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
@ -69,7 +69,7 @@
|
||||||
- name: Delete disk
|
- name: Delete disk
|
||||||
google.cloud.gcp_compute_disk:
|
google.cloud.gcp_compute_disk:
|
||||||
name: "{{ resource_prefix }}-{{ item.key }}"
|
name: "{{ resource_prefix }}-{{ item.key }}"
|
||||||
zone: us-central1-a
|
zone: "{{ gcp_zone }}"
|
||||||
project: "{{ gcp_project }}"
|
project: "{{ gcp_project }}"
|
||||||
auth_kind: "{{ gcp_cred_kind }}"
|
auth_kind: "{{ gcp_cred_kind }}"
|
||||||
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
service_account_file: "{{ gcp_cred_file | default(omit) }}"
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
- name: Generated tests
|
- name: Generated tests
|
||||||
ansible.builtin.include_tasks: autogen.yml
|
ansible.builtin.include_tasks: autogen.yml
|
||||||
|
|
||||||
- name: Test nic_type scenarios
|
- name: Extra non-autogen tests
|
||||||
block:
|
block:
|
||||||
- name: Create network
|
- name: Create network
|
||||||
google.cloud.gcp_compute_network:
|
google.cloud.gcp_compute_network:
|
||||||
|
@ -14,15 +14,23 @@
|
||||||
state: present
|
state: present
|
||||||
register: _network
|
register: _network
|
||||||
|
|
||||||
- name: Loop over testcase
|
- name: Test GVNIC test cases
|
||||||
ansible.builtin.include_tasks: gvnic.yml
|
ansible.builtin.include_tasks: gvnic.yml
|
||||||
loop: "{{ testcases | dict2items }}"
|
loop: "{{ testcases | dict2items }}"
|
||||||
vars:
|
vars:
|
||||||
|
gcp_disk_image: projects/centos-cloud/global/images/family/centos-stream-9
|
||||||
|
gcp_zone: us-central1-a
|
||||||
testcases:
|
testcases:
|
||||||
gvnic: GVNIC
|
gvnic: GVNIC
|
||||||
virtio: VIRTIO_NET
|
virtio: VIRTIO_NET
|
||||||
default: default
|
default: default
|
||||||
|
|
||||||
|
- name: Test attach disks
|
||||||
|
ansible.builtin.include_tasks: attach-disks.yml
|
||||||
|
vars:
|
||||||
|
gcp_disk_image: projects/centos-cloud/global/images/family/centos-stream-9
|
||||||
|
gcp_zone: us-central1-a
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: Delete network
|
- name: Delete network
|
||||||
google.cloud.gcp_compute_network:
|
google.cloud.gcp_compute_network:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue