Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provider doesn't handle Harbor project re-creation correctly #478

Open
anzinchenko89 opened this issue Nov 4, 2024 · 0 comments
Open

Provider doesn't handle Harbor project re-creation correctly #478

anzinchenko89 opened this issue Nov 4, 2024 · 0 comments

Comments

@anzinchenko89
Copy link

anzinchenko89 commented Nov 4, 2024

Describe the bug
When a project has been created by Terraform with a custom tag immutability rules, retention polices and after the project is removed via webUI manually, the provider crashed with the error that immutable tag rule cannot be found, instead of adding the rule alongside with the project.

To Reproduce
Deploy harbor project, retention policy and immutable tag rule

resource "harbor_project" "harbor_project" {
  for_each = {
    for project in local.harbor_projects : project.name => project
  }
  name                        = each.value.name
  public                      = each.value.public
  vulnerability_scanning      = coalesce(each.value.vulnerability_scanning, false)
  enable_content_trust        = each.value.enable_content_trust
  enable_content_trust_cosign = each.value.enable_content_trust_cosign
  auto_sbom_generation        = each.value.auto_sbom_generation
  storage_quota               = each.value.storage_quota

  registry_id = each.value.project_type == "proxy_cache" ? harbor_registry.harbor_registry_main[each.key].registry_id : null

}
resource "harbor_retention_policy" "retention_policy" {
  for_each = {
    for project in local.harbor_projects : project.name => project if length(coalesce(project.retention_rules, [])) > 0
  }
  scope    = harbor_project.harbor_project[each.key].id
  schedule = each.value.retention_policy_schedule

  dynamic "rule" {
    for_each = coalesce(each.value.retention_rules, [])

    content {
      always_retain          = rule.value.always_retain
      most_recently_pulled   = rule.value.most_recently_pulled
      most_recently_pushed   = rule.value.most_recently_pushed
      n_days_since_last_pull = rule.value.n_days_since_last_pull
      n_days_since_last_push = rule.value.n_days_since_last_push
      repo_matching          = rule.value.repo_matching
      tag_matching           = rule.value.tag_matching
      repo_excluding         = rule.value.repo_excluding
      tag_excluding          = rule.value.tag_excluding
      untagged_artifacts     = rule.value.untagged_artifacts
    }

  }
  lifecycle {
    replace_triggered_by = [harbor_project.harbor_project[each.key].id]
  }
  depends_on = [harbor_project.harbor_project]
}
resource "harbor_immutable_tag_rule" "immutable_tag_rule" {
  for_each = {
    for project in local.harbor_projects : project.name => project if project.project_type != "proxy_cache" && length(coalesce(project.immutable_tag_rules, [])) > 0
  }
  disabled       = false
  project_id     = harbor_project.harbor_project[each.key].id
  repo_excluding = each.value.immutable_tag_rules[0].repo_excluding
  tag_excluding  = each.value.immutable_tag_rules[0].tag_excluding
  repo_matching  = each.value.immutable_tag_rules[0].repo_matching
  tag_matching   = each.value.immutable_tag_rules[0].tag_matching

  lifecycle {
    replace_triggered_by = [harbor_project.harbor_project[each.key].id]
  }

  depends_on = [harbor_project.harbor_project]
}

Now delete harbor project from webUi manually, on the next run the error occurs

│ Error: [ERROR] unexpected status code got: 500 expected: 200 
│  {"errors":[{"code":"UNKNOWN","message":"internal server error"}]}
│ 
│ 
│   with harbor_retention_policy.retention_policy["development"],
│   on main.tf line 136, in resource "harbor_retention_policy" "retention_policy":
│  136: resource "harbor_retention_policy" "retention_policy" {



│ Error: Resource not found /projects/21/immutabletagrules/5
│ 
│   with harbor_immutable_tag_rule.immutable_tag_rule["development"],
│   on main.tf line 163, in resource "harbor_immutable_tag_rule" "immutable_tag_rule":
│  163: resource "harbor_immutable_tag_rule" "immutable_tag_rule" {

Expected behavior
The provider should catch the error and re-create a project with corresponding immutable tag rule and retention policy

Additional context

  • Provider Version 3.10.15
  • Terraform Version 1.9.8
  • Harbor Version (harbor-core:v2.11.1, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant