
    Vh
                     f   d Z dZdZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ d Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d  Z$d'd!Z%d" Z&d# Z'd$ Z(d% Z)e*d&k(  r e)        yy)(a  
---
module: ec2_eni
version_added: 1.0.0
short_description: Create and optionally attach an Elastic Network Interface (ENI) to an instance
description:
  - Create and optionally attach an Elastic Network Interface (ENI) to an instance.
  - If O(eni_id) or O(private_ip_address) is provided, the existing ENI (if any) will be modified.
  - The O(attached) parameter controls the attachment status of the network interface.
author:
  - "Rob White (@wimnat)"
  - "Mike Healey (@healem)"
options:
  eni_id:
    description:
      - The ID of the ENI (to modify).
      - If O(eni_id=None) and O(state=present), a new ENI will be created.
    type: str
  instance_id:
    description:
      - Instance ID that you wish to attach ENI to.
    type: str
  private_ip_address:
    description:
      - Private IP address.
    type: str
  subnet_id:
    description:
      - ID of subnet in which to create the ENI.
    type: str
  description:
    description:
      - Optional description of the ENI.
    type: str
  security_groups:
    description:
      - List of security groups associated with the interface.
      - Ignored when O(state=absent).
    type: list
    elements: str
    default: []
  state:
    description:
      - Create or delete ENI.
    default: present
    choices: [ 'present', 'absent' ]
    type: str
  device_index:
    description:
      - The index of the device for the network interface attachment on the instance.
    default: 0
    type: int
  attached:
    description:
      - Specifies if network interface should be attached or detached from instance. If omitted, attachment status
        won't change
    type: bool
  force_detach:
    description:
      - Force detachment of the interface. This applies either when explicitly detaching the interface by setting O(instance_id=None)
        or when deleting an interface with O(state=absent).
    default: false
    type: bool
  delete_on_termination:
    description:
      - Delete the interface when the instance it is attached to is terminated. You can only specify this flag when the
        interface is being modified, not on creation.
    required: false
    type: bool
  source_dest_check:
    description:
      - By default, interfaces perform source/destination checks. NAT instances however need this check to be disabled.
        You can only specify this flag when the interface is being modified, not on creation.
    required: false
    type: bool
  secondary_private_ip_addresses:
    description:
      - A list of IP addresses to assign as secondary IP addresses to the network interface.
      - This option is mutually exclusive of O(secondary_private_ip_address_count).
    required: false
    type: list
    elements: str
  purge_secondary_private_ip_addresses:
    description:
      - To be used with O(secondary_private_ip_addresses) to determine whether or not to remove any secondary IP addresses other than those specified.
      - Set O(secondary_private_ip_addresses=[]) to purge all secondary addresses.
    default: false
    type: bool
  secondary_private_ip_address_count:
    description:
      - The number of secondary IP addresses to assign to the network interface.
      - This option is mutually exclusive of O(secondary_private_ip_addresses).
    required: false
    type: int
  allow_reassignment:
    description:
      - Indicates whether to allow an IP address that is already assigned to another network interface or instance
        to be reassigned to the specified network interface.
    required: false
    default: false
    type: bool
  name:
    description:
      - Name for the ENI. This will create a tag with the key V(Name) and the value assigned here.
      - This can be used in conjunction with O(subnet_id) as another means of identifiying a network interface.
      - AWS does not enforce unique V(Name) tags, so duplicate names are possible if you configure it that way.
        If that is the case, you will need to provide other identifying information such as O(private_ip_address) or O(eni_id).
    required: false
    type: str
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.tags
  - amazon.aws.boto3
notes:
  - This module identifies and ENI based on either the O(eni_id), a combination of O(private_ip_address) and O(subnet_id),
    or a combination of O(instance_id) and O(device_index). Any of these options will let you specify a particular ENI.
  - Support for O(tags) and O(purge_tags) was added in release 1.3.0.
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

# Create an ENI. As no security group is defined, ENI will be created in default security group
- amazon.aws.ec2_eni:
    private_ip_address: 172.31.0.20
    subnet_id: subnet-xxxxxxxx
    state: present

# Create an ENI and attach it to an instance
- amazon.aws.ec2_eni:
    instance_id: i-xxxxxxx
    device_index: 1
    private_ip_address: 172.31.0.20
    subnet_id: subnet-xxxxxxxx
    state: present

# Create an ENI with two secondary addresses
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxxx
    state: present
    secondary_private_ip_address_count: 2

# Assign a secondary IP address to an existing ENI
# This will purge any existing IPs
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxxx
    eni_id: eni-yyyyyyyy
    state: present
    secondary_private_ip_addresses:
      - 172.16.1.1

# Remove any secondary IP addresses from an existing ENI
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxxx
    eni_id: eni-yyyyyyyy
    state: present
    secondary_private_ip_address_count: 0

# Destroy an ENI, detaching it from any instance if necessary
- amazon.aws.ec2_eni:
    eni_id: eni-xxxxxxx
    force_detach: true
    state: absent

# Update an ENI
- amazon.aws.ec2_eni:
    eni_id: eni-xxxxxxx
    description: "My new description"
    state: present

# Update an ENI using name and subnet_id
- amazon.aws.ec2_eni:
    name: eni-20
    subnet_id: subnet-xxxxxxx
    description: "My new description"
    state: present

# Update an ENI identifying it by private_ip_address and subnet_id
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxx
    private_ip_address: 172.16.1.1
    description: "My new description"

# Detach an ENI from an instance
- amazon.aws.ec2_eni:
    eni_id: eni-xxxxxxx
    instance_id: None
    state: present

### Delete an interface on termination
# First create the interface
- amazon.aws.ec2_eni:
    instance_id: i-xxxxxxx
    device_index: 1
    private_ip_address: 172.31.0.20
    subnet_id: subnet-xxxxxxxx
    state: present
  register: eni

# Modify the interface to enable the delete_on_terminaton flag
- amazon.aws.ec2_eni:
    eni_id: "{{ eni.interface.id }}"
    delete_on_termination: true
a  
interface:
  description: Network interface attributes.
  returned: when state != absent
  type: complex
  contains:
    attachment:
      description: The network interface attachment.
      type: dict
      sample: {
            "attach_time": "2024-04-25T20:57:20+00:00",
            "attachment_id": "eni-attach-0ddce58b341a1846f",
            "delete_on_termination": true,
            "device_index": 0,
            "instance_id": "i-032cb1cceb29250d2",
            "status": "attached"
        }
    description:
      description: Interface description.
      type: str
      sample: Firewall network interface
    groups:
      description: Dict of security groups.
      type: dict
      sample: { "sg-f8a8a9da": "default" }
    id:
      description: Network interface id.
      type: str
      sample: "eni-1d889198"
    mac_address:
      description: Interface's physical address.
      type: str
      sample: "00:00:5E:00:53:23"
    name:
      description: The name of the ENI.
      type: str
      sample: "my-eni-20"
    owner_id:
      description: AWS account id.
      type: str
      sample: 812381371
    private_ip_address:
      description: Primary ip address of this interface.
      type: str
      sample: 10.20.30.40
    private_ip_addresses:
      description: List of all private ip addresses associated to this interface.
      type: list
      elements: dict
      sample: [ { "primary_address": true, "private_ip_address": "10.20.30.40" } ]
    source_dest_check:
      description: Value of source/dest check flag.
      type: bool
      sample: True
    status:
      description: Network interface status.
      type: str
      sample: "pending"
    subnet_id:
      description: Which vpc subnet the interface is bound.
      type: str
      sample: subnet-b0a0393c
    tags:
      description: The dictionary of tags associated with the ENI.
      type: dict
      sample: { "Name": "my-eni", "group": "Finance" }
    vpc_id:
      description: Which vpc this network interface is bound.
      type: str
      sample: vpc-9a9a9da
    N)
