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

project_environment creation fails if more than one project_environment is slated to be created. #184

Open
richardmcsong opened this issue Dec 11, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@richardmcsong
Copy link

richardmcsong commented Dec 11, 2024

Given:

locals {
  envs = toset(["CUSTOM-REMOTE", "CUSTOM-ENV"])
}

resource "project" "tenant" {
  key          = "projkey"
  display_name = "projkey"
  admin_privileges {
    index_resources  = false
    manage_members   = false
    manage_resources = false
  }
}

resource "project_environment" "env" {
  for_each    = toset(var.environments)
  name        = each.key
  project_key = project.tenant.key
}

Running this, there's a race condition that happens between CUSTOM-REMOTE and
CUSTOM-ENV. Given that CUSTOM-ENV successfully gets created, we get the
following error:

Error: Unable to Create Resource

[...]

An unexpected error occurred while creating the resource update request.
Please report this issue to the provider developers.

Error: BAD_REQUEST - Project Admin should contain all global and project
environments. Project environments: [PROD, DEV, projkey-CUSTOM-REMOTE,
projkey-CUSTOM-ENV] but got: [PROD, DEV, projkey-CUSTOM-REMOTE]

CUSTOM-ENV then gets stuck in a tainted state. Any further operations results in:

  ╷
  │ Error: Unable to Delete Resource
  │ 
  │ An unexpected error occurred while attempting to delete the resource.
  │ Please retry the operation or report this issue to the provider developers.
  │ 
  │ Error: NOT_FOUND - Could not find environment `projkey-CUSTOM-REMOTE`
  ╵

Key info:

  Terraform v1.8.4
  on darwin_arm64

provider: 1.9.0
artifactory version: Enterprise Plus 7.90.15

@richardmcsong richardmcsong added the bug Something isn't working label Dec 11, 2024
@alexhung
Copy link
Member

@richardmcsong Thanks for the report. This is caused by Terraform executing the resource creation in parallel with the use of for_each. I'll see what I can do to resolve this.

@alexhung
Copy link
Member

alexhung commented Dec 18, 2024

@richardmcsong I am able to reproduce this issue, although only sporadically.

A couple of things:

  1. I find a bug in the code for error handling in create/update functions where the code doesn't return after an error and therefore the resource is stored into the state. I'll be opening a PR to fix this.
  2. The race condition error comes from the project REST API. Unfortunately, the environment in question actually is created successfully in Artifactory (at least in my test environment). This prevents running terraform apply again to create the environment again as TF will error out with resource already exists. I've created a bug report with the Project team for investigation.

In the meantime, there isn't any good solution I can offer. You can try to use depends_on meta-argument to make the environment resource to be managed in a sequential manner. See this StackOverflow thread

alexhung added a commit that referenced this issue Dec 19, 2024
Ensure create/update funcs return early if errored for project_environment resource
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

2 participants