
    Vh                     L   d dl mZmZmZ eZdZdZdZdZ	d dl
Z
d dlZd dlZd dlmZ d dlmZ dZ	 d dlZd	ZdZ	 d dlZd dlmZ d	Zd dlmZmZ  G d d      Zd Ze dk(  r e        yy# e$ r  ej*                         Zd
ZY Lw xY w# e$ r  ej*                         Zd
ZdZY ]w xY w)    )absolute_importdivisionprint_functiona  
module: clc_loadbalancer
short_description: Create, Delete shared loadbalancers in CenturyLink Cloud
description:
  - An Ansible module to Create, Delete shared loadbalancers in CenturyLink Cloud.
deprecated:
  removed_in: 11.0.0
  why: >
    Lumen Public Cloud (formerly known as CenturyLink Cloud) has gone End-of-Life in September 2023.
    See more at U(https://www.ctl.io/knowledge-base/release-notes/2023/lumen-public-cloud-platform-end-of-life-notice/?).
  alternative: There is none.
extends_documentation_fragment:
  - community.general.attributes
  - community.general.clc
author:
  - "CLC Runner (@clc-runner)"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - The name of the loadbalancer.
    type: str
    required: true
  description:
    description:
      - A description for the loadbalancer.
    type: str
  alias:
    description:
      - The alias of your CLC Account.
    type: str
    required: true
  location:
    description:
      - The location of the datacenter where the load balancer resides in.
    type: str
    required: true
  method:
    description:
      - The balancing method for the load balancer pool.
    type: str
    choices: ['leastConnection', 'roundRobin']
  persistence:
    description:
      - The persistence method for the load balancer.
    type: str
    choices: ['standard', 'sticky']
  port:
    description:
      - Port to configure on the public-facing side of the load balancer pool.
    type: str
    choices: ['80', '443']
  nodes:
    description:
      - A list of nodes that needs to be added to the load balancer pool.
    type: list
    default: []
    elements: dict
  status:
    description:
      - The status of the loadbalancer.
    type: str
    default: enabled
    choices: ['enabled', 'disabled']
  state:
    description:
      - Whether to create or delete the load balancer pool.
    type: str
    default: present
    choices: ['present', 'absent', 'port_absent', 'nodes_present', 'nodes_absent']
a  
# Note - You must set the CLC_V2_API_USERNAME And CLC_V2_API_PASSWD Environment variables before running these examples
- name: Create Loadbalancer
  hosts: localhost
  connection: local
  tasks:
    - name: Actually Create things
      community.general.clc_loadbalancer:
        name: test
        description: test
        alias: TEST
        location: WA1
        port: 443
        nodes:
          - ipAddress: 10.11.22.123
            privatePort: 80
        state: present

- name: Add node to an existing loadbalancer pool
  hosts: localhost
  connection: local
  tasks:
    - name: Actually Create things
      community.general.clc_loadbalancer:
        name: test
        description: test
        alias: TEST
        location: WA1
        port: 443
        nodes:
          - ipAddress: 10.11.22.234
            privatePort: 80
        state: nodes_present

- name: Remove node from an existing loadbalancer pool
  hosts: localhost
  connection: local
  tasks:
    - name: Actually Create things
      community.general.clc_loadbalancer:
        name: test
        description: test
        alias: TEST
        location: WA1
        port: 443
        nodes:
          - ipAddress: 10.11.22.234
            privatePort: 80
        state: nodes_absent

- name: Delete LoadbalancerPool
  hosts: localhost
  connection: local
  tasks:
    - name: Actually Delete things
      community.general.clc_loadbalancer:
        name: test
        description: test
        alias: TEST
        location: WA1
        port: 443
        nodes:
          - ipAddress: 10.11.22.123
            privatePort: 80
        state: port_absent

- name: Delete Loadbalancer
  hosts: localhost
  connection: local
  tasks:
    - name: Actually Delete things
      community.general.clc_loadbalancer:
        name: test
        description: test
        alias: TEST
        location: WA1
        port: 443
        nodes:
          - ipAddress: 10.11.22.123
            privatePort: 80
        state: absent
a  
loadbalancer:
    description: The load balancer result object from CLC.
    returned: success
    type: dict
    sample:
        {
           "description":"test-lb",
           "id":"ab5b18cb81e94ab9925b61d1ca043fb5",
           "ipAddress":"66.150.174.197",
           "links":[
              {
                 "href":"/v2/sharedLoadBalancers/wfad/wa1/ab5b18cb81e94ab9925b61d1ca043fb5",
                 "rel":"self",
                 "verbs":[
                    "GET",
                    "PUT",
                    "DELETE"
                 ]
              },
              {
                 "href":"/v2/sharedLoadBalancers/wfad/wa1/ab5b18cb81e94ab9925b61d1ca043fb5/pools",
                 "rel":"pools",
                 "verbs":[
                    "GET",
                    "POST"
                 ]
              }
           ],
           "name":"test-lb",
           "pools":[

           ],
           "status":"enabled"
        }
z
${version}N)sleep)LooseVersionTF)APIFailedResponse)AnsibleModulemissing_required_libc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed        Zd Zed        Zy)ClcLoadBalancerNc                    t         | _        || _        i | _        t        s*| j                  j                  t        d      t               t        s*| j                  j                  t        d      t               t        j                  rAt        t        j                        t        d      k  r| j                  j                  d       | j                  | j                         y)z"
        Construct module
        zclc-sdk)msg	exceptionrequestsz2.5.0z,requests library  version should be >= 2.5.0r   N)clc_sdkclcmodulelb_dict	CLC_FOUND	fail_jsonr
   CLC_IMP_ERRREQUESTS_FOUNDREQUESTS_IMP_ERRr   __version__r   _set_user_agent)selfr   s     v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/clc_loadbalancer.py__init__zClcLoadBalancer.__init__   s     KK!!&:9&EQ\!]KK!!&::&FRb!cL1E1E$FV]I^$^KK!!B " D 	TXX&    c                 "   d}d}| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d	      }	| j                   j                  j                  d
      }
