
    VhZ                     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: loadbalancer
short_description: Manage Octavia load-balancer in an OpenStack cloud
author: OpenStack Ansible SIG
description:
  - Add, update or remove Octavia load-balancer from OpenStack cloud.
options:
  assign_floating_ip:
    description:
      - Allocate floating ip address and associate with the VIP automatically.
      - Deprecated, use M(openstack.cloud.floating_ip) instead.
    type: bool
    default: false
    aliases: ['auto_public_ip']
  delete_floating_ip:
    description:
      - When I(state) is C(present) and I(delete_floating_ip) is C(true), then
        any floating ip address associated with the VIP will be deleted.
      - When I(state) is C(absent) and I(delete_floating_ip) is C(true), then
        any floating ip address associated with the VIP will be deleted along
        with the load balancer.
      - Deprecated, use M(openstack.cloud.floating_ip) instead.
    type: bool
    default: false
    aliases: ['delete_public_ip']
  description:
    description:
      - A human-readable description for the load-balancer.
    type: str
  flavor:
    description:
      - The flavor of the load balancer.
      - This attribute cannot be updated.
    type: str
  floating_ip_address:
    description:
      - Floating ip address aka public ip address associated with the VIP.
      - Deprecated, use M(openstack.cloud.floating_ip) instead.
    type: str
    aliases: ['public_ip_address']
  floating_ip_network:
    description:
      - Name or ID of a Neutron external network where floating ip address will
        be created on.
      - Deprecated, use M(openstack.cloud.floating_ip) instead.
    type: str
    aliases: ['public_network']
  name:
    description:
      - The name of the load balancer.
      - This attribute cannot be updated.
    required: true
    type: str
  state:
    description:
      - Should the resource be present or absent.
    choices: [present, absent]
    default: present
    type: str
  vip_address:
    description:
      - IP address of the load balancer virtual IP.
      - This attribute cannot be updated.
    type: str
  vip_network:
    description:
      - The name or id of the network for the virtual IP of the load balancer.
      - One of I(vip_network), I(vip_subnet), or I(vip_port) must be specified
        for creation.
      - This attribute cannot be updated.
    type: str
  vip_port:
    description:
      - The name or id of the load balancer virtual IP port. One of
      - One of I(vip_network), I(vip_subnet), or I(vip_port) must be specified
        for creation.
      - This attribute cannot be updated.
    type: str
  vip_subnet:
    description:
      - The name or id of the subnet for the virtual IP of the load balancer.
      - One of I(vip_network), I(vip_subnet), or I(vip_port) must be specified
        for creation.
      - This attribute cannot be updated.
    type: str
extends_documentation_fragment:
  - openstack.cloud.openstack
a1  
floating_ip:
  description: Dictionary describing the floating ip address attached to the
               load-balancer.
  type: dict
  returned: On success when I(state) is C(present) and I(assign_floating_ip) is
            C(true).
  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
load_balancer:
  description: Dictionary describing the load-balancer.
  returned: On success when I(state) is C(present).
  type: dict
  contains:
    additional_vips:
      description: Additional VIPs.
      type: str
    availability_zone:
      description: Name of the target Octavia availability zone.
      type: str
    created_at:
      description: Timestamp when the load balancer was created.
      type: str
    description:
      description: The load balancer description.
      type: str
    flavor_id:
      description: The load balancer flavor ID.
      type: str
    id:
      description: Unique UUID.
      type: str
    is_admin_state_up:
      description: The administrative state of the load balancer.
      type: bool
    listeners:
      description: The associated listener IDs, if any.
      type: list
    name:
      description: Name given to the load balancer.
      type: str
    operating_status:
      description: The operating status of the load balancer.
      type: str
    pools:
      description: The associated pool IDs, if any.
      type: list
    project_id:
      description: The ID of the project this load balancer is associated with.
      type: str
    provider:
      description: Provider name for the load balancer.
      type: str
    provisioning_status:
      description: The provisioning status of the load balancer.
      type: str
    tags:
      description: A list of associated tags.
      type: str
    updated_at:
      description: Timestamp when the load balancer was last updated.
      type: str
    vip_address:
      description: The load balancer virtual IP address.
      type: str
    vip_network_id:
      description: Network ID the load balancer virtual IP port belongs in.
      type: str
    vip_port_id:
      description: The load balancer virtual IP port ID.
      type: str
    vip_qos_policy_id:
      description: VIP qos policy id.
      type: str
    vip_subnet_id:
      description: Subnet ID the load balancer virtual IP port belongs in.
      type: str