ip_address)
ip_network)AnsibleEC2Error)assign_private_ip_addresses)attach_network_interface)create_network_interface)delete_network_interface)describe_network_interfaces)describe_subnets)detach_network_interfaceensure_ec2_tags)%get_ec2_security_group_ids_from_names)"modify_network_interface_attribute)unassign_private_ip_addresses)AnsibleAWSModule)boto3_tag_list_to_ansible_dict)boto3_tag_specifications)wait_for_resource_statec                    g }d| v r$| d   D ]  }|j                  |d   |d   d        i }d| v r| d   D ]  }|d   ||d   <    | j                  d      | j                  d	      | j                  d
      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      ||d}d| v rt        | d         }d|v r|d   |d<   ||d<   d| v rx| d   j                  d      | d   j                  d      | d   j                  d      | d   j                  d      | d   j                  d      | d   j                  d      d|d<   |S )NPrivateIpAddressesPrivateIpAddressPrimary)private_ip_addressprimary_addressGroups	GroupNameGroupIdNetworkInterfaceIdSubnetIdVpcIdDescriptionOwnerIdStatus
MacAddressSourceDestCheck)id	subnet_idvpc_iddescriptionowner_idstatusmac_addressr   source_dest_checkgroupsprivate_ip_addressesTagSetNamenametags
AttachmentAttachmentId
InstanceIdDeviceIndex
AttachTimeDeleteOnTermination)attachment_idinstance_iddevice_indexr,   attach_timedelete_on_termination
attachment)appendgetr   )	interfaceprivate_addressesipr/   groupinterface_infor4   s          f/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/ec2_eni.pyget_eni_inforI   4  s   y(01 	wB$$B?Q<Rgijsgt%uv	w F9x( 	:E',['9F5#$	: mm01]]:.--( }}]3MM),--) }}\2'mm,>?&]]+<= 1N 9-i.ABT>%)&\N6"!%vy &|488H$\266|D%l377F-11(;$\266|D%.|%<%@%@AV%W(
|$     c                     t        | ||      }t               }d|v r|d   D ]  }|j                  |d           t        |      }|j                  |      S Nr   r   )describe_enisetaddissubset
connectionip_listmoduleeni_idenirD   rE   ip_sets           rH   correct_ipsrX   a  sd    
z66
2Cs"*+ 	:B!!"%7"89	: \F??,--rJ   c                     t        | ||      }t               }d|v r|d   D ]  }|j                  |d           t        |      }|j                  |       S rL   )rM   rN   rO   unionrQ   s           rH   
absent_ipsr[   m  sg    
z66
2Cs"*+ 	:B!!"%7"89	: \F||-...rJ   c                     t        | ||      }t               }d|v r|d   D ]  }|j                  |d           t        t	        |      |k(        S rL   )rM   rN   rO   boollen)rR   ip_countrT   rU   rV   rD   rE   s          rH   correct_ip_countr`   y  sa    
z66
2Cs"*+ 	:B!!"%7"89	: %&(233rJ   c                 h    d}d}d}||k  r't        j                  |       ||z  } | | ry ||k  r&y y )N      r   )timesleep)function_pointerargsmax_waitinterval_timecurrent_waits        rH   wait_forrk     sD    HML

!

=!%T"	 
!rJ   c                 B   |j                   j                  d      }|j                   j                  d      }|dk(  rd }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }t        |j                   j                  d      | |	      }	|j                   j                  d
      }
