UnifiedSearch: Introduce a DocumentBuilder interface (#96738)

This commit is contained in:
Ryan McKinley
2024-11-21 07:53:25 +02:00
committed by GitHub
parent 8d4db7ac85
commit 0cb6c3d7bf
24 changed files with 1428 additions and 8 deletions
+41
View File
@@ -1,5 +1,10 @@
package resource
import (
"fmt"
"strings"
)
func verifyRequestKey(key *ResourceKey) *ErrorResult {
if key == nil {
return NewBadRequestError("missing resource key")
@@ -28,3 +33,39 @@ func matchesQueryKey(query *ResourceKey, key *ResourceKey) bool {
}
return true
}
const clusterNamespace = "**cluster**"
// Convert the key to a search ID string
func (x *ResourceKey) SearchID() string {
var sb strings.Builder
if x.Namespace == "" {
sb.WriteString(clusterNamespace)
} else {
sb.WriteString(x.Namespace)
}
sb.WriteString("/")
sb.WriteString(x.Group)
sb.WriteString("/")
sb.WriteString(x.Resource)
sb.WriteString("/")
sb.WriteString(x.Name)
return sb.String()
}
func (x *ResourceKey) ReadSearchID(v string) error {
parts := strings.Split(v, "/")
if len(parts) != 4 {
return fmt.Errorf("invalid search id (expecting 3 slashes)")
}
x.Namespace = parts[0]
x.Group = parts[1]
x.Resource = parts[2]
x.Name = parts[3]
if x.Namespace == clusterNamespace {
x.Namespace = ""
}
return nil
}