|
| 1 | +package v0alpha1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + |
| 6 | + "k8s.io/kube-openapi/pkg/common" |
| 7 | + spec "k8s.io/kube-openapi/pkg/validation/spec" |
| 8 | +) |
| 9 | + |
| 10 | +//go:embed query.schema.json query.definition.schema.json |
| 11 | +var f embed.FS |
| 12 | + |
| 13 | +func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition { |
| 14 | + return map[string]common.OpenAPIDefinition{ |
| 15 | + "github.com/grafana/grafana-plugin-sdk-go/backend.DataResponse": schemaDataResponse(ref), |
| 16 | + "github.com/grafana/grafana-plugin-sdk-go/data.Frame": schemaDataFrame(ref), |
| 17 | + "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1.DataQuery": schemaDataQuery(ref), |
| 18 | + "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1.QueryTypeDefinitionSpec": schemaQueryTypeDefinitionSpec(ref), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Individual response |
| 23 | +func schemaDataResponse(_ common.ReferenceCallback) common.OpenAPIDefinition { |
| 24 | + return common.OpenAPIDefinition{ |
| 25 | + Schema: spec.Schema{ |
| 26 | + SchemaProps: spec.SchemaProps{ |
| 27 | + Description: "todo... improve schema", |
| 28 | + Type: []string{"object"}, |
| 29 | + AdditionalProperties: &spec.SchemaOrBool{Allows: true}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func schemaDataFrame(_ common.ReferenceCallback) common.OpenAPIDefinition { |
| 36 | + return common.OpenAPIDefinition{ |
| 37 | + Schema: spec.Schema{ |
| 38 | + SchemaProps: spec.SchemaProps{ |
| 39 | + Description: "any object for now", |
| 40 | + Type: []string{"object"}, |
| 41 | + Properties: map[string]spec.Schema{}, |
| 42 | + AdditionalProperties: &spec.SchemaOrBool{Allows: true}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func schemaQueryTypeDefinitionSpec(_ common.ReferenceCallback) common.OpenAPIDefinition { |
| 49 | + s, _ := loadSchema("query.definition.schema.json") |
| 50 | + if s == nil { |
| 51 | + s = &spec.Schema{} |
| 52 | + } |
| 53 | + return common.OpenAPIDefinition{ |
| 54 | + Schema: *s, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func schemaDataQuery(_ common.ReferenceCallback) common.OpenAPIDefinition { |
| 59 | + s, _ := DataQuerySchema() |
| 60 | + if s == nil { |
| 61 | + s = &spec.Schema{} |
| 62 | + } |
| 63 | + s.SchemaProps.Type = []string{"object"} |
| 64 | + s.SchemaProps.AdditionalProperties = &spec.SchemaOrBool{Allows: true} |
| 65 | + return common.OpenAPIDefinition{Schema: *s} |
| 66 | +} |
| 67 | + |
| 68 | +// Get the cached feature list (exposed as a k8s resource) |
| 69 | +func DataQuerySchema() (*spec.Schema, error) { |
| 70 | + return loadSchema("query.schema.json") |
| 71 | +} |
| 72 | + |
| 73 | +// Get the cached feature list (exposed as a k8s resource) |
| 74 | +func loadSchema(path string) (*spec.Schema, error) { |
| 75 | + body, err := f.ReadFile(path) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + s := &spec.Schema{} |
| 80 | + err = s.UnmarshalJSON(body) |
| 81 | + return s, err |
| 82 | +} |
0 commit comments