| j                   j                  j                  d      }| j                   j                  j                  d      }||}| j                          | j	                  ||      | _        |dk(  rS| j                  |||||      \  }}}|r| j                  |||||	|      \  }}}|
r| j                  |||||
      \  }}nx|dk(  r| j                  |||      \  }}n[|dk(  r| j                  ||||      \  }}n=|dk(  r| j                  |||||
      \  }}n|dk(  r| j                  |||||
      \  }}| j                   j                  ||       y)zZ
        Execute the main code path, and handle the request
        :return: none
        FNnamealiaslocationdescriptionportmethodpersistencenodesstatusstate)r#   r$   presentr"   r#   r$   r%   r*   )lb_idr#   r$   r'   r(   r&   )r#   r$   r"   r&   r)   absent)r"   r#   r$   port_absent)r#   r$   r"   r&   nodes_presentnodes_absent)changedloadbalancer)r   paramsget_set_clc_credentials_from_env_get_loadbalancer_listr   ensure_loadbalancer_presentensure_loadbalancerpool_presentensure_lbpool_nodes_setensure_loadbalancer_absentensure_loadbalancerpool_absentensure_lbpool_nodes_presentensure_lbpool_nodes_absent	exit_json)r   r3   	result_lbloadbalancer_nameloadbalancer_aliasloadbalancer_locationloadbalancer_descriptionloadbalancer_portloadbalancer_methodloadbalancer_persistenceloadbalancer_nodesloadbalancer_statusr+   r.   result_poolpool_idresult_nodess                    r   process_requestzClcLoadBalancer.process_request
  s   
 	 KK..226:![[//33G< $ 2 2 6 6z B#';;#5#5#9#9-#H  KK..226:"kk0044X>#';;#5#5#9#9-#H ![[//33G<"kk0044X>""&&w/#+'8$**,22$* 3 , I(,(H(H&(.4* )I ),%GY !040T0T,2. 8* 1U 1,-g &,0,H,H0!6..0 -I -2)G\ h!%!@!@&(. "A "0GY
 m#!%!D!D(.&&	 "E "(GY o%!%!A!A(.&&( "B "*GY n$!%!@!@(.&&( "A "*GY 	gIFr    c                     d}|}| j                  |      }|s?| j                  j                  s'| j                  |||||      }|j	                  d      }d}|||fS )a  
        Checks to see if a load balancer exists and creates one if it does not.
        :param name: Name of loadbalancer
        :param alias: Alias of account
        :param location: Datacenter
        :param description: Description of loadbalancer
        :param status: Enabled / Disabled
        :return: (changed, result, lb_id)
            changed: Boolean whether a change was made
            result: The result object from the CLC load balancer request
            lb_id: The load balancer id
        Fr"   r-   idT)_loadbalancer_existsr   
check_modecreate_loadbalancerr6   )	r   r"   r#   r$   r%   r*   r3   resultr.   s	            r   r9   z+ClcLoadBalancer.ensure_loadbalancer_present[  sw     ))t)4;;))11t8=;C>I9?	 2 A
 

4(G%%r    c                     d}|}|s|ddfS | j                  ||||      }	|	s@| j                  j                  s(| j                  ||||||      }|j	                  d      }	d}|||	fS )a  
        Checks to see if a load balancer pool exists and creates one if it does not.
        :param lb_id: The loadbalancer id
        :param alias: The account alias
        :param location: the datacenter the load balancer resides in
        :param method: the load balancing method
        :param persistence: the load balancing persistence type
        :param port: the port that the load balancer will listen on
        :return: (changed, group, pool_id) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
            pool_id: The string id of the load balancer pool
        FNr#   r$   r&   r.   )r#   r$   r.   r'   r(   r&   rQ   T)_loadbalancerpool_existsr   rS   create_loadbalancerpoolr6   )
