
    VhPh                     ^    d Z dZdZddlmZ ddlmZ  G d de      Zd Ze	d	k(  r e        y
y
)a  
---
module: router
short_description: Create or delete routers from OpenStack
author: OpenStack Ansible SIG
description:
   - Create or Delete routers from OpenStack. Although Neutron allows
     routers to share the same name, this module enforces name uniqueness
     to be more user friendly.
options:
    enable_snat:
      description:
        - Enable Source NAT (SNAT) attribute.
      type: bool
    external_fixed_ips:
      description:
        - The IP address parameters for the external gateway network. Each
          is a dictionary with the subnet name or ID (subnet) and the IP
          address to assign on the subnet (ip_address). If no IP is specified,
          one is automatically assigned from that subnet.
      type: list
      elements: dict
      suboptions:
        ip_address:
           description: The fixed IP address to attempt to allocate.
           type: str
           aliases: ['ip']
        subnet_id:
           description: The subnet to attach the IP address to.
           required: true
           type: str
           aliases: ['subnet']
    external_gateway_info:
      description:
       - Information about the router's external gateway
      type: dict
      suboptions:
        network:
          description:
            - Unique name or ID of the external gateway network.
            - required I(interfaces) or I(enable_snat) are provided.
          type: str
        enable_snat:
          description:
            - Unique name or ID of the external gateway network.
            - required I(interfaces) or I(enable_snat) are provided.
          type: bool
        external_fixed_ips:
          description:
            - The IP address parameters for the external gateway network. Each
              is a dictionary with the subnet name or ID (subnet) and the IP
              address to assign on the subnet (ip_address). If no IP is
              specified, one is automatically assigned from that subnet.
          type: list
          elements: dict
          suboptions:
            ip_address:
               description: The fixed IP address to attempt to allocate.
               type: str
               aliases: ['ip']
            subnet_id:
               description: The subnet to attach the IP address to.
               required: true
               type: str
               aliases: ['subnet']
    interfaces:
      description:
        - List of subnets to attach to the router internal interface. Default
          gateway associated with the subnet will be automatically attached
          with the router's internal interface.
          In order to provide an ip address different from the default
          gateway,parameters are passed as dictionary with keys as network
          name or ID (I(net)), subnet name or ID (I(subnet)) and the IP of
          port (I(portip)) from the network.
          User defined portip is often required when a multiple router need
          to be connected to a single subnet for which the default gateway has
          been already used.
      type: list
      elements: raw
    is_admin_state_up:
      description:
        - Desired admin state of the created or existing router.
      type: bool
      default: 'true'
      aliases: ['admin_state_up']
    name:
      description:
        - Name to be give to the router.
        - This router attribute cannot be updated.
      required: true
      type: str
    network:
      description:
        - Unique name or ID of the external gateway network.
        - Required if I(external_fixed_ips) or I(enable_snat) are provided.
        - This router attribute cannot be updated.
      type: str
    project:
      description:
        - Unique name or ID of the project.
        - This router attribute cannot be updated.
      type: str
    state:
      description:
        - Indicate desired state of the resource
      choices: ['present', 'absent']
      default: present
      type: str
extends_documentation_fragment:
- openstack.cloud.openstack
a  
# Create a simple router, not attached to a gateway or subnets.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: simple_router

# Create a router, not attached to a gateway or subnets for a given project.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: simple_router
    project: myproj

# Creates a router attached to ext_network1 on an IPv4 subnet and with one
# internal subnet interface.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: router1
    network: ext_network1
    external_fixed_ips:
      - subnet: public-subnet
        ip_address: 172.24.4.2
    interfaces:
      - private-subnet

# Create a router with two internal subnet interfaces and a user defined port
# ip and another with default gateway.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: router2
    network: ext_network1
    interfaces:
      - net: private-net
        subnet: private-subnet
        portip: 10.1.1.10
      - project-subnet

# Create a router with two internal subnet interface. One with user defined
# port ip and and another with default gateway.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: router2
    network: ext_network1
    interfaces:
      - net: private-net
        subnet: private-subnet
        portip: 10.1.1.10
      - project-subnet

# Create a router with two internal subnet interface. One with user defined
# port ip and and another  with default gateway.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: router2
    network: ext_network1
    interfaces:
      - net: private-net
        subnet: private-subnet
        portip: 10.1.1.10
      - project-subnet

