
    VhZ.                     :   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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e      Zd Zedk(  r e        yy# e$ r  ej$                         Zd	ZY Mw xY w# e$ r  ej$                         Zd	ZdZY ^w xY w)    )absolute_importdivisionprint_functiona  
module: clc_publicip
short_description: Add and Delete public IPs on servers in CenturyLink Cloud
description:
  - An Ansible module to add or delete public IP addresses on an existing server or servers 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:
  protocol:
    description:
      - The protocol that the public IP will listen for.
    type: str
    default: TCP
    choices: ['TCP', 'UDP', 'ICMP']
  ports:
    description:
      - A list of ports to expose. This is required when O(state=present).
    type: list
    elements: int
  server_ids:
    description:
      - A list of servers to create public IPs on.
    type: list
    required: true
    elements: str
  state:
    description:
      - Determine whether to create or delete public IPs. If V(present) module will not create a second public IP if one already
        exists.
    type: str
    default: present
    choices: ['present', 'absent']
  wait:
    description:
      - Whether to wait for the tasks to finish before returning.
    type: bool
    default: true
a  
# Note - You must set the CLC_V2_API_USERNAME And CLC_V2_API_PASSWD Environment variables before running these examples

- name: Add Public IP to Server
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
    - name: Create Public IP For Servers
      community.general.clc_publicip:
        protocol: TCP
        ports:
          - 80
        server_ids:
          - UC1TEST-SVR01
          - UC1TEST-SVR02
        state: present
      register: clc

    - name: Debug
      ansible.builtin.debug:
        var: clc

- name: Delete Public IP from Server
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
    - name: Create Public IP For Servers
      community.general.clc_publicip:
        server_ids:
          - UC1TEST-SVR01
          - UC1TEST-SVR02
        state: absent
      register: clc

    - name: Debug
      ansible.builtin.debug:
        var: clc
z
server_ids:
  description: The list of server IDs that are changed.
  returned: success
  type: list
  sample: ["UC1TEST-SVR01", "UC1TEST-SVR02"]
z
${version}N)LooseVersionTF)CLCException)AnsibleModulemissing_required_libc                   j    e Zd ZeZdZd Zd Zed        Z	d Z
d Zd Zd Zd	 Zd
 Zd Zed        Zy)ClcPublicIpNc                    || _         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)module	CLC_FOUND	fail_jsonr	   CLC_IMP_ERRREQUESTS_FOUNDREQUESTS_IMP_ERRr   __version__r   _set_user_agentclc)selfr   s     r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/clc_publicip.py__init__zClcPublicIp.__init__   s     KK!!&:9&EQ\!]KK!!&::&FRb!cL1E1E$FV]I^$^KK!!B " D 	TXX&    c                    | j                          | j                  j                  }|d   }|d   }|d   }|d   }|dk(  r| j                  |||      \  }}}n;|dk(  r| j	                  |      \  }}}n| j                  j                  d	|z   
      S | j                  |       | j                  j                  ||      S )zu
        Process the request - Main Code Path
        :return: Returns with either an exit_json or fail_json
        
server_idsportsprotocolstatepresent)r   r!   r    absent)r   zUnknown State: r   )changedr   )_set_clc_credentials_from_envr   paramsensure_public_ip_presentensure_public_ip_absentr   _wait_for_requests_to_complete	exit_json)	r   r'   r   r    r!   r"   r%   changed_server_idsr   s	            r   process_requestzClcPublicIp.process_request   s    
 	**,##L)
w*%wI484Q4Q% 5R 5G1G'h484P4P% 5Q 5'1G' ;;((->-F(GG++H5{{$$W0B % D 	Dr   c                      t        t        ddd      t        dg d      t        dd	      t        d
d      t        dddg            } | S )zk
        Define the argument spec for the ansible module
        :return: argument spec dictionary
        listTstr)typerequiredelementsTCP)r4   UDPICMP)defaultchoicesint)r1   r3   bool)r1   r7   r#   r$   )r   r!   r    waitr"   )dict)argument_specs    r   _define_module_argument_specz(ClcPublicIp._define_module_argument_spec   sP     $G%1GHFU3640y9h2GH
 r   c                    d}g }g }| j                  |d      }|D cg c]+  }t        |j                         j                        dk(  s*|- }	}|D 
cg c]  }
||
d	 }}
|	D ]X  }| j                  j
                  s#| j                  ||      }|j                  |       |j                  |j                         d}Z |||fS c c}w c c}
w )a  
        Ensures the given server ids having the public ip available
        :param server_ids: the list of server ids
        :param protocol: the ip protocol
        :param ports: the list of ports to expose
        :return: (changed, changed_server_ids, results)
                  changed: A flag indicating if there is any change
                  changed_server_ids : the list of server ids that are changed
                  results: The result list from clc public ip call
        F-Failed to obtain server list from the CLC APIr   )r!   portT)	_get_servers_from_clclen	PublicIPs