r   r.   r#   r$   r'   r(   r&   r3   rU   rL   s
             r   r:   z/ClcLoadBalancer.ensure_loadbalancerpool_presentx  s     D$&&//	 0 
 ;;))55%! + 6  !**T*G''r    c                     d}|}| j                  |      }|r,| j                  j                  s| j                  |||      }d}||fS )a\  
        Checks to see if a load balancer exists and deletes it if it does
        :param name: Name of the load balancer
        :param alias: Alias of account
        :param location: Datacenter
        :return: (changed, result)
            changed: Boolean whether a change was made
            result: The result from the CLC API Call
        FrP   )r#   r$   r"   T)rR   r   rS   delete_loadbalancer)r   r"   r#   r$   r3   rU   	lb_existss          r   r<   z*ClcLoadBalancer.ensure_loadbalancer_absent  s_     --4-8	;;))11;C7; 2 = Gr    c                     d}d}| j                  |      }|r`| j                  |      }| j                  ||||      }	|	r1d}| j                  j                  s| j                  ||||	      }||fS d}||fS d}||fS )	a  
        Checks to see if a load balancer pool exists and deletes it if it does
        :param alias: The account alias
        :param location: the datacenter the load balancer resides in
        :param name: the name of the load balancer
        :param port: the port that the load balancer listens on
        :return: (changed, result) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
        FNrP   rW   T)r#   r$   r.   rL   Pool doesn't existzLB Doesn't Exist)rR   _get_loadbalancer_idrX   r   rS   delete_loadbalancerpool)
