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

Rename folder cause deletion #38

Closed
czedyousfi opened this issue Feb 1, 2022 · 5 comments
Closed

Rename folder cause deletion #38

czedyousfi opened this issue Feb 1, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@czedyousfi
Copy link

czedyousfi commented Feb 1, 2022

TL;DR

I used the module to create a set of folders, then I tried to rename it but instead of update-in-place, the plan announced that it would be a destroy.

Expected behavior

~ update in-place

Terraform will perform the following actions:

google_folder.department1 will be updated in-place
~ resource "google_folder" "department1" {
~ display_name = "test-folder" -> "test-folders"
id = "folders/273697474191"
name = "folders/273697474191"
# (4 unchanged attributes hidden)
}

Observed behavior

module.folders_commoncoreplatform.google_folder.folders["Prod"] will be destroyed

  • resource "google_folder" "folders" {
    • create_time = "2022-01-26T10:44:19.350Z" -> null
    • display_name = "Prod" -> null
    • folder_id = "10526" -> null
    • id = "folders/1052649659043" -> null
    • lifecycle_state = "ACTIVE" -> null
    • name = "folders/10526" -> null
    • parent = "folders/6386" -> null
      }

module.folders_commoncoreplatform.google_folder.folders["Prods"] will be created

  • resource "google_folder" "folders" {
    • create_time = (known after apply)
    • display_name = "Prods"
    • folder_id = (known after apply)
    • id = (known after apply)
    • lifecycle_state = (known after apply)
    • name = (known after apply)
    • parent = "folders/6386"
      }

Terraform Configuration

module "folders_commoncoreplatform" {
  source  = "terraform-google-modules/folders/google"
  version = "~> 3.0"

  parent  = "${module.folders_infra.ids["Common-Core-Platform"]}" 

  names = [
    "Dev",
    "Labs",
    "Prod"
  ]
  depends_on = [
    module.folders_infra
  ]
}

Terraform Version

1.1.4.

Additional information

No response

@czedyousfi czedyousfi added the bug Something isn't working label Feb 1, 2022
@morgante
Copy link
Contributor

morgante commented Feb 1, 2022

If you want to rename the folders in the Terraform state, you should use terraform state mv.

terraform state mv 'module.folders_commoncoreplatform.google_folder.folders["Prod"]' 'module.folders_commoncoreplatform.google_folder.folders["Prods"]'

@czedyousfi
Copy link
Author

I'm going to try this then ! Thanks for your quick response

@mcdermg
Copy link

mcdermg commented Apr 29, 2022

Ran into a similar issue with renaming already created folders via the module. Shouldn't the behavior of the module mimic that of the folder resource. When creating a folder with the folder resource, the folder display_name name can be updated in place

  # google_folder.department1 will be updated in-place
  ~ resource "google_folder" "department1" {
      ~ display_name    = "Department 1" -> "Department 11"
        id              = "folders/123456789"
        name            = "folders/123456789"
        # (4 unchanged attributes hidden)
    }

When doing the same with the module, create then change the name of the folder, it will destroy the folder and re create it to alter the display_name.

If a folder is created via Terrafrom via the module and renamed via the console or gcloud on a subsequent run of Terraform plan/apply it will revert the display_name with an in place change.

The module does not seem to be following the same pattern when renaming as when using a simple resource and does seem able to make an in place change in a specif circumstance if the folder is renamed outside of Terraform but not when renaming it is handled by Terraform & the module alone.

I get that terraform state mv is a possible solution but I would be interested to know why the above behavior is occurring?
Not sure if this is some limitation due to the module or the provider but having the display_name altered via an in place change would make more sense to me over a destructive action and does seem possible given the right circumstances.

@morgante
Copy link
Contributor

morgante commented May 2, 2022

I get that terraform state mv is a possible solution but I would be interested to know why the above behavior is occurring?

Terraform requires every resource in its state to have a "key" for mapping to the Google Cloud API. For this module, the key is the folder name so changing the name necessarily means the state location is moving.

This is a fundamental limitation of Terraform, not something that can be fixed in this module. You can work around it using terraform state mv but we can't automate state moves.

@lemoo5sg
Copy link

lemoo5sg commented May 6, 2022

@morgante There is no terraform blocking topic here.
The issue with the current code version is that the folder name is used as the for_each key and this can be changed.

This has to be changed because:

  • If after the initial creation, we have created resources under the folder such as projects, the deletion will fail and the renaming will never occur.
  • the workaround to remove the folder from the state and delete the folder with manual action is not acceptable
  • Also the potential workaround to add the new folder name as a new value in the list and keep the old name as it is is not acceptable as we do not want to keep a second folder, we need a renaming.

Proposed solution:
The variables needs to be adapted to isolate the requested folder display name from the for_each resource key to allow renaming.

For example:
instead of flat names variable, use a list such as:
names= [
{folder_key = "my_folder_1", folder_name= "requested_display_name"},
]

and for folders resource, replace toset with something similar to:
resource "google_folder" "folders" {
for_each = {for folder in var.names: folder.folder_key => folder}
display_name = "${local.prefix}${each.value.folder_name}"

This way, when we change folder_name later to "new_name", we do not change folder_key, the state key is unchanged and the folder is renamed as expected.

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

4 participants