|j                   j                  d      }d}|j                   j                  d      xs
 t               }|j                   j                  d      }|r||d<   d|i}|r||d<   |r||d<   t	        |	      dkD  r|	|d<   |rt        |d      |d<   |rI	 t        | t        |      g      d   d   }t        |      t              v }|s|j                  dd       |j                  r|j                  dd       	 t        | fi |}d   }t!        | |d|g        |r%|#	 t#        | |||d   !       t!        | |d"|g        |%	 t'        | |d   |#       t)        t*        | |||       |
%	 t'        | |d   |
$       t)        t,        | |
||       t/        | ||      }d}|j                  |t1        |      %       y # t        $ r}|j                  |       Y d }~-d }~ww xY w# t        $ r}|j                  |       Y d }~d }~ww xY w# t        $ r(}t%        | |       |j                  |       Y d }~d }~ww xY w# t        $ r(}t%        | |       |j                  |       Y d }~d }~ww xY w# t        $ r(}t%        | |       |j                  |       Y d }~d }~ww xY w)&Nr<   attachedNoner=   r(   r   r*   security_groupsr)   secondary_private_ip_addresses"secondary_private_ip_address_countFr4   r3   r2   r    r   r"   r   r   znetwork-interface)typesTagSpecifications	SubnetIds	CidrBlockzSError: cannot create ENI - Address does not fall within the subnet's address range.changedmsgTz,Would have created ENI if not in check mode.r   network_interface_availableNetworkInterfaceIdsr7   r8   r   network_interface_attached)r   SecondaryPrivateIpAddressCountr   r   ry   rC   )paramsrB   r   dictr^   r   r   strr   fail_json_aws_errorr   r   	fail_json
check_mode	exit_jsonr   r   r   r	   r   rk   r`   rX   rM   rI   )rR   r)   rT   r<   rm   r=   r(   r   r*   ro   rq   rr   ry   r4   r3   rg   
cidr_blockevalid_private_iprV   rU   s                        rH   
create_enir     s   --##M2K}}  ,Hf==$$^4L!!+.I**+?@--##M2K;+,jO &,]]%6%67W%X")/):):;_)`&G==V$.D==V$DV	"D#5 )]
?a(X$<TI\$] ! 	*)*Y@PQRSTU`aJ &&89Z
=SSi   +YZ&&z:T: %&FJ0MdjcklK+		*$&(#&';#<	 	 
F4Pgmfno)5		*'#&';#</Q
 %z3UW]_ef
 &1		*'#&';#<#A
 [*.LfV\] z66
2CG
WS0ABy  	*&&q))	*  &""1%%&  	*$Z8&&q))	*  	*$Z8&&q))	*  	*$Z8&&q))	*sx   J6 4K L ?$L9 &$M- 6	K?KK	L'K==L	L6L11L69	M*M%%M*-	N6NNc           
      \
   |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }	|j                   j                  d      }
|j                   j                  d	      }|j                   j                  d
      }|j                   j                  d      }d}|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }t        | ||      }|d   }	 |*d|vs|d   |k7  r|j                  st	        | |d|i       d}t        |      dkD  rPt        || |d         }t        t        |d               t        |      k7  r|j                  st	        | ||       d}|*d|vs|d   |k7  r|j                  st	        | |d|i       d}|	Jd|v rF|d   d   |	ur<|j                  s.t	        | ||d   d   |	d        |	rd!}nd"}t        | |||g#       d}g }d$|v r|d$   D cg c]  }|d%   r	|d&    }}|
t        t        |      t        |
      z
        }|rO|rM|j                  s?t        | |t        t        |      t        |
      z
        '       t        t        | |||       d}t        t        |
      t        |      z
        }|r0|j                  s"t        | |||(       t        t         | |||       d}|t        |      }||kD  r4|j                  s%t        | |||z
  |)       t        t"        | |||       d}n<||k  r7|j                  s)||z
  }t        | ||d | '       t        t"        | |||       d}|du r{d|v rE|d   d*   |k7  r:|j                  s,t%        | ||       t'        | |||+       t        | |d,|g#       d}d|vrR|j                  st'        | |||+       t        | |d,|g#       d}n$|du r |t%        | ||      z  }t        | |d-|g#       |t)        | |||||      z  }t/        | ||      }|j                  r|r|j1                  |d.|d    d/0       |j1                  |t3        |      1       y c c}w # t*        $ r}|j-                  |       Y d }~vd }~ww xY w)2Nr<   rm   r=   r*   ro   r.   r?   rq   $purge_secondary_private_ip_addressesrr   allow_reassignmentFr4   r3   
purge_tagsr   r"   Value)r   r"   Tr   r!   rp   r   )r   r   r&   )r   r&   r5   r:   r6   )r6   r:   )r   r5   %network_interface_delete_on_terminate(network_interface_no_delete_on_terminater|   r   r   r   r   )r   r   AllowReassignment)r   r   r   r7   r~   r   r{   zWould have modified ENI: z if not in check moderx   r   )r   rB   uniquely_find_enir   r   r^   r   sortedget_sec_group_listr   listrN   r   rk   r[   r   rX   r`   
detach_enir   manage_tagsr   r   rM   r   rI   )rR   rT   rV   r<   rm   r=   r*   ro   r.   r?   rq   r   rr   r   ry   r4   r3   r   rU   r/   waitercurrent_secondary_addressesisecondary_addresses_to_removesecondary_addresses_to_addcurrent_secondary_address_count#secondary_addresses_to_remove_countr   s                               rH   
modify_enir     s   --##M2K}}  ,H==$$^4L--##M2Kmm''(9:O))*=>"MM--.EF%+]]%6%67W%X"+1==+<+<=c+d()/):):;_)`&**+?@G==V$D==V$D""<0J
J
4C%&F@&"C'3}+=+L((6"vGU`Ka !#:?J_bcj_klF(X78F6NJ((6zV\ekl( +s3D/EIZ/Z((6"vPWYjOk  ,1D< !67?TT((6"+1,/,=n,M3H$ -!H!K+J]c\de&(#3&JMNbJc*xQklmvkw1-?+@*x'*x)5,05P1QTWXvTw1w,x),1U((1"+1+/4O0PSVWuSv0v+w
 Z5RTZ\bc)-c2P.QTWXsTt.t)u&)((/"+1+E*<	 [*6PRXZ`a-9.12M.N+14SS((/"+1>A``*< -z;]_egmn36UU((7:\\ 8 2"+1+FGkHk+l
 -z;]_egmnts"s<'8'F+'U((z37,"#.$0+1	 ,"F,H_e^f 3&((,"#.$0+1	 ,"F,H_e^f z*c6::G#J8Ulrkst;z63dJOO
 z66
2CW#<SAU=V<WWl!m 	 	
 WS0AB} +yl  &""1%%&s2   DT 