r   r#   r$   r"   r&   r3   rU   r\   r.   rL   s
             r   r=   z.ClcLoadBalancer.ensure_loadbalancerpool_absent  s     --4-8	--4-8E33!	 4 G
 {{--!99#!)# '	 : )F  .  (Fr    c                    i }d}| j                  |      }|rc| j                  |      }	| j                  ||||	      }
|
r4| j                  |||	|
|      }|sd}| j	                  |||	|
|      }||fS d}||fS d}||fS )	aI  
        Checks to see if the provided list of nodes exist for the pool
         and set the nodes if any in the list those doesn't exist
        :param alias: The account alias
        :param location: the datacenter the load balancer resides in
        :param name: the name of the load balancer
        :param port: the port that the load balancer will listen on
        :param nodes: The list of nodes to be updated to the pool
        :return: (changed, result) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
        FrP   rW   )r#   r$   r.   rL   nodes_to_checkT)r#   r$   r.   rL   r)   r^   Load balancer doesn't Exist)rR   r_   rX   _loadbalancerpool_nodes_existsset_loadbalancernodes)r   r#   r$   r"   r&   r)   rU   r3   r\   r.   rL   nodes_exists               r   r;   z'ClcLoadBalancer.ensure_lbpool_nodes_set  s     --4-8	--4-8E33!	 4 G
 "AAKSHMJQQV	 B X
 #"G!77eAI>C@G>C	 8 EF  .  3Fr    c                     d}| j                  |      }|rL| j                  |      }| j                  ||||      }	|	r| j                  ||||	|      \  }}
||
fS d}
||
fS d}
||
fS )a  
        Checks to see if the provided list of nodes exist for the pool and add the missing nodes to the pool
        :param alias: The account alias
        :param location: the datacenter the load balancer resides in
        :param name: the name of the load balancer
        :param port: the port that the load balancer will listen on
        :param nodes: the list of nodes to be added
        :return: (changed, result) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
        FrP   rW   )r#   r$   r.   rL   nodes_to_addr^   rc   )rR   r_   rX   add_lbpool_nodesr   r#   r$   r"   r&   r)   r3   r\   r.   rL   rU   s              r   r>   z+ClcLoadBalancer.ensure_lbpool_nodes_present   s     --4-8	--4-8E33!	 4 G
 "&"7"7eAI>C@GEJ	 #8 #L  .  3Fr    c                     d}| j                  |      }|rL| j                  |      }| j                  ||||      }	|	r| j                  ||||	|      \  }}
||
fS d}
||
fS d}
||
fS )a  
        Checks to see if the provided list of nodes exist for the pool and removes them if found any
        :param alias: The account alias
        :param location: the datacenter the load balancer resides in
        :param name: the name of the load balancer
        :param port: the port that the load balancer will listen on
        :param nodes: the list of nodes to be removed
        :return: (changed, result) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
        FrP   rW   )r#   r$   r.   rL   nodes_to_remover^   rc   )rR   r_   rX   remove_lbpool_nodesrj   s              r   r?   z*ClcLoadBalancer.ensure_lbpool_nodes_absent!  s     --4-8	--4-8E33!	 4 G
 "&":":DLAFCJKP	 #; #R  .  3Fr    c           
      j   d}	 | j                   j                  j                  j                  dd|d|t	        j
                  |||d            }t        d       |S # t        $ rJ}| j                  j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)	aQ  
        Create a loadbalancer w/ params
        :param name: Name of loadbalancer
        :param alias: Alias of account
        :param location: Datacenter
        :param description: Description for loadbalancer to be created
        :param status: Enabled / Disabled
        :return: result: The result from the CLC API call
        NPOST/v2/sharedLoadBalancers//)r"   r%   r*      z)Unable to create load balancer "{0}". {1}r   )r   v2APICalljsondumpsr   r   r   r   formatstrresponse_text)r   r"   r#   r$   r%   r*   rU   es           r   rT   z#ClcLoadBalancer.create_loadbalancerB  s     	1XX[[__))&MRMU+W*.**dEP@F6H +IJF !H
 	 ! 	1KK!!?FF#aoo.0 " 1 1 		1s   AA 	B2(?B--B2c           
      \   d}	 | j                   j                  j                  j                  dd|d|d|dt	        j
                  |||d            }|S # t        $ rJ}| j                  j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)	a  
        Creates a pool on the provided load balancer
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the load balancer
        :param method: the load balancing method
        :param persistence: the load balancing persistence type
        :param port: the port that the load balancer will listen on
        :return: result: The result from the create API call
        Nro   rp   rq   /pools)r&   r'   r(   z5Unable to create pool for load balancer id "{0}". {1}r   )r   rs   rt   ru   rv   rw   r   r   r   rx   ry   rz   )	r   r#   r$   r.   r'   r(   r&   rU   r{   s	            r   rY   z'ClcLoadBalancer.create_loadbalancerpool[  s     
	2XX[[__))%)*.** ${+F 	 ! 	2KK!!KRR3q/1 " 2 2 		2s   AA 	B+!?B&&B+c           	      N   d}| j                  |      }	 | j                  j                  j                  j	                  dd|d|d|      }|S # t
        $ rJ}| j                  j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)z
        Delete CLC loadbalancer
        :param alias: Alias for account
        :param location: Datacenter
        :param name: Name of the loadbalancer to delete
        :return: result: The result from the CLC API call
        NrP   DELETErp   rq   z)Unable to delete load balancer "{0}". {1}r   )r_   r   rs   rt   ru   r   r   r   rx   ry   rz   )r   r#   r$   r"   rU   r.   r{   s          r   r[   z#ClcLoadBalancer.delete_loadbalanceru  s     ))t)4	1XX[[__))%)*F 	 ! 	1KK!!?FF#aoo.0 " 1 1 		1s   9A 	B$?BB$c                 0   d}	 | j                   j                  j                  j                  dd|d|d|d|      }|S # t        $ rJ}| j
                  j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)ab  
        Delete the pool on the provided load balancer
        :param alias: The account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the load balancer
        :param pool_id: the id string of the load balancer pool
        :return: result: The result from the delete API call
        Nr   rp   rq   /pools/z5Unable to delete pool for load balancer id "{0}". {1}r   
