
    VhNL                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: gitlab_group
short_description: Creates/updates/deletes GitLab Groups
description:
  - When the group does not exist in GitLab, it will be created.
  - When the group does exist and state=absent, the group will be deleted.
author:
  - Werner Dijkerman (@dj-wasabi)
  - Guillaume Martinez (@Lunik)
requirements:
  - python-gitlab python module
extends_documentation_fragment:
  - community.general.auth_basic
  - community.general.gitlab
  - community.general.attributes

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

options:
  auto_devops_enabled:
    description:
      - Default to Auto DevOps pipeline for all projects within this group.
    type: bool
    version_added: 3.7.0
  avatar_path:
    description:
      - Absolute path image to configure avatar. File size should not exceed 200 kb.
      - This option is only used on creation, not for updates.
    type: path
    version_added: 4.2.0
  default_branch:
    description:
      - All merge requests and commits are made against this branch unless you specify a different one.
    type: str
    version_added: 9.5.0
  description:
    description:
      - A description for the group.
    type: str
  enabled_git_access_protocol:
    description:
      - V(all) means SSH and HTTP(S) is enabled.
      - V(ssh) means only SSH is enabled.
      - V(http) means only HTTP(S) is enabled.
      - Only available for top level groups.
    choices: ["all", "ssh", "http"]
    type: str
    version_added: 9.5.0
  force_delete:
    description:
      - Force delete group even if projects in it.
      - Used only when O(state=absent).
    type: bool
    default: false
    version_added: 7.5.0
  lfs_enabled:
    description:
      - Projects in this group can use Git LFS.
    type: bool
    version_added: 9.5.0
  lock_duo_features_enabled:
    description:
      - Enforce GitLab Duo features for all subgroups.
      - Only available for top level groups.
    type: bool
    version_added: 9.5.0
  membership_lock:
    description:
      - Users cannot be added to projects in this group.
    type: bool
    version_added: 9.5.0
  mentions_disabled:
    description:
      - Group mentions are disabled.
    type: bool
    version_added: 9.5.0
  name:
    description:
      - Name of the group you want to create.
    required: true
    type: str
  parent:
    description:
      - Allow to create subgroups.
      - ID or Full path of parent group in the form of group/name.
    type: str
  path:
    description:
      - The path of the group you want to create, this will be api_url/group_path.
      - If not supplied, the group_name will be used.
    type: str
  prevent_forking_outside_group:
    description:
      - Prevent forking outside of the group.
    type: bool
    version_added: 9.5.0
  prevent_sharing_groups_outside_hierarchy:
    description:
      - Members cannot invite groups outside of this group and its subgroups.
      - Only available for top level groups.
    type: bool
    version_added: 9.5.0
  project_creation_level:
    description:
      - Determine if developers can create projects in the group.
    choices: ["developer", "maintainer", "noone"]
    type: str
    version_added: 3.7.0
  request_access_enabled:
    description:
      - Users can request access (if visibility is public or internal).
    type: bool
    version_added: 9.5.0
  service_access_tokens_expiration_enforced:
    description:
      - Service account token expiration.
      - Changes will not affect existing token expiration dates.
      - Only available for top level groups.
    type: bool
    version_added: 9.5.0
  share_with_group_lock:
    description:
      - Projects cannot be shared with other groups.
    type: bool
    version_added: 9.5.0
  require_two_factor_authentication:
    description:
      - Require all users in this group to setup two-factor authentication.
    type: bool
    version_added: 3.7.0
  state:
    description:
      - Create or delete group.
      - Possible values are present and absent.
    default: present
    type: str
    choices: ["present", "absent"]
  subgroup_creation_level:
    description:
      - Allowed to create subgroups.
    choices: ["maintainer", "owner"]
    type: str
    version_added: 3.7.0
  two_factor_grace_period:
    description:
      - Delay 2FA enforcement (hours).
    type: str
    version_added: 9.5.0
  visibility:
    description:
      - Default visibility of the group.
    choices: ["private", "internal", "public"]
    default: private
    type: str
  wiki_access_level:
    description:
      - V(enabled) means everyone can access the wiki.
      - V(private) means only members of this group can access the wiki.
      - V(disabled) means group-level wiki is disabled.
    choices: ["enabled", "private", "disabled"]
    type: str
    version_added: 9.5.0
a  
- name: "Delete GitLab Group"
  community.general.gitlab_group:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    name: my_first_group
    state: absent

- name: "Create GitLab Group"
  community.general.gitlab_group:
    api_url: https://gitlab.example.com/
    validate_certs: true
    api_username: dj-wasabi
    api_password: "MySecretPassword"
    name: my_first_group
    path: my_first_group
    state: present

# The group will by created at https://gitlab.dj-wasabi.local/super_parent/parent/my_first_group
- name: "Create GitLab SubGroup"
  community.general.gitlab_group:
    api_url: https://gitlab.example.com/
    validate_certs: true
    api_username: dj-wasabi
    api_password: "MySecretPassword"
    name: my_first_group
    path: my_first_group
    state: present
    parent: "super_parent/parent"