a  
- name: Create a load balancer
  openstack.cloud.loadbalancer:
    cloud: devstack
    name: my_lb
    state: present
    vip_subnet: my_subnet

- name: Create another load balancer
  openstack.cloud.loadbalancer:
    cloud: devstack
    name: my_lb
    state: present
    vip_address: 192.168.0.11
    vip_network: my_network

- name: Delete a load balancer and all its related resources
  openstack.cloud.loadbalancer:
    cloud: devstack
    name: my_lb
    state: absent

- name: Delete a load balancer, its related resources and its floating ip
  openstack.cloud.loadbalancer:
    cloud: devstack
    delete_floating_ip: true
    name: my_lb
    state: absent
    )OpenStackModulec                   *   e Zd Z e edddg       edddg       e        e        edg       edg       ed	
       edddg       e        e        e        e             Z edgdgd	      Zd Zd Zd Zd Z	d Z
d Zd Zd Zd Zy)LoadBalancerModuleFboolauto_public_ip)defaulttypealiasesdelete_public_ippublic_ip_address)r
   public_networkT)requiredpresentabsent)r   choices)assign_floating_ipdelete_floating_ipdescriptionflavorfloating_ip_addressfloating_ip_networknamestatevip_addressvip_networkvip_port
vip_subnet)r   r   )r   r   r   Tr   r   )required_ifmutually_exclusivesupports_check_modec           	      $   | j                   d   }| j                         }| j                  j                  r"| j	                  | j                  ||             |dk(  rd|sb| j                         \  }} | j                  d
d|j                  d      d|t        |j                  d             y t                y |dk(  r|r| j                  |      \  }}|r| j                  ||      \  }} | j                  d
t        |      |j                  d      d|t        |j                  d             y t                y |d	k(  r&|r$| j                  |       | j	                  d       y |d	k(  r|s| j	                  d       y y y )Nr   )changedr   TF)computed)r#   load_balancer)floating_ipr    )params_findansible
check_mode	exit_json_will_change_createto_dictdict_build_update_updater   _delete)selfr   r%   r&   updates        p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/openstack/cloud/plugins/modules/loadbalancer.pyrunzLoadBalancerModule.run*  s   G$

