From 81fbe1debe3a6f49023e2d33951a2a2b8d741e57 Mon Sep 17 00:00:00 2001 From: Jorge Gallegos Date: Thu, 5 Jun 2025 18:53:57 -0700 Subject: [PATCH 1/3] Add support for nicType Fixes #645 You can now specify the type of NIC attached to your VM Signed-off-by: Jorge Gallegos --- plugins/modules/gcp_compute_instance.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugins/modules/gcp_compute_instance.py b/plugins/modules/gcp_compute_instance.py index a0923948..ab7ce1c7 100644 --- a/plugins/modules/gcp_compute_instance.py +++ b/plugins/modules/gcp_compute_instance.py @@ -395,6 +395,19 @@ options: field to "{{ name-of-resource }}"' required: false type: dict + nic_type: + description: + - Type of network interface card attached to instance. + - If unspecified it will use the default provided by GCP. + - As the next generation network interface which succeeds VirtIO, gVNIC + replaces VirtIO-Net as the only supported network interface in Compute + Engine for all new machine types (Generation 3 and onwards). + - Newer machine series and networking features require gVNIC instead of VirtIO. + required: false + type: str + choices: + - VIRTIO_NET + - GVNIC scheduling: description: - Sets the scheduling options for this instance. @@ -1174,6 +1187,7 @@ def main(): network=dict(type='dict'), network_ip=dict(type='str'), subnetwork=dict(type='dict'), + nic_type=dict(type='str', choices=['VIRTIO_NET', 'GVNIC']), ), ), scheduling=dict( @@ -1715,6 +1729,7 @@ class InstanceNetworkinterfacesArray(object): u'network': replace_resource_dict(item.get(u'network', {}), 'selfLink'), u'networkIP': item.get('network_ip'), u'subnetwork': replace_resource_dict(item.get(u'subnetwork', {}), 'selfLink'), + u'nicType': item.get('nic_type'), } ) @@ -1726,6 +1741,7 @@ class InstanceNetworkinterfacesArray(object): u'network': item.get(u'network'), u'networkIP': item.get(u'networkIP'), u'subnetwork': item.get(u'subnetwork'), + u'nicType': item.get(u'nicType'), } ) From 24bba779d81a0228916efb07cc1a602e3680f740 Mon Sep 17 00:00:00 2001 From: Jorge Gallegos Date: Thu, 5 Jun 2025 18:54:43 -0700 Subject: [PATCH 2/3] Update integration tests to account for nicType changes Signed-off-by: Jorge Gallegos --- .../gcp_compute_instance/tasks/main.yml | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/tests/integration/targets/gcp_compute_instance/tasks/main.yml b/tests/integration/targets/gcp_compute_instance/tasks/main.yml index fe47378c..4cd0f881 100644 --- a/tests/integration/targets/gcp_compute_instance/tasks/main.yml +++ b/tests/integration/targets/gcp_compute_instance/tasks/main.yml @@ -1,3 +1,143 @@ --- - name: Generated tests ansible.builtin.include_tasks: autogen.yml + +- name: Test nic_type scenarios + block: + - name: Create disk for virtio + google.cloud.gcp_compute_disk: + name: "{{ resource_prefix }}-virtio" + size_gb: 50 + source_image: projects/rhel-cloud/global/images/rhel-9-v20250513 + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: present + register: _virtio_disk + + - name: Create disk for gVNIC + google.cloud.gcp_compute_disk: + name: "{{ resource_prefix }}-gvnic" + size_gb: 50 + source_image: projects/rhel-cloud/global/images/rhel-9-v20250513 + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: present + register: _gvnic_disk + + - name: Create network + google.cloud.gcp_compute_network: + name: "{{ resource_prefix }}" + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + auto_create_subnetworks: true + state: present + register: _network + + - name: Create virtio instance + google.cloud.gcp_compute_instance: + name: "{{ resource_name }}-virtio" + machine_type: n1-standard-1 + disks: + - auto_delete: "true" + boot: "true" + source: "{{ _virtio_disk }}" + network_interfaces: + - network: "{{ _network }}" + nic_type: VIRTIO_NET + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: present + register: _virtio_result + + - name: Verify virtio instance was created + google.cloud.gcp_compute_instance_info: + filters: + - name = {{ resource_name }}-virtio + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + scopes: + - https://www.googleapis.com/auth/compute + register: _virtio_info + + - name: Pass assertions + ansible.builtin.assert: + that: + - _virtio_result.changed == true + - _virtio_result.networkInterfaces[0].nicType == 'VIRTIO_NET' + - _virtio_info.resources[0].networkInterfaces[0].nicType == 'VIRTIO_NET' + + - name: Create gvnic instance + google.cloud.gcp_compute_instance: + name: "{{ resource_name }}-gvnic" + machine_type: n1-standard-1 + disks: + - auto_delete: "true" + boot: "true" + source: "{{ _gvnic_disk }}" + network_interfaces: + - network: "{{ _network }}" + nic_type: GVNIC + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: present + register: _gvnic_result + + - name: Verify gvnic instance was created + google.cloud.gcp_compute_instance_info: + filters: + - name = {{ resource_name }}-gvnic + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + scopes: + - https://www.googleapis.com/auth/compute + register: _gvnic_info + + - name: Pass assertions + ansible.builtin.assert: + that: + - _gvnic_result.changed == true + - _gvnic_result.networkInterfaces[0].nicType == 'GVNIC' + - _gvnic_info.resources[0].networkInterfaces[0].nicType == 'GVNIC' + + always: + - name: Delete virtio instance + google.cloud.gcp_compute_instance: + name: "{{ resource_name }}-virtio" + machine_type: n1-standard-1 + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: absent + + - name: Delete gVNIC instance + google.cloud.gcp_compute_instance: + name: "{{ resource_name }}-gvnic" + machine_type: n1-standard-1 + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: absent + + - name: Delete network + google.cloud.gcp_compute_network: + name: "{{ resource_prefix }}" + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + auto_create_subnetworks: true + state: absent From d980418ea743158a6052fbab4fbe458ae3cb6aef Mon Sep 17 00:00:00 2001 From: Jorge Gallegos Date: Fri, 6 Jun 2025 13:41:17 -0700 Subject: [PATCH 3/3] Complete test cases with default option Signed-off-by: Jorge Gallegos --- .../gcp_compute_instance/tasks/gvnic.yml | 75 +++++++++++ .../gcp_compute_instance/tasks/main.yml | 125 ++---------------- 2 files changed, 83 insertions(+), 117 deletions(-) create mode 100644 tests/integration/targets/gcp_compute_instance/tasks/gvnic.yml diff --git a/tests/integration/targets/gcp_compute_instance/tasks/gvnic.yml b/tests/integration/targets/gcp_compute_instance/tasks/gvnic.yml new file mode 100644 index 00000000..ad2338fe --- /dev/null +++ b/tests/integration/targets/gcp_compute_instance/tasks/gvnic.yml @@ -0,0 +1,75 @@ +--- +- name: Debug + ansible.builtin.debug: + msg: "Testing {{ item.key }} scenario" + +- block: + - name: Create disk + google.cloud.gcp_compute_disk: + name: "{{ resource_prefix }}-{{ item.key }}" + size_gb: 50 + source_image: projects/rhel-cloud/global/images/rhel-9-v20250513 + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: present + register: _disk + + - name: Create instance + google.cloud.gcp_compute_instance: + name: "{{ resource_name }}-{{ item.key }}" + machine_type: n1-standard-1 + disks: + - auto_delete: "true" + boot: "true" + source: "{{ _disk }}" + network_interfaces: + - network: "{{ _network }}" + nic_type: "{{ item.value if item.value != 'default' else omit }}" + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: present + register: _result + + - name: Verify instance was created + google.cloud.gcp_compute_instance_info: + filters: + - name = {{ resource_name }}-{{ item.key }} + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + scopes: + - https://www.googleapis.com/auth/compute + register: _info + + # The default option won't expose nicType via API, param will be missing + - name: Pass assertions + ansible.builtin.assert: + that: + - _result.changed == true + - _result.networkInterfaces[0].nicType | default('default') == item.value + - _info.resources[0].networkInterfaces[0].nicType | default('default') == item.value + + always: + - name: Delete instance + google.cloud.gcp_compute_instance: + name: "{{ resource_name }}-{{ item.key }}" + machine_type: n1-standard-1 + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: absent + + - name: Delete disk + google.cloud.gcp_compute_disk: + name: "{{ resource_prefix }}-{{ item.key }}" + zone: us-central1-a + project: "{{ gcp_project }}" + auth_kind: "{{ gcp_cred_kind }}" + service_account_file: "{{ gcp_cred_file | default(omit) }}" + state: absent diff --git a/tests/integration/targets/gcp_compute_instance/tasks/main.yml b/tests/integration/targets/gcp_compute_instance/tasks/main.yml index 4cd0f881..1cd682ce 100644 --- a/tests/integration/targets/gcp_compute_instance/tasks/main.yml +++ b/tests/integration/targets/gcp_compute_instance/tasks/main.yml @@ -4,30 +4,6 @@ - name: Test nic_type scenarios block: - - name: Create disk for virtio - google.cloud.gcp_compute_disk: - name: "{{ resource_prefix }}-virtio" - size_gb: 50 - source_image: projects/rhel-cloud/global/images/rhel-9-v20250513 - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - state: present - register: _virtio_disk - - - name: Create disk for gVNIC - google.cloud.gcp_compute_disk: - name: "{{ resource_prefix }}-gvnic" - size_gb: 50 - source_image: projects/rhel-cloud/global/images/rhel-9-v20250513 - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - state: present - register: _gvnic_disk - - name: Create network google.cloud.gcp_compute_network: name: "{{ resource_prefix }}" @@ -38,101 +14,16 @@ state: present register: _network - - name: Create virtio instance - google.cloud.gcp_compute_instance: - name: "{{ resource_name }}-virtio" - machine_type: n1-standard-1 - disks: - - auto_delete: "true" - boot: "true" - source: "{{ _virtio_disk }}" - network_interfaces: - - network: "{{ _network }}" - nic_type: VIRTIO_NET - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - state: present - register: _virtio_result - - - name: Verify virtio instance was created - google.cloud.gcp_compute_instance_info: - filters: - - name = {{ resource_name }}-virtio - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - scopes: - - https://www.googleapis.com/auth/compute - register: _virtio_info - - - name: Pass assertions - ansible.builtin.assert: - that: - - _virtio_result.changed == true - - _virtio_result.networkInterfaces[0].nicType == 'VIRTIO_NET' - - _virtio_info.resources[0].networkInterfaces[0].nicType == 'VIRTIO_NET' - - - name: Create gvnic instance - google.cloud.gcp_compute_instance: - name: "{{ resource_name }}-gvnic" - machine_type: n1-standard-1 - disks: - - auto_delete: "true" - boot: "true" - source: "{{ _gvnic_disk }}" - network_interfaces: - - network: "{{ _network }}" - nic_type: GVNIC - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - state: present - register: _gvnic_result - - - name: Verify gvnic instance was created - google.cloud.gcp_compute_instance_info: - filters: - - name = {{ resource_name }}-gvnic - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - scopes: - - https://www.googleapis.com/auth/compute - register: _gvnic_info - - - name: Pass assertions - ansible.builtin.assert: - that: - - _gvnic_result.changed == true - - _gvnic_result.networkInterfaces[0].nicType == 'GVNIC' - - _gvnic_info.resources[0].networkInterfaces[0].nicType == 'GVNIC' + - name: Loop over testcase + ansible.builtin.include_tasks: gvnic.yml + loop: "{{ testcases | dict2items }}" + vars: + testcases: + gvnic: GVNIC + virtio: VIRTIO_NET + default: default always: - - name: Delete virtio instance - google.cloud.gcp_compute_instance: - name: "{{ resource_name }}-virtio" - machine_type: n1-standard-1 - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - state: absent - - - name: Delete gVNIC instance - google.cloud.gcp_compute_instance: - name: "{{ resource_name }}-gvnic" - machine_type: n1-standard-1 - zone: us-central1-a - project: "{{ gcp_project }}" - auth_kind: "{{ gcp_cred_kind }}" - service_account_file: "{{ gcp_cred_file | default(omit) }}" - state: absent - - name: Delete network google.cloud.gcp_compute_network: name: "{{ resource_prefix }}"