# Update existing router1 external gateway to include the IPv6 subnet.
# Note that since 'interfaces' is not provided, any existing internal
# interfaces on an existing router will be left intact.
- openstack.cloud.router:
    cloud: mycloud
    state: present
    name: router1
    network: ext_network1
    external_fixed_ips:
      - subnet: public-subnet
        ip_address: 172.24.4.2
      - subnet: ipv6-public-subnet
        ip_address: 2001:db8::3

# Delete router1
- openstack.cloud.router:
    cloud: mycloud
    state: absent
    name: router1
aJ  
router:
    description: Dictionary describing the router.
    returned: On success when I(state) is 'present'
    type: dict
    contains:
        availability_zones:
            description: Availability zones
            returned: success
            type: list
        availability_zone_hints:
            description: Availability zone hints
            returned: success
            type: list
        created_at:
            description: Date and time when the router was created
            returned: success
            type: str
        description:
            description: Description notes of the router
            returned: success
            type: str
        external_gateway_info:
            description: The external gateway information of the router.
            returned: success
            type: dict
            sample: |
                {
                    "enable_snat": true,
                    "external_fixed_ips": [
                        {
                           "ip_address": "10.6.6.99",
                           "subnet_id": "4272cb52-a456-4c20-8f3c-c26024ecfa81"
                        }
                    ]
                }
        flavor_id:
            description: ID of the flavor of the router
            returned: success
            type: str
        id:
            description: Unique UUID.
            returned: success
            type: str
            sample: "474acfe5-be34-494c-b339-50f06aa143e4"
        is_admin_state_up:
            description: Network administrative state
            returned: success
            type: bool
        is_distributed:
            description: Indicates a distributed router.
            returned: success
            type: bool
        is_ha:
            description: Indicates a highly-available router.
            returned: success
            type: bool
        name:
            description: Name given to the router.
            returned: success
            type: str
            sample: "router1"
        project_id:
            description: Project id associated with this router.
            returned: success
            type: str
        revision_number:
            description: Revision number
            returned: success
            type: int
        routes:
            description: The extra routes configuration for L3 router.
            returned: success
            type: list
        status:
            description: Router status.
            returned: success
            type: str
            sample: "ACTIVE"
        tags:
            description: List of tags
            returned: success
            type: list
        tenant_id:
            description: Owner tenant ID
            returned: success
            type: str
        updated_at:
            description: Date of last update on the router
            returned: success
            type: str
    )OpenStackModule)defaultdictc                   `   e Zd Z edd e edg       eddg            	      Z e ed
      e ed e e        ed
      e             edd       ed
ddg       ed       e        e        edddg      	      Z eg dddd      Zd Zd Zd Z	d Z
d Zd  Zd! Zy")#RouterModulelistdictip)aliasesTsubnet)requiredr
   
ip_address	subnet_id)typeelementsoptionsbool)r   )networkenable_snatexternal_fixed_ips)r   r   raw)r   r   admin_state_up)r   defaultr
   )r   presentabsent)r   choices)	r   r   external_gateway_info
interfacesis_admin_state_upnamer   projectstate))r   r   )r   r   )r   r   r   )r   r   )mutually_exclusiverequired_byc                 @   |d   | j                   d   k7  ry|d   }d|v r!|y|d   }dD ]  }	|	|v s||	   ||	   k7  s y |d   }|xs i j                  dg       }
t        t              }|
D ]!  }d|v s||d      j	                  |d          # t        t              }|v|D ]!  }d|v s||d      j	                  |d          # |D ]+  }|d   }|j                  dd      }||v r|!|||   vs) y y |
D ]  }|d   }|d   }||v r
|||   vs y y |s|s|ryy	)
z+Decide if the given router needs an update.r   Tr   N)r   
network_idr   r   r   F)paramsgetr   setadd)selfrouterkwargsr   to_add	to_removemissing_port_idscur_ext_gw_infoupdateattrcur_ext_fipscur_fip_mappreq_fip_mapfipr   r	   s                    j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/openstack/cloud/plugins/modules/router.py_needs_updatezRouterModule._needs_updateW  s    %&$++6I*JJ !89"f,&34F5  6>od&;vd|&K  !!89'-2S%r* 	 "#& 	AAq AkN+//,@	A "#&) ( E1$+/33AlODE
 * 	 [)WW\40[(~"K4G*G#  	  $ 	 [)&[(V!44#  	  Y"2    c                 H   d| j                   d   i}|s| j                   d   |d<   i }|r0|j                  |d<   | j                   d   | j                   d   |d<   |r||d<   |r||d<   d|vr6|r|d   nd }|r|j                  dg       ng }t        |      dkD  r	|d   g|d<   |S )	Nr   r    r&   r   r   r      r   )r'   idr(   len)r+   r,   r   ext_fixed_ipsr-   r   curr_ext_gw_infocurr_ext_fixed_ipss           r9   _build_kwargszRouterModule._build_kwargs  s    -@!A
 ![[0F6N
 !#29**!,/{{=)5KK. &m4:G!"67 .CF*+'<<
 4:./t  $ !$$%92>)+  %&* 'q)?+%&:; r;   c           
         d }g }g }d }| j                   d   r| j                   d   j                  d      }|xs | j                   d   }|rhg }|D ]a  } | j                  j                  j                  |d   fddi|}t        |j                        }d|v r|d   |d<   |j                  |       c | j                   d   rg }	| j                   d   D ]  }t        |t              rO | j                  j                  j                  |fddi|}|j                  t        |j                               ct        |t
              st | j                  j                  j                  |d	   fddi|}d
|vr| j                  d       | j                  j                  j                  |d
   d      }
d|vr'|j                  t        |j                               
|d   s| j                  d       #|d   }|
d|
j                  ini } | j                  j                  j                  di |}|D ]l  }|d   D ]b  }|d   |j                  k7  s|d   |k7  r|	j                  |d          |j                  t        |j                  |j                  |             d n ||	vs|j                  |j                  ||j                  dgd        |||dS )Nr   r   r   ignore_missingF)r   r   r   r   netz.Network name missing from interface definitionrE   portipzJput an ip in portip or remove itfrom list to assign default port to routermsgr&   	fixed_ips)port_idr   r   r   )r&   rK   )r   internal_ports_missinginternal_ifaces )r'   r(   connr   find_subnetr   r>   append
isinstancestrfailfind_networkportsr&   )r+   filtersr   rM   rN   r@   ifacer   r8   internal_ipsrF   rH   port_kwargsexisting_portsports                  r9   _build_router_interface_configz+RouterModule._build_router_interface_config  s/   !!# ;;./ KK(?@)* %J5I)J!#& /6**66+&I7<I@GIVYY/5((-l(;C%"))#./ ;;|$L\2 4eS):TYY..::@.3@7>@F#**4&))+DEt,:TYY..::hJ8=JAHJF E)		LN))++88uHM 9 OC  u,'..tfii/HI"8_		 'O	 P "'x*-/ )5cff'=?A $)@):):)@)@)O;)O$2 
=D'+K'8 	=$'$4		$A&),&76&A$, , 3 3C4E F / 6 6$(39994:%<!=	=
= "5 399.4.?.?=C<BII/G .H; a4n #5&<.
 	
r;   c                 z   |D ]3  }| j                   j                  j                  ||j                         5 |D ]P  } | j                   j                  j                  di |}|s,|j                  t        |j                               R |D ])  } | j                   j                  j                  |fi | + y )NrL   rO   )rP   r   remove_interface_from_routerr>   create_portrR   r   add_interface_to_router)r+   r,   r.   r/   missing_portsr]   r6   rY   s           r9   _update_ifaceszRouterModule._update_ifaces  s     	)DII:: ; )	) " 	2D-		!!--55Ad14401	2  	GE5DII55fFF	Gr;   c                 f    | j                   d   }| j                   d   r| j                   d   d   }|S )Nr   r   )r'   )r+   network_name_or_ids     r9   "_get_external_gateway_network_namez/RouterModule._get_external_gateway_network_name"  s;    ![[3;;./34Y? !!r;   c                 Z   |d   D cg c]  }|d   	 }}g }|r| j                   j                  |d      }i }|D ]  }d|vr|d   D ]  }|d   |f||d   <      t        |j                               t        |      z
  |d   }g }	g }
|D ]V  }|d   }||vr$|j	                  dd        |	j                  |       0||   \  }}d|v s=||d   k7  sF|
j                  |       X |D ]0  }d|vrt        fd|d   D              s |
j                  |       2 t        |	|
|      S c c}w )NrN   r   internalrK   r   c              3   ,   K   | ]  }|d    v   yw)r   NrO   ).0r8   obsolete_subnet_idss     r9   	<genexpr>z1RouterModule._get_port_changes.<locals>.<genexpr>O  s$      1 {#':: 1s   )r.   r/   router_ifs_internal)rP   list_router_interfacesr)   keyspoprR   anyr   )r+   r,   ifs_cfgrY   requested_subnet_idsro   existing_subnet_ipsr8   rN   r.   r/   r   r	   existing_portr]   rm   s                  @r9   _get_port_changeszRouterModule._get_port_changes)  s   #*+<#= ?uk 2  ?  ? !"&))"B"B
#$ !( 	@E%'[) @9<\9J9>9@#C$45@	@  ##6#;#;#=>!$%9!: ; ""34	$ 
	0Ek*I 33		,-e$ 3I >Bu$u\/B)B   /
	0 ( 	'D$& 1";/1 1  &	' 6Y(;= 	=Q ?s   D(c                 V   | j                   d   }| j                   d   }| j                         }| j                   d   }| j                   d   r|s| j                  d       i }d }d }|1| j                  j                  j                  |d      }|d	   }||d
<    | j                  j                  j                  |fi |}d }	|rP | j                  j                  j                  |fi |}	|	s'| j                  j                  j                  |d      }	| j                  |      }
|
d   }| j                  ||
      }|d   }|d   }|d   }|
d   }| j                  j                  rY|dk(  r|rd}n=|dk(  r|sd}n3|dk(  r|sd}n)| j                  ||	|      }| j                  ||||||      }| j                  |       |dk(  rd}|
d   }|
d   }| j                  ||	|      }|sDd}|r||d
<    | j                  j                  j                   di |}| j#                  ||g |       nY| j                  ||||||      rCd} | j                  j                  j$                  |fi |}|s|s|r| j#                  ||||       | j                  ||j'                  d             y |dk(  r~|s| j                  d       y |D ],  }| j                  j                  j)                  ||d	          . | j                  j                  j+                  |       | j                  d       y y )Nr"   r    r!   r   z5network is required when supplying external_fixed_ipsrI   FrG   r>   
project_idrM   r.   r/   ro   r   Tr   )changedrN   )computed)r{   r,   r`   rO   )r'   rh   rU   rP   identityfind_projectr   find_routerrV   r^   rx   ansible
check_moderC   r:   	exit_jsoncreate_routerre   update_routerto_dictra   delete_router)r+   r"   r    rg   project_name_or_idquery_filtersr!   rz   r,   r   router_ifs_cfgmissing_internal_portsport_changesr.   r/   ro   r   r{   r-   rN   r]   s                        r9   runzRouterModule.runU  s   G${{6"!DDF![[3;;+,5GIIK  M 
)ii((556HEJ 6 LG J*4M,'.""..tE}E4dii''445G F7DFG))++889KHM 9 O
 <<]K!/0H!I--fnEh' -	*+@A+,@A<<"" V("6)#F++FG,>@,,F$6	*, NN7N+IG!/0D!E,->?O''(:<F +5F<(8**88B6B##FOR$:<
 %%ff6H&,i&<> #G<TYY..<<VNvNF.D++FFI,BD NN7"(..%."@  B hu- 0 4DII%%BBT
 C 44 		!!//7t, r;   N)__name__
__module____qualname__r   external_fixed_ips_specargument_specmodule_kwargsr:   rC   r^   re   rh   rx   r   rO   r;   r9   r   r   /  s    "TF+D8*=
 f%2"F&)69
 
 Ve4F'+(8'9; 4 9x.CDM$ 
 #,$

M>@'RT
l
G"*=Xg-r;   r   c                  &    t               }  |         y )N)r   )modules    r9   mainr     s    ^F
Hr;   __main__N)
DOCUMENTATIONEXAMPLESRETURNBansible_collections.openstack.cloud.plugins.module_utils.openstackr   collectionsr   r   r   r   rO   r;   r9   <module>r      sQ   n`Un[
z _ #M-? M-`
 zF r;   