Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cit committed Dec 27, 2023
1 parent 6c696df commit 94222eb
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions modules/project_cleanup/function_source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func processProjectsResponsePage(removeProjectById func(projectId string)) func(
ageFilter := func(project *cloudresourcemanager.Project) bool {
projectCreatedAt, err := time.Parse(time.RFC3339, project.CreateTime)
if err != nil {
logger.Printf("Fail to parse CreateTime for [%s], skip it. Error [%s]", project.Name, err.Error())
logger.Printf("Failed to parse CreateTime for [%s], skipping it, error [%s]", project.Name, err.Error())
return false
}
return projectCreatedAt.Before(resourceCreationCutoff)
Expand All @@ -143,7 +143,7 @@ func getCorrectMaxAgeInHoursOrTerminateExecution() int64 {
maxAgeInHoursStr := os.Getenv(MaxProjectAgeHours)
maxAgeInHours, err := strconv.ParseInt(os.Getenv(MaxProjectAgeHours), 10, 0)
if err != nil {
logger.Fatalf("Could not convert [%s] to integer. Specify correct value, Please.", maxAgeInHoursStr)
logger.Fatalf("Could not convert [%s] to integer. Specify correct value for environment variable [%s] and try again.", maxAgeInHoursStr, MaxProjectAgeHours)
}
return maxAgeInHours
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func getLabelsMapFromEnv(envVariableName string) map[string]string {

err := json.Unmarshal([]byte(targetExcludedLabels), &labels)
if err != nil {
logger.Printf("Fail to get labels map from [%s] env variable, error [%s]", envVariableName, err.Error())
logger.Printf("Failed to get labels map from [%s] env variable, error [%s]", envVariableName, err.Error())
} else {
logger.Printf("Got labels map [%s] from [%s] env variable", labels, envVariableName)
}
Expand All @@ -204,7 +204,7 @@ func getTagKeysListFromEnv(envVariableName string) []string {
var tagKeys []string
err := json.Unmarshal([]byte(targetExcludedTagKeys), &tagKeys)
if err != nil {
logger.Printf("Fail to get Tag Keys list from [%s] env variable, error [%s]", envVariableName, err.Error())
logger.Printf("Failed to get Tag Keys list from [%s] env variable, error [%s]", envVariableName, err.Error())
} else {
logger.Printf("Got Tag Keys list [%s] from [%s] env variable", tagKeys, envVariableName)
}
Expand All @@ -214,11 +214,11 @@ func getTagKeysListFromEnv(envVariableName string) []string {
func getCleanUpTagKeysOrTerminateExecution() bool {
cleanUpTagKeys, exists := os.LookupEnv(CleanUpTagKeys)
if !exists {
logger.Fatalf("Clean up Tag Keys env variable not set [%s]. Specify correct value, Please.", CleanUpTagKeys)
logger.Fatalf("Clean up Tag Keys environment variable [%s] not set, set the environment variable and try again.", CleanUpTagKeys)
}
result, err := strconv.ParseBool(cleanUpTagKeys)
if err != nil {
logger.Fatalf("Invalid Clean up Tag Keys env variable [%s] value [%s]. Specify correct value, Please.", CleanUpTagKeys, cleanUpTagKeys)
logger.Fatalf("Invalid Clean up Tag Keys value [%s], specify correct value for environment variable [%s] and try again.", cleanUpTagKeys, CleanUpTagKeys)
}
return result
}
Expand All @@ -227,7 +227,7 @@ func getCorrectFolderIdOrTerminateExecution() string {
targetFolderIdString := os.Getenv(TargetFolderId)
matched, err := regexp.MatchString(targetFolderRegexp, targetFolderIdString)
if err != nil || !matched {
logger.Fatalf("Invalid folder id [%s]. Specify correct value, Please.", targetFolderIdString)
logger.Fatalf("Invalid folder id [%s], specify correct value and try again.", targetFolderIdString)
}
return targetFolderIdString
}
Expand All @@ -236,7 +236,7 @@ func getCorrectOrganizationIdOrTerminateExecution() string {
targetOrganizationIdString := os.Getenv(TargetOrganizationId)
matched, err := regexp.MatchString(targetOrganizationRegexp, targetOrganizationIdString)
if err != nil || !matched {
logger.Fatalf("Invalid organization id [%s]. Specify correct value, Please.", targetOrganizationIdString)
logger.Fatalf("Invalid organization id [%s], specify correct value and try again.", targetOrganizationIdString)
}
return targetOrganizationIdString
}
Expand All @@ -253,7 +253,7 @@ func getResourceManagerServiceOrTerminateExecution(client *http.Client) *cloudre
logger.Println("Try to get Cloud Resource Manager")
cloudResourceManagerService, err := cloudresourcemanager.New(client)
if err != nil {
logger.Fatalf("Fail to get Cloud Resource Manager with error [%s], terminate execution", err.Error())
logger.Fatalf("Failed to get Cloud Resource Manager with error [%s], terminate execution", err.Error())
}
logger.Println("Got Cloud Resource Manager")
return cloudResourceManagerService
Expand All @@ -263,7 +263,7 @@ func getFolderServiceOrTerminateExecution(client *http.Client) *cloudresourceman
logger.Println("Try to get Folders Service")
cloudResourceManagerService, err := cloudresourcemanager2.New(client)
if err != nil {
logger.Fatalf("Fail to get Folders Service with error [%s], terminate execution", err.Error())
logger.Fatalf("Failed to get Folders Service with error [%s], terminate execution", err.Error())
}
logger.Println("Got Folders Service")
return cloudResourceManagerService.Folders
Expand All @@ -273,7 +273,7 @@ func getTagKeysServiceOrTerminateExecution(client *http.Client) *cloudresourcema
logger.Println("Try to get TagKeys Service")
cloudResourceManagerService, err := cloudresourcemanager3.New(client)
if err != nil {
logger.Fatalf("Fail to get TagKeys Service with error [%s], terminate execution", err.Error())
logger.Fatalf("Failed to get TagKeys Service with error [%s], terminate execution", err.Error())
}
logger.Println("Got TagKeys Service")
return cloudResourceManagerService.TagKeys
Expand All @@ -283,7 +283,7 @@ func getTagValuesServiceOrTerminateExecution(client *http.Client) *cloudresource
logger.Println("Try to get TagValues Service")
cloudResourceManagerService, err := cloudresourcemanager3.New(client)
if err != nil {
logger.Fatalf("Fail to get TagValues Service with error [%s], terminate execution", err.Error())
logger.Fatalf("Failed to get TagValues Service with error [%s], terminate execution", err.Error())
}
logger.Println("Got TagValues Service")
return cloudResourceManagerService.TagValues
Expand All @@ -293,7 +293,7 @@ func getFirewallPoliciesServiceOrTerminateExecution(client *http.Client) *comput
logger.Println("Try to get Firewall Policies Service")
computeService, err := compute.New(client)
if err != nil {
logger.Fatalf("Fail to get Firewall Policies Service with error [%s], terminate execution", err.Error())
logger.Fatalf("Failed to get Firewall Policies Service with error [%s], terminate execution", err.Error())
}
logger.Println("Got Firewall Policies Service")
return computeService.FirewallPolicies
Expand All @@ -303,7 +303,7 @@ func initializeGoogleClient(ctx context.Context) *http.Client {
logger.Println("Try to initialize Google client")
client, err := google.DefaultClient(ctx, cloudresourcemanager.CloudPlatformScope)
if err != nil {
logger.Fatalf("Fail to initialize Google client with error [%s], terminate execution", err.Error())
logger.Fatalf("Failed to initialize Google client with error [%s], terminate execution", err.Error())
}
logger.Println("Initialized Google client")
return client
Expand All @@ -322,7 +322,7 @@ func invoke(ctx context.Context) {
logger.Printf("Try to remove lien [%s]", name)
_, err := cloudResourceManagerService.Liens.Delete(name).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to remove lien [%s], error [%s]", name, err.Error())
logger.Printf("Failed to remove lien [%s], error [%s]", name, err.Error())
} else {
logger.Printf("Removed lien [%s]", name)
}
Expand All @@ -331,7 +331,7 @@ func invoke(ctx context.Context) {
tagKeyAgeFilter := func(tagKey *cloudresourcemanager3.TagKey) bool {
tagKeyCreatedAt, err := time.Parse(time.RFC3339, tagKey.CreateTime)
if err != nil {
logger.Printf("Fail to parse CreateTime for tagKey [%s], skip it, error [%s]", tagKey.Name, err.Error())
logger.Printf("Failed to parse CreateTime for tagKey [%s], skipping it, error [%s]", tagKey.Name, err.Error())
return false
}
return tagKeyCreatedAt.Before(resourceCreationCutoff)
Expand All @@ -341,13 +341,13 @@ func invoke(ctx context.Context) {
logger.Printf("Try to remove Tag Values from TagKey [%s]", tagKey)
tagValuesList, err := tagValuesService.List().Parent(tagKey).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to list Tag values from TagKey [%s], error [%s]", tagKey, err.Error())
logger.Printf("Failed to list Tag values from TagKey [%s], error [%s]", tagKey, err.Error())
return
}
for _, tagValue := range tagValuesList.TagValues {
_, err := tagValuesService.Delete(tagValue.Name).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to delete tagValue from TagKey [%s], error [%s]", tagKey, err.Error())
logger.Printf("Failed to delete tagValue from TagKey [%s], error [%s]", tagKey, err.Error())
}
}
}
Expand All @@ -357,15 +357,15 @@ func invoke(ctx context.Context) {
parent := fmt.Sprintf("organizations/%s", organization)
tagKeysList, err := tagKeyService.List().Parent(parent).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to list Tag Keys from organization [%s], error [%s]", organization, err.Error())
logger.Printf("Failed to list Tag Keys from organization [%s], error [%s]", organization, err.Error())
return
}
for _, tagKey := range tagKeysList.TagKeys {
if !checkIfTagKeyShortNameExcluded(tagKey.ShortName, excludedTagKeysList) && tagKeyAgeFilter(tagKey) {
removeTagValues(tagKey.Name)
_, err := tagKeyService.Delete(tagKey.Name).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to delete tagKey from organization [%s], error [%s]", organization, err.Error())
logger.Printf("Failed to delete tagKey from organization [%s], error [%s]", organization, err.Error())
}
}
}
Expand All @@ -375,19 +375,19 @@ func invoke(ctx context.Context) {
logger.Printf("Try to remove Firewall Policies from folder [%s]", folder)
firewallPolicyList, err := firewallPoliciesService.List().ParentId(folder).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to list Firewall Policies from folder [%s], error [%s]", folder, err.Error())
logger.Printf("Failed to list Firewall Policies from folder [%s], error [%s]", folder, err.Error())
return
}
for _, policy := range firewallPolicyList.Items {
for _, association := range policy.Associations {
_, err := firewallPoliciesService.RemoveAssociation(policy.Name).Name(association.Name).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to Remove Association for Firewall Policies from folder [%s], error [%s]", folder, err.Error())
logger.Printf("Failed to Remove Association for Firewall Policies from folder [%s], error [%s]", folder, err.Error())
}
}
_, err := firewallPoliciesService.Delete(policy.Name).Context(ctx).Do()
if err != nil {
logger.Printf("Fail to delete Firewall Policy [%s] from folder [%s], error [%s]", policy.Name, folder, err.Error())
logger.Printf("Failed to delete Firewall Policy [%s] from folder [%s], error [%s]", policy.Name, folder, err.Error())
}
}
}
Expand All @@ -401,7 +401,7 @@ func invoke(ctx context.Context) {
logger.Printf("Try to remove endpoints for [%s]", projectId)
listResponse, err := endpointService.Services.List().ProducerProjectId(projectId).Do()
if err != nil {
logger.Printf("Fail to list services for [%s], error [%s]", projectId, err.Error())
logger.Printf("Failed to list services for [%s], error [%s]", projectId, err.Error())
return
}

Expand All @@ -413,7 +413,7 @@ func invoke(ctx context.Context) {
logger.Printf("Try to remove service: %s", service.ServiceName)
_, err = endpointService.Services.Delete(service.ServiceName).Do()
if err != nil {
logger.Printf("Fail to delete service [%s] for [%s], error [%s]", service.ServiceName, projectId, err.Error())
logger.Printf("Failed to delete service [%s] for [%s], error [%s]", service.ServiceName, projectId, err.Error())
}
}

Expand All @@ -429,7 +429,7 @@ func invoke(ctx context.Context) {
err = removeProjectById(projectId)
}
if err != nil {
logger.Printf("Fail to remove project [%s], error [%s]", projectId, err.Error())
logger.Printf("Failed to remove project [%s], error [%s]", projectId, err.Error())
} else {
logger.Printf("Removed project [%s]", projectId)
}
Expand All @@ -447,7 +447,7 @@ func invoke(ctx context.Context) {
cleanupProjectById(projectId)
return nil
}); err != nil {
logger.Printf("Fail to get all liens for the project [%s], error [%s]", projectId, err.Error())
logger.Printf("Failed to get all liens for the project [%s], error [%s]", projectId, err.Error())
}
}

Expand All @@ -461,7 +461,7 @@ func invoke(ctx context.Context) {
return
}, 5, time.Minute)
if err != nil {
logger.Printf("Fail to get projects for the folder with id [%s], error [%s]", localFolderId, err.Error())
logger.Printf("Failed to get projects for the folder with id [%s], error [%s]", localFolderId, err.Error())
} else {
logger.Printf("Got and processed all projects for the folder with id [%s]", localFolderId)
}
Expand All @@ -470,7 +470,7 @@ 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())
logger.Printf("Failed to parse CreateTime for folder [%s], skipping it, error [%s]", folder.Name, err.Error())
return false
}
return folderCreatedAt.Before(resourceCreationCutoff)
Expand Down Expand Up @@ -501,14 +501,14 @@ func invoke(ctx context.Context) {
}
return nil
}); err != nil {
logger.Fatalf("Fail to get subfolders for the folder with id [%s], error [%s]", folderId, err.Error())
logger.Fatalf("Failed to get subfolders for the folder with id [%s], error [%s]", folderId, err.Error())
}
}

rootFolderId := fmt.Sprintf("folders/%s", rootFolderId)
rootFolder, err := folderService.Get(rootFolderId).Do()
if err != nil {
logger.Printf("Fail to get parent folder [%s], error [%s]", rootFolderId, err.Error())
logger.Printf("Failed to get parent folder [%s], error [%s]", rootFolderId, err.Error())
} else {
getSubFoldersAndRemoveProjectsFoldersRecursively(rootFolder, getSubFoldersAndRemoveProjectsFoldersRecursively)
}
Expand Down

0 comments on commit 94222eb

Please sign in to comment.