T*T1G?T T 	T+T&&T+c                    t        | |      }|s|j                  d       |j                  r|j                  dd       |d   }|j                  j	                  d      }	 |du r7d|v r&t        | |d   d	   d
       t        | |ddd|g       t        | |      }nt        | |      }|j                  |       y # t        $ r}|j                  |       Y d }~y d }~ww xY w)NF)ry   Tz,Would have deleted ENI if not in check mode.rx   r   force_detachr5   r6   r;   forcer{      P   delaymax_attemptsr}   )
r   r   r   r   rB   r   r   r	   r   r   )rR   rT   rV   rU   r   ry   r   s          rH   
delete_enir     s    
J
/C'+YZ%&F==$$^4L&4s"("%l"3N"C
 (1!#)/ /z6BG.z6BG) &""1%%&s   !AB; ;	CCCc                     |j                   r|j                  dd       |d   }|j                  j                  d      }d|v r't	        | |d   d   |       t        | |d	d
d|g       yy)NTz-Would have detached ENI if not in check mode.rx   r   r   r5   r6   r   r{   r   r   r   F)r   r   r   rB   r   r   )rR   rV   rT   rU   r   s        rH   r   r     s    +Z[%&F==$$^4Ls l+N;	

 	  =QUWntmu	
 rJ   c                     	 d }t        | |g      }|r|d   }|S # t        $ r}|j                  |       Y d }~y d }~ww xY w)Nr|   r   )r
   r   r   )rR   rT   rU   resultenisr   s         rH   rM   rM     sL    &*:F8T!WF &""1%%&s    	?:?c                    |rd|v r|d   }nd }n|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }	g }
