hardcode custom spec

This commit is contained in:
Ryan McKinley
2025-07-02 23:08:20 -07:00
parent 4e3197a58f
commit 251b7b4b4e
8 changed files with 64 additions and 8 deletions
@@ -35,6 +35,10 @@ func (u Unstructured) OpenAPIDefinition() openapi.OpenAPIDefinition {
}
}
func (u *Unstructured) IsZero() bool {
return len(u.Object) == 0
}
func (u *Unstructured) UnstructuredContent() map[string]interface{} {
if u.Object == nil {
return make(map[string]interface{})
+2 -2
View File
@@ -47,14 +47,14 @@ type DataSourceSpec struct {
// Server URL
URL string `json:"url,omitempty"`
User string `json:"use,omitempty"`
User string `json:"user,omitempty"`
Database string `json:"database,omitempty"`
BasicAuth bool `json:"basicAuth,omitempty"`
BasicAuthUser string `json:"basicAuthUser,omitempty"`
WithCredentials bool `json:"withCredentials,omitempty"`
// Generic unstructured configuration settings
JsonData common.Unstructured `json:"jsonData,omitempty"`
JsonData common.Unstructured `json:"jsonData,omitzero"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -167,7 +167,7 @@ func schema_pkg_apis_datasource_v0alpha1_DataSourceSpec(ref common.ReferenceCall
Format: "",
},
},
"use": {
"user": {
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "",
@@ -204,7 +204,7 @@ func schema_pkg_apis_datasource_v0alpha1_DataSourceSpec(ref common.ReferenceCall
},
},
},
Required: []string{"title"},
Required: []string{"title", "jsonData"},
},
},
Dependencies: []string{
@@ -1,2 +1 @@
API rule violation: names_match,github.com/grafana/grafana/pkg/apis/datasource/v0alpha1,DataSourceSpec,User
API rule violation: streaming_list_type_json_tags,github.com/grafana/grafana/pkg/apis/datasource/v0alpha1,DataSourceList,ListMeta
@@ -40,6 +40,10 @@ func (s *ExposedSecureValue) DangerouslyExposeAndConsumeValue() string {
return string(tmp)
}
func (s ExposedSecureValue) IsZero() bool {
return s == "" // exclude from JSON
}
// String must not return the exposed secure value.
func (s ExposedSecureValue) String() string {
return redacted
+1 -1
View File
@@ -15,7 +15,7 @@ type InlineSecureValue struct {
UID string `json:"uid,omitempty"`
// Remove this value -- cascading delete to the secret service if necessary
Remove bool `json:"remove,omitempty"`
Remove bool `json:"remove,omitzero"`
}
// Collection of secure values
@@ -378,11 +378,13 @@ func schema_pkg_apis_secret_v0alpha1_InlineSecureValue(ref common.ReferenceCallb
"remove": {
SchemaProps: spec.SchemaProps{
Description: "Remove this value -- cascading delete to the secret service if necessary",
Default: false,
Type: []string{"boolean"},
Format: "",
},
},
},
Required: []string{"remove"},
},
},
}
+49 -2
View File
@@ -14,12 +14,14 @@ import (
genericapiserver "k8s.io/apiserver/pkg/server"
openapi "k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec"
"k8s.io/utils/strings/slices"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/apimachinery/utils"
datasourceV0 "github.com/grafana/grafana/pkg/apis/datasource/v0alpha1"
queryV0 "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
secretsV0 "github.com/grafana/grafana/pkg/apis/secret/v0alpha1"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
@@ -267,16 +269,61 @@ func (b *DataSourceAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op
// The root api URL
root := "/apis/" + b.datasourceResourceInfo.GroupVersion().String() + "/"
// Add queries to the request properties
// Add queries to the request properties
err := queryschema.AddQueriesToOpenAPI(queryschema.OASQueryOptions{
Swagger: oas,
PluginJSON: &b.pluginJSON,
QueryTypes: b.queryTypes,
Root: root,
QueryPath: "namespaces/{namespace}/connections/{name}/query",
QueryPath: "namespaces/{namespace}/datasources/{name}/query",
QueryDescription: fmt.Sprintf("Query the %s datasources", b.pluginJSON.Name),
})
ds, ok := oas.Components.Schemas["com.github.grafana.grafana.pkg.apis.datasource.v0alpha1.DataSource"]
if !ok {
return nil, fmt.Errorf("missing DS type")
}
// Spec
p := spec.MapProperty(nil)
p.Description = "HELLO!"
p.Required = []string{"x", "y"}
p.Example = map[string]any{
"x": 10,
"y": "hello",
"url": "http://xxxx",
}
oas.Components.Schemas["DataSourceSpec"] = p
ds.Properties["spec"] = spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef("#/components/schemas/DataSourceSpec"),
},
}
// Secure fields
p = spec.MapProperty(&spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef("#/components/schemas/com.github.grafana.grafana.pkg.apis.secret.v0alpha1.InlineSecureValue"),
},
})
p.Required = []string{"a", "b"}
oas.Components.Schemas["SecureFields"] = p
ds.Properties["secure"] = spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef("#/components/schemas/SecureFields"),
},
}
p.Example = secretsV0.InlineSecureValues{
"a": secretsV0.InlineSecureValue{
Create: "ExampleA",
},
"b": secretsV0.InlineSecureValue{
Create: "ExampleB",
},
}
ds.Properties["apiVersion"] = *spec.StringProperty().WithEnum(b.GetGroupVersion().String())
ds.Properties["kind"] = *spec.StringProperty().WithEnum("DataSource")
return oas, err
}