Fix Terraform error when importing GitLab project to infra-mgmt
Description:
When importing existing GitLab projects into Terraform in infra-mgmt, the plan looks as expected but when applying it fails due to an approval rule already existing. Error:
╷
│ Error: POST https://gitlab.com/api/v4/projects/41840368/approval_rules: 400 {message: {rule_type: [any-approver for the project already exists]}}
│
│ with module.project_gitlab_dedicated_managed_repos["aws-instance-health-exporter"].gitlab_project_approval_rule.approval["CODEOWNERS"],
│ on .terraform/modules/project_gitlab_dedicated_managed_repos/approvals.tf line 2, in resource "gitlab_project_approval_rule" "approval":
│ 2: resource "gitlab_project_approval_rule" "approval" {
│
Example MRs to workaround by manually importing the rules:
- https://gitlab.com/gitlab-com/gl-infra/infra-mgmt/-/merge_requests/1096
- https://gitlab.com/gitlab-com/gl-infra/infra-mgmt/-/merge_requests/1151
This has happened at least three times, and seems to be an issue with the provider. It's become a bit of a papercut each time it happens when adding a project into infra-mgmt, so we should look into fixing it.
Provider resources:
- Provider: https://gitlab.com/gitlab-org/terraform-provider-gitlab
- Related MR: gitlab-org/terraform-provider-gitlab!1425 (merged)
if an "any_approver" rule is already present on a project at creation time, and that rule requires 0 approvers, the rule will be automatically imported to prevent a common error with this resource
One option is to set the any_approver rule to 0 approvers before importing to terraform, but that doesn't prevent us from hitting this again in the future.