| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019, Guillaume Martinez ([email protected]) |
| 4 |
# Copyright (c) 2015, Werner Dijkerman ([email protected]) |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: gitlab_project |
| 12 |
short_description: Creates/updates/deletes GitLab Projects |
| 13 |
description: |
| 14 |
- When the project does not exist in GitLab, it is created. |
| 15 |
- When the project does exist and O(state=absent), the project is deleted. |
| 16 |
- When changes are made to the project, the project is updated. |
| 17 |
author: |
| 18 |
- Werner Dijkerman (@dj-wasabi) |
| 19 |
- Guillaume Martinez (@Lunik) |
| 20 |
requirements: |
| 21 |
- python-gitlab python module |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._auth_basic |
| 24 |
- community.general._gitlab |
| 25 |
- community.general._attributes |
| 26 |
|
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
|
| 33 |
options: |
| 34 |
allow_merge_on_skipped_pipeline: |
| 35 |
description: |
| 36 |
- Allow merge when skipped pipelines exist. |
| 37 |
type: bool |
| 38 |
version_added: "3.4.0" |
| 39 |
avatar_path: |
| 40 |
description: |
| 41 |
- Absolute path image to configure avatar. File size should not exceed 200 kb. |
| 42 |
- This option is only used on creation, not for updates. |
| 43 |
type: path |
| 44 |
version_added: "4.2.0" |
| 45 |
build_timeout: |
| 46 |
description: |
| 47 |
- Maximum number of seconds a CI job can run. |
| 48 |
- If not specified on creation, GitLab imposes a default value. |
| 49 |
type: int |
| 50 |
version_added: "10.6.0" |
| 51 |
builds_access_level: |
| 52 |
description: |
| 53 |
- V(private) means that repository CI/CD is allowed only to project members. |
| 54 |
- V(disabled) means that repository CI/CD is disabled. |
| 55 |
- V(enabled) means that repository CI/CD is enabled. |
| 56 |
type: str |
| 57 |
choices: ["private", "disabled", "enabled"] |
| 58 |
version_added: "6.2.0" |
| 59 |
ci_config_path: |
| 60 |
description: |
| 61 |
- Custom path to the CI configuration file for this project. |
| 62 |
type: str |
| 63 |
version_added: "3.7.0" |
| 64 |
container_expiration_policy: |
| 65 |
description: |
| 66 |
- Project cleanup policy for its container registry. |
| 67 |
type: dict |
| 68 |
suboptions: |
| 69 |
cadence: |
| 70 |
description: |
| 71 |
- How often cleanup should be run. |
| 72 |
type: str |
| 73 |
choices: ["1d", "7d", "14d", "1month", "3month"] |
| 74 |
enabled: |
| 75 |
description: |
| 76 |
- Enable the cleanup policy. |
| 77 |
type: bool |
| 78 |
keep_n: |
| 79 |
description: |
| 80 |
- Number of tags kept per image name. |
| 81 |
- V(0) clears the field. |
| 82 |
type: int |
| 83 |
choices: [0, 1, 5, 10, 25, 50, 100] |
| 84 |
older_than: |
| 85 |
description: |
| 86 |
- Destroy tags older than this. |
| 87 |
- V(0d) clears the field. |
| 88 |
type: str |
| 89 |
choices: ["0d", "7d", "14d", "30d", "90d"] |
| 90 |
name_regex: |
| 91 |
description: |
| 92 |
- Destroy tags matching this regular expression. |
| 93 |
type: str |
| 94 |
name_regex_keep: |
| 95 |
description: |
| 96 |
- Keep tags matching this regular expression. |
| 97 |
type: str |
| 98 |
version_added: "9.3.0" |
| 99 |
container_registry_access_level: |
| 100 |
description: |
| 101 |
- V(private) means that container registry is allowed only to project members. |
| 102 |
- V(disabled) means that container registry is disabled. |
| 103 |
- V(enabled) means that container registry is enabled. |
| 104 |
type: str |
| 105 |
choices: ["private", "disabled", "enabled"] |
| 106 |
version_added: "6.2.0" |
| 107 |
default_branch: |
| 108 |
description: |
| 109 |
- The default branch name for this project. |
| 110 |
- For project creation, this option requires O(initialize_with_readme=true). |
| 111 |
- For project update, the branch must exist. |
| 112 |
- Supports project's default branch update since community.general 8.0.0. |
| 113 |
type: str |
| 114 |
version_added: "4.2.0" |
| 115 |
description: |
| 116 |
description: |
| 117 |
- An description for the project. |
| 118 |
type: str |
| 119 |
environments_access_level: |
| 120 |
description: |
| 121 |
- V(private) means that deployment to environment is allowed only to project members. |
| 122 |
- V(disabled) means that deployment to environment is disabled. |
| 123 |
- V(enabled) means that deployment to environment is enabled. |
| 124 |
type: str |
| 125 |
choices: ["private", "disabled", "enabled"] |
| 126 |
version_added: "6.4.0" |
| 127 |
feature_flags_access_level: |
| 128 |
description: |
| 129 |
- V(private) means that feature rollout is allowed only to project members. |
| 130 |
- V(disabled) means that feature rollout is disabled. |
| 131 |
- V(enabled) means that feature rollout is enabled. |
| 132 |
type: str |
| 133 |
choices: ["private", "disabled", "enabled"] |
| 134 |
version_added: "6.4.0" |
| 135 |
forking_access_level: |
| 136 |
description: |
| 137 |
- V(private) means that repository forks is allowed only to project members. |
| 138 |
- V(disabled) means that repository forks are disabled. |
| 139 |
- V(enabled) means that repository forks are enabled. |
| 140 |
type: str |
| 141 |
choices: ["private", "disabled", "enabled"] |
| 142 |
version_added: "6.2.0" |
| 143 |
group: |
| 144 |
description: |
| 145 |
- ID or the full path of the group of which this projects belongs to. |
| 146 |
type: str |
| 147 |
import_url: |
| 148 |
description: |
| 149 |
- Git repository which is imported into gitlab. |
| 150 |
- GitLab server needs read access to this git repository. |
| 151 |
type: str |
| 152 |
infrastructure_access_level: |
| 153 |
description: |
| 154 |
- V(private) means that configuring infrastructure is allowed only to project members. |
| 155 |
- V(disabled) means that configuring infrastructure is disabled. |
| 156 |
- V(enabled) means that configuring infrastructure is enabled. |
| 157 |
type: str |
| 158 |
choices: ["private", "disabled", "enabled"] |
| 159 |
version_added: "6.4.0" |
| 160 |
initialize_with_readme: |
| 161 |
description: |
| 162 |
- Initializes the project with a default C(README.md). |
| 163 |
- Is only used when the project is created, and ignored otherwise. |
| 164 |
type: bool |
| 165 |
default: false |
| 166 |
version_added: "4.0.0" |
| 167 |
issues_access_level: |
| 168 |
description: |
| 169 |
- V(private) means that accessing issues tab is allowed only to project members. |
| 170 |
- V(disabled) means that accessing issues tab is disabled. |
| 171 |
- V(enabled) means that accessing issues tab is enabled. |
| 172 |
- O(issues_access_level) and O(issues_enabled) are mutually exclusive. |
| 173 |
type: str |
| 174 |
choices: ["private", "disabled", "enabled"] |
| 175 |
version_added: "9.4.0" |
| 176 |
issues_enabled: |
| 177 |
description: |
| 178 |
- Whether you want to create issues or not. |
| 179 |
- O(issues_access_level) and O(issues_enabled) are mutually exclusive. |
| 180 |
type: bool |
| 181 |
default: true |
| 182 |
lfs_enabled: |
| 183 |
description: |
| 184 |
- Enable Git large file systems to manages large files such as audio, video, and graphics files. |
| 185 |
type: bool |
| 186 |
default: false |
| 187 |
version_added: "2.0.0" |
| 188 |
merge_method: |
| 189 |
description: |
| 190 |
- What requirements are placed upon merges. |
| 191 |
- Possible values are V(merge), V(rebase_merge) merge commit with semi-linear history, V(ff) fast-forward merges only. |
| 192 |
type: str |
| 193 |
choices: ["ff", "merge", "rebase_merge"] |
| 194 |
default: merge |
| 195 |
version_added: "1.0.0" |
| 196 |
merge_requests_enabled: |
| 197 |
description: |
| 198 |
- If merge requests can be made or not. |
| 199 |
type: bool |
| 200 |
default: true |
| 201 |
model_registry_access_level: |
| 202 |
description: |
| 203 |
- V(private) means that accessing model registry tab is allowed only to project members. |
| 204 |
- V(disabled) means that accessing model registry tab is disabled. |
| 205 |
- V(enabled) means that accessing model registry tab is enabled. |
| 206 |
type: str |
| 207 |
choices: ["private", "disabled", "enabled"] |
| 208 |
version_added: "9.3.0" |
| 209 |
monitor_access_level: |
| 210 |
description: |
| 211 |
- V(private) means that monitoring health is allowed only to project members. |
| 212 |
- V(disabled) means that monitoring health is disabled. |
| 213 |
- V(enabled) means that monitoring health is enabled. |
| 214 |
type: str |
| 215 |
choices: ["private", "disabled", "enabled"] |
| 216 |
version_added: "6.4.0" |
| 217 |
name: |
| 218 |
description: |
| 219 |
- The name of the project. |
| 220 |
required: true |
| 221 |
type: str |
| 222 |
only_allow_merge_if_all_discussions_are_resolved: |
| 223 |
description: |
| 224 |
- All discussions on a merge request (MR) have to be resolved. |
| 225 |
type: bool |
| 226 |
version_added: "3.4.0" |
| 227 |
only_allow_merge_if_pipeline_succeeds: |
| 228 |
description: |
| 229 |
- Only allow merges if pipeline succeeded. |
| 230 |
type: bool |
| 231 |
version_added: "3.4.0" |
| 232 |
packages_enabled: |
| 233 |
description: |
| 234 |
- Enable GitLab package repository. |
| 235 |
type: bool |
| 236 |
version_added: "3.4.0" |
| 237 |
pages_access_level: |
| 238 |
description: |
| 239 |
- V(private) means that accessing pages tab is allowed only to project members. |
| 240 |
- V(disabled) means that accessing pages tab is disabled. |
| 241 |
- V(enabled) means that accessing pages tab is enabled. |
| 242 |
type: str |
| 243 |
choices: ["private", "disabled", "enabled"] |
| 244 |
version_added: "9.3.0" |
| 245 |
path: |
| 246 |
description: |
| 247 |
- The path of the project you want to create, this is server_url/O(group)/O(path). |
| 248 |
- If not supplied, O(name) is used. |
| 249 |
type: str |
| 250 |
releases_access_level: |
| 251 |
description: |
| 252 |
- V(private) means that accessing release is allowed only to project members. |
| 253 |
- V(disabled) means that accessing release is disabled. |
| 254 |
- V(enabled) means that accessing release is enabled. |
| 255 |
type: str |
| 256 |
choices: ["private", "disabled", "enabled"] |
| 257 |
version_added: "6.4.0" |
| 258 |
remove_source_branch_after_merge: |
| 259 |
description: |
| 260 |
- Remove the source branch after merge. |
| 261 |
type: bool |
| 262 |
version_added: "3.4.0" |
| 263 |
repository_access_level: |
| 264 |
description: |
| 265 |
- V(private) means that accessing repository is allowed only to project members. |
| 266 |
- V(disabled) means that accessing repository is disabled. |
| 267 |
- V(enabled) means that accessing repository is enabled. |
| 268 |
type: str |
| 269 |
choices: ["private", "disabled", "enabled"] |
| 270 |
version_added: "9.3.0" |
| 271 |
security_and_compliance_access_level: |
| 272 |
description: |
| 273 |
- V(private) means that accessing security and complicance tab is allowed only to project members. |
| 274 |
- V(disabled) means that accessing security and complicance tab is disabled. |
| 275 |
- V(enabled) means that accessing security and complicance tab is enabled. |
| 276 |
type: str |
| 277 |
choices: ["private", "disabled", "enabled"] |
| 278 |
version_added: "6.4.0" |
| 279 |
service_desk_enabled: |
| 280 |
description: |
| 281 |
- Enable Service Desk. |
| 282 |
type: bool |
| 283 |
version_added: "9.3.0" |
| 284 |
shared_runners_enabled: |
| 285 |
description: |
| 286 |
- Enable shared runners for this project. |
| 287 |
type: bool |
| 288 |
version_added: "3.7.0" |
| 289 |
snippets_enabled: |
| 290 |
description: |
| 291 |
- If creating snippets should be available or not. |
| 292 |
type: bool |
| 293 |
default: true |
| 294 |
squash_option: |
| 295 |
description: |
| 296 |
- Squash commits when merging. |
| 297 |
type: str |
| 298 |
choices: ["never", "always", "default_off", "default_on"] |
| 299 |
version_added: "3.4.0" |
| 300 |
state: |
| 301 |
description: |
| 302 |
- Create or delete project. |
| 303 |
- Possible values are present and absent. |
| 304 |
default: present |
| 305 |
type: str |
| 306 |
choices: ["present", "absent"] |
| 307 |
topics: |
| 308 |
description: |
| 309 |
- A topic or list of topics to be assigned to a project. |
| 310 |
- It is compatible with old GitLab server releases (versions before 14, correspond to C(tag_list)). |
| 311 |
type: list |
| 312 |
elements: str |
| 313 |
version_added: "6.6.0" |
| 314 |
username: |
| 315 |
description: |
| 316 |
- Used to create a personal project under a user's name. |
| 317 |
type: str |
| 318 |
version_added: "3.3.0" |
| 319 |
visibility: |
| 320 |
description: |
| 321 |
- V(private) Project access must be granted explicitly for each user. |
| 322 |
- V(internal) The project can be cloned by any logged in user. |
| 323 |
- V(public) The project can be cloned without any authentication. |
| 324 |
default: private |
| 325 |
type: str |
| 326 |
choices: ["private", "internal", "public"] |
| 327 |
aliases: |
| 328 |
- visibility_level |
| 329 |
wiki_enabled: |
| 330 |
description: |
| 331 |
- If an wiki for this project should be available or not. |
| 332 |
type: bool |
| 333 |
default: true |
| 334 |
""" |
| 335 |
|
| 336 |
EXAMPLES = r""" |
| 337 |
- name: Create GitLab Project |
| 338 |
community.general.gitlab_project: |
| 339 |
api_url: https://gitlab.example.com/ |
| 340 |
api_token: "{{ api_token }}" |
| 341 |
name: my_first_project |
| 342 |
group: "10481470" |
| 343 |
|
| 344 |
- name: Delete GitLab Project |
| 345 |
community.general.gitlab_project: |
| 346 |
api_url: https://gitlab.example.com/ |
| 347 |
api_token: "{{ access_token }}" |
| 348 |
name: my_first_project |
| 349 |
state: absent |
| 350 |
delegate_to: localhost |
| 351 |
|
| 352 |
- name: Create GitLab Project in group Ansible |
| 353 |
community.general.gitlab_project: |
| 354 |
api_url: https://gitlab.example.com/ |
| 355 |
validate_certs: true |
| 356 |
api_username: dj-wasabi |
| 357 |
api_password: "MySecretPassword" |
| 358 |
name: my_first_project |
| 359 |
group: ansible |
| 360 |
issues_enabled: false |
| 361 |
merge_method: rebase_merge |
| 362 |
wiki_enabled: true |
| 363 |
snippets_enabled: true |
| 364 |
import_url: http://git.example.com/example/lab.git |
| 365 |
initialize_with_readme: true |
| 366 |
state: present |
| 367 |
delegate_to: localhost |
| 368 |
|
| 369 |
- name: get the initial root password |
| 370 |
ansible.builtin.shell: | |
| 371 |
grep 'Password:' /etc/gitlab/initial_root_password | sed -e 's/Password\: \(.*\)/\1/' |
| 372 |
register: initial_root_password |
| 373 |
|
| 374 |
- name: Create a GitLab Project using a username/password via oauth_token |
| 375 |
community.general.gitlab_project: |
| 376 |
api_url: https://gitlab.example.com/ |
| 377 |
api_username: root |
| 378 |
api_password: "{{ initial_root_password }}" |
| 379 |
name: my_second_project |
| 380 |
group: "10481470" |
| 381 |
""" |
| 382 |
|
| 383 |
RETURN = r""" |
| 384 |
msg: |
| 385 |
description: Success or failure message. |
| 386 |
returned: always |
| 387 |
type: str |
| 388 |
sample: "Success" |
| 389 |
|
| 390 |
result: |
| 391 |
description: JSON-parsed response from the server. |
| 392 |
returned: always |
| 393 |
type: dict |
| 394 |
|
| 395 |
error: |
| 396 |
description: The error message returned by the GitLab API. |
| 397 |
returned: failed |
| 398 |
type: str |
| 399 |
sample: "400: path is already in use" |
| 400 |
|
| 401 |
project: |
| 402 |
description: API object. |
| 403 |
returned: always |
| 404 |
type: dict |
| 405 |
""" |
| 406 |
|
| 407 |
|
| 408 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 409 |
from ansible.module_utils.basic import AnsibleModule |
| 410 |
|
| 411 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 412 |
auth_argument_spec, |
| 413 |
find_group, |
| 414 |
find_project, |
| 415 |
gitlab, |
| 416 |
gitlab_authentication, |
| 417 |
) |
| 418 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 419 |
|
| 420 |
|
| 421 |
class GitLabProject: |
| 422 |
def __init__(self, module, gitlab_instance): |
| 423 |
self._module = module |
| 424 |
self._gitlab = gitlab_instance |
| 425 |
self.project_object = None |
| 426 |
|
| 427 |
""" |
| 428 |
@param project_name Name of the project |
| 429 |
@param namespace Namespace Object (User or Group) |
| 430 |
@param options Options of the project |
| 431 |
""" |
| 432 |
|
| 433 |
def create_or_update_project(self, module, project_name, namespace, options): |
| 434 |
changed = False |
| 435 |
project_options = { |
| 436 |
"allow_merge_on_skipped_pipeline": options["allow_merge_on_skipped_pipeline"], |
| 437 |
"builds_access_level": options["builds_access_level"], |
| 438 |
"build_timeout": options["build_timeout"], |
| 439 |
"ci_config_path": options["ci_config_path"], |
| 440 |
"container_expiration_policy": options["container_expiration_policy"], |
| 441 |
"container_registry_access_level": options["container_registry_access_level"], |
| 442 |
"description": options["description"], |
| 443 |
"environments_access_level": options["environments_access_level"], |
| 444 |
"feature_flags_access_level": options["feature_flags_access_level"], |
| 445 |
"forking_access_level": options["forking_access_level"], |
| 446 |
"infrastructure_access_level": options["infrastructure_access_level"], |
| 447 |
"issues_access_level": options["issues_access_level"], |
| 448 |
"issues_enabled": options["issues_enabled"], |
| 449 |
"lfs_enabled": options["lfs_enabled"], |
| 450 |
"merge_method": options["merge_method"], |
| 451 |
"merge_requests_enabled": options["merge_requests_enabled"], |
| 452 |
"model_registry_access_level": options["model_registry_access_level"], |
| 453 |
"monitor_access_level": options["monitor_access_level"], |
| 454 |
"name": project_name, |
| 455 |
"only_allow_merge_if_all_discussions_are_resolved": options[ |
| 456 |
"only_allow_merge_if_all_discussions_are_resolved" |
| 457 |
], |
| 458 |
"only_allow_merge_if_pipeline_succeeds": options["only_allow_merge_if_pipeline_succeeds"], |
| 459 |
"packages_enabled": options["packages_enabled"], |
| 460 |
"pages_access_level": options["pages_access_level"], |
| 461 |
"releases_access_level": options["releases_access_level"], |
| 462 |
"remove_source_branch_after_merge": options["remove_source_branch_after_merge"], |
| 463 |
"repository_access_level": options["repository_access_level"], |
| 464 |
"security_and_compliance_access_level": options["security_and_compliance_access_level"], |
| 465 |
"service_desk_enabled": options["service_desk_enabled"], |
| 466 |
"shared_runners_enabled": options["shared_runners_enabled"], |
| 467 |
"snippets_enabled": options["snippets_enabled"], |
| 468 |
"squash_option": options["squash_option"], |
| 469 |
"visibility": options["visibility"], |
| 470 |
"wiki_enabled": options["wiki_enabled"], |
| 471 |
} |
| 472 |
|
| 473 |
# topics was introduced on gitlab >=14 and replace tag_list. We get current gitlab version |
| 474 |
# and check if less than 14. If yes we use tag_list instead topics |
| 475 |
if LooseVersion(self._gitlab.version()[0]) < LooseVersion("14"): |
| 476 |
project_options["tag_list"] = options["topics"] |
| 477 |
else: |
| 478 |
project_options["topics"] = options["topics"] |
| 479 |
|
| 480 |
# Because we have already call userExists in main() |
| 481 |
if self.project_object is None: |
| 482 |
if options["default_branch"] and not options["initialize_with_readme"]: |
| 483 |
module.fail_json(msg="Param default_branch needs param initialize_with_readme set to true") |
| 484 |
project_options.update( |
| 485 |
{ |
| 486 |
"path": options["path"], |
| 487 |
"import_url": options["import_url"], |
| 488 |
} |
| 489 |
) |
| 490 |
if options["initialize_with_readme"]: |
| 491 |
project_options["initialize_with_readme"] = options["initialize_with_readme"] |
| 492 |
if options["default_branch"]: |
| 493 |
project_options["default_branch"] = options["default_branch"] |
| 494 |
|
| 495 |
project_options = self.get_options_with_value(project_options) |
| 496 |
project = self.create_project(namespace, project_options) |
| 497 |
|
| 498 |
# add avatar to project |
| 499 |
if options["avatar_path"]: |
| 500 |
try: |
| 501 |
project.avatar = open(options["avatar_path"], "rb") |
| 502 |
except OSError as e: |
| 503 |
self._module.fail_json(msg=f"Cannot open {options['avatar_path']}: {e}") |
| 504 |
|
| 505 |
changed = True |
| 506 |
else: |
| 507 |
if options["default_branch"]: |
| 508 |
project_options["default_branch"] = options["default_branch"] |
| 509 |
changed, project = self.update_project(self.project_object, project_options) |
| 510 |
|
| 511 |
self.project_object = project |
| 512 |
if changed: |
| 513 |
if self._module.check_mode: |
| 514 |
self._module.exit_json(changed=True, msg=f"Successfully created or updated the project {project_name}") |
| 515 |
|
| 516 |
try: |
| 517 |
project.save() |
| 518 |
except Exception as e: |
| 519 |
self._module.fail_json(msg=f"Failed to update project: {e} ") |
| 520 |
return True |
| 521 |
return False |
| 522 |
|
| 523 |
""" |
| 524 |
@param namespace Namespace Object (User or Group) |
| 525 |
@param arguments Attributes of the project |
| 526 |
""" |
| 527 |
|
| 528 |
def create_project(self, namespace, arguments): |
| 529 |
if self._module.check_mode: |
| 530 |
return True |
| 531 |
|
| 532 |
arguments["namespace_id"] = namespace.id |
| 533 |
if "container_expiration_policy" in arguments: |
| 534 |
arguments["container_expiration_policy_attributes"] = arguments["container_expiration_policy"] |
| 535 |
try: |
| 536 |
project = self._gitlab.projects.create(arguments) |
| 537 |
except gitlab.exceptions.GitlabCreateError as e: |
| 538 |
self._module.fail_json(msg=f"Failed to create project: {e} ") |
| 539 |
|
| 540 |
return project |
| 541 |
|
| 542 |
""" |
| 543 |
@param arguments Attributes of the project |
| 544 |
""" |
| 545 |
|
| 546 |
def get_options_with_value(self, arguments): |
| 547 |
ret_arguments = {k: v for k, v in arguments.items() if v is not None} |
| 548 |
return ret_arguments |
| 549 |
|
| 550 |
""" |
| 551 |
@param project Project Object |
| 552 |
@param arguments Attributes of the project |
| 553 |
""" |
| 554 |
|
| 555 |
def update_project(self, project, arguments): |
| 556 |
changed = False |
| 557 |
|
| 558 |
for arg_key, arg_value in arguments.items(): |
| 559 |
if arg_value is not None: |
| 560 |
if getattr(project, arg_key, None) != arg_value: |
| 561 |
if arg_key == "container_expiration_policy": |
| 562 |
old_val = getattr(project, arg_key, {}) |
| 563 |
final_val = {key: value for key, value in arg_value.items() if value is not None} |
| 564 |
|
| 565 |
if final_val.get("older_than") == "0d": |
| 566 |
final_val["older_than"] = None |
| 567 |
if final_val.get("keep_n") == 0: |
| 568 |
final_val["keep_n"] = None |
| 569 |
|
| 570 |
if all(old_val.get(key) == value for key, value in final_val.items()): |
| 571 |
continue |
| 572 |
project.container_expiration_policy_attributes = final_val |
| 573 |
else: |
| 574 |
setattr(project, arg_key, arg_value) |
| 575 |
changed = True |
| 576 |
|
| 577 |
return (changed, project) |
| 578 |
|
| 579 |
def delete_project(self): |
| 580 |
if self._module.check_mode: |
| 581 |
return True |
| 582 |
|
| 583 |
project = self.project_object |
| 584 |
|
| 585 |
return project.delete() |
| 586 |
|
| 587 |
""" |
| 588 |
@param namespace User/Group object |
| 589 |
@param name Name of the project |
| 590 |
""" |
| 591 |
|
| 592 |
def exists_project(self, namespace, path): |
| 593 |
# When project exists, object will be stored in self.project_object. |
| 594 |
project = find_project(self._gitlab, f"{namespace.full_path}/{path}") |
| 595 |
if project: |
| 596 |
self.project_object = project |
| 597 |
return True |
| 598 |
return False |
| 599 |
|
| 600 |
|
| 601 |
def main(): |
| 602 |
argument_spec = basic_auth_argument_spec() |
| 603 |
argument_spec.update(auth_argument_spec()) |
| 604 |
argument_spec.update( |
| 605 |
dict( |
| 606 |
allow_merge_on_skipped_pipeline=dict(type="bool"), |
| 607 |
avatar_path=dict(type="path"), |
| 608 |
builds_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 609 |
build_timeout=dict(type="int"), |
| 610 |
ci_config_path=dict(type="str"), |
| 611 |
container_expiration_policy=dict( |
| 612 |
type="dict", |
| 613 |
options=dict( |
| 614 |
cadence=dict(type="str", choices=["1d", "7d", "14d", "1month", "3month"]), |
| 615 |
enabled=dict(type="bool"), |
| 616 |
keep_n=dict(type="int", choices=[0, 1, 5, 10, 25, 50, 100]), |
| 617 |
older_than=dict(type="str", choices=["0d", "7d", "14d", "30d", "90d"]), |
| 618 |
name_regex=dict(type="str"), |
| 619 |
name_regex_keep=dict(type="str"), |
| 620 |
), |
| 621 |
), |
| 622 |
container_registry_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 623 |
default_branch=dict(type="str"), |
| 624 |
description=dict(type="str"), |
| 625 |
environments_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 626 |
feature_flags_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 627 |
forking_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 628 |
group=dict(type="str"), |
| 629 |
import_url=dict(type="str"), |
| 630 |
infrastructure_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 631 |
initialize_with_readme=dict(type="bool", default=False), |
| 632 |
issues_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 633 |
issues_enabled=dict(type="bool", default=True), |
| 634 |
lfs_enabled=dict(default=False, type="bool"), |
| 635 |
merge_method=dict(type="str", default="merge", choices=["merge", "rebase_merge", "ff"]), |
| 636 |
merge_requests_enabled=dict(type="bool", default=True), |
| 637 |
model_registry_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 638 |
monitor_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 639 |
name=dict(type="str", required=True), |
| 640 |
only_allow_merge_if_all_discussions_are_resolved=dict(type="bool"), |
| 641 |
only_allow_merge_if_pipeline_succeeds=dict(type="bool"), |
| 642 |
packages_enabled=dict(type="bool"), |
| 643 |
pages_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 644 |
path=dict(type="str"), |
| 645 |
releases_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 646 |
remove_source_branch_after_merge=dict(type="bool"), |
| 647 |
repository_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 648 |
security_and_compliance_access_level=dict(type="str", choices=["private", "disabled", "enabled"]), |
| 649 |
service_desk_enabled=dict(type="bool"), |
| 650 |
shared_runners_enabled=dict(type="bool"), |
| 651 |
snippets_enabled=dict(default=True, type="bool"), |
| 652 |
squash_option=dict(type="str", choices=["never", "always", "default_off", "default_on"]), |
| 653 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 654 |
topics=dict(type="list", elements="str"), |
| 655 |
username=dict(type="str"), |
| 656 |
visibility=dict( |
| 657 |
type="str", default="private", choices=["internal", "private", "public"], aliases=["visibility_level"] |
| 658 |
), |
| 659 |
wiki_enabled=dict(type="bool", default=True), |
| 660 |
) |
| 661 |
) |
| 662 |
|
| 663 |
module = AnsibleModule( |
| 664 |
argument_spec=argument_spec, |
| 665 |
mutually_exclusive=[ |
| 666 |
["api_username", "api_token"], |
| 667 |
["api_username", "api_oauth_token"], |
| 668 |
["api_username", "api_job_token"], |
| 669 |
["api_token", "api_oauth_token"], |
| 670 |
["api_token", "api_job_token"], |
| 671 |
["group", "username"], |
| 672 |
["issues_access_level", "issues_enabled"], |
| 673 |
], |
| 674 |
required_together=[ |
| 675 |
["api_username", "api_password"], |
| 676 |
], |
| 677 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 678 |
supports_check_mode=True, |
| 679 |
) |
| 680 |
|
| 681 |
# check prerequisites and connect to gitlab server |
| 682 |
gitlab_instance = gitlab_authentication(module) |
| 683 |
|
| 684 |
allow_merge_on_skipped_pipeline = module.params["allow_merge_on_skipped_pipeline"] |
| 685 |
avatar_path = module.params["avatar_path"] |
| 686 |
builds_access_level = module.params["builds_access_level"] |
| 687 |
build_timeout = module.params["build_timeout"] |
| 688 |
ci_config_path = module.params["ci_config_path"] |
| 689 |
container_expiration_policy = module.params["container_expiration_policy"] |
| 690 |
container_registry_access_level = module.params["container_registry_access_level"] |
| 691 |
default_branch = module.params["default_branch"] |
| 692 |
environments_access_level = module.params["environments_access_level"] |
| 693 |
feature_flags_access_level = module.params["feature_flags_access_level"] |
| 694 |
forking_access_level = module.params["forking_access_level"] |
| 695 |
group_identifier = module.params["group"] |
| 696 |
import_url = module.params["import_url"] |
| 697 |
infrastructure_access_level = module.params["infrastructure_access_level"] |
| 698 |
initialize_with_readme = module.params["initialize_with_readme"] |
| 699 |
issues_access_level = module.params["issues_access_level"] |
| 700 |
issues_enabled = module.params["issues_enabled"] |
| 701 |
lfs_enabled = module.params["lfs_enabled"] |
| 702 |
merge_method = module.params["merge_method"] |
| 703 |
merge_requests_enabled = module.params["merge_requests_enabled"] |
| 704 |
model_registry_access_level = module.params["model_registry_access_level"] |
| 705 |
monitor_access_level = module.params["monitor_access_level"] |
| 706 |
only_allow_merge_if_all_discussions_are_resolved = module.params["only_allow_merge_if_all_discussions_are_resolved"] |
| 707 |
only_allow_merge_if_pipeline_succeeds = module.params["only_allow_merge_if_pipeline_succeeds"] |
| 708 |
packages_enabled = module.params["packages_enabled"] |
| 709 |
pages_access_level = module.params["pages_access_level"] |
| 710 |
project_description = module.params["description"] |
| 711 |
project_name = module.params["name"] |
| 712 |
project_path = module.params["path"] |
| 713 |
releases_access_level = module.params["releases_access_level"] |
| 714 |
remove_source_branch_after_merge = module.params["remove_source_branch_after_merge"] |
| 715 |
repository_access_level = module.params["repository_access_level"] |
| 716 |
security_and_compliance_access_level = module.params["security_and_compliance_access_level"] |
| 717 |
service_desk_enabled = module.params["service_desk_enabled"] |
| 718 |
shared_runners_enabled = module.params["shared_runners_enabled"] |
| 719 |
snippets_enabled = module.params["snippets_enabled"] |
| 720 |
squash_option = module.params["squash_option"] |
| 721 |
state = module.params["state"] |
| 722 |
topics = module.params["topics"] |
| 723 |
username = module.params["username"] |
| 724 |
visibility = module.params["visibility"] |
| 725 |
wiki_enabled = module.params["wiki_enabled"] |
| 726 |
|
| 727 |
# Set project_path to project_name if it is empty. |
| 728 |
if project_path is None: |
| 729 |
project_path = project_name.replace(" ", "_") |
| 730 |
|
| 731 |
gitlab_project = GitLabProject(module, gitlab_instance) |
| 732 |
|
| 733 |
namespace = None |
| 734 |
namespace_id = None |
| 735 |
if group_identifier: |
| 736 |
group = find_group(gitlab_instance, group_identifier) |
| 737 |
if group is None: |
| 738 |
module.fail_json(msg=f"Failed to create project: group {group_identifier} doesn't exist") |
| 739 |
|
| 740 |
namespace_id = group.id |
| 741 |
else: |
| 742 |
if username: |
| 743 |
namespace = gitlab_instance.namespaces.list(search=username, all=False)[0] |
| 744 |
else: |
| 745 |
namespace = gitlab_instance.namespaces.list(search=gitlab_instance.user.username, all=False)[0] |
| 746 |
namespace_id = namespace.id |
| 747 |
|
| 748 |
if not namespace_id: |
| 749 |
module.fail_json(msg="Failed to find the namespace or group ID which is required to look up the namespace") |
| 750 |
|
| 751 |
try: |
| 752 |
namespace = gitlab_instance.namespaces.get(namespace_id) |
| 753 |
except gitlab.exceptions.GitlabGetError as e: |
| 754 |
module.fail_json(msg=f"Failed to find the namespace for the given user: {e}") |
| 755 |
|
| 756 |
if not namespace: |
| 757 |
module.fail_json(msg="Failed to find the namespace for the project") |
| 758 |
project_exists = gitlab_project.exists_project(namespace, project_path) |
| 759 |
|
| 760 |
if state == "absent": |
| 761 |
if project_exists: |
| 762 |
gitlab_project.delete_project() |
| 763 |
module.exit_json(changed=True, msg=f"Successfully deleted project {project_name}") |
| 764 |
module.exit_json(changed=False, msg="Project deleted or does not exist") |
| 765 |
|
| 766 |
if state == "present": |
| 767 |
if gitlab_project.create_or_update_project( |
| 768 |
module, |
| 769 |
project_name, |
| 770 |
namespace, |
| 771 |
{ |
| 772 |
"allow_merge_on_skipped_pipeline": allow_merge_on_skipped_pipeline, |
| 773 |
"avatar_path": avatar_path, |
| 774 |
"builds_access_level": builds_access_level, |
| 775 |
"build_timeout": build_timeout, |
| 776 |
"ci_config_path": ci_config_path, |
| 777 |
"container_expiration_policy": container_expiration_policy, |
| 778 |
"container_registry_access_level": container_registry_access_level, |
| 779 |
"default_branch": default_branch, |
| 780 |
"description": project_description, |
| 781 |
"environments_access_level": environments_access_level, |
| 782 |
"feature_flags_access_level": feature_flags_access_level, |
| 783 |
"forking_access_level": forking_access_level, |
| 784 |
"import_url": import_url, |
| 785 |
"infrastructure_access_level": infrastructure_access_level, |
| 786 |
"initialize_with_readme": initialize_with_readme, |
| 787 |
"issues_access_level": issues_access_level, |
| 788 |
"issues_enabled": issues_enabled, |
| 789 |
"lfs_enabled": lfs_enabled, |
| 790 |
"merge_method": merge_method, |
| 791 |
"merge_requests_enabled": merge_requests_enabled, |
| 792 |
"model_registry_access_level": model_registry_access_level, |
| 793 |
"monitor_access_level": monitor_access_level, |
| 794 |
"only_allow_merge_if_all_discussions_are_resolved": only_allow_merge_if_all_discussions_are_resolved, |
| 795 |
"only_allow_merge_if_pipeline_succeeds": only_allow_merge_if_pipeline_succeeds, |
| 796 |
"packages_enabled": packages_enabled, |
| 797 |
"pages_access_level": pages_access_level, |
| 798 |
"path": project_path, |
| 799 |
"releases_access_level": releases_access_level, |
| 800 |
"remove_source_branch_after_merge": remove_source_branch_after_merge, |
| 801 |
"repository_access_level": repository_access_level, |
| 802 |
"security_and_compliance_access_level": security_and_compliance_access_level, |
| 803 |
"service_desk_enabled": service_desk_enabled, |
| 804 |
"shared_runners_enabled": shared_runners_enabled, |
| 805 |
"snippets_enabled": snippets_enabled, |
| 806 |
"squash_option": squash_option, |
| 807 |
"topics": topics, |
| 808 |
"visibility": visibility, |
| 809 |
"wiki_enabled": wiki_enabled, |
| 810 |
}, |
| 811 |
): |
| 812 |
module.exit_json( |
| 813 |
changed=True, |
| 814 |
msg=f"Successfully created or updated the project {project_name}", |
| 815 |
project=gitlab_project.project_object._attrs, |
| 816 |
) |
| 817 |
module.exit_json( |
| 818 |
changed=False, |
| 819 |
msg=f"No need to update the project {project_name}", |
| 820 |
project=gitlab_project.project_object._attrs, |
| 821 |
) |
| 822 |
|
| 823 |
|
| 824 |
if __name__ == "__main__": |
| 825 |
main() |