3
0

Add support for decoding applications from yaml def

This commit is contained in:
Denis Arh 2020-10-23 06:42:48 +02:00 committed by Tomaž Jerman
parent 0450b85dfc
commit 0c339758cd
4 changed files with 169 additions and 3 deletions

View File

@ -0,0 +1,111 @@
package yaml
import (
"github.com/cortezaproject/corteza-server/system/types"
"gopkg.in/yaml.v3"
)
type (
application struct {
// when application is at least partially defined
res *types.Application `yaml:",inline"`
// all known modules on a application
modules ComposeModuleSet
// module's RBAC rules
*rbacRules
}
applicationSet []*application
)
// UnmarshalYAML resolves set of application definitions, either sequence or map
//
// When resolving map, key is used as handle
// Also supporting { handle: name } definitions
//
func (wset *applicationSet) UnmarshalYAML(n *yaml.Node) error {
return eachSeq(n, func(v *yaml.Node) (err error) {
var (
wrap = &application{}
)
if v == nil {
return nodeErr(n, "malformed application definition")
}
wrap.res = &types.Application{Enabled: true}
if isKind(v, yaml.ScalarNode) {
if err = decodeScalar(v, "application name", &wrap.res.Name); err != nil {
return
}
*wset = append(*wset, wrap)
return
}
if err = v.Decode(&wrap.res); err != nil {
return
}
*wset = append(*wset, wrap)
return
})
}
//func (wset applicationSet) MarshalEnvoy() ([]envoy.Node, error) {
// // application usually have bunch of sub-resources defined
// nn := make([]envoy.Node, 0, len(wset)*10)
//
// for _, res := range wset {
// if tmp, err := res.MarshalEnvoy(); err != nil {
// return nil, err
// } else {
// nn = append(nn, tmp...)
// }
// }
//
// return nn, nil
//}
func (wrap *application) UnmarshalYAML(n *yaml.Node) (err error) {
if !isKind(n, yaml.MappingNode) {
return nodeErr(n, "application definition must be a map or scalar")
}
if wrap.res == nil {
wrap.res = &types.Application{}
}
if err = n.Decode(&wrap.res); err != nil {
return
}
if wrap.rbacRules, err = decodeResourceAccessControl(types.ApplicationRBACResource, n); err != nil {
return
}
return nil
}
//func (wrap application) MarshalEnvoy() ([]envoy.Node, error) {
// nn := make([]envoy.Node, 0, 1+len(wrap.modules))
// nn = append(nn, &envoy.ApplicationNode{Ns: wrap.res})
//
// if tmp, err := wrap.modules.MarshalEnvoy(); err != nil {
// return nil, err
// } else {
// nn = append(nn, tmp...)
// }
//
// // @todo rbac
//
// //if tmp, err := wrap.rules.MarshalEnvoy(); err != nil {
// // return nil, err
// //} else {
// // nn = append(nn, tmp...)
// //}
//
// return nn, nil
//}

View File

@ -0,0 +1,48 @@
package yaml
import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"testing"
)
func TestApplication_UnmarshalYAML(t *testing.T) {
var (
parseString = func(src string) (*application, error) {
w := &application{}
return w, yaml.Unmarshal([]byte(src), w)
}
)
t.Run("empty", func(t *testing.T) {
req := require.New(t)
w, err := parseString(``)
req.NoError(err)
req.NotNil(w)
req.Nil(w.res)
})
t.Run("simple name", func(t *testing.T) {
req := require.New(t)
w, err := parseString(`{ name: Test }`)
req.NoError(err)
req.NotNil(w)
req.NotNil(w.res)
req.NotEmpty(w.res.Name)
})
t.Run("application 1", func(t *testing.T) {
req := require.New(t)
doc, err := parseDocument("application_1")
req.NoError(err)
req.NotNil(doc)
req.Len(doc.applications, 2)
req.NotNil(doc.applications[0])
req.NotNil(doc.applications[1])
req.Equal("one", doc.applications[0].res.Name)
req.Equal("two", doc.applications[1].res.Name)
})
}

View File

@ -9,9 +9,10 @@ import (
type (
// Document defines the supported yaml structure
Document struct {
compose *compose
roles roleSet
users userSet
compose *compose
roles roleSet
users userSet
applications applicationSet
*rbacRules
}
)
@ -33,6 +34,9 @@ func (doc *Document) UnmarshalYAML(n *yaml.Node) (err error) {
case "users":
return v.Decode(&doc.users)
case "applications":
return v.Decode(&doc.applications)
}
return nil
})

View File

@ -0,0 +1,3 @@
applications:
- one
- name: two