Files
grafana/pkg/services/store/entity/key.go
T
Dan Cech 9f6144059a Storage: Switch from tenant to namespace & remove GRN (#79250)
* remove GRN and switch tenant to namespace

* clean up remaining references

* simplify and remove inconsistency in With* parameters

* parse listing keys so we can use db index

* bump the schema version

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2023-12-11 12:26:05 -05:00

62 lines
1.1 KiB
Go

package entity
import (
"fmt"
"strings"
)
type Key struct {
Group string
Resource string
Namespace string
Name string
Subresource string
}
func ParseKey(key string) (*Key, error) {
// /<group>/<resource>/<namespace>(/<name>(/<subresource>))
parts := strings.SplitN(key, "/", 6)
if len(parts) < 4 {
return nil, fmt.Errorf("invalid key (expecting at least 3 parts): %s", key)
}
if parts[0] != "" {
return nil, fmt.Errorf("invalid key (expecting leading slash): %s", key)
}
k := &Key{
Group: parts[1],
Resource: parts[2],
Namespace: parts[3],
}
if len(parts) > 4 {
k.Name = parts[4]
}
if len(parts) > 5 {
k.Subresource = parts[5]
}
return k, nil
}
func (k *Key) String() string {
s := k.Group + "/" + k.Resource + "/" + k.Namespace
if len(k.Name) > 0 {
s += "/" + k.Name
if len(k.Subresource) > 0 {
s += "/" + k.Subresource
}
}
return s
}
func (k *Key) IsEqual(other *Key) bool {
return k.Group == other.Group &&
k.Resource == other.Resource &&
k.Namespace == other.Namespace &&
k.Name == other.Name &&
k.Subresource == other.Subresource
}