<<""NN4#4#4UM#JNKIm)-&M;DNN <+33U3C< "- K$7$7$7$GH< 486	< iM"&"4"4]"CFK-1\\-:@.B*{ DNN <V+33U3C< "- K$7$7$7$GH< 486	< h=LL'NN4N(h}NN5N) (5    c                     i }dD cg c](  } j                   |    j                   |   |   k7  r|* }} j                   d   }|J j                  j                  j                  |d      }d   |j                  k7  r|j                  d        j                   d   }|J j                  j                  j                  |d      }d   |j                  k7  r|j                  d        j                   d   }	|	J j                  j                  j                  |	d      }
d	   |
j                  k7  r|j                  d	        j                   d
   }|J j                  j                  j                  |d      }d   |j                  k7  r|j                  d       |r! j                  dj                  |             t         fddD              }|r||d<    j                        \  }}i |||fS c c}w )N)r   r   r   Fignore_missing	flavor_idr   vip_network_idr   vip_subnet_idr   vip_port_idzCannot update parameters {0}msgc              3      K   | ]9  }j                   |   (j                   |   |   k7  r|j                   |   f ; y wNr(   ).0kr%   r4   s     r6   	<genexpr>z3LoadBalancerModule._build_update.<locals>.<genexpr>x  sK      B![[^7"kk!na0@@ dkk!n- Bs   ?A)r   
attributes)r(   connr%   find_flavoridappendnetworkfind_networkfind_subnet	find_port	fail_jsonformatr0   _build_update_floating_ip)r4   r%   r5   rF   non_updateable_keysflavor_name_or_idr   vip_network_name_or_idrM   vip_subnet_name_or_idsubnetvip_port_name_or_idportrH   r&   floating_ip_updates   ``              r6   r1   z LoadBalancerModule._build_updateQ  s5   *A GQ"&++a."<#';;q>]15E#E  ! G G !KK1(YY,,88!% 9 9F[)VYY6#**;7!%]!;!-ii''44&u 5 >G-.'**<#**+;< $L 9 ,YY&&22%e 3 =F_-:#**?;"kk*5*99$$..#E / ;D]+tww6#**=9NN= &': ;  =  B#2B B

 #-F<  **=9 	(' 0&/./<<_Gs   -G?c                    | j                   d   }| j                   d   }| j                   d   }||s|s| j                  d       | j                   d   }||s|s| j                  d       t        | j                  j                  j                  |j                  |j                              }t        |      d	kD  r| j                  d
       |s|s*|sd i fS t        |      d	k7  rt        d      |d   }|d|ifS |sd t        t        ||            fS t        |      d	k7  rt        d      |d   }|Z| j                  j                  j                  |d      }|j                  |j                  k7  r|t        t        ||      |      fS |)||j                  k7  r|t        t        ||      |      fS |i fS )Nr   r   r   zUassign_floating_ip or delete_floating_ip must be true when floating_ip_address is setr@   r   zUassign_floating_ip or delete_floating_ip must be true when floating_ip_network is setport_idfixed_ip_address   z@Only a single floating ip address per load-balancer is supportedz A single floating ip is expectedr   )r   r   )r   Fr:   r   )r(   rQ   listrI   rM   ipsr?   r   lenAssertionErrorr0   rN   floating_network_idrK   r   )	r4   r%   r   r   r   r   rb   iprM   s	            r6   rS   z,LoadBalancerModule._build_update_floating_ip  s   ![[)=>![[)=>"kk*?@*&/ANN  JN K #kk*?@*&/ANN  JN K 499$$((!--*66 ) 8 9 s8a<NN  AN B %7Rx3x1}$%GHHQB,b111 #'(;(;$=> > >
 s8q= !CDDV*ii''445HDI 5 KG%%34'+,?,?(A (*	+ + + *"b&<&<<t#'(;(;$= $&	' ' ' 2vr8   c                     t         fddD              } j                  d   }|6 j                  j                  j	                  |d      }|j
                  |d<    j                  d   }|6 j                  j                  j                  |d      }|j
                  |d<    j                  d	   }|6 j                  j                  j                  |d      }|j
                  |d
<    j                  d   }|6 j                  j                  j                  |d      }	|	j
                  |d<     j                  j                  j                  di |}
 j                  d   r> j                  j                  j                  |
j
                   j                  d         }
 j                  |
      \  }}|r j                  |
|      \  }
}|
|fS )Nc              3   ^   K   | ]$  }j                   |   |j                   |   f & y wrC   rD   )rE   rF   r4   s     r6   rG   z-LoadBalancerModule._create.<locals>.<genexpr>  s4      5Q3 $++a.) 5s   *-)r   r   r   r   Fr:   r<   r   r=   r   r>   r   r?   waittimeoutri   r'   )r0   r(   rI   r%   rJ   rK   rM   rN   rO   rP   create_load_balancerwait_for_load_balancerrS   _update_floating_ip)r4   kwargsrU   r   rV   rM   rW   rX   rY   rZ   r%   r&   r5   s   `            r6   r.   zLoadBalancerModule._create  s    5E5 5 !KK1(YY,,88!% 9 9F"())F;!%]!;!-ii''44&u 5 >G'.zzF#$ $L 9 ,YY&&22%e 3 =F&,iiF?#"kk*5*99$$..#E / ;D$(GGF=!D		//DDNvN;;v II33JJ  [[+ K -M #<<]KV((? 'M; k))r8   c                 \   | j                   d   rEt        | j                  j                  j	                  |j
                  |j                              }ng }| j                  j                  j                  |j                  d       | j                   d   rj| j                  j                  j                  | j                   d   d      D ]3  }| j                  j                  j                  |j                        3 n |D ]'  }| j                  j                  j                  |       ) y )	Nr   r]   T)cascaderi   rj   z.Timeout waiting for load-balancer to be absent)rj   message)r(   ra   rI   rM   rb   r?   r   r%   delete_load_balancerrK   sdkutilsiterate_timeoutfind_load_balancer	delete_ip)r4   r%   rb   countrf   s        r6   r3   zLoadBalancerModule._delete  s   ;;+,tyy((,,%11!.!:!: - < =C C 			44]5E5E=A 	5 	C ;;v77I.H 8   99**%%m&6&67@  	,BII''+	,r8   c                 l    | j                   d   }| j                  j                  j                  |      S )Nr   )
