Add logic for resource translation import/export
This commit is contained in:
parent
c42cf298de
commit
7af889c164
24
server/automation/envoy/yaml_decode.gen.go
generated
24
server/automation/envoy/yaml_decode.gen.go
generated
@ -820,16 +820,17 @@ func unmarshalFlatRBACNode(n *yaml.Node, acc rbac.Access) (out envoyx.NodeSet, e
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
return out, y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
err = y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
langTag := systemTypes.Lang{Tag: language.Make(lang.Value)}
|
||||
|
||||
return y7s.EachMap(loc, func(res, kv *yaml.Node) error {
|
||||
return y7s.EachMap(kv, func(k, msg *yaml.Node) error {
|
||||
out = append(out, &envoyx.Node{
|
||||
Resource: &systemTypes.ResourceTranslation{
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
Resource: res.Value,
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
},
|
||||
// Providing resource type as plain text to reduce cross component references
|
||||
ResourceType: "corteza::system:resource-translation",
|
||||
@ -839,6 +840,21 @@ func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
})
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, o := range out {
|
||||
for _, r := range o.References {
|
||||
if r.Scope.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
o.Scope = r.Scope
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
@ -704,16 +704,17 @@ func unmarshalFlatRBACNode(n *yaml.Node, acc rbac.Access) (out envoyx.NodeSet, e
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
return out, y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
err = y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
langTag := systemTypes.Lang{Tag: language.Make(lang.Value)}
|
||||
|
||||
return y7s.EachMap(loc, func(res, kv *yaml.Node) error {
|
||||
return y7s.EachMap(kv, func(k, msg *yaml.Node) error {
|
||||
out = append(out, &envoyx.Node{
|
||||
Resource: &systemTypes.ResourceTranslation{
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
Resource: res.Value,
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
},
|
||||
// Providing resource type as plain text to reduce cross component references
|
||||
ResourceType: "corteza::system:resource-translation",
|
||||
@ -723,6 +724,21 @@ func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
})
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, o := range out {
|
||||
for _, r := range o.References {
|
||||
if r.Scope.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
o.Scope = r.Scope
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
24
server/compose/envoy/yaml_decode.gen.go
generated
24
server/compose/envoy/yaml_decode.gen.go
generated
@ -1882,16 +1882,17 @@ func unmarshalFlatRBACNode(n *yaml.Node, acc rbac.Access) (out envoyx.NodeSet, e
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
return out, y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
err = y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
langTag := systemTypes.Lang{Tag: language.Make(lang.Value)}
|
||||
|
||||
return y7s.EachMap(loc, func(res, kv *yaml.Node) error {
|
||||
return y7s.EachMap(kv, func(k, msg *yaml.Node) error {
|
||||
out = append(out, &envoyx.Node{
|
||||
Resource: &systemTypes.ResourceTranslation{
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
Resource: res.Value,
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
},
|
||||
// Providing resource type as plain text to reduce cross component references
|
||||
ResourceType: "corteza::system:resource-translation",
|
||||
@ -1901,6 +1902,21 @@ func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
})
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, o := range out {
|
||||
for _, r := range o.References {
|
||||
if r.Scope.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
o.Scope = r.Scope
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
90
server/pkg/envoyx/localeutils.go
Normal file
90
server/pkg/envoyx/localeutils.go
Normal file
@ -0,0 +1,90 @@
|
||||
package envoyx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/system/types"
|
||||
)
|
||||
|
||||
type (
|
||||
localer interface {
|
||||
ResourceTranslation() string
|
||||
}
|
||||
)
|
||||
|
||||
func ResourceTranslationsForNodes(tt types.ResourceTranslationSet, nn ...*Node) (translations NodeSet, err error) {
|
||||
translations = make(NodeSet, 0, len(tt)/2)
|
||||
dups := make(map[types.Lang]map[string]map[string]bool)
|
||||
|
||||
for _, n := range nn {
|
||||
c, ok := n.Resource.(localer)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Split up the path of this resource
|
||||
//
|
||||
// @todo move over to those generated functions
|
||||
resPath := splitResourcePath(c.ResourceTranslation())
|
||||
|
||||
// Find all of the translations that fall under this resource
|
||||
for _, r := range tt {
|
||||
|
||||
// Split up the path of the rule
|
||||
//
|
||||
// @todo move over to that generated function
|
||||
rulePath := splitResourcePath(r.Resource)
|
||||
// @note resource translations don't support wildcards
|
||||
if !isPathSubset(rulePath, resPath, false) {
|
||||
// Mismatch; skip
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if this translation has already been seen
|
||||
if dups[r.Lang] != nil && dups[r.Lang][r.Resource][r.K] {
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse the path so we can process it further
|
||||
// @todo make a generic function for RBAC rules and res. tr.
|
||||
_, res, path, err := ParseRule(r.Resource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get the refs
|
||||
rf := make(map[string]Ref, 2)
|
||||
for i, ref := range append(path, res) {
|
||||
// Whenever you'd use a wildcard, it will produce a nil so it
|
||||
// needs to be skipped
|
||||
if ref == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ref.Scope = n.Scope
|
||||
|
||||
// @todo make the thing not a pointer
|
||||
rf[fmt.Sprintf("Path.%d", i)] = *ref
|
||||
}
|
||||
|
||||
translations = append(translations, &Node{
|
||||
Resource: r,
|
||||
|
||||
ResourceType: types.ResourceTranslationResourceType,
|
||||
References: rf,
|
||||
Scope: n.Scope,
|
||||
})
|
||||
|
||||
// Update the dup checking index
|
||||
if dups[r.Lang] == nil {
|
||||
dups[r.Lang] = make(map[string]map[string]bool)
|
||||
}
|
||||
if dups[r.Lang][r.Resource] == nil {
|
||||
dups[r.Lang][r.Resource] = make(map[string]bool)
|
||||
}
|
||||
dups[r.Lang][r.Resource][r.K] = true
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@ -41,7 +41,7 @@ func RBACRulesForNodes(rr rbac.RuleSet, nn ...*Node) (rules NodeSet, err error)
|
||||
// @todo move over to that generated function
|
||||
rulePath := splitResourcePath(r.Resource)
|
||||
|
||||
if !isPathSubset(rulePath, resPath) {
|
||||
if !isPathSubset(rulePath, resPath, true) {
|
||||
// Mismatch; skip
|
||||
continue
|
||||
}
|
||||
@ -104,7 +104,7 @@ func splitResourcePath(p string) []string {
|
||||
return strings.Split(p, "/")[1:]
|
||||
}
|
||||
|
||||
func isPathSubset(rulePath, resPath []string) bool {
|
||||
func isPathSubset(rulePath, resPath []string, wildcards bool) bool {
|
||||
if len(rulePath) == 0 && len(resPath) == 0 {
|
||||
return true
|
||||
}
|
||||
@ -115,7 +115,7 @@ func isPathSubset(rulePath, resPath []string) bool {
|
||||
}
|
||||
|
||||
for i := 0; i < len(resPath); i++ {
|
||||
if rulePath[i] == "*" {
|
||||
if wildcards && rulePath[i] == "*" {
|
||||
// Rule matches everything from now on; if we got this far, we're good
|
||||
return true
|
||||
}
|
||||
|
||||
@ -14,6 +14,8 @@ func (e StoreEncoder) prepare(ctx context.Context, p envoyx.EncodeParams, s stor
|
||||
switch rt {
|
||||
case rbac.RuleResourceType:
|
||||
return e.prepareRbacRule(ctx, p, s, nn)
|
||||
case types.ResourceTranslationResourceType:
|
||||
return e.prepareResourceTranslation(ctx, p, s, nn)
|
||||
}
|
||||
|
||||
return
|
||||
@ -23,6 +25,8 @@ func (e StoreEncoder) encode(ctx context.Context, p envoyx.EncodeParams, s store
|
||||
switch rt {
|
||||
case rbac.RuleResourceType:
|
||||
return e.encodeRbacRules(ctx, p, s, nn, tree)
|
||||
case types.ResourceTranslationResourceType:
|
||||
return e.encodeResourceTranslations(ctx, p, s, nn, tree)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
83
server/system/envoy/store_encode_locale.go
Normal file
83
server/system/envoy/store_encode_locale.go
Normal file
@ -0,0 +1,83 @@
|
||||
package envoy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/envoyx"
|
||||
"github.com/cortezaproject/corteza/server/pkg/id"
|
||||
"github.com/cortezaproject/corteza/server/store"
|
||||
"github.com/cortezaproject/corteza/server/system/types"
|
||||
)
|
||||
|
||||
func (e StoreEncoder) prepareResourceTranslation(ctx context.Context, p envoyx.EncodeParams, s store.Storer, nn envoyx.NodeSet) (err error) {
|
||||
// @todo existing resource translations?
|
||||
for _, n := range nn {
|
||||
if n.Resource == nil {
|
||||
panic("unexpected state: cannot call prepareResourceTranslation with nodes without a defined Resource")
|
||||
}
|
||||
|
||||
res, ok := n.Resource.(*types.ResourceTranslation)
|
||||
if !ok {
|
||||
panic("unexpected resource type: node expecting type of ResourceTranslation")
|
||||
}
|
||||
|
||||
// Run expressions on the nodes
|
||||
err = e.runEvals(ctx, false, n)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res.ID = id.Next()
|
||||
|
||||
// @todo merge conflicts if we do existing assertion
|
||||
|
||||
n.Resource = res
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// encodeResourceTranslations encodes a set of resource into the database
|
||||
func (e StoreEncoder) encodeResourceTranslations(ctx context.Context, p envoyx.EncodeParams, s store.Storer, nn envoyx.NodeSet, tree envoyx.Traverser) (err error) {
|
||||
for _, n := range nn {
|
||||
err = e.encodeResourceTranslation(ctx, p, s, n, tree)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// encodeResourceTranslation encodes the resource into the database
|
||||
func (e StoreEncoder) encodeResourceTranslation(ctx context.Context, p envoyx.EncodeParams, s store.Storer, n *envoyx.Node, tree envoyx.Traverser) (err error) {
|
||||
// Grab dependency references
|
||||
var auxID uint64
|
||||
for fieldLabel, ref := range n.References {
|
||||
rn := tree.ParentForRef(n, ref)
|
||||
if rn == nil {
|
||||
err = fmt.Errorf("missing node for ref %v", ref)
|
||||
return
|
||||
}
|
||||
|
||||
auxID = rn.Resource.GetID()
|
||||
if auxID == 0 {
|
||||
err = fmt.Errorf("related resource doesn't provide an ID")
|
||||
return
|
||||
}
|
||||
|
||||
err = n.Resource.SetValue(fieldLabel, 0, auxID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Flush to the DB
|
||||
err = store.UpsertResourceTranslation(ctx, s, n.Resource.(*types.ResourceTranslation))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
24
server/system/envoy/yaml_decode.gen.go
generated
24
server/system/envoy/yaml_decode.gen.go
generated
@ -3309,16 +3309,17 @@ func unmarshalFlatRBACNode(n *yaml.Node, acc rbac.Access) (out envoyx.NodeSet, e
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
return out, y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
err = y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
langTag := systemTypes.Lang{Tag: language.Make(lang.Value)}
|
||||
|
||||
return y7s.EachMap(loc, func(res, kv *yaml.Node) error {
|
||||
return y7s.EachMap(kv, func(k, msg *yaml.Node) error {
|
||||
out = append(out, &envoyx.Node{
|
||||
Resource: &systemTypes.ResourceTranslation{
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
Resource: res.Value,
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
},
|
||||
// Providing resource type as plain text to reduce cross component references
|
||||
ResourceType: "corteza::system:resource-translation",
|
||||
@ -3328,6 +3329,21 @@ func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
})
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, o := range out {
|
||||
for _, r := range o.References {
|
||||
if r.Scope.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
o.Scope = r.Scope
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
@ -31,6 +31,75 @@ func (e YamlEncoder) encode(ctx context.Context, base *yaml.Node, p envoyx.Encod
|
||||
switch rt {
|
||||
case rbac.RuleResourceType:
|
||||
return e.encodeRbacRules(ctx, base, p, nodes, tt)
|
||||
case types.ResourceTranslationResourceType:
|
||||
return e.encodeResourceTranslations(ctx, base, p, nodes, tt)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e YamlEncoder) encodeResourceTranslations(ctx context.Context, base *yaml.Node, p envoyx.EncodeParams, nodes envoyx.NodeSet, tt envoyx.Traverser) (out *yaml.Node, err error) {
|
||||
err = e.resolveRulePathDeps(ctx, tt, nodes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
byLang := make(map[string][]*envoyx.Node)
|
||||
|
||||
for _, n := range nodes {
|
||||
byLang[n.Resource.(*types.ResourceTranslation).Lang.String()] = append(byLang[n.Resource.(*types.ResourceTranslation).Lang.String()], n)
|
||||
}
|
||||
|
||||
out = base
|
||||
|
||||
var aux *yaml.Node
|
||||
for lang, nodes := range byLang {
|
||||
aux, err = e.encodeResourceTranslationsByResource(p, nodes, tt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
out, err = y7s.AddMap(out, lang, aux)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return y7s.MakeMap("locale", out)
|
||||
}
|
||||
|
||||
func (e YamlEncoder) encodeResourceTranslationsByResource(p envoyx.EncodeParams, nodes envoyx.NodeSet, tt envoyx.Traverser) (out *yaml.Node, err error) {
|
||||
byResource := make(map[string][]*envoyx.Node)
|
||||
|
||||
for _, n := range nodes {
|
||||
byResource[n.Resource.(*types.ResourceTranslation).Resource] = append(byResource[n.Resource.(*types.ResourceTranslation).Resource], n)
|
||||
}
|
||||
|
||||
var aux *yaml.Node
|
||||
for resource, nodes := range byResource {
|
||||
aux, err = e.encodeResourceTranslationsKv(p, nodes, tt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
out, err = y7s.AddMap(out, resource, aux)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e YamlEncoder) encodeResourceTranslationsKv(p envoyx.EncodeParams, nodes envoyx.NodeSet, tt envoyx.Traverser) (out *yaml.Node, err error) {
|
||||
out, _ = y7s.MakeMap()
|
||||
|
||||
for _, n := range nodes {
|
||||
rt := n.Resource.(*types.ResourceTranslation)
|
||||
out, err = y7s.AddMap(out, rt.K, rt.Message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@ -11,6 +11,8 @@ resource_translation: {
|
||||
}
|
||||
|
||||
model: {
|
||||
defaultSetter: true
|
||||
|
||||
// lengths for the lang, resource fields are now a bit shorter
|
||||
// Reason for that is supported index length in MySQL
|
||||
attributes: {
|
||||
|
||||
3
server/system/types/getters_setters.gen.go
generated
3
server/system/types/getters_setters.gen.go
generated
@ -530,6 +530,9 @@ func (r *ResourceTranslation) SetValue(name string, pos uint, value any) (err er
|
||||
case "updatedBy", "UpdatedBy":
|
||||
return cast2.Uint64(value, &r.UpdatedBy)
|
||||
|
||||
default:
|
||||
return r.setValue(name, pos, value)
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -2,9 +2,11 @@ package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/cast2"
|
||||
"github.com/cortezaproject/corteza/server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza/server/pkg/locale"
|
||||
"golang.org/x/text/language"
|
||||
@ -72,6 +74,30 @@ func (a *ResourceTranslation) Compare(b *locale.ResourceTranslation) bool {
|
||||
return strings.EqualFold(a.K, b.Key) && a.Lang.Tag.String() == b.Lang
|
||||
}
|
||||
|
||||
func (rt *ResourceTranslation) setValue(name string, pos uint, v any) (err error) {
|
||||
pp := strings.Split(name, ".")
|
||||
|
||||
switch pp[0] {
|
||||
case "resource", "Resource", "Path", "path":
|
||||
ix, err := strconv.ParseUint(pp[1], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res := strings.Split(rt.Resource, "/")
|
||||
|
||||
aux := ""
|
||||
err = cast2.String(v, &aux)
|
||||
|
||||
// +1 bacause the first bit is the resource
|
||||
res[ix+1] = aux
|
||||
rt.Resource = strings.Join(res, "/")
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (set ResourceTranslationSet) New(bb locale.ResourceTranslationSet) (out ResourceTranslationSet) {
|
||||
outer:
|
||||
for _, b := range bb {
|
||||
@ -109,6 +135,24 @@ func (set ResourceTranslationSet) Old(bb locale.ResourceTranslationSet) (out [][
|
||||
return
|
||||
}
|
||||
|
||||
func (set ResourceTranslationSet) FilterLanguage(tag language.Tag) (out ResourceTranslationSet) {
|
||||
for _, a := range set {
|
||||
if a.Lang.Tag == tag {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (set ResourceTranslationSet) FilterKey(key string) (out ResourceTranslationSet) {
|
||||
for _, a := range set {
|
||||
if a.K == key {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FromLocale(ll locale.ResourceTranslationSet) (out ResourceTranslationSet) {
|
||||
for _, l := range ll {
|
||||
out = append(out, &ResourceTranslation{
|
||||
|
||||
@ -126,6 +126,7 @@ func cleanup(t *testing.T) {
|
||||
store.TruncateDalConnections(ctx, defaultStore),
|
||||
store.TruncateDalSensitivityLevels(ctx, defaultStore),
|
||||
store.TruncateRbacRules(ctx, defaultStore),
|
||||
store.TruncateResourceTranslations(ctx, defaultStore),
|
||||
|
||||
store.TruncateAutomationWorkflows(ctx, defaultStore),
|
||||
store.TruncateAutomationTriggers(ctx, defaultStore),
|
||||
|
||||
242
server/tests/envoy/res_tr_test.go
Normal file
242
server/tests/envoy/res_tr_test.go
Normal file
@ -0,0 +1,242 @@
|
||||
package envoy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/compose/types"
|
||||
"github.com/cortezaproject/corteza/server/pkg/envoyx"
|
||||
"github.com/cortezaproject/corteza/server/store"
|
||||
systemTypes "github.com/cortezaproject/corteza/server/system/types"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func TestResTrImportExport(t *testing.T) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
req = require.New(t)
|
||||
nodes envoyx.NodeSet
|
||||
gg *envoyx.DepGraph
|
||||
err error
|
||||
)
|
||||
|
||||
cleanup(t)
|
||||
|
||||
// The test
|
||||
//
|
||||
// * imports some YAML files
|
||||
// * checks the DB state
|
||||
// * exports the DB into a YAML
|
||||
// * clears the DB
|
||||
// * imports the YAML
|
||||
// * checks the DB state the same way as before
|
||||
//
|
||||
// The above outlined flow allows us to trivially check if the data is both
|
||||
// imported and exported correctly.
|
||||
//
|
||||
// The initial step could also manually populate the DB but the YAML import
|
||||
// is more convenient.
|
||||
|
||||
t.Run("initial import", func(t *testing.T) {
|
||||
t.Run("parse configs", func(t *testing.T) {
|
||||
nodes, err = defaultEnvoy.Decode(ctx, envoyx.DecodeParams{
|
||||
Type: envoyx.DecodeTypeURI,
|
||||
Params: map[string]any{
|
||||
"uri": "file://testdata/locale",
|
||||
},
|
||||
})
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
t.Run("bake", func(t *testing.T) {
|
||||
gg, err = defaultEnvoy.Bake(ctx, envoyx.EncodeParams{
|
||||
Type: envoyx.EncodeTypeStore,
|
||||
Params: map[string]any{
|
||||
"storer": defaultStore,
|
||||
"dal": defaultDal,
|
||||
},
|
||||
}, nodes...)
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
t.Run("import into DB", func(t *testing.T) {
|
||||
err = defaultEnvoy.Encode(ctx, envoyx.EncodeParams{
|
||||
Type: envoyx.EncodeTypeStore,
|
||||
Params: map[string]any{
|
||||
"storer": defaultStore,
|
||||
"dal": defaultDal,
|
||||
},
|
||||
}, gg)
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
req.NoError(err)
|
||||
assertLocaleState(ctx, t, defaultStore, req)
|
||||
})
|
||||
|
||||
// Prepare a temp file where we'll dump the YAML into
|
||||
auxFile, err := os.CreateTemp(os.TempDir(), "*.yaml")
|
||||
req.NoError(err)
|
||||
spew.Dump(auxFile.Name())
|
||||
// defer os.Remove(auxFile.Name())
|
||||
defer auxFile.Close()
|
||||
|
||||
var translations envoyx.NodeSet
|
||||
t.Run("export", func(t *testing.T) {
|
||||
t.Run("export from DB", func(t *testing.T) {
|
||||
nodes, err = defaultEnvoy.Decode(ctx, envoyx.DecodeParams{
|
||||
Type: envoyx.DecodeTypeStore,
|
||||
Params: map[string]any{
|
||||
"storer": defaultStore,
|
||||
},
|
||||
Filter: map[string]envoyx.ResourceFilter{
|
||||
types.ModuleResourceType: {},
|
||||
types.NamespaceResourceType: {},
|
||||
|
||||
systemTypes.RoleResourceType: {},
|
||||
},
|
||||
})
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
var tt systemTypes.ResourceTranslationSet
|
||||
t.Run("get all rules", func(t *testing.T) {
|
||||
tt, _, err = store.SearchResourceTranslations(ctx, defaultStore, systemTypes.ResourceTranslationFilter{})
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
t.Run("connect rules to resources", func(t *testing.T) {
|
||||
translations, err = envoyx.ResourceTranslationsForNodes(tt, nodes...)
|
||||
req.NoError(err)
|
||||
nodes = append(nodes, translations...)
|
||||
})
|
||||
|
||||
t.Run("bake", func(t *testing.T) {
|
||||
gg, err = defaultEnvoy.Bake(ctx, envoyx.EncodeParams{
|
||||
Type: envoyx.EncodeTypeStore,
|
||||
Params: map[string]any{
|
||||
"storer": defaultStore,
|
||||
"dal": defaultDal,
|
||||
},
|
||||
}, nodes...)
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
t.Run("write file", func(t *testing.T) {
|
||||
err = defaultEnvoy.Encode(ctx, envoyx.EncodeParams{
|
||||
Type: envoyx.EncodeTypeIo,
|
||||
Params: map[string]any{
|
||||
"writer": auxFile,
|
||||
},
|
||||
}, gg)
|
||||
req.NoError(err)
|
||||
})
|
||||
})
|
||||
|
||||
cleanup(t)
|
||||
|
||||
t.Run("second import", func(t *testing.T) {
|
||||
t.Run("yaml parse", func(t *testing.T) {
|
||||
nodes, err = defaultEnvoy.Decode(ctx, envoyx.DecodeParams{
|
||||
Type: envoyx.DecodeTypeURI,
|
||||
Params: map[string]any{
|
||||
"uri": fmt.Sprintf("file://%s", auxFile.Name()),
|
||||
},
|
||||
})
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
t.Run("bake", func(t *testing.T) {
|
||||
gg, err = defaultEnvoy.Bake(ctx, envoyx.EncodeParams{
|
||||
Type: envoyx.EncodeTypeStore,
|
||||
Params: map[string]any{
|
||||
"storer": defaultStore,
|
||||
"dal": defaultDal,
|
||||
},
|
||||
}, nodes...)
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
t.Run("run import", func(t *testing.T) {
|
||||
err = defaultEnvoy.Encode(ctx, envoyx.EncodeParams{
|
||||
Type: envoyx.EncodeTypeStore,
|
||||
Params: map[string]any{
|
||||
"storer": defaultStore,
|
||||
"dal": defaultDal,
|
||||
},
|
||||
}, gg)
|
||||
req.NoError(err)
|
||||
})
|
||||
|
||||
assertLocaleState(ctx, t, defaultStore, req)
|
||||
})
|
||||
}
|
||||
|
||||
func assertLocaleState(ctx context.Context, t *testing.T, s store.Storer, req *require.Assertions) {
|
||||
t.Run("check state", func(t *testing.T) {
|
||||
namespaces, _, err := store.SearchComposeNamespaces(ctx, defaultStore, types.NamespaceFilter{})
|
||||
req.NoError(err)
|
||||
ns1 := namespaces[0]
|
||||
|
||||
modules, _, err := store.SearchComposeModules(ctx, defaultStore, types.ModuleFilter{})
|
||||
req.NoError(err)
|
||||
mod1 := modules[0]
|
||||
|
||||
ll, _, err := store.SearchResourceTranslations(ctx, defaultStore, systemTypes.ResourceTranslationFilter{})
|
||||
req.NoError(err)
|
||||
|
||||
en := ll.FilterLanguage(language.English)
|
||||
compareResourceTranslations(req, *en.FilterKey("res_tr_1")[0], systemTypes.ResourceTranslation{
|
||||
Resource: fmt.Sprintf("corteza::compose:namespace/%d", ns1.ID),
|
||||
K: "res_tr_1",
|
||||
Message: "res_tr_1_text",
|
||||
})
|
||||
|
||||
compareResourceTranslations(req, *en.FilterKey("res_tr_2")[0], systemTypes.ResourceTranslation{
|
||||
Resource: fmt.Sprintf("corteza::compose:namespace/%d", ns1.ID),
|
||||
K: "res_tr_2",
|
||||
Message: "res_tr_2_text",
|
||||
})
|
||||
|
||||
compareResourceTranslations(req, *en.FilterKey("res_tr_3")[0], systemTypes.ResourceTranslation{
|
||||
Resource: fmt.Sprintf("corteza::compose:module/%d/%d", ns1.ID, mod1.ID),
|
||||
K: "res_tr_3",
|
||||
Message: "res_tr_3_text",
|
||||
})
|
||||
|
||||
de := ll.FilterLanguage(language.English)
|
||||
compareResourceTranslations(req, *de.FilterKey("res_tr_1")[0], systemTypes.ResourceTranslation{
|
||||
Resource: fmt.Sprintf("corteza::compose:namespace/%d", ns1.ID),
|
||||
K: "res_tr_1",
|
||||
Message: "res_tr_1_text",
|
||||
})
|
||||
|
||||
compareResourceTranslations(req, *de.FilterKey("res_tr_2")[0], systemTypes.ResourceTranslation{
|
||||
Resource: fmt.Sprintf("corteza::compose:namespace/%d", ns1.ID),
|
||||
K: "res_tr_2",
|
||||
Message: "res_tr_2_text",
|
||||
})
|
||||
|
||||
compareResourceTranslations(req, *de.FilterKey("res_tr_3")[0], systemTypes.ResourceTranslation{
|
||||
Resource: fmt.Sprintf("corteza::compose:module/%d/%d", ns1.ID, mod1.ID),
|
||||
K: "res_tr_3",
|
||||
Message: "res_tr_3_text",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func compareResourceTranslations(req *require.Assertions, a, b systemTypes.ResourceTranslation) {
|
||||
if a.Resource != b.Resource {
|
||||
req.FailNow("Resource missmatch")
|
||||
}
|
||||
if a.K != b.K {
|
||||
req.FailNow("K missmatch")
|
||||
}
|
||||
if a.Message != b.Message {
|
||||
req.FailNow("Message missmatch")
|
||||
}
|
||||
}
|
||||
20
server/tests/envoy/testdata/locale/locale.yaml
vendored
Normal file
20
server/tests/envoy/testdata/locale/locale.yaml
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
namespace:
|
||||
test_ns_1:
|
||||
name: Test Namespace 1
|
||||
modules:
|
||||
test_mod_1:
|
||||
name: Test Module 1
|
||||
|
||||
locale:
|
||||
en:
|
||||
corteza::compose:namespace/test_ns_1:
|
||||
res_tr_1: res_tr_1_text
|
||||
res_tr_2: res_tr_2_text
|
||||
corteza::compose:module/test_ns_1/test_mod_1:
|
||||
res_tr_3: res_tr_3_text
|
||||
de:
|
||||
corteza::compose:namespace/test_ns_1:
|
||||
res_tr_1: res_tr_1_text
|
||||
res_tr_2: res_tr_2_text
|
||||
corteza::compose:module/test_ns_1/test_mod_1:
|
||||
res_tr_3: res_tr_3_text
|
||||
Loading…
x
Reference in New Issue
Block a user