Allow renaming a folder name without destroying the folder
lemoo5sg opened this issue · 1 comments
lemoo5sg commented
TL;DR
If we rename a previously created folder through names variable, terraform will destroy the folder and recreate a new one, as the folder name is used as the for_each key.
Note 1: 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.
Note 2: the workaround to remove the folder from the state and delete the folder with manual action is not acceptable
Note 3: 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.
Terraform Resources
names = ["previous_name"]
changed to:
names = ["new_name"]
-------------------------------
Plan result:
# module.module1.google_folder.folders["previous_name"] will be destroyed
[...]
# module.module1.google_folder.folders["new_name"] will be created
[...]
Detailed design
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.
Additional information
No response