r   rs   rt   ru   r   r   r   rx   ry   rz   r   r#   r$   r.   rL   rU   r{   s          r   r`   z'ClcLoadBalancer.delete_loadbalancerpool  s     	2XX[[__))%23F 	 ! 	2KK!!KRR3q/1 " 2 2 		2s   <A 	B?BBc                 x    d}| j                   D ](  }|j                  d      |k(  s|j                  d      }* |S )z
        Retrieves unique ID of loadbalancer
        :param name: Name of loadbalancer
        :return: Unique ID of the loadbalancer
        Nr"   rQ   r   r6   )r   r"   rQ   lbs       r   r_   z$ClcLoadBalancer._get_loadbalancer_id  s@     ,, 	"Bvvf~%VVD\	" 	r    c           	      $   d}	 | j                   j                  j                  j                  dd|d|      }|S # t        $ rJ}| j
                  j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)z
        Retrieve a list of loadbalancers
        :param alias: Alias for account
        :param location: Datacenter
        :return: JSON data for all loadbalancers at datacenter
        NGETrp   rq   z4Unable to fetch load balancers for account: {0}. {1}r   r   )r   r#   r$   rU   r{   s        r   r8   z&ClcLoadBalancer._get_loadbalancer_list  s     	2XX[[__))%JLF 	 ! 	2KK!!JQQ3q/1 " 2 2 		2s   6< 	B?B