# Other group which only allows sub-groups - no projects
- name: "Create GitLab Group for SubGroups only"
  community.general.gitlab_group:
    api_url: https://gitlab.example.com/
    validate_certs: true
    api_username: dj-wasabi
    api_password: "MySecretPassword"
    name: my_main_group
    path: my_main_group
    state: present
    project_creation_level: noone
    auto_devops_enabled: false
    subgroup_creation_level: maintainer
a  
msg:
  description: Success or failure message.
  returned: always
  type: str
  sample: "Success"

result:
  description: JSON-parsed response from the server.
  returned: always
  type: dict

error:
  description: The error message returned by the GitLab API.
  returned: failed
  type: str
  sample: "400: path is already in use"

group:
  description: API object.
  returned: always
  type: dict
)basic_auth_argument_spec)AnsibleModule)	to_native)auth_argument_spec
find_groupgitlab_authenticationgitlabc                   D    e Zd Zd Z	 d Z	 d Z	 d Z	 d Z	 d	dZ	 d Z	y)
GitLabGroupc                 .    || _         || _        d | _        y N)_module_gitlabgroup_object)selfmodulegitlab_instances      r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/gitlab_group.py__init__zGitLabGroup.__init__  s    &     c                      ||j                   S y r   )id)r   groups     r   get_group_idzGitLabGroup.get_group_id  s    88Or   c                 R   d}i d|d   d|d   d|d   d|d   d|d   d|d   d|d	|d	   d
|d
   d|d   d|d   d|d   d|d   d|d   d|d   d|d   }|j                  d      r
||d   |d<   |j                  d      r
||d   |d<   |j                  d      r
||d   |d<   |j                  d      r
||d   |d<   |j                  d      rt        |d         |d<   | j                  B| j                  |      |d<   | j	                  |      }|d   r	 t        |d   d      |_        d}n| j                  | j                  |      \  }}|| _        |rH| j                  j                  r | j                  j                  dd|z         	 |j                          yy# t        $ r9}| j                  j                  dj                  |d   |             Y d }~d }~ww xY w# t        $ r)}| j                  j                  d|z         Y d }~yd }~ww xY w) NFauto_devops_enableddefault_branchdescriptionlfs_enabledmembership_lockmentions_disablednamepathprevent_forking_outside_groupproject_creation_levelrequest_access_enabled!require_two_factor_authenticationshare_with_group_locksubgroup_creation_level
visibilitywiki_access_levelenabled_git_access_protocollock_duo_features_enabled(prevent_sharing_groups_outside_hierarchy)service_access_tokens_expiration_enforcedtwo_factor_grace_period	parent_idavatar_pathrbzCannot open {0}: {1}msgT,Successfully created or updated the group %schangedr8   zFailed to update group: %s )getintr   r   create_groupopenavatarIOErrorr   	fail_jsonformatupdate_group
check_mode	exit_jsonsave	Exception)r   r%   parentoptionsr;   payloadr   es           r   create_or_update_groupz"GitLabGroup.create_or_update_group  s   
!7+@#A
g&67
 7=1
 7=1	

 w'89
  )<!=
 D
 GFO
 ,W5T-U
 %g.F&G
 %g.F&G
 09\1]
 $W-D%E
 &w/H'I
 ',/
   )<!=!
$ ;;45&.5<=Z5[G12;;233:;V3WG/0;;ABv~BIJtBuG>?;;BCCJKvCwG?@;;0114W=V5W1XG-. $#'#4#4V#<GK %%g.E }%i#'(>#EEL G!..t/@/@'JNGU!||&&&&t9gjn9n&oN

 #  iLL**/E/L/LWUbMcef/g*hhi  N&&+H1+L&MMNs0   'F/ G4 /	G18/G,,G14	H&=H!!H&c                 ~   | j                   j                  ry	 |j                         D ci c]  \  }}|	|| }}}| j                  j                  j                  |      }|S c c}}w # t        j                  j                  $ r3}| j                   j                  dt        |      z         Y d }~S d }~ww xY w)NTzFailed to create group: %s r7   )r   rE   itemsr   groupscreater   
exceptionsGitlabCreateErrorrB   r   )r   	argumentsarg_key	arg_valuefilteredr   rL   s          r   r>   zGitLabGroup.create_groupT  s    <<""	UEN__EVp/Aw	ZcZo*pHpLL''..x8E  q !!33 	ULL""'DyQR|'S"TT	Us,   A, 
A&A&(A, &A, ,B<	(B77B<c                     d}|j                         D ]'  \  }}|	t        ||      |k7  st        |||       d}) ||fS )NFT)rO   getattrsetattr)r   r   rT   r;   rU   rV   s         r   rD   zGitLabGroup.update_groupf  sU    "+//"3 	#GY$5'*i7E7I6"G		# r   c                 v   | j                   }|sEt        |j                  j                  d            dk\  r| j                  j                  d       y | j                  j                  ry	 |j                          y # t        $ r2}| j                  j                  dt        |      z         Y d }~y d }~ww xY w)NF)all   zThere are still projects in this group. These needs to be moved or deleted before this group can be removed. Use 'force_delete' to 'true' to force deletion of existing projects.r7   TzFailed to delete group: %s )
r   lenprojectslistr   rB   rE   deleterH   r   )r   forcer   rL   s       r   delete_groupzGitLabGroup.delete_groupt  s    !!U^^00U0;<ALL""\ #  ||&&Y Y&&+H9UV<+W&XXYs   ,A= =	B8(B33B8c                 D    t        | j                  |      }|r|| _        yy)NTF)r
   r   r   )r   project_identifierr   s      r   exists_groupzGitLabGroup.exists_group  s$    4<<);< %Dr   N)F)
