Skip to content

Commit

Permalink
ec2_eni: change data type of device_index to str when passing it to…
Browse files Browse the repository at this point in the history
… api as expected by api call (ansible-collections#877)

ec2_eni: change data type of `device_index` to str when passing it to api as expected by api call

SUMMARY

Currently the data type for parameter device_index here while being passed to api call is integer but one of the API calls later used here in the module (describe_network_interfaces) expects it to be string as per boto3 api documentation.
Fixes ansible-collections#870

ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

ec2_eni

Reviewed-by: Mark Chappell <None>
Reviewed-by: Jill R <None>
Reviewed-by: Alina Buzachis <None>
  • Loading branch information
mandar242 authored Jun 22, 2022
1 parent 922800d commit f0fa859
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_eni - Change parameter ``device_index`` data type to string when passing to `describe_network_inter` api call (https://github.com/ansible-collections/amazon.aws/pull/877).
2 changes: 1 addition & 1 deletion plugins/modules/ec2_eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def uniquely_find_eni(connection, module, eni=None):
filters.append({'Name': 'attachment.instance-id',
'Values': [instance_id]})
filters.append({'Name': 'attachment.device-index',
'Values': [device_index]})
'Values': [str(device_index)]})

if name and subnet_id and not filters:
filters.append({'Name': 'tag:Name',
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/ec2_eni/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
- name: test attaching and detaching network interfaces
include_tasks: ./test_attachment.yaml

- name: test attaching and detaching multiple network interfaces
include_tasks: ./test_create_attached_multiple.yml

- name: test modifying source_dest_check
include_tasks: ./test_modifying_source_dest_check.yaml

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
- name: Create instance to test attaching and detaching network interfaces for this test
ec2_instance:
name: "{{ resource_prefix }}-instance"
image_id: "{{ ec2_ami_id }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
instance_type: t2.micro
register: ec2_instances

- name: set variable for the instance ID
set_fact:
instance_id_3: "{{ ec2_instances.instances[0].instance_id }}"

#=================================================================

- name: Create and attach another interface to above instance - check_mode
amazon.aws.ec2_eni:
name: "{{ resource_prefix }}-eni"
instance_id: "{{ instance_id_3 }}"
device_index: 1
subnet_id: "{{ vpc_subnet_id }}"
state: present
attached: true
delete_on_termination: true
check_mode: true
register: result

# Get the instance info and ENI info to verify attachment of second eni
- ec2_instance_info:
instance_ids:
- "{{ instance_id_3 }}"
register: instance_info_result

- assert:
that:
- result is changed
- result is not failed
- instance_info_result.instances[0].network_interfaces | length == 1
- '"Would have created ENI if not in check mode." in result.msg'
- "'ec2:CreateNetworkInterface' not in {{ result.resource_actions }}"

- name: Create and attach another interface to above instance
amazon.aws.ec2_eni:
name: "{{ resource_prefix }}-eni"
instance_id: "{{ instance_id_3 }}"
device_index: 1
subnet_id: "{{ vpc_subnet_id }}"
state: present
attached: true
delete_on_termination: true
register: result

- name: Set variable for the ENI ID
set_fact:
eni_id_attached_multiple: "{{ result.interface.id }}"

# Get the instance info and ENI info to verify attachment of second eni
- ec2_instance_info:
instance_ids:
- "{{ instance_id_3 }}"
register: instance_info_result
- ec2_eni_info:
eni_id: "{{ eni_id_attached_multiple }}"
register: eni_info

- name: Assert that the interface attachment was successful
assert:
that:
- result is changed
- result is not failed
- instance_info_result.instances[0].network_interfaces | length == 2
- eni_info.network_interfaces[0].attachment.instance_id == instance_id_3
- eni_info.network_interfaces[0].attachment.device_index == 1

- name: Create and attach another interface to above instance - check_mode - idempotent
amazon.aws.ec2_eni:
name: "{{ resource_prefix }}-eni"
instance_id: "{{ instance_id_3 }}"
device_index: 1
subnet_id: "{{ vpc_subnet_id }}"
state: present
attached: true
delete_on_termination: true
check_mode: true
register: result

# Get the instance info and ENI info to verify attachment of second eni
- ec2_instance_info:
instance_ids:
- "{{ instance_id_3 }}"
register: instance_info_result

- name: Assert that the interface would have been modified if not in check_mode
assert:
that:
- result is changed
- result is not failed
- instance_info_result.instances[0].network_interfaces | length == 2
- '"Would have modified ENI: {{ eni_id_attached_multiple }} if not in check mode" in result.msg'
- "'ec2:CreateNetworkInterface' not in {{ result.resource_actions }}"
- "'ec2:ModifyNetworkInterfaceAttribute' not in {{ result.resource_actions }}"

#=================================================================

- name: remove the network interface created in this test
ec2_eni:
eni_id: "{{ eni_id_attached_multiple }}"
force_detach: True
state: absent
ignore_errors: true
retries: 5

- name: terminate the instance created in this test
ec2_instance:
state: absent
instance_ids:
- "{{ instance_id_3 }}"
wait: True
ignore_errors: true
retries: 5
when: instance_id_3 is defined

0 comments on commit f0fa859

Please sign in to comment.