diff --git a/pkg/envoy/yaml/applications.go b/pkg/envoy/yaml/applications.go new file mode 100644 index 000000000..c6ce8ed4b --- /dev/null +++ b/pkg/envoy/yaml/applications.go @@ -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 +//} diff --git a/pkg/envoy/yaml/applications_test.go b/pkg/envoy/yaml/applications_test.go new file mode 100644 index 000000000..bcde5bf6c --- /dev/null +++ b/pkg/envoy/yaml/applications_test.go @@ -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) + }) +} diff --git a/pkg/envoy/yaml/doc.go b/pkg/envoy/yaml/doc.go index f22d1277a..da9e5a692 100644 --- a/pkg/envoy/yaml/doc.go +++ b/pkg/envoy/yaml/doc.go @@ -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 }) diff --git a/pkg/envoy/yaml/testdata/application_1.yaml b/pkg/envoy/yaml/testdata/application_1.yaml new file mode 100644 index 000000000..72cd96c07 --- /dev/null +++ b/pkg/envoy/yaml/testdata/application_1.yaml @@ -0,0 +1,3 @@ +applications: + - one + - name: two