
    Vh)                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZmZmZ d	Zd
Zd Zd ZddZd Zd Zd ZeedZd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: scaleway_lb
short_description: Scaleway load-balancer management module
author: Remy Leone (@remyleone)
description:
  - This module manages load-balancers on Scaleway.
extends_documentation_fragment:
  - community.general.scaleway
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:

  name:
    type: str
    description:
      - Name of the load-balancer.
    required: true

  description:
    type: str
    description:
      - Description of the load-balancer.
    required: true

  organization_id:
    type: str
    description:
      - Organization identifier.
    required: true

  state:
    type: str
    description:
      - Indicate desired state of the instance.
    default: present
    choices:
      - present
      - absent

  region:
    type: str
    description:
      - Scaleway zone.
    required: true
    choices:
      - nl-ams
      - fr-par
      - pl-waw

  tags:
    type: list
    elements: str
    default: []
    description:
      - List of tags to apply to the load-balancer.
  wait:
    description:
      - Wait for the load-balancer to reach its desired state before returning.
    type: bool
    default: false

  wait_timeout:
    type: int
    description:
      - Time to wait for the load-balancer to reach the expected state.
    required: false
    default: 300

  wait_sleep_time:
    type: int
    description:
      - Time to wait before every attempt to check the state of the load-balancer.
    required: false
    default: 3
a{  
- name: Create a load-balancer
  community.general.scaleway_lb:
    name: foobar
    state: present
    organization_id: 951df375-e094-4d26-97c1-ba548eeb9c42
    region: fr-par
    tags:
      - hello

- name: Delete a load-balancer
  community.general.scaleway_lb:
    name: foobar
    state: absent
    organization_id: 951df375-e094-4d26-97c1-ba548eeb9c42
    region: fr-par
ak  
{
   "scaleway_lb": {
      "backend_count": 0,
      "frontend_count": 0,
      "description": "Description of my load-balancer",
      "id": "00000000-0000-0000-0000-000000000000",
      "instances": [
         {
            "id": "00000000-0000-0000-0000-000000000000",
            "ip_address": "10.0.0.1",
            "region": "fr-par",
            "status": "ready"
         },
         {
            "id": "00000000-0000-0000-0000-000000000000",
            "ip_address": "10.0.0.2",
            "region": "fr-par",
            "status": "ready"
         }
      ],
      "ip": [
         {
            "id": "00000000-0000-0000-0000-000000000000",
            "ip_address": "192.168.0.1",
            "lb_id": "00000000-0000-0000-0000-000000000000",
            "region": "fr-par",
            "organization_id": "00000000-0000-0000-0000-000000000000",
            "reverse": ""
         }
      ],
      "name": "lb_ansible_test",
      "organization_id": "00000000-0000-0000-0000-000000000000",
      "region": "fr-par",
      "status": "ready",
      "tags": [
        "first_tag",
        "second_tag"
      ]
   }
}
N)AnsibleModule)now)SCALEWAY_REGIONSSCALEWAY_ENDPOINTscaleway_argument_specScaleway)readyabsent)namedescriptionc                 (    | d   | d   | d   | d   dS )Norganization_idr   tagsr   )r   r   r   r    )	wished_lbs    q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/scaleway_lb.pypayload_from_wished_lbr      s-    $%67&!&! /	     c                 (   | j                   j                  d|d   z         | j                  | j                  d|d   z  z         }|j                  dk(  ry|j
                  s8d|j                  d|j                  }| j                   j                  |	       	 | j                   j                  d
|d   d|j                  d          |j                  d   S # t        $ r, | j                   j                  d|j                  z  	       Y y w xY w)Nz fetch_state of load-balancer: %sid/%spathi  r   zError during state fetching: (z) msgzLoad-balancer z in state: statuszCould not fetch state in %s)	moduledebuggetapi_pathstatus_codeokjson	fail_jsonKeyError)apilbresponser   s       r   fetch_stater,      s    JJ7"T(BCwwCLL52d8+;;w<Hs";;8@8L8Lhmm\

%P

BtHhmmT\F]^_}}X&& P

