Unable to import existing resources to gitlab_branch resource

Created by: genei09

GitLab Provider version

3.16.1

GitLab version

15.3.0 SaaS

Terraform version

Terraform v0.14.6

Relevant Terraform Configuration

resource "gitlab_branch" "production" {
  for_each = gitlab_project.this
  project  = each.value.id
  name     = "production"
  ref      = "main"

  lifecycle {
    prevent_destroy = true
  }
}

Relevant log output

- '  # gitlab_branch.production["gitlab-project-settings-test"] must be replaced'
    - -/+ resource "gitlab_branch" "production" {
    - '      ~ can_push            = true -> (known after apply)'
    - '      ~ commit              = ['
    - '          - {'
    - '              - author_email    = "redacted"'
    - '              - author_name     = "Aaron Sua"'
    - '              - authored_date   = "2022-07-31T21:45:24Z"'
    - '              - committed_date  = "2022-07-31T21:45:24Z"'
    - '              - committer_email = "redacted"'
    - '              - committer_name  = "Aaron Sua"'
    - '              - id              = "redacted"'
    - '              - message         = "Add README.md"'
    - '              - parent_ids      = []'
    - '              - short_id        = "39332ccb"'
    - '              - title           = "Add README.md"'
    - '            },'
    - '        ] -> (known after apply)'
    - '      ~ default             = false -> (known after apply)'
    - '      ~ developer_can_merge = false -> (known after apply)'
    - '      ~ developer_can_push  = false -> (known after apply)'
    - '      ~ id                  = "38232732:production" -> (known after apply)'
    - '      ~ merged              = false -> (known after apply)'
    - '        name                = "production"'
    - '      ~ protected           = true -> (known after apply)'
    - '      + ref                 = "main" # forces replacement'
    - '      ~ web_url             = "https://gitlab.com/redacted/demo_repos/demos/gitlab-project-settings-test/-/tree/production" -> (known after apply)'
    - '        # (1 unchanged attribute hidden)'
    - '    }'

Description

after importing an existing branch the required parameter of "ref" causes the branch to be destroyed which conflicts with a lifecycle aiming to keep the branches protected from deletion.

it took some time to figure this out. At least documenting this case would be good, but really I think that import should be able to record ref and assume that the code is correct. If I didn't care about it being destroyed why would I run import? I'd just go delete the branch and let it be recreated.

Edited by Timo Furrer