-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschemagen.go
42 lines (36 loc) · 1014 Bytes
/
schemagen.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
package main
import (
"encoding/json"
"fmt"
"github.com/juju/juju/apiserver"
"github.com/juju/rpcreflect"
"github.com/juju/schemagen/jsonschema"
)
// FacadeSchema describes the jsonschema of the RPC interface for a Facacde
type FacadeSchema struct {
Name string
Version int
Schema *jsonschema.Schema
}
// DescribeFacadeSchemas returns the list of available Facades and their Versions
func DescribeFacadeSchemas() []FacadeSchema {
registry := apiserver.AllFacades()
facades := registry.List()
result := make([]FacadeSchema, len(facades))
for i, facade := range facades {
result[i].Name = facade.Name
version := facade.Versions[len(facade.Versions)-1]
result[i].Version = version
kind, err := registry.GetType(facade.Name, version)
if err == nil {
objtype := rpcreflect.ObjTypeOf(kind)
result[i].Schema = jsonschema.ReflectFromObjType(objtype)
}
}
return result
}
func main() {
s := DescribeFacadeSchemas()
b, _ := json.MarshalIndent(s, "", " ")
fmt.Printf("%s\n", b)
}