||||y |r|
j                  d	|gd
       |r.|r,|
s*|
j                  d|gd
       |
j                  d|gd
       |s9|r7|r5|
s3|
j                  d|gd
       |
j                  dt        |      gd
       |	r.|r,|
s*|
j                  d|	gd
       |
j                  d|gd
       |
sy 	 d }t	        | |
      }t        |      dk(  r|d   }|S # t        $ r}|j                  |       Y d }~y d }~ww xY w)Nr   rU   r   r(   r<   r=   rm   r3   znetwork-interface-id)r2   Valueszprivate-ip-addressz	subnet-idzattachment.instance-idzattachment.device-indexztag:Name)Filters   r   )r   rB   rA   r   r
   r^   r   r   )rR   rT   rV   rU   r   r(   r<   r=   rm   r3   filtersr   r   r   s                 rH   r   r     s   
3&-.FF""8,**+?@!!+.I--##M2K==$$^4L}}  ,H==V$DG ~,4+:MR^Rf 66(KLi 4AS@TUV	{CDW 8[MRS 9c,FWEXYZ	'
tf=>	{CD&*:wGt9>!WF &""1%%&s   #F2 2	G;GGc                 @    g }| D ]  }|j                  |d           |S )Nr   )rA   )r/   remote_security_groupsrF   s      rH   r   r     s2     8%%eI&678 "!rJ   c                 z    	 t        | |g      d   d   S # t        $ r}|j                  |       Y d }~y d }~ww xY w)Nru   r   r!   )r   r   r   )rR   rT   r(   r   s       rH   _get_vpc_idr     sA    &
ykB1EgNN &""1%%&s    	:5:c                 J    |d}i }|r||d<   |d   }t        | ||||      }|S )NFr2   r   )r4   r   r   )rR   rT   rV   r3   r4   r   rU   ry   s           rH   r   r     s@    |
V%&Fj&&tPZ[GNrJ   c            
         t        d)i dt        d d      dt        d d      dt        d      dt        d      dt        d      d	t        g d
