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

Adding user to environment returns: "A record with matching key values already exists." #623

Open
eduardodfmex opened this issue Mar 6, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@eduardodfmex
Copy link
Contributor

Adding user to environment returns: "A record with matching key values already exists. Unexpected HTTP status code. Expected: [200 204], received: [412] 412".

Adding Application users by their AAD ID returns:

Unexpected HTTP status code. Expected: [200 204], received: [412] 412
Precondition Failed | "error":"code":"0x80040237","message":"A record
with matching key values already exists."***

Sample Terraform Code

Sample Terraform code to create environment, get roles and add application User, app user for this case is a Service Principal

terraform {
  required_providers {
    powerplatform = {
      source = "microsoft/power-platform"
    }
  }
}

provider "powerplatform" {
  use_cli = true
}

resource "powerplatform_environment" "development" {
  display_name     = "example_environment"
  description      = "example environment description"
  location         = "europe"
  azure_region     = "northeurope"
  environment_type = "Sandbox"
  cadence          = "Moderate"
  dataverse = {
    language_code     = "1033"
    currency_code     = "USD"
    security_group_id = "00000000-0000-0000-0000-000000000000"
  }
}

variable "appuser_security_role" {
  description = "Security role for the application user, for example: System Administrator."
  type        = string
  default     = "System Administrator"
}

locals {
  appuser_security_roles = [for role in data.powerplatform_security_roles.all_roles.security_roles : role.role_id if(var.appuser_security_role == role.name)]

}


variable "pp_environment_app_users_map" {
  description = "Combined map of all the users and groups"
  type        = map(string)
  values      = {
    app_user01 = "app_user01_client_id"
    app_user02 = "app_user02_client_id"
    app_user03 = "app_user03_client_id"
  }

}


resource "powerplatform_data_record" "pp_environment_app_user" {
  for_each           = var.pp_environment_app_users_map
  table_logical_name = "systemuser"
  environment_id     = powerplatform_environment.development.id
  disable_on_destroy = true
  columns = {
    applicationid = each.value
    businessunitid = {
      table_logical_name = "businessunit"
      data_record_id     = data.powerplatform_data_records.business_unit_root.rows[0].businessunitid
    }
    systemuserroles_association = tolist([for rid in local.appuser_security_roles : { table_logical_name = "role", data_record_id = tostring(rid) }])
  }
  depends_on = [time_sleep.wait_pp_environment]
}

Expected behavior

System Information

  • Provider Version: 3.3.0
  • OS & Version: Linux GitHub Runner

Additional context

Error:

##[debug]│ Error: Client error when creating
##[debug]│
##[debug]│ with module.dealer4.module.power_platform_production.powerplatform_data_record.pp_environment_app_user["app_01"],
##[debug]│ on .terraform/modules/dealer4/modules/power_platform/add_users_to_pp_environment.tf line 21, in resource "powerplatform_data_record" "pp_environment_app_user":
##[debug]│ 21: resource "powerplatform_data_record" "pp_environment_app_user" ***
##[debug]│
##[debug]│ Unexpected HTTP status code. Expected: [200 204], received: [412] 412
##[debug]│ Precondition Failed | "error":"code":"0x80040237","message":"A record
##[debug]│ with matching key values already exists."***

Contribution

Do you plan to raise a PR to address this issue? YES.

@eduardodfmex eduardodfmex added the bug Something isn't working label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant