* add model serving * add right provider config * rename model_serving to modelserving * add model serving custom endpoint everywhere * rename file * add default region, docs for model serving * add right order of wait handler * rotate after to token * fixes * add initial doc files * address code comments * refactor region description * remove warning for not found resources * add service enablement * address code comments * address code comments * fix datasource * fix acc test * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * embed markdown description * go tidy --------- Co-authored-by: Mauritz Uphoff <mauritz.uphoff@me.com> Co-authored-by: Mauritz Uphoff <39736813+h3adex@users.noreply.github.com>
116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
|
)
|
|
|
|
const (
|
|
SKEServiceId = "cloud.stackit.ske"
|
|
ModelServingServiceId = "cloud.stackit.model-serving"
|
|
)
|
|
|
|
var (
|
|
LegacyProjectRoles = []string{"project.admin", "project.auditor", "project.member", "project.owner"}
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// SimplifyBackupSchedule removes 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
|
|
}
|
|
|
|
func SupportedValuesDocumentation(values []string) string {
|
|
if len(values) == 0 {
|
|
return ""
|
|
}
|
|
return "Supported values are: " + strings.Join(QuoteValues(values), ", ") + "."
|
|
}
|
|
|
|
func QuoteValues(values []string) []string {
|
|
quotedValues := make([]string, len(values))
|
|
for i, value := range values {
|
|
quotedValues[i] = fmt.Sprintf("`%s`", value)
|
|
}
|
|
return quotedValues
|
|
}
|
|
|
|
func IsLegacyProjectRole(role string) bool {
|
|
return utils.Contains(LegacyProjectRoles, role)
|
|
}
|
|
|
|
type value interface {
|
|
IsUnknown() bool
|
|
IsNull() bool
|
|
}
|
|
|
|
// IsUndefined checks if a passed value is unknown or null
|
|
func IsUndefined(val value) bool {
|
|
return val.IsUnknown() || val.IsNull()
|
|
}
|