
    Vh@                     :   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_group
short_description: Create/delete Server Groups at Centurylink Cloud
description:
  - Create or delete Server Groups at Centurylink 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 Server Group.
    type: str
    required: true
  description:
    description:
      - A description of the Server Group.
    type: str
    required: false
  parent:
    description:
      - The parent group of the server group. If parent is not provided, it creates the group at top level.
    type: str
    required: false
  location:
    description:
      - Datacenter to create the group in. If location is not provided, the group gets created in the default datacenter associated
        with the account.
    type: str
    required: false
  state:
    description:
      - Whether to create or delete the group.
    type: str
    default: present
    choices: ['present', 'absent']
  wait:
    description:
      - Whether to wait for the tasks to finish before returning.
    type: bool
    default: true
    required: false
a  
# Create a Server Group
- name: Create Server Group
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
    - name: Create / Verify a Server Group at CenturyLink Cloud
      community.general.clc_group:
        name: My Cool Server Group
        parent: Default Group
        state: present
      register: clc

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

# Delete a Server Group
- name: Delete Server Group
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
    - name: Delete / Verify Absent a Server Group at CenturyLink Cloud
      community.general.clc_group:
        name: My Cool Server Group
        parent: Default Group
        state: absent
      register: clc

    - name: Debug
      ansible.builtin.debug:
        var: clc
a  
group:
    description: The group information.
    returned: success
    type: dict
    sample:
        {
           "changeInfo":{
              "createdBy":"service.wfad",
              "createdDate":"2015-07-29T18:52:47Z",
              "modifiedBy":"service.wfad",
              "modifiedDate":"2015-07-29T18:52:47Z"
           },
           "customFields":[

           ],
           "description":"test group",
           "groups":[

           ],
           "id":"bb5f12a3c6044ae4ad0a03e73ae12cd1",
           "links":[
              {
                 "href":"/v2/groups/wfad",
                 "rel":"createGroup",
                 "verbs":[
                    "POST"
                 ]
              },
              {
                 "href":"/v2/servers/wfad",
                 "rel":"createServer",
                 "verbs":[
                    "POST"
                 ]
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1",
                 "rel":"self",
                 "verbs":[
                    "GET",
                    "PATCH",
                    "DELETE"
                 ]
              },
              {
                 "href":"/v2/groups/wfad/086ac1dfe0b6411989e8d1b77c4065f0",
                 "id":"086ac1dfe0b6411989e8d1b77c4065f0",
                 "rel":"parentGroup"
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/defaults",
                 "rel":"defaults",
                 "verbs":[
                    "GET",
                    "POST"
                 ]
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/billing",
                 "rel":"billing"
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/archive",
                 "rel":"archiveGroupAction"
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/statistics",
                 "rel":"statistics"
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/upcomingScheduledActivities",
                 "rel":"upcomingScheduledActivities"
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/horizontalAutoscalePolicy",
                 "rel":"horizontalAutoscalePolicyMapping",
                 "verbs":[
                    "GET",
                    "PUT",
                    "DELETE"
                 ]
              },
              {
                 "href":"/v2/groups/wfad/bb5f12a3c6044ae4ad0a03e73ae12cd1/scheduledActivities",
                 "rel":"scheduledActivities",
                 "verbs":[
                    "GET",
                    "POST"
                 ]
              }
           ],
           "locationId":"UC1",
           "name":"test group",
           "status":"active",
           "type":"default"
        }
z
${version}N)LooseVersionTF)CLCException)AnsibleModulemissing_required_libc                   x    e Zd ZdZdZd Zd Zed        Zd Z	d Z
d Zd Zd	 Zd
 ZddZd Zd Zed        Zy)ClcGroupNc                    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
group_dict	CLC_FOUND	fail_jsonr	   CLC_IMP_ERRREQUESTS_FOUNDREQUESTS_IMP_ERRr   __version__r   _set_user_agent)selfr   s     o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/clc_group.py__init__zClcGroup.__init__   s     KK!!&:9&EQ\!]KK!!&::&FRb!cL1E1E$FV]I^$^KK!!B " D 	TXX&    c                    | j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                   j                  j                  d      }| j                          | j	                  |      | _        |dk(  r+| j                  ||      \  }}}|r)| j                  |       n| j                  |||	      \  }}	 |j                  }| j                   j                  ||
       y# t        $ r |}Y +w xY w)zZ
        Execute the main code path, and handle the request
        :return: none
        locationnameparentdescriptionstate)