Bc                 x    d}| j                   D ](  }|j                  d      |k(  s|j                  d      }* |S )z
        Verify a loadbalancer exists
        :param name: Name of loadbalancer
        :return: False or the ID of the existing loadbalancer
        Fr"   rQ   r   )r   r"   rU   r   s       r   rR   z$ClcLoadBalancer._loadbalancer_exists  s@     ,, 	&Bvvf~%	& r    c           
         d}	 | j                   j                  j                  j                  dd|d|d|d      }|D ]:  }t        |j                  d	            t        |      k(  s*|j                  d
      }< |S # t        $ rI}| j
                  j                  dj                  |t        |j                                    cY d}~S d}~ww xY w)a  
        Checks to see if a pool exists on the specified port on the provided load balancer
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param port: the port to check and see if it exists
        :param lb_id: the id string of the provided load balancer
        :return: result: The id string of the pool or False
        Fr   rp   rq   r}   zJUnable to fetch the load balancer pools for for load balancer id: {0}. {1}r   Nr&   rQ   )r   rs   rt   ru   r   r   r   rx   ry   rz   intr6   )	r   r#   r$   r&   r.   rU   	pool_listr{   pools	            r   rX   z(ClcLoadBalancer._loadbalancerpool_exists  s     	2,,%)*I  	(D488F#$D	1$	(  ! 	2;;((`gg3q/1 ) 2 2	2s   :A? ?	C>CCCc                 ~    d}| j                  ||||      }|D ]!  }|j                  d      sd|d<   ||v rd} d}# |S )a  
        Checks to see if a set of nodes exists on the specified port on the provided load balancer
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the provided load balancer
        :param pool_id: the id string of the load balancer pool
        :param nodes_to_check: the list of nodes to check for
        :return: result: True / False indicating if the given nodes exist
        Fr*   enabledT)_get_lbpool_nodesr6   )	r   r#   r$   r.   rL   rb   rU   r)   nodes	            r   rd   z.ClcLoadBalancer._loadbalancerpool_nodes_exists  s[     &&uhwG" 	D88H%!*Xu}	 r    c                    d}|s|S | j                   j                  sT	 | j                  j                  j                  j                  dd|d|d|d|d	t        j                  |            }|S |S # t        $ rJ}| j                   j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)	a  
        Updates nodes to the provided pool
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the load balancer
        :param pool_id: the id string of the pool
        :param nodes: a list of dictionaries containing the nodes to set
        :return: result: The result from the CLC API call
        NPUTrp   rq   r   /nodesz<Unable to set nodes for the load balancer pool id "{0}". {1}r   )r   rS   r   rs   rt   ru   rv   rw   r   r   rx   ry   rz   )r   r#   r$   r.   rL   r)   rU   r{   s           r   re   z%ClcLoadBalancer.set_loadbalancernodes  s     M{{%%8--e16%/RSWS]S]^cSdf v	 % 8%%V]]Q__!57 & 8 8 	8s   AA3 3	C<?CCc                    d}i }| j                  ||||      }|D ]0  }	|	j                  d      sd|	d<   |	|vsd}|j                  |	       2 |du r+| j                  j                  s| j                  |||||      }||fS )a  
        Add nodes to the provided pool
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the load balancer
        :param pool_id: the id string of the pool
        :param nodes_to_add: a list of dictionaries containing the nodes to add
        :return: (changed, result) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
        Fr*   r   T)r   r6   appendr   rS   re   )
r   r#   r$   r.   rL   rh   r3   rU   r)   r   s
             r   ri   z ClcLoadBalancer.add_lbpool_nodes  s     &&uhwG  	#D88H%!*X5 T"	# d?4;;#9#9//F r    c                    d}i }| j                  ||||      }|D ]0  }	|	j                  d      sd|	d<   |	|v sd}|j                  |	       2 |du r+| j                  j                  s| j                  |||||      }||fS )a  
        Removes nodes from the provided pool
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the load balancer
        :param pool_id: the id string of the pool
        :param nodes_to_remove: a list of dictionaries containing the nodes to remove
        :return: (changed, result) -
            changed: Boolean whether a change was made
            result: The result from the CLC API call
        Fr*   r   T)r   r6   remover   rS   re   )
