fill stubs for cloud config
This commit is contained in:
@@ -3,11 +3,12 @@ package datasource
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -51,9 +52,9 @@ func (s *connectionAccess) ConvertToTable(ctx context.Context, object runtime.Ob
|
||||
}
|
||||
|
||||
func (s *connectionAccess) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
||||
return s.datasources.Get(ctx, name)
|
||||
return s.datasources.GetConnection(ctx, name)
|
||||
}
|
||||
|
||||
func (s *connectionAccess) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
||||
return s.datasources.List(ctx)
|
||||
return s.datasources.ListConnections(ctx)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
gapiutil "github.com/grafana/grafana/pkg/services/apiserver/utils"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
@@ -23,6 +24,7 @@ func asConnection(ds *datasources.DataSource, ns string) (*v0alpha1.DataSourceCo
|
||||
Namespace: ns,
|
||||
CreationTimestamp: metav1.NewTime(ds.Created),
|
||||
ResourceVersion: fmt.Sprintf("%d", ds.Updated.UnixMilli()),
|
||||
Generation: int64(ds.Version),
|
||||
},
|
||||
Title: ds.Name,
|
||||
}
|
||||
@@ -45,6 +47,7 @@ func (r *converter) asGenericDataSource(ds *datasources.DataSource) (*v0alpha1.G
|
||||
Namespace: r.mapper(ds.OrgID),
|
||||
CreationTimestamp: metav1.NewTime(ds.Created),
|
||||
ResourceVersion: fmt.Sprintf("%d", ds.Updated.UnixMilli()),
|
||||
Generation: int64(ds.Version),
|
||||
},
|
||||
Spec: v0alpha1.GenericDataSourceSpec{
|
||||
Title: ds.Name,
|
||||
@@ -77,3 +80,26 @@ func (r *converter) asGenericDataSource(ds *datasources.DataSource) (*v0alpha1.G
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (r *converter) toAddCommand(ds *v0alpha1.GenericDataSource) (*datasources.AddDataSourceCommand, error) {
|
||||
cmd := &datasources.AddDataSourceCommand{
|
||||
Name: ds.Name,
|
||||
Type: "", // TODO... group > datasource type????
|
||||
Access: datasources.DsAccess(ds.Spec.Access),
|
||||
URL: ds.Spec.URL,
|
||||
Database: ds.Spec.Database,
|
||||
// TODO... secrets
|
||||
}
|
||||
|
||||
if len(ds.Spec.JsonData.Object) > 0 {
|
||||
cmd.JsonData = simplejson.NewFromAny(ds.Spec.JsonData.Object)
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func (r *converter) toUpdateCommand(ds *v0alpha1.GenericDataSource) (*datasources.UpdateDataSourceCommand, error) {
|
||||
cmd := &datasources.UpdateDataSourceCommand{}
|
||||
// TODO!!!
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package datasource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -17,9 +19,10 @@ var (
|
||||
_ rest.Getter = (*legacyStorage)(nil)
|
||||
_ rest.Lister = (*legacyStorage)(nil)
|
||||
_ rest.Storage = (*legacyStorage)(nil)
|
||||
// _ rest.Creater = (*legacyStorage)(nil)
|
||||
// _ rest.Updater = (*legacyStorage)(nil)
|
||||
// _ rest.GracefulDeleter = (*legacyStorage)(nil)
|
||||
_ rest.Creater = (*legacyStorage)(nil)
|
||||
_ rest.Updater = (*legacyStorage)(nil)
|
||||
_ rest.GracefulDeleter = (*legacyStorage)(nil)
|
||||
_ rest.CollectionDeleter = (*legacyStorage)(nil)
|
||||
)
|
||||
|
||||
type legacyStorage struct {
|
||||
@@ -56,3 +59,53 @@ func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListO
|
||||
func (s *legacyStorage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
||||
return s.datasources.GetDataSource(ctx, name)
|
||||
}
|
||||
|
||||
// Create implements rest.Creater.
|
||||
func (s *legacyStorage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
|
||||
ds, ok := obj.(*v0alpha1.GenericDataSource)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected a datasource object")
|
||||
}
|
||||
return s.datasources.CreateDataSource(ctx, ds)
|
||||
}
|
||||
|
||||
// Update implements rest.Updater.
|
||||
func (s *legacyStorage) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
|
||||
old, err := s.Get(ctx, name, &metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
obj, err := objInfo.UpdatedObject(ctx, old)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
ds, ok := obj.(*v0alpha1.GenericDataSource)
|
||||
if !ok {
|
||||
return nil, false, fmt.Errorf("expected a datasource object")
|
||||
}
|
||||
|
||||
ds, err = s.datasources.UpdateDataSource(ctx, ds)
|
||||
return ds, false, err
|
||||
}
|
||||
|
||||
// Delete implements rest.GracefulDeleter.
|
||||
func (s *legacyStorage) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
||||
err := s.datasources.Delete(ctx, name)
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// DeleteCollection implements rest.CollectionDeleter.
|
||||
func (s *legacyStorage) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) {
|
||||
dss, err := s.datasources.ListDataSource(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, ds := range dss.Items {
|
||||
if err = s.datasources.Delete(ctx, ds.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ import (
|
||||
// limit which namespace/tenant/org we are talking to
|
||||
type PluginDatasourceProvider interface {
|
||||
// Get gets a specific datasource (that the user in context can see)
|
||||
Get(ctx context.Context, uid string) (*v0alpha1.DataSourceConnection, error)
|
||||
GetConnection(ctx context.Context, uid string) (*v0alpha1.DataSourceConnection, error)
|
||||
|
||||
// List lists all data sources the user in context can see
|
||||
List(ctx context.Context) (*v0alpha1.DataSourceConnectionList, error)
|
||||
ListConnections(ctx context.Context) (*v0alpha1.DataSourceConnectionList, error)
|
||||
|
||||
// Get a single datasurce
|
||||
GetDataSource(ctx context.Context, uid string) (*v0alpha1.GenericDataSource, error)
|
||||
@@ -30,6 +30,15 @@ type PluginDatasourceProvider interface {
|
||||
// List all datasources
|
||||
ListDataSource(ctx context.Context) (*v0alpha1.GenericDataSourceList, error)
|
||||
|
||||
// Create a data source
|
||||
CreateDataSource(ctx context.Context, ds *v0alpha1.GenericDataSource) (*v0alpha1.GenericDataSource, error)
|
||||
|
||||
// Update a data source
|
||||
UpdateDataSource(ctx context.Context, ds *v0alpha1.GenericDataSource) (*v0alpha1.GenericDataSource, error)
|
||||
|
||||
// Delete datasurce
|
||||
Delete(ctx context.Context, uid string) error
|
||||
|
||||
// Return settings (decrypted!) for a specific plugin
|
||||
// This will require "query" permission for the user in context
|
||||
GetInstanceSettings(ctx context.Context, uid string) (*backend.DataSourceInstanceSettings, error)
|
||||
@@ -88,7 +97,7 @@ var (
|
||||
_ ScopedPluginDatasourceProvider = (*cachingDatasourceProvider)(nil)
|
||||
)
|
||||
|
||||
func (q *scopedDatasourceProvider) Get(ctx context.Context, uid string) (*v0alpha1.DataSourceConnection, error) {
|
||||
func (q *scopedDatasourceProvider) GetConnection(ctx context.Context, uid string) (*v0alpha1.DataSourceConnection, error) {
|
||||
user, err := identity.GetRequester(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -100,7 +109,7 @@ func (q *scopedDatasourceProvider) Get(ctx context.Context, uid string) (*v0alph
|
||||
return q.converter.asConnection(ds)
|
||||
}
|
||||
|
||||
func (q *scopedDatasourceProvider) List(ctx context.Context) (*v0alpha1.DataSourceConnectionList, error) {
|
||||
func (q *scopedDatasourceProvider) ListConnections(ctx context.Context) (*v0alpha1.DataSourceConnectionList, error) {
|
||||
info, err := request.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -131,6 +140,53 @@ func (q *scopedDatasourceProvider) GetInstanceSettings(ctx context.Context, uid
|
||||
return q.contextProvider.GetDataSourceInstanceSettings(ctx, uid)
|
||||
}
|
||||
|
||||
// CreateDataSource implements PluginDatasourceProvider.
|
||||
func (q *scopedDatasourceProvider) CreateDataSource(ctx context.Context, ds *v0alpha1.GenericDataSource) (*v0alpha1.GenericDataSource, error) {
|
||||
cmd, err := q.converter.toAddCommand(ds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := q.dsService.AddDataSource(ctx, cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return q.converter.asGenericDataSource(out)
|
||||
}
|
||||
|
||||
// UpdateDataSource implements PluginDatasourceProvider.
|
||||
func (q *scopedDatasourceProvider) UpdateDataSource(ctx context.Context, ds *v0alpha1.GenericDataSource) (*v0alpha1.GenericDataSource, error) {
|
||||
cmd, err := q.converter.toUpdateCommand(ds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := q.dsService.UpdateDataSource(ctx, cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return q.converter.asGenericDataSource(out)
|
||||
}
|
||||
|
||||
// Delete implements PluginDatasourceProvider.
|
||||
func (q *scopedDatasourceProvider) Delete(ctx context.Context, uid string) error {
|
||||
user, err := identity.GetRequester(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ds, err := q.dsCache.GetDatasourceByUID(ctx, uid, user, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ds == nil {
|
||||
return fmt.Errorf("not found")
|
||||
}
|
||||
return q.dsService.DeleteDataSource(ctx, &datasources.DeleteDataSourceCommand{
|
||||
ID: ds.ID,
|
||||
UID: ds.UID,
|
||||
OrgID: ds.OrgID,
|
||||
Name: ds.Name,
|
||||
})
|
||||
}
|
||||
|
||||
// GetDataSource implements PluginDatasourceProvider.
|
||||
func (q *scopedDatasourceProvider) GetDataSource(ctx context.Context, uid string) (*v0alpha1.GenericDataSource, error) {
|
||||
user, err := identity.GetRequester(ctx)
|
||||
|
||||
@@ -121,6 +121,21 @@ var _ PluginDatasourceProvider = (*mockDatasources)(nil)
|
||||
type mockDatasources struct {
|
||||
}
|
||||
|
||||
// CreateDataSource implements PluginDatasourceProvider.
|
||||
func (m mockDatasources) CreateDataSource(ctx context.Context, ds *v0alpha1.GenericDataSource) (*v0alpha1.GenericDataSource, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// UpdateDataSource implements PluginDatasourceProvider.
|
||||
func (m mockDatasources) UpdateDataSource(ctx context.Context, ds *v0alpha1.GenericDataSource) (*v0alpha1.GenericDataSource, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Delete implements PluginDatasourceProvider.
|
||||
func (m mockDatasources) Delete(ctx context.Context, uid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDataSource implements PluginDatasourceProvider.
|
||||
func (m mockDatasources) GetDataSource(ctx context.Context, uid string) (*v0alpha1.GenericDataSource, error) {
|
||||
return nil, nil
|
||||
@@ -132,12 +147,12 @@ func (m mockDatasources) ListDataSource(ctx context.Context) (*v0alpha1.GenericD
|
||||
}
|
||||
|
||||
// Get gets a specific datasource (that the user in context can see)
|
||||
func (m mockDatasources) Get(ctx context.Context, uid string) (*v0alpha1.DataSourceConnection, error) {
|
||||
func (m mockDatasources) GetConnection(ctx context.Context, uid string) (*v0alpha1.DataSourceConnection, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// List lists all data sources the user in context can see
|
||||
func (m mockDatasources) List(ctx context.Context) (*v0alpha1.DataSourceConnectionList, error) {
|
||||
func (m mockDatasources) ListConnections(ctx context.Context) (*v0alpha1.DataSourceConnectionList, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user