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

feat(resource_folder): add retryable validation of parent_folder_uid existence #2041

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

paulojmdias
Copy link

Description

When creating nested Grafana folders using Terraform, Terraform did not guarantee parent folders were made before their child folders. This resulted in 404 errors when creating child folders before their parents existed.

This issue occurs because Terraform does not strictly enforce resource creation order, even when defining a parent-child relationship via parent_folder_uid in the same resource within a loop.

Solution

  1. Implements a retry mechanism (up to 2 minutes) when fetching the parent folder using GetFolderByIDorUID to allow Terraform time to create it.
  2. Ensures that child folders are only created once their parent is confirmed to exist, preventing the 404 error.
  3. Fails gracefully if the parent folder does not exist within the retry window.

Changes summary:

  • Calls GetFolderByIDorUID before creating the child folder.
  • Implements retry.RetryContext to repeatedly check for the parent folder’s existence before attempting to create the child folder.
  • If the parent folder does not exist yet, the creation is retried instead of failing immediately.

Terraform code example

locals {
  folders = [
    "level1",
    "level1/level2",
    "level1/level2/level3",
    "level1/level2/level3/level4"
  ]
}

resource "grafana_folder" "this" {
  for_each = toset(local.folders)

  title = basename(each.value)          # Extracts only the last level of the folder path
  uid   = replace(each.value, "/", "_") # Consistent UID for lookup

  parent_folder_uid = (
    length(split("/", each.value)) > 1
    ? replace(join("/", slice(split("/", each.value), 0, length(split("/", each.value)) - 1)), "/", "_")
    : null
  )
}

Output before this change

grafana_folder.this["level1"]: Creation complete after 0s [id=0:level1]
grafana_folder.this["level1/level2"]: Creation complete after 0s [id=0:level1_level2]
╷
│ Error: failed to create folder: [POST /folders] createFolder (status 404): {}
│
│   with grafana_folder.this["level1/level2/level3"],
│   on folder.tf line 10, in resource "grafana_folder" "this":
│   10: resource "grafana_folder" "this" {
│
╵
│ Error: failed to create folder: [POST /folders] createFolder (status 404): {}
│
│   with grafana_folder.this["level1/level2/level3/level4"],
│   on folder.tf line 10, in resource "grafana_folder" "this":
│   10: resource "grafana_folder" "this" {
│
╵

Output after this change

grafana_folder.this["level1"]: Creation complete after 0s [id=0:level1]
grafana_folder.this["level1/level2"]: Creation complete after 0s [id=0:level1_level2]
grafana_folder.this["level1/level2/level3"]: Creation complete after 1s [id=0:level1_level2_level3]
grafana_folder.this["level1/level2/level3/level4"]: Creation complete after 3s [id=0:level1_level2_level3_level4]

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

…existence

Signed-off-by: Paulo Dias <paulodias.gm@gmail.com>
@paulojmdias paulojmdias requested a review from a team as a code owner February 23, 2025 22:24
@CLAassistant
Copy link

CLAassistant commented Feb 23, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

In order to lower resource usage and have a faster runtime, PRs will not run Cloud tests automatically.
To do so, a Grafana Labs employee must trigger the cloud acceptance tests workflow manually.

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

Successfully merging this pull request may close these issues.

2 participants