r   r#   r$   r.   rL   rl   r3   rU   r)   r   s
             r   rm   z#ClcLoadBalancer.remove_lbpool_nodes*  s     &&uhwG# 	#D88H%!*Xu}T"	# d?4;;#9#9//F r    c                 2   d}	 | j                   j                  j                  j                  dd|d|d|d|d	      }|S # t        $ rJ}| j
                  j                  dj                  |t        |j                                     Y d}~|S d}~ww xY w)	aZ  
        Return the list of nodes available to the provided load balancer pool
        :param alias: the account alias
        :param location: the datacenter the load balancer resides in
        :param lb_id: the id string of the load balancer
        :param pool_id: the id string of the pool
        :return: result: The list of nodes
        Nr   rp   rq   r   r   zKUnable to fetch list of available nodes for load balancer pool id: {0}. {1}r   r   r   s          r   r   z!ClcLoadBalancer._get_lbpool_nodesI  s     	4XX[[__))%-2HeW+NOF 	 ! 	4KK!!ahhS13 " 4 4 		4s   =A 	B?BBc                     t        t        d      t               t        d      t        d      t        ddg      t        ddg      t        dd	g      t        d
g d      t        dddg      t        dg d      
      } | S )zk
        Define the argument spec for the ansible module
        :return: argument spec dictionary
        T)requiredP   i  )choicesleastConnection
roundRobinstandardstickylistdict)typedefaultelementsr   disabled)r   r   r,   )r,   r/   r0   r1   r2   )
r"   r%   r$   r#   r&   r'   r(   r)   r*   r+   )r   )argument_specs    r   define_argument_specz$ClcLoadBalancer.define_argument_spec]  s     t$4(%r3i(!2L ABj(%;<FB@	Iz3JK!$%
& r    c                    t         j                  }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|r|| j                  j                  _        |r6|r4|| j                  _        d| j                  _        || j                  _        y|r*|r(| j                  j                  j                  ||       y| j                  j                  d	
      S )zk
        Set the CLC Credentials on the sdk by reading environment variables
        :return: none
        CLC_V2_API_TOKENFCLC_V2_API_USERNAMECLC_V2_API_PASSWDCLC_ACCT_ALIASCLC_V2_API_URLT)api_username
api_passwdzPYou must set the CLC_V2_API_USERNAME and CLC_V2_API_PASSWD environment variablesr   N)osenvironr6   r   defaultsENDPOINT_URL_V2_LOGIN_TOKEN_V2_V2_ENABLEDALIASrs   SetCredentialsr   r   )r   envv2_api_tokenv2_api_usernamev2_api_passwd	clc_aliasapi_urls          r   r7   z-ClcLoadBalancer._set_clc_credentials_from_envx  s    
 jjww159''"7? 3U;GG,e4	''*E207DHH-I'3DHH$#'DHH &DHHNHHKK&&,( ' * ;;((, ) - -r    c                     t        | d      rfdt        z   }t        j                         }|j                  j                  d|i       |j                  dxx   d|z   z  cc<   | j                  |       y y )NSetRequestsSessionzClcAnsibleModule/z
Api-Clientz
User-Agent )hasattrr   r   Sessionheadersupdater   )r   agent_stringsess      r   r   zClcLoadBalancer._set_user_agent  sh    3,-.<L""$CKKl;<KK%|);;%""3' .r    )__name__
__module____qualname__r   r   rN   r9   r:   r<   r=   r;   r>   r?   rT   rY   r[   r`   r_   r8   rR   rX   rd   re   ri   rm   r   staticmethodr   r7   r    r    r   r   r      s    
C'$OGb&:$(L*!F(TBB24((
".,0<>(  4-8 ( (r    r   c                  x    t        t        j                         d      } t        |       }|j                          y)zb
    The main function.  Instantiates the module and calls process_request.
    :return: none
    T)r   supports_check_modeN)r	   r   r   rN   )r   clc_loadbalancers     r   mainr     s3    
 )M)M)O/35F&v.$$&r    __main__)!
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rv   r   	tracebacktimer   Bansible_collections.community.general.plugins.module_utils.versionr   r   r   r   ImportError
format_excr   r   r   r   r   ansible.module_utils.basicr	   r
   r   r   r   r   r    r   <module>r      s    A @JXQf#
J   	   [ 
 N % I Jg
( g
(T' zF [  +y++-N  &)&&(KIGs"   A( 
B (BBB#"B#