-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
ignore_changes
helper functions to a separate file.
Signed-off-by: Cem Mergenci <[email protected]>
- Loading branch information
Showing
2 changed files
with
62 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package controller | ||
|
||
import "fmt" | ||
|
||
// getTerraformIgnoreChanges returns a sorted Terraform `ignore_changes` | ||
// lifecycle meta-argument expression by looking for differences between | ||
// the `initProvider` and `forProvider` maps. The ignored fields are the ones | ||
// that are present in initProvider, but not in forProvider. | ||
// TODO: This method is copy-pasted from `pkg/resource/ignored.go` and adapted. | ||
// Consider merging this implementation with the original one. | ||
func getTerraformIgnoreChanges(forProvider, initProvider map[string]any) []string { | ||
ignored := getIgnoredFieldsMap("%s", forProvider, initProvider) | ||
return ignored | ||
} | ||
|
||
// TODO: This method is copy-pasted from `pkg/resource/ignored.go` and adapted. | ||
// Consider merging this implementation with the original one. | ||
func getIgnoredFieldsMap(format string, forProvider, initProvider map[string]any) []string { | ||
ignored := []string{} | ||
|
||
for k := range initProvider { | ||
if _, ok := forProvider[k]; !ok { | ||
ignored = append(ignored, fmt.Sprintf(format, k)) | ||
} else { | ||
// both are the same type so we dont need to check for forProvider type | ||
if _, ok = initProvider[k].(map[string]any); ok { | ||
ignored = append(ignored, getIgnoredFieldsMap(fmt.Sprintf(format, k)+".%v", forProvider[k].(map[string]any), initProvider[k].(map[string]any))...) | ||
} | ||
// if its an array, we need to check if its an array of maps or not | ||
if _, ok = initProvider[k].([]any); ok { | ||
ignored = append(ignored, getIgnoredFieldsArray(fmt.Sprintf(format, k), forProvider[k].([]any), initProvider[k].([]any))...) | ||
} | ||
|
||
} | ||
} | ||
return ignored | ||
} | ||
|
||
// TODO: This method is copy-pasted from `pkg/resource/ignored.go` and adapted. | ||
// Consider merging this implementation with the original one. | ||
func getIgnoredFieldsArray(format string, forProvider, initProvider []any) []string { | ||
ignored := []string{} | ||
for i := range initProvider { | ||
// Construct the full field path with array index and prefix. | ||
fieldPath := fmt.Sprintf("%s.%d", format, i) | ||
if i < len(forProvider) { | ||
if _, ok := initProvider[i].(map[string]any); ok { | ||
ignored = append(ignored, getIgnoredFieldsMap(fieldPath+".%s", forProvider[i].(map[string]any), initProvider[i].(map[string]any))...) | ||
} | ||
if _, ok := initProvider[i].([]any); ok { | ||
ignored = append(ignored, getIgnoredFieldsArray(fieldPath, forProvider[i].([]any), initProvider[i].([]any))...) | ||
} | ||
} else { | ||
ignored = append(ignored, fieldPath) | ||
} | ||
} | ||
return ignored | ||
} |