Skip to content

Commit

Permalink
Refactor detach wait/timeout code
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Oct 4, 2021
1 parent 9986bb1 commit 5b38b21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 13 additions & 7 deletions plugins/modules/ec2_eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,16 @@ def modify_eni(connection, module, eni):
module.exit_json(changed=changed, interface=get_eni_info(eni))


def _wait_for_detach(connection, module, eni_id):
try:
get_waiter(connection, 'network_interface_available').wait(
NetworkInterfaceIds=[eni_id],
WaiterConfig={'Delay': 5, 'MaxAttempts': 80},
)
except botocore.exceptions.WaiterError as e:
module.fail_json_aws(e, "Timeout waiting for ENI {0} to detach".format(eni_id))


def delete_eni(connection, module):

eni = uniquely_find_eni(connection, module)
Expand All @@ -660,10 +670,9 @@ def delete_eni(connection, module):
connection.detach_network_interface(
aws_retry=True,
AttachmentId=eni["Attachment"]["AttachmentId"],
Force=True
Force=True,
)
# Wait to allow detachment to finish
get_waiter(connection, 'network_interface_available').wait(NetworkInterfaceIds=[eni_id])
_wait_for_detach(connection, module, eni_id)
connection.delete_network_interface(aws_retry=True, NetworkInterfaceId=eni_id)
changed = True
else:
Expand All @@ -689,10 +698,7 @@ def detach_eni(connection, eni, module):
AttachmentId=eni["Attachment"]["AttachmentId"],
Force=force_detach,
)
get_waiter(connection, 'network_interface_available').wait(
NetworkInterfaceIds=[eni_id],
WaiterConfig={'Delay': 5, 'MaxAttempts': 80},
)
_wait_for_detach(connection, module, eni_id)
return True

return False
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/targets/ec2_eni/tasks/test_attachment.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# ============================================================
- name: Ensure test instances are running
ec2_instance:
state: running
instance_ids:
- "{{ instance_id_1 }}"
- "{{ instance_id_2 }}"
wait: True

- name: attach the network interface to instance 1
ec2_eni:
instance_id: "{{ instance_id_1 }}"
Expand Down

0 comments on commit 5b38b21

Please sign in to comment.