Not able to generate dynamic block

Hello Everyone,

I am trying to generate dynamic block.But facing issue.Below is resource block

resource "azuredevops_branch_policy_auto_reviewers" "azure_repo_auto_reviewer" {
  project_id = data.azuredevops_project.azure_project_id.id
  enabled  = true
  blocking = true
  settings {
    auto_reviewer_ids  = local.approverList
    submitter_can_vote = false
    message            = "Auto reviewer"
    dynamic "scope" {
     for_each = tomap(var.azureRepoLevelBranch)
      scope {
        repository_id  = azuredevops_git_repository.azure_repo_creation.id
        repository_ref = scope.key
        match_type     = scope.value
       }
     }
  }
}

So the scope part needs to created something like this.Depending how many values are present in the variable file

  settings {
    auto_reviewer_ids  = local.approverList
    submitter_can_vote = false
    message            = "Auto reviewer"
      scope {
        repository_id  = azuredevops_git_repository.azure_repo_creation.id
        repository_ref = ref/heads/master
        match_type     = prefix
  }
      scope {
        repository_id  = azuredevops_git_repository.azure_repo_creation.id
        repository_ref = ref/heads/releases
        match_type     = exact
  }
}

Below is my variable data is of map type

azureRepoLevelBranch = {
    "refs/heads/releases" = "prefix"
    "refs/heads/develop" = "exact"
}

Following is error i see

srcmnik@SRCMNIKs-MacBook-Pro Terraform_Team_Specifiy % terraform plan
╷
│ Error: Insufficient scope blocks
│ 
│   on main.tf line 38, in resource "azuredevops_branch_policy_auto_reviewers" "azure_repo_auto_reviewer":
│   38:   settings {
│ 
│ At least 1 "scope" blocks are required.
╵
╷
│ Error: Unsupported block type
│ 
│   on main.tf line 44, in resource "azuredevops_branch_policy_auto_reviewers" "azure_repo_auto_reviewer":
│   44:       scope {
│ 
│ Blocks of type "scope" are not expected here.

Hi @omkarsrcm,

a dynamic block contains a single blocked labeled “content”. I think you are looking for something like:

    dynamic "scope" {
      for_each = tomap(var.azureRepoLevelBranch)
      content {
        repository_id  = azuredevops_git_repository.azure_repo_creation.id
        repository_ref = scope.key
        match_type     = scope.value
      }
    }