
    VhJ                     R    d Z dZdZddlmZ  G d de      Zd Zedk(  r e        y	y	)
a  
---
module: floating_ip
author: OpenStack Ansible SIG
short_description: Manage floating IP addresses for OpenStack servers
description:
   - Add or remove an floating ip address to/from an OpenStack server.
   - Returns the floating IP when attaching only if I(wait) is C(true).
   - When detaching a floating IP there might be a delay until an server
     does not list the floating IP any more.
options:
   fixed_address:
     description:
        - To which fixed IP of server the floating IP address should be
          attached to.
     type: str
   floating_ip_address:
     description:
        - A floating IP address to attach or to detach. When I(state) is
          present can be used to specify a IP address to attach.
          I(floating_ip_address) requires I(network) to be set.
     type: str
   nat_destination:
     description:
        - The name or id of a neutron private network that the fixed IP to
          attach floating IP is on
     aliases: ["fixed_network", "internal_network"]
     type: str
   network:
     description:
        - The name or ID of a neutron external network or a nova pool name.
     type: str
   purge:
     description:
        - When I(state) is absent, indicates whether or not to delete the
          floating IP completely, or only detach it from the server.
          Default is to detach only.
     type: bool
     default: 'false'
   reuse:
     description:
        - When I(state) is present, and I(floating_ip_address) is not present,
          this parameter can be used to specify whether we should try to reuse
          a floating IP address already allocated to the project.
        - When I(reuse) is C(true), I(network) is defined and
          I(floating_ip_address) is undefined, then C(nat_destination) and
          C(fixed_address) will be ignored.
     type: bool
     default: 'false'
   server:
     description:
        - The name or ID of the server to which the IP address
          should be assigned.
     required: true
     type: str
   state:
     description:
       - Should the resource be present or absent.
     choices: [present, absent]
     default: present
     type: str
extends_documentation_fragment:
- openstack.cloud.openstack
av  
# Assign a floating IP to the first interface of `cattle001` from an existing
# external network or nova pool. A new floating IP from the first available
# external network is allocated to the project.
- openstack.cloud.floating_ip:
     cloud: dguerri
     server: cattle001

# Assign a new floating IP to the server fixed ip `192.0.2.3` of
# `cattle001`. If a free floating IP is already allocated to the project, it is
# reused; if not, a new one is created.
- openstack.cloud.floating_ip:
     cloud: dguerri
     state: present
     reuse: true
     server: cattle001
     network: ext_net
     fixed_address: 192.0.2.3
     wait: true
     timeout: 180

# Assign a new floating IP from the network `ext_net` to the server fixed
# ip in network `private_net` of `cattle001`.
- openstack.cloud.floating_ip:
     cloud: dguerri
     state: present
     server: cattle001
     network: ext_net
     nat_destination: private_net
     wait: true
     timeout: 180

# Detach a floating IP address from a server
- openstack.cloud.floating_ip:
     cloud: dguerri
     state: absent
     floating_ip_address: 203.0.113.2
     server: cattle001
a  
floating_ip:
  description: Dictionary describing the floating ip address.
  type: dict
  returned: success
  contains:
    created_at:
      description: Timestamp at which the floating IP was assigned.
      type: str
    description:
      description: The description of a floating IP.
      type: str
    dns_domain:
      description: The DNS domain.
      type: str
    dns_name:
      description: The DNS name.
      type: str
    fixed_ip_address:
      description: The fixed IP address associated with a floating IP address.
      type: str
    floating_ip_address:
      description: The IP address of a floating IP.
      type: str
    floating_network_id:
      description: The id of the network associated with a floating IP.
      type: str
    id:
      description: Id of the floating ip.
      type: str
    name:
      description: Name of the floating ip.
      type: str
    port_details:
      description: |
        The details of the port that this floating IP associates
        with. Present if C(fip-port-details) extension is loaded.
      type: dict
    port_id:
      description: The port ID floating ip associated with.
      type: str
    project_id:
      description: The ID of the project this floating IP is associated with.
      type: str
    qos_policy_id:
      description: The ID of the QoS policy attached to the floating IP.
      type: str
    revision_number:
      description: Revision number.
      type: str
    router_id:
      description: The id of the router floating ip associated with.
      type: str
    status:
      description: |
        The status of a floating IP, which can be 'ACTIVE' or 'DOWN'.
      type: str
    subnet_id:
      description: The id of the subnet the floating ip associated with.
      type: str
    tags:
      description: List of tags.
      type: list
      elements: str
    updated_at:
      description: Timestamp at which the floating IP was last updated.
      type: str
    )OpenStackModulec                       e Zd Z e e        e        eddg       e        edd       edd       ed       ed	d
d	g            Z edd
dgggddi      Zd Zd Zd Zd Z	d Z
d Z	 	 ddZd Zy)NetworkingFloatingIPModulefixed_networkinternal_network)aliasesboolF)typedefaultT)requiredpresentabsent)r   choices)fixed_addressfloating_ip_addressnat_destinationnetworkpurgereuseserverstater   r   r   )required_ifrequired_byc                     | j                          | j                  d   dk(  r| j                          y | j                          y )Nr   r   )_initparams_create_and_attach_detach_and_delete)selfs    o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/openstack/cloud/plugins/modules/floating_ip.pyrunzNetworkingFloatingIPModule.run   s6    

;;w9,##% ##%    c           
         d}| j                   d   }| j                   d   }| j                   d   }| j                  r| j                  d   nd }| j                  | j                  ||||      }|r|d   nd }|r|s*| j                  j                  j                  ||       d	}n\|j                  rP|j                  d
   dk(  r>|| j                  | j                        vr!| j                  dj                  |             |r|| j                  | j                        vrH| j                  j                  | j                  |g| j                   d   | j                   d   |       d	}n[nZ|sW| j                  j                  | j                  |d | j                   d   || j                   d   | j                   d   |       d	}n	 |rS| j                  j                  j                  | j                        | _        | j                  | j                  ||||      }| j                  ||r|d   j                  d             y d        y )NFr   r   r   idr   r   