d      dt        dd      dt        dddg      dt        dd      dt        d d      dt        d d      dt        d d
d      dt        dd      dt        d d      dt        dd      dt        d d      dt        d d      dt        d d!g"      d#t        d$d      } t        | ddggdd$dgfdd$dgfgd$%      }|j                  d&      }|j                  j	                  d      }|dk(  rft        ||      }|J|j                  j	                  d      }||j                  d'(       t        |||      }t        |||       y t        |||       y |dk(  rt        ||       y y )*NrU   r   )defaulttyper<   r   )r   r(   r*   ro   r   )r   r   elementsr=   r   intstatepresentabsent)r   choicesr   nor]   r.   r?   rq   r   Frr   r   rm   r3   r4   r   resource_tags)r   aliasesr   T)argument_specmutually_exclusiverequired_ifsupports_check_modeec2z-subnet_id is required when creating a new ENI)rz    )r   r   clientr   rB   r   r   r   r   r   r   )r   rT   rR   r   rV   r(   r)   s          rH   mainr   %  s9    Du-E2  U+ E"	
 e$ RfuE !%0 9y(.CD $V4 t&9 #4f= (,DvPU'V .2%f-M ,05+I  F;  d0!" $U+#$ v'89%& 62'M, #=?cde TM?37@`?ab
 !
F u%JMMg&E	
F3;))+6I   %T U VY?Fz662z63/	(	:v& 
rJ   __main__)N)+DOCUMENTATIONEXAMPLESRETURNrd   	ipaddressr   r   7ansible_collections.amazon.aws.plugins.module_utils.ec2r   r   r   r   r	   r
   r   r   r   r   r   r   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r   ;ansible_collections.amazon.aws.plugins.module_utils.waitersr   rI   rX   r[   r`   rk   r   r   r   r   rM   r   r   r   r   r   __name__r   rJ   rH   <module>r      s   vpTnF
P      S _ \ \ \ _ T \ S i f a X f ` _*Z	.	/4`CF[C|!&H*&0&f"&3'l zF rJ   