__name__
__module____qualname__r   r   rM   r>   rD   rc   rf    r   r   r   r     sE    !


:x	 Y$r   r   c                  F	   t               } | j                  t                      | j                  t        d@i dt        d      dt        d      dt        d      dt        d      d	t        dg d
      dt        dd      dt        d      dt        d      dt        d      dt        d      dt        dd      dt        d      dt        d      dt        d      dt        d      dt        dg d      dt        d      dt        d      dt        d      dt        d      dt        dd d!d g"      d#t        dd$d%g      d&t        d      d't        dd(g d)"      d*t        dg d+             t	        | d,d-gd,d.gd/d-gd/d.gd/d,ggd/d0ggg d1gd2      }t        |      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d	   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d'   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d#   }|j                  d&   }|j                  d*   }|
|	j                  d3d4      }
t        ||      }d }|rBt        ||      }|s|j                  d56       |j                  |j                  d7z   |
z         }n|j                  |
      }|d!k(  r>|r)|j                  |8       |j                  dd9|	z  :       n|j                  dd;:       |d k(  r|j                  |	|i d|d|d|d|d	|d|d|d|d|d|
d|d|d|d|d|d|d|||||d<      r,|j                  dd=|	z  |j                   j"                  >       y |j                  dd?|	z  |j                   j"                  >       y y )ANr   bool)typer5   r&   r    strr!   r/   )r\   sshhttp)rm   choicesforce_deleteF)rm   defaultr"   r0   r#   r$   r%   T)rm   requiredrI   r'   r1   r(   )	developer
maintainernooner)   r*   r2   r+   statepresentabsent)rm   rs   rq   r,   rv   ownerr3   r-   private)internalr|   publicr.   )enabledr|   disabled	api_tokenapi_job_tokenapi_oauth_tokenapi_usernameapi_password)r   r   r   r   )argument_specmutually_exclusiverequired_togetherrequired_one_ofsupports_check_mode _z9Failed to create GitLab group: Parent group doesn't existr7   /)rb   zSuccessfully deleted group %sr:   zGroup deleted or does not exist)r,   r3   r-   r.   r9   )r;   r8   r   zNo need to update the group %srj   )r   updater	   dictr   r   paramsreplacer   r
   rB   rf   	full_pathrc   rF   rM   r   _attrs)r   r   r   r   r5   r    r!   r/   rr   
group_name
group_pathgroup_visibilityr"   r0   r#   r$   parent_identifierr'   r1   r(   r)   r*   r2   r+   rx   r,   r3   r.   gitlab_groupparent_groupgroup_existss                                  r   mainr     s   ,.M+-.  f-f% ' e$	
 %)e=S$T vu5 f% #'F"3 &) F+ ut,  u '+&7 2661B   $8\]!"  $0#$ +/F*;%& 37F2C'( #/)* y8Y:OP+, !%%,9P Q-. !%% 0/0 UI?`a12 E3UV3 8 #/*+,_-./[)
 ^,
 N
 !F& ,F3O --(=>--.K]]#34N--.K"(--0M"N==0Lv&Jv&J}}\2--.K &.I Jmm$56O&9:h/$*MM2Q$R!/5}}=g/h,#]]+CD#]]+CD(.6Y(Z%06>i0j-"MM*ABMM'"E$mm,EF$mm,EF&9: ''S1
v7LL!/3DE!\]#001G1G#1MPZ1Z[#00<%%L%9T/NQ[/[\U0QR	..z< J
!#6J
;J
 nJ
 ;	J

 *+FJ
 ;J
 ()BJ
 J
  !2J
 JJ
 ,-JJ
 78`J
 %&<J
 %&<J
 01RJ
  89b!J
" $%:#J
$ (?'>*!2+J
 . T/]`j/jr~  sL  sL  sS  sS  TU0PS]0]eqe~e~  fF  fF  G5 r   __main__N)
__future__r   r   r   rm   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.apir   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   Aansible_collections.community.general.plugins.module_utils.gitlabr	   r
   r   r   objectr   r   rg   rj   r   r   <module>r      sk    A @fP+Z
0 > 4 A 
N& NbGD zF r   