/
foundry_v3.go
63 lines (53 loc) · 1.46 KB
/
foundry_v3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package openapi
import (
"fmt"
"sync"
"github.com/getkin/kin-openapi/openapi3"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func NewFoundryFromSpecV3(spec []byte) (Foundry, error) {
loader := openapi3.NewLoader()
oapi3, err := loader.LoadFromData(spec)
if err != nil {
return nil, err
}
return &foapiv3{doc: oapi3}, nil
}
func SchemaToSpec(key string, crschema map[string]interface{}) map[string]interface{} {
schema := make(map[string]interface{})
for k, v := range crschema {
schema[k] = v
}
return map[string]interface{}{
"openapi": "3.0",
"info": map[string]interface{}{
"title": "CRD schema wrapper",
"version": "1.0.0",
},
"paths": map[string]interface{}{},
"components": map[string]interface{}{
"schemas": map[string]interface{}{
key: schema,
},
},
}
}
type foapiv3 struct {
doc *openapi3.T
gate sync.Mutex
typeCache sync.Map
}
func (f *foapiv3) GetTypeByGVK(_ schema.GroupVersionKind) (tftypes.Type, map[string]string, error) {
f.gate.Lock()
defer f.gate.Unlock()
var hints map[string]string = make(map[string]string)
ap := tftypes.AttributePath{}
sref := f.doc.Components.Schemas[""]
sch, err := resolveSchemaRef(sref, f.doc.Components.Schemas)
if err != nil {
return nil, hints, fmt.Errorf("failed to resolve schema: %s", err)
}
tftype, err := getTypeFromSchema(sch, 50, &(f.typeCache), f.doc.Components.Schemas, ap, hints)
return tftype, hints, err
}