name_or_id)r(   rI   r%   rw   )r4   r   s     r6   r)   zLoadBalancerModule._find  s.    {{6"yy&&99T9JJr8   c                 V   |j                  d      }|r1 | j                  j                  j                  |j                  fi |}| j
                  d   r>| j                  j                  j                  |j                  | j
                  d         }| j                  ||      \  }}||fS )NrH   ri   rj   rk   )getrI   r%   update_load_balancerrK   r(   rm   rn   )r4   r%   r5   rH   r&   s        r6   r2   zLoadBalancerModule._update  s    ZZ-
<		''<<]=M=M K?IK  ;;v II33JJ  [[+ K -M
 $$]F; 	#{ k))r8   c                 z   d }|j                  d      }|r/| j                  j                  j                  |j                         |j                  d      }|r|d   }|d   }|(| j                  j                  j                  |d      }nd }|:|d|j                  ini }	 | j                  j                  j                  |fi |	}
nd }
|
r|
d   6| j                  d	j                  |
j                  |
j                        
       | j                  j                  j                  |
j                  |j                  |j                        }||fS |rT|d|j                  ini }	 | j                  j                  j                  d|j                  ||j                  d|	}||fS |r| j                  j                  j                  |j                        D 
cg c]	  }
|
d   |
 }}
|rO|d   }
| j                  j                  j                  |
j                  |j                  |j                        }||fS | j                  j                  j                  |j                  |j                  |j                        }||fS | j                  j                  j!                         }
|
rJ| j                  j                  j                  |
j                  |j                  |j                        }||fS | j                  j                  j                  |j                  |j                        }||fS c c}
w )Nr   r   r   r   Fr:   re   r^   zEFloating ip {0} is associated to another fixed ip address {1} alreadyr@   )r_   r^   )r_   r   r^   )re   r   )r_   re   r^   r'   )r}   rI   rM   rx   rK   rN   find_iprQ   rR   r   r_   	update_ipr   r?   	create_iprb   find_available_ip)r4   r%   r5   r&   r   r   r   r   rM   ro   rf   rb   s               r6   rn   z&LoadBalancerModule._update_floating_ip"  sd   #ZZ(<=II''(:(=(=>#ZZ(<="45J"K"45J"K".))++889LHM 9 O ".$0 1'**=68 .TYY&&../BMfMi=,NN3396 " 6 68K8K4M # N #ii//99EEM,E,E)55 : 7b k))[ % %0 1'**=68 9dii//99 %2%>%>(;)55 	R k))G  yy((,,,L1Y-/  1 1 QB"&))"3"3"="=0I0I - 9 9 #> #;K4 k))) #'))"3"3"="=)6)B)B,3JJ - 9 9 #> #;K( k)) YY&&88:"&))"3"3"="=0I0I - 9 9 #> #;K k))	 #'))"3"3"="=)6)B)B - 9 9 #> #;K k))C1s   L8c                 l    |dk(  r|sy|dk(  r|rt        | j                  |      d         S |dk(  r|ryy)Nr   Tr   r   F)r   r1   )r4   r   r%   s      r6   r-   zLoadBalancerModule._will_changeu  sF    ImiM**=9!<==h= r8   N)__name__
__module____qualname__r0   argument_specmodule_kwargsr7   r1   rS   r.   r3   r)   r2   rn   r-   r'   r8   r6   r   r     s    F)9(:<F);(<>Fv *=)>? *:);<4 9x.CDFF6M  

 9
 !	M%*N2=hBH)*V,2K*"Q*f	r8   r   c                  &    t               }  |         y rC   )r   )modules    r6   mainr     s    !F
Hr8   __main__N)DOCUMENTATIONRETURNEXAMPLESBansible_collections.openstack.cloud.plugins.module_utils.openstackr   r   r   r   r'   r8   r6   <module>r      sL   XtI
V< _q qh
 zF r8   