!>!NOPs   ?C 2DDc                 h   | j                   j                  d   }|s|sy | j                   j                  d   }| j                   j                  d   }t               }|t        j                  |      z   }t               |k  r| j                   j                  d       t        | |      }|t        v rD| j                   j                  d       | j                   j                  dt        | |      z         y t        j                  |       t               |k  r| j                   j                  d	       y )
Nwaitwait_timeoutwait_sleep_time)secondszCWe are going to wait for the load-balancer to finish its transitionz=It seems that the load-balancer is not in transition anymore.zload-balancer in state: %sz.Server takes too long to finish its transitionr   )r    paramsr   datetime	timedeltar!   r,   STABLE_STATEStimesleepr'   )	r)   r*   
force_waitr.   r/   r0   startendstates	            r   !wait_to_complete_state_transitionr<      s    ::V$DJ::$$^4Ljj''(9:OEE
($$\:
:C
%#+

^_C$M!JJ\]JJ9KR<PPQ

?# %#+ 	

!QRr   c                     t         D ci c]  }| |   ||   k7  s|||    }}|rt         D ci c]  }|||   
 c}S i S c c}w c c}w )N)MUTABLE_ATTRIBUTES)	target_lbr   attrdiffs       r   lb_attributes_should_be_changedrB      s^    .@gdIdOW`aeWfDfD)D/!gDg2DE$io%EEI h Fs   A A Ac                    d}| j                  | j                        }|j                  sC| j                  j	                  dj                  |j                  |j                  d                |j                  d   }|D ci c]  }|d   |
 }}|d   |j                         vr
d}| j                  j                  r|d	d
ifS | j                  t        |             | j                  | j                  t        |            }|j                  sLd|j                  d   d|j                  d   d|j                  d}| j                  j	                  |       t        | |j                         | j                  | j                  d|j                  d   z  z         }||j                  fS ||d      }	t        |	|      }
|
s||	fS d}| j                  j                  r|d	difS | j!                  | j                  d|	d   z  z   |
      }|j                  sC| j                  j	                  dj                  |j                  |j                  d                t        | |	       ||j                  fS c c}w )NFr   'Error getting load-balancers [{0}: {1}]messager   lbsr   Tr   z!A load-balancer would be created.)r   datazError during lb creation: r   z: 'z' ()r)   r*   r   r   )r?   r   z*Load-balancer attributes would be changed.z8Error during load-balancer attributes update: [{0}: {1}])r"   r#   r%   r    r'   formatr$   r&   keys
check_modewarnr   postinfor<   rB   put)r)   r   changedr+   lbs_listr*   	lb_lookupcreation_responser   r?   patch_payloadlb_patch_responses               r   present_strategyrW      su   GwwCLLw)H;;

!J!Q!Q  (--	":"< 	= }}U#H*23BFR3I3	 00::  X'JKKK 	'	23HH#,,*@*K % M !##?P?U?UV[?\?P?U?UV_?`?P?U?UWC JJ  S ))c6G6L6LM77u7H7M7Md7S/S S7T%%)F+,I3i>GIM 	!!G
zz#OPPPS\\EIdO4K%K%2   4 

![!b!b))+<+A+A)+L"N 	O &#)<%****S 4s   I9c                    | j                  | j                        }d}|j                  }|j                  }|d   }|j                  s9| j
                  j                  dj                  ||j                  d                |D ci c]  }|d   |
 }}|d   |j                         vr|i fS ||d      }	d}| j
                  j                  r|d	d
ifS t        | |	d       | j                  | j                  d|	d   z  z         }|j                  s@| j
                  j                  dj                  |j                  |j                               t        | |	       ||j                  fS c c}w )Nr   FrF   rD   rE   r   r   Tr   z Load-balancer would be destroyed)r)   r*   r8   r   r   z'Error deleting load-balancer [{0}: {1}]rI   )r"   r#   r$   r&   r%   r    r'   rJ   rK   rL   r<   delete)
r)   r   r+   rQ   r$   lbs_jsonrR   r*   rS   r?   s
             r   absent_strategyr[     sd   wwCLLw)HG&&K}}HH;;

!J!Q!Qy1"3 	4 +33BFR3I3	 00{)F+,IG
zz#EFFF%#)Mzzs||eio.EEzFH;;

!J!Q!Q  (--"1 	2 &#)<HMM!!! 4s   E-)presentr   c                 X   | j                   d   }| j                   d   | j                   d   | j                   d   | j                   d   | j                   d   d}t        | j                   d<   t        | 	      }d
|z  |_        t	        |d      ||      \  }}| j                  ||       y )Nregionr;   r   r   r   r   )r;   r   r   r   r   api_url)r    zlb/v1/regions/%s/lbs)r)   r   )rQ   scaleway_lb)r2   r	   r   r#   state_strategy	exit_json)r    r^   wished_load_balancerr)   rQ   summarys         r   corere   C  s    ]]8$Fw'f%}}]3f%!==):;  1FMM)
&
!C)F2CL%&:7&CDOceGW
W':r   c                     t               } | j                  t        t        d      t        d      t        dt              t        t	        t
        j                               d      t        ddg       t        d      t        d	d
      t        dd      t        dd      	             t        | d      }t        |       y )NT)required)rg   choicesr\   )rh   defaultliststr)typeelementsri   boolF)rl   ri   inti,     )	r   r   r^   r;   r   r   r.   r/   r0   )argument_specsupports_check_mode)	r
   updatedictr   rj   ra   rK   r   re   )rq   r    s     r   mainru   U  s    *,M4 $'T+;<4 3 3 56	Jvr:d+vu-uc2%3
 
 # F
 	Lr   __main__)F) 
__future__r   r   r   rl   __metaclass__DOCUMENTATIONEXAMPLESRETURNSr3   r6   ansible.module_utils.basicr   Cansible_collections.community.general.plugins.module_utils.datetimer   Cansible_collections.community.general.plugins.module_utils.scalewayr   r	   r
   r   r5   r>   r   r,   r<   rB   rW   r[   ra   re   ru   __name__r   r   r   <module>r      s    A @Pd$)V   4 S V  V
 P$S*2+j"@  ;$* zF r   