package utils import ( "fmt" "regexp" "strings" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-framework/types" ) // ReconcileStringSlices reconciles two string lists by removing elements from the // first list that are not in the second list and appending elements from the // second list that are not in the first list. // This preserves the order of the elements in the first list that are also in // the second list, which is useful when using ListAttributes in Terraform. // The source of truth for the order is the first list and the source of truth for the content is the second list. func ReconcileStringSlices(list1, list2 []string) []string { // Create a copy of list1 to avoid modifying the original list list1Copy := append([]string{}, list1...) // Create a map to quickly check if an element is in list2 inList2 := make(map[string]bool) for _, elem := range list2 { inList2[elem] = true } // Remove elements from list1Copy that are not in list2 i := 0 for _, elem := range list1Copy { if inList2[elem] { list1Copy[i] = elem i++ } } list1Copy = list1Copy[:i] // Append elements to list1Copy that are in list2 but not in list1Copy inList1 := make(map[string]bool) for _, elem := range list1Copy { inList1[elem] = true } for _, elem := range list2 { if !inList1[elem] { list1Copy = append(list1Copy, elem) } } return list1Copy } func ListValuetoStringSlice(list basetypes.ListValue) ([]string, error) { result := []string{} for _, el := range list.Elements() { elStr, ok := el.(types.String) if !ok { return result, fmt.Errorf("expected record to be of type %T, got %T", types.String{}, elStr) } result = append(result, elStr.ValueString()) } return result, nil } // Remove leading 0s from backup schedule numbers (e.g. "00 00 * * *" becomes "0 0 * * *") // Needed as the API does it internally and would otherwise cause inconsistent result in Terraform func SimplifyBackupSchedule(schedule string) string { regex := regexp.MustCompile(`0+\d+`) // Matches series of one or more zeros followed by a series of one or more digits simplifiedSchedule := regex.ReplaceAllStringFunc(schedule, func(match string) string { simplified := strings.TrimLeft(match, "0") if simplified == "" { simplified = "0" } return simplified }) return simplifiedSchedule }