network_idr   nat_destination_name_or_idr   )r   floating_network_idTstatusACTIVEz5Floating ip {0} has been attached to different server)msgwaittimeout)r   ipsr,   r-   r   r   )r   ip_poolr.   r   r   r,   r-   r   )computed)changedfloating_ip)r   r   	_find_ipsr   conn	create_ipport_details_filter_ips	fail_jsonformatadd_ip_listadd_ips_to_servercompute
get_server	exit_jsonto_dict)r   r1   r   r   r'   r&   r.   ips           r    r   z-NetworkingFloatingIPModule._create_and_attach   sU   O4"kk*?@%)[[1B%C"+/<<T\\$'T
nn;; 3!''A  C SV  		!!++(;(2 , 4 
 ??(AX(M+43C3C4% % NN%%+V,?%@ # B %T-=-=dkk-JJ 		%%;;,-V, KK	2"/ & 1  ^ II''{{"kk'*+[[(I. : ( < G ))++66t{{CDK ..0*9;C 	:=A6 	 	ICG 	 	Ir"   c                    | j                  | j                  | j                  d   | j                  r| j                  d   nd | j                  d   | j                  d         }|s| j	                  d       d}|D ]p  }|d   r/| j
                  j                  | j                  d   |d   	       d
}| j                  d   sG| j
                  j                  j                  |d          d
}r | j	                  |       y )Nr   r$   r   r   r%   F)r1   fixed_ip_address)	server_idfloating_ip_idTr   )r3   r   r   r   r>   r4   detach_ip_from_server	delete_ip)r   r.   r1   r@   s       r    r   z-NetworkingFloatingIPModule._detach_and_delete_  s    nn;; $,A B-1\\t||D)t++o6'+{{3D'E  G NN5N) 	B$% 		//$++d:K?A$x 0 I {{7#		!!++BtH5#	& 	w'r"   c                     d }|d   %| j                   j                  j                  |      }|d   sg S  ||d   j                               D cg c]  }|d   dk(  r|d    c}S c c}w )Nc                 @    | D cg c]  }|D ]  }|  c}}S c c}}w N )listssublistitems      r    _flattenz8NetworkingFloatingIPModule._filter_ips.<locals>._flatten  s"    (-BW'B$DBDBBBs   	addressesOS-EXT-IPS:typefloatingaddr)r4   r<   r=   values)r   r   rN   addresss       r    r7   z&NetworkingFloatingIPModule._filter_ips  s    	C +&YY&&11&9Fk"I
  ({(;(B(B(DE=,-;  = 	= =s   A'c                     |r,| j                   j                  j                  |      }|r|gS g S |s|r| j                  ||      S | j	                  |||      S rI   )r4   r   find_ip_find_ips_by_nat_destination)_find_ips_by_network_id_and_fixed_address)r   r   r   r&   r   r'   r@   s          r    r3   z$NetworkingFloatingIPModule._find_ips  sn     ""**+>?BB4%2%$> 4424 4 AAz3 3r"   c                 @   |d   sy | j                   j                  j                  |d      }|d   j                  |d   g       D cg c]  }|d   dk(  r| }}|sy |D cg c],  }| j                   j                  j	                  |d   d      . c}S c c}w c c}w )NrO   Fignore_missingnamerP   rQ   rR   )r4   r   find_networkgetrV   )r   r   r'   r   rR   fips_with_nat_destinationfips          r    rW   z7NetworkingFloatingIPModule._find_ips_by_nat_destination  s     k" ))++88&u 9 >
 k"&&v'>C%6%&*4 %6! %6
 )
 56 		!!))#f+e)L 6 	6%66s   B"1BNc                 &   | j                   j                  j                         D cg c]  }|d   | j                  |      v r| }}g }|D ]=  }|r	|d   |k7  r|s|j	                  |       |s$|d   |k(  s-|j	                  |       ? |S c c}w )Nr   r(   rB   )r4   r   r.   r7   append)r   r   r   r&   r@   r.   matching_ipss          r    rX   zDNetworkingFloatingIPModule._find_ips_by_network_id_and_fixed_address  s    
 !II--113 Ib*+t/?/?/GG  I I  	(Bb!67:E  ##B'"%7"8M"I ##B'	( 'Is   Bc                 N   | j                   d   }| j                  j                  j                  |d      }| j                  j                  j	                  |      | _        | j                   d   }|r-| j                  j                  j                  |d      | _        y d | _        y )Nr   FrZ   r   )
name_or_idr[   )r   r4   r<   find_serverr=   r   r   r]   )r   server_name_or_idr   network_name_or_ids       r    r   z NetworkingFloatingIPModule._init  s     KK1""../@>C / E ii''226:![[399,,99-e : EDL  DLr"   )NN)__name__
__module____qualname__dictargument_specmodule_kwargsr!   r   r   r7   r3   rW   rX   r   rJ   r"   r    r   r      s    f Fo7I%JK..T"9x.CD	M h!6 78
 "I
	M&HIT (D=&3466 AE=A4 r"   r   c                  &    t               }  |         y rI   )r   )modules    r    mainrq     s    ')F
Hr"   __main__N)DOCUMENTATIONEXAMPLESRETURNBansible_collections.openstack.cloud.plugins.module_utils.openstackr   r   rq   ri   rJ   r"   r    <module>rw      sL   ?B&PC
J _u  u p	
 zF r"   