datacenterabsent
group_nameparent_name)r)   r*   group_description)changedgroupN)r   paramsget_set_clc_credentials_from_env_get_group_tree_for_datacenterr   _ensure_group_is_absent_wait_for_requests_to_complete_ensure_group_is_presentdataAttributeError	exit_json)	r   r!   r)   r*   r+   r%   r,   r-   r   s	            r   process_requestzClcGroup.process_request  sJ   
 ;;%%))*5[[''++F3
kk((,,X6 KK..22=A""&&w/**,== > ! H'+'C'C%; (D (@$GUH33H=!::%;Rc ; eNGU	JJE 	gU;  	E	s   )E E! E!c                      t        t        d      t               t               t               t        dddg      t        dd            } | S )	zk
        Define the argument spec for the ansible module
        :return: argument spec dictionary
        T)requiredpresentr'   )defaultchoicesbool)typer<   )r"   r$   r#   r!   r%   wait)dict)argument_specs    r   _define_module_argument_specz%ClcGroup._define_module_argument_spec"  sF     t$6Vy9h2GH6402 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environr/   r   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   r0   z&ClcGroup._set_clc_credentials_from_env2  s    
 jjww159''"7? 3U;GG,e4	''*E207DHH-I'3DHH$#'DHH &DHHNHHKK&&,( ' * ;;((, ) - -r   c                     d}g }g }| j                  ||      rK| j                  j                  s3|j                  |       | j	                  |      }|j                  |       d}|||fS )a  
        Ensure that group_name is absent by deleting it if necessary
        :param group_name: string - the name of the clc server group to delete
        :param parent_name: string - the name of the parent group for group_name
        :return: changed, group
        Fr(   T)_group_existsr   
check_modeappend_delete_group)r   r)   r*   r,   r-   resultsresults          r   r2   z ClcGroup._ensure_group_is_absentN  sm     M;;))Z(++J7v&Gw&&r   c                    d}| j                   j                  |      \  }}	 |j                         }|S # t        $ rA}| j                  j                  dj                  ||j                               Y d}~|S d}~ww xY w)z
        Delete the provided server group
        :param group_name: string - the server group to delete
        :return: none
        Nz Failed to delete group :{0}. {1}r   )r   r/   Deleter   r   r   formatresponse_text)r   r)   responser-   r#   exs         r   r_   zClcGroup._delete_groupa  s     ++J7v	||~H
 	  	KK!!&H&O&OB,,'!   		s   4 	A>6A99A>c                    | j                   st        d      ||n| j                   j                  }|}d}|}| j                  |d      }| j                  ||      }	|r|	r| j                  |   \  }}d}||fS |r2|	s0| j
                  j                  s| j                  |||      }d}||fS | j
                  j                  d|z   dz   	       ||fS )
a  
        Checks to see if a server group exists, creates it if it doesn't.
        :param group_name: the name of the group to validate/create
        :param parent_name: the name of the parent group for group_name
        :param group_description: a short description of the server group (used when creating)
        :return: (changed, group) -
            changed:  Boolean- whether a change was made,
            group:  A clc group object for the group
        z(Implementation Error: Root Group not setNFr(   )r-   r#   r$   Tzparent group: z does not existr   )	
root_groupAssertionErrorr"   r\   r   r   r]   _create_groupr   )
r   r)   r*   r+   r#   r$   r,   r-   parent_existschild_existss
             r   r4   z!ClcGroup._ensure_group_is_presentq  s     !KLL + 7T__=Q=Q'**f$*O))! *   \ OOJ7ME6G ~ <;;))**! + + - G ~ KK!!$!" " #
 ~r   c                     d}| j                   |   \  }}	 |j                  ||      }|S # t        $ rA}| j                  j	                  dj                  ||j                               Y d}~|S d}~ww xY w)a)  
        Create the provided server group
        :param group: clc_sdk.Group - the group to create
        :param parent: clc_sdk.Parent - the parent group for {group}
        :param description: string - a text description of the group
        :return: clc_sdk.Group - the created group
        N)r"   r$   z Failed to create group :{0}. {1}r   )r   Creater   r   r   rd   re   )r   r-   r#   r$   rf   grandparentrg   s          r   rk   zClcGroup._create_group  s      $ 7	*}}%[}IH   	*KK!!&H&O&Or''')! * *	*s   + 	A56A00A5c                 p    d}|| j                   v r%| j                   |   \  }}|||j                  k(  rd}|S )z
        Check to see if a group exists
        :param group_name: string - the group to check
        :param parent_name: string - the parent of group_name
        :return: boolean - whether the group exists
        FT)r   r"   )r   r)   r*   ra   r-   r#   s         r   r\   zClcGroup._group_exists  sB     ("ooj9OUF"kV[[&@r   c                     | j                   j                  j                  |      j                         | _        | j                  d| j                        S )z
        Walk the tree of groups for a datacenter
        :param datacenter: string - the datacenter to walk (ex: 'UC1')
        :return: a dictionary of groups and parents
        )r!   N)parent_groupchild_group)r   rS   
Datacenter	RootGroupri   _walk_groups_recursive)r   r&   s     r   r1   z'ClcGroup._get_group_tree_for_datacenter  sR     ((++00 1 !!* 	** + ) 	)r   c                     t        |      ||fi}|j                         j                  }t        |      dkD  r8|D ]3  }|j                  dk7  r|j                  | j                  ||             5 |S )a.  
        Walk a parent-child tree of groups, starting with the provided child group
        :param parent_group: clc_sdk.Group - the parent group to start the walk
        :param child_group: clc_sdk.Group - the child group to start the walk
        :return: a dictionary of groups and parents
        r   r<   )str	Subgroupsgroupslenr?   updaterw   )r   rs   rt   ra   r{   r-   s         r   rw   zClcGroup._walk_groups_recursive  sw     k"[,$?@&&(//v;? O::*d99+uMN	O
 r   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 group requestr   )r   r.   WaitUntilCompleter   Statusr   )r   requests_lstrequestrequest_detailss       r   r3   z'ClcGroup._wait_for_requests_to_complete  st     {{!!&)# 	?G%%'#*#3#3 ?"))+{:KK))= * ??	?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headersr}   r   )r   agent_stringsess      r   r   zClcGroup._set_user_agent  sh    3,-.<L""$CKKl;<KK%|);;%""3' .r   )N)__name__
__module____qualname__r   ri   r   r8   staticmethodrC   r0   r2   r_   r4   rk   r\   r1   rw   r3   r    r   r   r   r      sl    
CJ'$<:  -8'& *X"
)"? ( (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)rB   supports_check_modeN)r   r   rC   r8   )r   	clc_groups     r   mainr     s5    
 ;;= "F  Ir   __main__)
__future__r   r   r   r?   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rL   	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 @7r"Ha
F  	  [ 
 N   I JA(v A(H
  zF S	  +y++-N  &)&&(KIGs"   A 
A= A:9A:=BB