Skip to content

Commit

Permalink
[PR #8908/e7ccbc2f backport][stable-9] Add gitlab group params (#8981)
Browse files Browse the repository at this point in the history
Add gitlab group params (#8908)

Add new gitlab_group parameters

(cherry picked from commit e7ccbc2)

Co-authored-by: Julien Lecomte <[email protected]>
  • Loading branch information
patchback[bot] and julien-lecomte authored Oct 4, 2024
1 parent 9b4decd commit 200ab04
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 28 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8908-add-gitlab-group-params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- gitlab_group - add many new parameters (https://github.com/ansible-collections/community.general/pull/8908).
174 changes: 148 additions & 26 deletions plugins/modules/gitlab_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,52 @@
- 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.
Expand All @@ -70,12 +105,40 @@
- 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.
Expand All @@ -94,12 +157,25 @@
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
'''

EXAMPLES = '''
Expand Down Expand Up @@ -202,23 +278,38 @@ def get_group_id(self, group):
def create_or_update_group(self, name, parent, options):
changed = False

payload = {
'auto_devops_enabled': options['auto_devops_enabled'],
'default_branch': options['default_branch'],
'description': options['description'],
'lfs_enabled': options['lfs_enabled'],
'membership_lock': options['membership_lock'],
'mentions_disabled': options['mentions_disabled'],
'name': name,
'path': options['path'],
'prevent_forking_outside_group': options['prevent_forking_outside_group'],
'project_creation_level': options['project_creation_level'],
'request_access_enabled': options['request_access_enabled'],
'require_two_factor_authentication': options['require_two_factor_authentication'],
'share_with_group_lock': options['share_with_group_lock'],
'subgroup_creation_level': options['subgroup_creation_level'],
'visibility': options['visibility'],
'wiki_access_level': options['wiki_access_level'],
}
if options.get('enabled_git_access_protocol') and parent is None:
payload['enabled_git_access_protocol'] = options['enabled_git_access_protocol']
if options.get('lock_duo_features_enabled') and parent is None:
payload['lock_duo_features_enabled'] = options['lock_duo_features_enabled']
if options.get('prevent_sharing_groups_outside_hierarchy') and parent is None:
payload['prevent_sharing_groups_outside_hierarchy'] = options['prevent_sharing_groups_outside_hierarchy']
if options.get('service_access_tokens_expiration_enforced') and parent is None:
payload['service_access_tokens_expiration_enforced'] = options['service_access_tokens_expiration_enforced']
if options.get('two_factor_grace_period'):
payload['two_factor_grace_period'] = int(options['two_factor_grace_period'])

# Because we have already call userExists in main()
if self.group_object is None:
parent_id = self.get_group_id(parent)

payload = {
'auto_devops_enabled': options['auto_devops_enabled'],
'name': name,
'parent_id': parent_id,
'path': options['path'],
'project_creation_level': options['project_creation_level'],
'subgroup_creation_level': options['subgroup_creation_level'],
'visibility': options['visibility'],
}
if options.get('description'):
payload['description'] = options['description']
if options.get('require_two_factor_authentication'):
payload['require_two_factor_authentication'] = options['require_two_factor_authentication']
payload['parent_id'] = self.get_group_id(parent)
group = self.create_group(payload)

# add avatar to group
Expand All @@ -229,15 +320,7 @@ def create_or_update_group(self, name, parent, options):
self._module.fail_json(msg='Cannot open {0}: {1}'.format(options['avatar_path'], e))
changed = True
else:
changed, group = self.update_group(self.group_object, {
'auto_devops_enabled': options['auto_devops_enabled'],
'description': options['description'],
'name': name,
'project_creation_level': options['project_creation_level'],
'require_two_factor_authentication': options['require_two_factor_authentication'],
'subgroup_creation_level': options['subgroup_creation_level'],
'visibility': options['visibility'],
})
changed, group = self.update_group(self.group_object, payload)

self.group_object = group
if changed:
Expand Down Expand Up @@ -324,16 +407,29 @@ def main():
argument_spec.update(dict(
auto_devops_enabled=dict(type='bool'),
avatar_path=dict(type='path'),
default_branch=dict(type='str'),
description=dict(type='str'),
enabled_git_access_protocol=dict(type='str', choices=['all', 'ssh', 'http']),
force_delete=dict(type='bool', default=False),
lfs_enabled=dict(type='bool'),
lock_duo_features_enabled=dict(type='bool'),
membership_lock=dict(type='bool'),
mentions_disabled=dict(type='bool'),
name=dict(type='str', required=True),
parent=dict(type='str'),
path=dict(type='str'),
prevent_forking_outside_group=dict(type='bool'),
prevent_sharing_groups_outside_hierarchy=dict(type='bool'),
project_creation_level=dict(type='str', choices=['developer', 'maintainer', 'noone']),
request_access_enabled=dict(type='bool'),
require_two_factor_authentication=dict(type='bool'),
service_access_tokens_expiration_enforced=dict(type='bool'),
share_with_group_lock=dict(type='bool'),
state=dict(type='str', default="present", choices=["absent", "present"]),
subgroup_creation_level=dict(type='str', choices=['maintainer', 'owner']),
two_factor_grace_period=dict(type='str'),
visibility=dict(type='str', default="private", choices=["internal", "private", "public"]),
wiki_access_level=dict(type='str', choices=['enabled', 'private', 'disabled']),
))

module = AnsibleModule(
Expand All @@ -359,16 +455,29 @@ def main():

auto_devops_enabled = module.params['auto_devops_enabled']
avatar_path = module.params['avatar_path']
default_branch = module.params['default_branch']
description = module.params['description']
enabled_git_access_protocol = module.params['enabled_git_access_protocol']
force_delete = module.params['force_delete']
group_name = module.params['name']
group_path = module.params['path']
group_visibility = module.params['visibility']
lfs_enabled = module.params['lfs_enabled']
lock_duo_features_enabled = module.params['lock_duo_features_enabled']
membership_lock = module.params['membership_lock']
mentions_disabled = module.params['mentions_disabled']
parent_identifier = module.params['parent']
prevent_forking_outside_group = module.params['prevent_forking_outside_group']
prevent_sharing_groups_outside_hierarchy = module.params['prevent_sharing_groups_outside_hierarchy']
project_creation_level = module.params['project_creation_level']
request_access_enabled = module.params['request_access_enabled']
require_two_factor_authentication = module.params['require_two_factor_authentication']
service_access_tokens_expiration_enforced = module.params['service_access_tokens_expiration_enforced']
share_with_group_lock = module.params['share_with_group_lock']
state = module.params['state']
subgroup_creation_level = module.params['subgroup_creation_level']
two_factor_grace_period = module.params['two_factor_grace_period']
wiki_access_level = module.params['wiki_access_level']

# Define default group_path based on group_name
if group_path is None:
Expand All @@ -380,7 +489,7 @@ def main():
if parent_identifier:
parent_group = find_group(gitlab_instance, parent_identifier)
if not parent_group:
module.fail_json(msg="Failed create GitLab group: Parent group doesn't exists")
module.fail_json(msg="Failed to create GitLab group: Parent group doesn't exist")

group_exists = gitlab_group.exists_group(parent_group.full_path + '/' + group_path)
else:
Expand All @@ -391,18 +500,31 @@ def main():
gitlab_group.delete_group(force=force_delete)
module.exit_json(changed=True, msg="Successfully deleted group %s" % group_name)
else:
module.exit_json(changed=False, msg="Group deleted or does not exists")
module.exit_json(changed=False, msg="Group deleted or does not exist")

if state == 'present':
if gitlab_group.create_or_update_group(group_name, parent_group, {
"auto_devops_enabled": auto_devops_enabled,
"avatar_path": avatar_path,
"default_branch": default_branch,
"description": description,
"enabled_git_access_protocol": enabled_git_access_protocol,
"lfs_enabled": lfs_enabled,
"lock_duo_features_enabled": lock_duo_features_enabled,
"membership_lock": membership_lock,
"mentions_disabled": mentions_disabled,
"path": group_path,
"prevent_forking_outside_group": prevent_forking_outside_group,
"prevent_sharing_groups_outside_hierarchy": prevent_sharing_groups_outside_hierarchy,
"project_creation_level": project_creation_level,
"request_access_enabled": request_access_enabled,
"require_two_factor_authentication": require_two_factor_authentication,
"service_access_tokens_expiration_enforced": service_access_tokens_expiration_enforced,
"share_with_group_lock": share_with_group_lock,
"subgroup_creation_level": subgroup_creation_level,
"two_factor_grace_period": two_factor_grace_period,
"visibility": group_visibility,
"wiki_access_level": wiki_access_level,
}):
module.exit_json(changed=True, msg="Successfully created or updated the group %s" % group_name, group=gitlab_group.group_object._attrs)
else:
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/gitlab_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
short_description: Creates/updates/deletes GitLab Projects
description:
- When the project does not exist in GitLab, it will be created.
- When the project does exists and O(state=absent), the project will be deleted.
- When the project does exist and O(state=absent), the project will be deleted.
- When changes are made to the project, the project will be updated.
author:
- Werner Dijkerman (@dj-wasabi)
Expand Down Expand Up @@ -716,7 +716,7 @@ def main():
if group_identifier:
group = find_group(gitlab_instance, group_identifier)
if group is None:
module.fail_json(msg="Failed to create project: group %s doesn't exists" % group_identifier)
module.fail_json(msg="Failed to create project: group %s doesn't exist" % group_identifier)

namespace_id = group.id
else:
Expand Down

0 comments on commit 200ab04

Please sign in to comment.