microsoft / terraform-provider-azuredevops

Terraform Azure DevOps provider
https://www.terraform.io/docs/providers/azuredevops/
MIT License
387 stars 276 forks source link

Project creation and permission of default repository #697

Closed Joerg-L closed 2 weeks ago

Joerg-L commented 1 year ago

hi there, so we want to create a terraform configuration which is able to do following things:

We have created following code snipped to do the permission job

data "azuredevops_project" "project" {
  project_id = var.project_id
}

data "azuredevops_git_repository" "default_repo" {
  project_id = var.project_id
  name       = data.azuredevops_project.project.name
}

data "azuredevops_groups" "project-groups" {
  project_id = var.project_id
}

resource "azuredevops_git_permissions" "default_permissions_default_repo" {
  for_each = {for group in data.azuredevops_groups.project-groups.groups:  group.principal_name => group}
  project_id    = var.project_id
  repository_id = data.azuredevops_git_repository.default_repo.id
  principal     = each.value.descriptor
  replace = true
  permissions = {
    CreateTag             = "Deny"
    CreateBranch          = "Deny"
    GenericContribute     = "Deny"
    PullRequestContribute = "Deny"
    ForcePush             = "Deny"
    ManagePermissions     = "Deny"
  }
}

We are recieving following error

for_each = {for group in data.azuredevops_groups.project-groups.groups: group.principal_name => group} │ ├──────────────── │ │ data.azuredevops_groups.project-groups.groups is a set of object, known only after apply │ │ The "for_each" map includes keys derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.

We have also tryied to fix that with two separate calls of terrform with

but that isn't fixing the issue. Someone an idea how to solve that topic?

Joerg-L commented 1 year ago

Okay, we have solved that now by chnage the call approach

While the child module has

module "default_permissions" {
  count  = var.run_permissions ? 1 : 0
  source = "./modules/default_permissions"
  project_id = azuredevops_project.this.id
}

in.

It's not perfect world solution, but it will work.

It would be more easier, if azuredevops_project would also deliver the groups back so that is not required to use

data "azuredevops_groups" "project-groups" {
  project_id = var.project_id
}
Joerg-L commented 1 year ago

Argh. Its not really solving the issue as

will result in dropping the permissions again

any ideas?

xuzhang3 commented 1 year ago

@Joerg-L have you try depends_on (depends_on = [data.azuredevops_groups.project-groups]). depends_on will blocked the azuredevops_git_permissions until azuredevops_groups finished.