public_ipsr   
check_mode_add_publicip_to_serverappendid)r   r   r!   r    r%   resultsr,   serversserverservers_to_changerA   ports_to_exposeresults                r   r(   z$ClcPublicIp.ensure_public_ip_present   s     ,,;= ")5C  "---/23-4F5 5 (-.# )1$? . .' 	F;;))55foNv&%%fii0G	 *G335.s   +C	CCc                     d }	 |j                         j                  |      }|S # t        $ rK}| j                  j	                  dj                  |j                  |j                               Y d }~|S d }~ww xY w)Nz0Failed to add public ip to the server : {0}. {1}r   )rD   Addr   r   r   formatrI   response_text)r   rL   rN   rO   exs        r   rG   z#ClcPublicIp._add_publicip_to_server   s|    	%%'++O<F
 	  	KK!!&X&_&_		2++'!   		s   % 	A9A A44A9c                 j   d}g }g }| j                  |d      }|D cg c]+  }t        |j                         j                        dkD  s*|- }}|D ]W  }| j                  j
                  s"| j                  |      }|j                  |       |j                  |j                         d}Y |||fS c c}w )a  
        Ensures the given server ids having the public ip removed if there is any
        :param server_ids: the list of server ids
        :return: (changed, changed_server_ids, results)
                  changed: A flag indicating if there is any change
                  changed_server_ids : the list of server ids that are changed
                  results: The result list from clc public ip call
        Fr@   r   T)	rB   rC   rD   rE   r   rF   _remove_publicip_from_serverrH   rI   )	r   r   r%   rJ   r,   rK   rL   rM   rO   s	            r   r)   z#ClcPublicIp.ensure_public_ip_absent   s     ,,;= ")4C  "---/12-3F4 4 ( 	F;;))::6Bv&%%fii0G	 *G334s   +B0	B0c                    d }	 |j                         j                  D ]  }|j                         } 	 |S # t        $ rK}| j                  j                  dj                  |j                  |j                               Y d }~|S d }~ww xY w)Nz5Failed to remove public ip from the server : {0}. {1}r   )	rD   rE   Deleter   r   r   rR   rI   rS   )r   rL   rO   
ip_addressrT   s        r   rV   z(ClcPublicIp._remove_publicip_from_server  s    	$..0;; -
#**,- 	  	KK!!&]&d&d		2++'!   		s   /6 	B
A BB
c                     | j                   j                  d   sy|D ]S  }|j                          |j                  D ]2  }|j	                         dk7  s| j                   j                  d       4 U y)z
        Waits until the CLC requests are complete if the wait argument is True
        :param requests_lst: The list of CLC request objects
        :return: none
        r;   N	succeededz#Unable to process public ip requestr   )r   r'   WaitUntilCompleter   Statusr   )r   requests_lstrequestrequest_detailss       r   r*   z*ClcPublicIp._wait_for_requests_to_complete  sy     {{!!&)# 	CG%%'#*#3#3 C"))+{:KK))A * CC	C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environgetr   defaultsENDPOINT_URL_V2_LOGIN_TOKEN_V2_V2_ENABLEDALIASv2SetCredentialsr   r   )r   envv2_api_tokenv2_api_usernamev2_api_passwd	clc_aliasapi_urls          r   r&   z)ClcPublicIp._set_clc_credentials_from_env*  s    
 jjww159''"7? 3U;GG,e4	''*E207DHH-I'3DHH$#'DHH &DHHNHHKK&&,( ' * ;;((, ) - -r   c                     	 | j                   j                  j                  |      j                  S # t        $ r,}| j
                  j                  |d|z  z          Y d}~yd}~ww xY w)z3
        Gets list of servers form CLC api
        z: %sr   N)r   rq   ServersrK   r   r   r   )r   r   messager   s       r   rB   z!ClcPublicIp._get_servers_from_clcF  s[    	D88;;&&z2::: 	DKK!!g0B&B!CC	Ds   .1 	A&"A!!A&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ClcPublicIp._set_user_agentO  sh    3,-.<L""$CKKl;<KK%|);;%""3' .r   )__name__
__module____qualname__clc_sdkr   r   r   r-   staticmethodr>   r(   rG   r)   rV   r*   r&   rB   r    r   r   r   r      sd    
CF'D0  4<44	C-8D ( (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>   r-   )r   clc_public_ips     r   mainr   Y  s5    
 !>>@ F  'M!!#r   __main__)
__future__r   r   r   r1   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ri   	tracebackBansible_collections.community.general.plugins.module_utils.versionr   r   r   r   ImportError
format_excr   r   r   r   r   ansible.module_utils.basicr   r	   objectr   r   r   r   r   r   <module>r      s    A @3j'R
  	  [ 
 N   I JB(& B(J
$ zF U  +y++-N  &)&&(KIGs"   A 
A= A:9A:=BB