From f4ab92b878ef75b961ab971e5ef31f82d76ee768 Mon Sep 17 00:00:00 2001 From: Daniel Andrade Date: Thu, 9 Jun 2022 18:20:00 -0300 Subject: [PATCH] feat: removal of folder should use the same time constraint used for projects (#79) --- modules/project_cleanup/function_source/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/project_cleanup/function_source/main.go b/modules/project_cleanup/function_source/main.go index eaa492b..b408068 100644 --- a/modules/project_cleanup/function_source/main.go +++ b/modules/project_cleanup/function_source/main.go @@ -343,6 +343,15 @@ func invoke(ctx context.Context) { } } + folderAgeFilter := func(folder *cloudresourcemanager2.Folder) bool { + folderCreatedAt, err := time.Parse(time.RFC3339, folder.CreateTime) + if err != nil { + logger.Printf("Fail to parse CreateTime for folder [%s], skip it. Error [%s]", folder.Name, err.Error()) + return false + } + return folderCreatedAt.Before(resourceCreationCutoff) + } + removeFolder := func(folder *cloudresourcemanager2.Folder) { folderId := folder.Name removeFirewallPolicies(folderId) @@ -363,7 +372,7 @@ func invoke(ctx context.Context) { recursion(folder, recursion) } removeProjectsInFolder(folderId) - if folder.Parent != fmt.Sprintf("folders/%s", rootFolderId) && folder.Name != fmt.Sprintf("folders/%s", rootFolderId) { + if folder.Parent != fmt.Sprintf("folders/%s", rootFolderId) && folder.Name != fmt.Sprintf("folders/%s", rootFolderId) && folderAgeFilter(folder) { removeFolder(folder) } return nil