pull/89331/head
Ryan McKinley 1 year ago
parent 29714d87fe
commit 19481990d1
  1. 118
      pkg/apimachinery/identity/context_test.go
  2. 33
      pkg/apimachinery/utils/meta.go
  3. 2
      pkg/services/store/entity/db/migrations/resource_mig.go

@ -17,10 +17,126 @@ func TestRequesterFromContext(t *testing.T) {
}) })
t.Run("should return user set by ContextWithUser", func(t *testing.T) { t.Run("should return user set by ContextWithUser", func(t *testing.T) {
expected := &identity.StaticRequester{UserUID: "AAA"} expected := &dummyUser{UID: "AAA"}
ctx := identity.WithRequester(context.Background(), expected) ctx := identity.WithRequester(context.Background(), expected)
actual, err := identity.GetRequester(ctx) actual, err := identity.GetRequester(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected.GetUID(), actual.GetUID()) require.Equal(t, expected.GetUID(), actual.GetUID())
}) })
} }
type dummyUser struct {
UID string
}
// GetAuthID implements identity.Requester.
func (d *dummyUser) GetAuthID() string {
panic("unimplemented")
}
// GetAuthenticatedBy implements identity.Requester.
func (d *dummyUser) GetAuthenticatedBy() string {
panic("unimplemented")
}
// GetCacheKey implements identity.Requester.
func (d *dummyUser) GetCacheKey() string {
panic("unimplemented")
}
// GetDisplayName implements identity.Requester.
func (d *dummyUser) GetDisplayName() string {
panic("unimplemented")
}
// GetEmail implements identity.Requester.
func (d *dummyUser) GetEmail() string {
panic("unimplemented")
}
// GetGlobalPermissions implements identity.Requester.
func (d *dummyUser) GetGlobalPermissions() map[string][]string {
panic("unimplemented")
}
// GetID implements identity.Requester.
func (d *dummyUser) GetID() identity.NamespaceID {
panic("unimplemented")
}
// GetIDToken implements identity.Requester.
func (d *dummyUser) GetIDToken() string {
panic("unimplemented")
}
// GetIsGrafanaAdmin implements identity.Requester.
func (d *dummyUser) GetIsGrafanaAdmin() bool {
panic("unimplemented")
}
// GetLogin implements identity.Requester.
func (d *dummyUser) GetLogin() string {
panic("unimplemented")
}
// GetNamespacedID implements identity.Requester.
func (d *dummyUser) GetNamespacedID() (namespace identity.Namespace, identifier string) {
panic("unimplemented")
}
// GetOrgID implements identity.Requester.
func (d *dummyUser) GetOrgID() int64 {
panic("unimplemented")
}
// GetOrgName implements identity.Requester.
func (d *dummyUser) GetOrgName() string {
panic("unimplemented")
}
// GetOrgRole implements identity.Requester.
func (d *dummyUser) GetOrgRole() identity.RoleType {
panic("unimplemented")
}
// GetPermissions implements identity.Requester.
func (d *dummyUser) GetPermissions() map[string][]string {
panic("unimplemented")
}
// GetTeams implements identity.Requester.
func (d *dummyUser) GetTeams() []int64 {
panic("unimplemented")
}
// GetUID implements identity.Requester.
func (d *dummyUser) GetUID() identity.NamespaceID {
return identity.NewNamespaceIDString(identity.NamespaceUser, d.UID)
}
// HasRole implements identity.Requester.
func (d *dummyUser) HasRole(role identity.RoleType) bool {
panic("unimplemented")
}
// HasUniqueId implements identity.Requester.
func (d *dummyUser) HasUniqueId() bool {
panic("unimplemented")
}
// IsAuthenticatedBy implements identity.Requester.
func (d *dummyUser) IsAuthenticatedBy(providers ...string) bool {
panic("unimplemented")
}
// IsEmailVerified implements identity.Requester.
func (d *dummyUser) IsEmailVerified() bool {
panic("unimplemented")
}
// IsNil implements identity.Requester.
func (d *dummyUser) IsNil() bool {
return false
}
var _ identity.Requester = &dummyUser{}

@ -3,7 +3,6 @@ package utils
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strconv"
"time" "time"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
@ -53,8 +52,6 @@ type GrafanaMetaAccessor interface {
metav1.Object metav1.Object
GetGroupVersionKind() schema.GroupVersionKind GetGroupVersionKind() schema.GroupVersionKind
GetResourceVersionInt64() (int64, error)
SetResourceVersionInt64(int64)
GetUpdatedTimestamp() (*time.Time, error) GetUpdatedTimestamp() (*time.Time, error)
SetUpdatedTimestamp(v *time.Time) SetUpdatedTimestamp(v *time.Time)
@ -65,8 +62,6 @@ type GrafanaMetaAccessor interface {
SetUpdatedBy(user string) SetUpdatedBy(user string)
GetFolder() string GetFolder() string
SetFolder(uid string) SetFolder(uid string)
SetAnnotation(key string, val string)
GetSlug() string GetSlug() string
SetSlug(v string) SetSlug(v string)
@ -104,7 +99,7 @@ func MetaAccessor(raw interface{}) (GrafanaMetaAccessor, error) {
return nil, err return nil, err
} }
// reflection to find title and other non object properties // look for Spec.Title or Spec.Name
r := reflect.ValueOf(raw) r := reflect.ValueOf(raw)
if r.Kind() == reflect.Ptr || r.Kind() == reflect.Interface { if r.Kind() == reflect.Ptr || r.Kind() == reflect.Interface {
r = r.Elem() r = r.Elem()
@ -112,19 +107,11 @@ func MetaAccessor(raw interface{}) (GrafanaMetaAccessor, error) {
return &grafanaMetaAccessor{raw, obj, r}, nil return &grafanaMetaAccessor{raw, obj, r}, nil
} }
func (m *grafanaMetaAccessor) GetResourceVersionInt64() (int64, error) { func (m *grafanaMetaAccessor) Object() metav1.Object {
v := m.obj.GetResourceVersion() return m.obj
if v == "" {
return 0, nil
}
return strconv.ParseInt(v, 10, 64)
}
func (m *grafanaMetaAccessor) SetResourceVersionInt64(rv int64) {
m.obj.SetResourceVersion(strconv.FormatInt(rv, 10))
} }
func (m *grafanaMetaAccessor) SetAnnotation(key string, val string) { func (m *grafanaMetaAccessor) set(key string, val string) {
anno := m.obj.GetAnnotations() anno := m.obj.GetAnnotations()
if val == "" { if val == "" {
if anno != nil { if anno != nil {
@ -161,7 +148,7 @@ func (m *grafanaMetaAccessor) SetUpdatedTimestampMillis(v int64) {
t := time.UnixMilli(v) t := time.UnixMilli(v)
m.SetUpdatedTimestamp(&t) m.SetUpdatedTimestamp(&t)
} else { } else {
m.SetAnnotation(AnnoKeyUpdatedTimestamp, "") // will clear the annotation m.set(AnnoKeyUpdatedTimestamp, "") // will clear the annotation
} }
} }
@ -170,7 +157,7 @@ func (m *grafanaMetaAccessor) SetUpdatedTimestamp(v *time.Time) {
if v != nil && v.Unix() != 0 { if v != nil && v.Unix() != 0 {
txt = v.UTC().Format(time.RFC3339) txt = v.UTC().Format(time.RFC3339)
} }
m.SetAnnotation(AnnoKeyUpdatedTimestamp, txt) m.set(AnnoKeyUpdatedTimestamp, txt)
} }
func (m *grafanaMetaAccessor) GetCreatedBy() string { func (m *grafanaMetaAccessor) GetCreatedBy() string {
@ -178,7 +165,7 @@ func (m *grafanaMetaAccessor) GetCreatedBy() string {
} }
func (m *grafanaMetaAccessor) SetCreatedBy(user string) { func (m *grafanaMetaAccessor) SetCreatedBy(user string) {
m.SetAnnotation(AnnoKeyCreatedBy, user) m.set(AnnoKeyCreatedBy, user)
} }
func (m *grafanaMetaAccessor) GetUpdatedBy() string { func (m *grafanaMetaAccessor) GetUpdatedBy() string {
@ -186,7 +173,7 @@ func (m *grafanaMetaAccessor) GetUpdatedBy() string {
} }
func (m *grafanaMetaAccessor) SetUpdatedBy(user string) { func (m *grafanaMetaAccessor) SetUpdatedBy(user string) {
m.SetAnnotation(AnnoKeyUpdatedBy, user) m.set(AnnoKeyUpdatedBy, user)
} }
func (m *grafanaMetaAccessor) GetFolder() string { func (m *grafanaMetaAccessor) GetFolder() string {
@ -194,7 +181,7 @@ func (m *grafanaMetaAccessor) GetFolder() string {
} }
func (m *grafanaMetaAccessor) SetFolder(uid string) { func (m *grafanaMetaAccessor) SetFolder(uid string) {
m.SetAnnotation(AnnoKeyFolder, uid) m.set(AnnoKeyFolder, uid)
} }
func (m *grafanaMetaAccessor) GetSlug() string { func (m *grafanaMetaAccessor) GetSlug() string {
@ -202,7 +189,7 @@ func (m *grafanaMetaAccessor) GetSlug() string {
} }
func (m *grafanaMetaAccessor) SetSlug(v string) { func (m *grafanaMetaAccessor) SetSlug(v string) {
m.SetAnnotation(AnnoKeySlug, v) m.set(AnnoKeySlug, v)
} }
func (m *grafanaMetaAccessor) SetOriginInfo(info *ResourceOriginInfo) { func (m *grafanaMetaAccessor) SetOriginInfo(info *ResourceOriginInfo) {

@ -74,7 +74,7 @@ func initResourceTables(mg *migrator.Migrator) string {
{Cols: []string{"operation"}, Type: migrator.IndexType}, {Cols: []string{"operation"}, Type: migrator.IndexType},
{Cols: []string{"namespace"}, Type: migrator.IndexType}, {Cols: []string{"namespace"}, Type: migrator.IndexType},
{Cols: []string{"group", "resource", "name"}, Type: migrator.IndexType}, {Cols: []string{"group", "resource", "name"}, Type: migrator.IndexType},
{Cols: []string{"blob_path"}, Type: migrator.IndexType}, {Cols: []string{"blob_path_hash"}, Type: migrator.IndexType},
}, },
}) })

Loading…
Cancel
Save