Implement base envoy resources
Compose namespace, module, record set.
This commit is contained in:
parent
e17d63f05f
commit
c19ef384e8
@ -1,85 +0,0 @@
|
||||
package envoy
|
||||
|
||||
type (
|
||||
Resource interface {
|
||||
Identifiers() ResourceIdentifiers
|
||||
ResourceType() string
|
||||
Refs() NodeRefSet
|
||||
}
|
||||
|
||||
NodeRefSet []*NodeRef
|
||||
NodeRef struct {
|
||||
// @todo check with Denis regarding strings here (the cdocs comment)
|
||||
ResourceType string
|
||||
Identifiers ResourceIdentifiers
|
||||
}
|
||||
|
||||
ResourceIdentifiers map[string]bool
|
||||
|
||||
nodeIndex map[string]map[string]*node
|
||||
)
|
||||
|
||||
func (ri ResourceIdentifiers) Add(ii ...string) ResourceIdentifiers {
|
||||
for _, i := range ii {
|
||||
if len(i) > 0 {
|
||||
ri[i] = true
|
||||
}
|
||||
}
|
||||
|
||||
return ri
|
||||
}
|
||||
|
||||
func (ri ResourceIdentifiers) Remove(ii ...string) ResourceIdentifiers {
|
||||
for _, i := range ii {
|
||||
delete(ri, i)
|
||||
}
|
||||
|
||||
return ri
|
||||
}
|
||||
|
||||
func (ri ResourceIdentifiers) HasAny(ii ...string) bool {
|
||||
for _, i := range ii {
|
||||
if ri[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (ri ResourceIdentifiers) StringSlice() []string {
|
||||
ss := make([]string, 0, len(ri))
|
||||
for k := range ri {
|
||||
ss = append(ss, k)
|
||||
}
|
||||
return ss
|
||||
}
|
||||
|
||||
func (ri nodeIndex) Add(nn ...*node) {
|
||||
for _, n := range nn {
|
||||
rt := n.res.ResourceType()
|
||||
if _, has := ri[rt]; !has {
|
||||
ri[rt] = make(map[string]*node)
|
||||
}
|
||||
|
||||
for i := range n.res.Identifiers() {
|
||||
ri[rt][i] = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ri nodeIndex) GetRef(ref *NodeRef) *node {
|
||||
res, has := ri[ref.ResourceType]
|
||||
if !has {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := range ref.Identifiers {
|
||||
r, has := res[i]
|
||||
if has {
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
47
pkg/envoy/resource/base.go
Normal file
47
pkg/envoy/resource/base.go
Normal file
@ -0,0 +1,47 @@
|
||||
package resource
|
||||
|
||||
type (
|
||||
base struct {
|
||||
rt string
|
||||
ii Identifiers
|
||||
rr RefSet
|
||||
}
|
||||
)
|
||||
|
||||
// State management methods
|
||||
|
||||
// AddIdentifier adds a set of identifiers to the current resource
|
||||
func (t *base) AddIdentifier(ss ...string) {
|
||||
if t.ii == nil {
|
||||
t.ii = make(Identifiers)
|
||||
}
|
||||
|
||||
t.ii.Add(ss...)
|
||||
}
|
||||
|
||||
// AddRef adds a new reference to the current resource
|
||||
func (t *base) AddRef(rt string, ii ...string) {
|
||||
if t.rr == nil {
|
||||
t.rr = make(RefSet, 0, 10)
|
||||
}
|
||||
|
||||
ref := &Ref{ResourceType: rt, Identifiers: Identifiers{}.Add(ii...)}
|
||||
t.rr = append(t.rr, ref)
|
||||
}
|
||||
|
||||
// SetResourceType sets the resource type of the current resource struct
|
||||
func (t *base) SetResourceType(rt string) {
|
||||
t.rt = rt
|
||||
}
|
||||
|
||||
// Resource interface methods
|
||||
|
||||
func (t *base) Identifiers() Identifiers {
|
||||
return t.ii
|
||||
}
|
||||
func (t *base) ResourceType() string {
|
||||
return t.rt
|
||||
}
|
||||
func (t *base) Refs() RefSet {
|
||||
return t.rr
|
||||
}
|
||||
56
pkg/envoy/resource/compose_module.go
Normal file
56
pkg/envoy/resource/compose_module.go
Normal file
@ -0,0 +1,56 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
)
|
||||
|
||||
type (
|
||||
ComposeModule struct {
|
||||
*base
|
||||
Res *types.Module
|
||||
}
|
||||
)
|
||||
|
||||
func NewComposeModule(mod *types.Module) *ComposeModule {
|
||||
r := &ComposeModule{base: &base{}}
|
||||
r.SetResourceType("compose:module")
|
||||
r.Res = mod
|
||||
|
||||
if mod.Handle != "" {
|
||||
r.AddIdentifier(mod.Handle)
|
||||
}
|
||||
if mod.Name != "" {
|
||||
r.AddIdentifier(mod.Name)
|
||||
}
|
||||
if mod.ID > 0 {
|
||||
r.AddIdentifier(strconv.FormatUint(mod.ID, 10))
|
||||
}
|
||||
|
||||
// Field deps.
|
||||
for _, f := range mod.Fields {
|
||||
switch f.Kind {
|
||||
case "Record":
|
||||
refM := f.Options.String("module")
|
||||
if refM != "" && refM != "0" {
|
||||
r.AddRef("compose:module", refM)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (m *ComposeModule) SearchQuery() types.ModuleFilter {
|
||||
f := types.ModuleFilter{Query: ""}
|
||||
|
||||
f.Handle = m.Res.Handle
|
||||
f.Name = m.Res.Name
|
||||
if m.Res.ID > 0 {
|
||||
f.Query = fmt.Sprintf("moduleID=%d", m.Res.ID)
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
44
pkg/envoy/resource/compose_namespace.go
Normal file
44
pkg/envoy/resource/compose_namespace.go
Normal file
@ -0,0 +1,44 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
)
|
||||
|
||||
type (
|
||||
ComposeNamespace struct {
|
||||
*base
|
||||
Res *types.Namespace
|
||||
}
|
||||
)
|
||||
|
||||
func NewComposeNamespace(ns *types.Namespace) *ComposeNamespace {
|
||||
r := &ComposeNamespace{base: &base{}}
|
||||
r.SetResourceType("compose:namespace")
|
||||
r.Res = ns
|
||||
|
||||
if ns.Slug != "" {
|
||||
r.AddIdentifier(ns.Slug)
|
||||
}
|
||||
if ns.Name != "" {
|
||||
r.AddIdentifier(ns.Name)
|
||||
}
|
||||
if ns.ID > 0 {
|
||||
r.AddIdentifier(strconv.FormatUint(ns.ID, 10))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (m *ComposeNamespace) SearchQuery() types.NamespaceFilter {
|
||||
f := types.NamespaceFilter{Query: ""}
|
||||
|
||||
f.Slug = m.Res.Slug
|
||||
f.Name = m.Res.Name
|
||||
if m.Res.ID > 0 {
|
||||
f.Query = fmt.Sprintf("namespaceID=%d", m.Res.ID)
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
23
pkg/envoy/resource/compose_record_set.go
Normal file
23
pkg/envoy/resource/compose_record_set.go
Normal file
@ -0,0 +1,23 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
)
|
||||
|
||||
type (
|
||||
ComposeRecordSet struct {
|
||||
*base
|
||||
|
||||
Walk Walker
|
||||
}
|
||||
|
||||
Walker func(r *types.Record) error
|
||||
)
|
||||
|
||||
// @todo add record provider
|
||||
func NewComposeRecordSet() *ComposeRecordSet {
|
||||
r := &ComposeRecordSet{base: &base{}}
|
||||
r.SetResourceType("compose:record")
|
||||
|
||||
return r
|
||||
}
|
||||
67
pkg/envoy/resource/types.go
Normal file
67
pkg/envoy/resource/types.go
Normal file
@ -0,0 +1,67 @@
|
||||
package resource
|
||||
|
||||
type (
|
||||
Interface interface {
|
||||
Identifiers() Identifiers
|
||||
ResourceType() string
|
||||
Refs() RefSet
|
||||
}
|
||||
|
||||
RefSet []*Ref
|
||||
Ref struct {
|
||||
// @todo check with Denis regarding strings here (the cdocs comment)
|
||||
// @todo should this become node type instead?
|
||||
ResourceType string
|
||||
Identifiers Identifiers
|
||||
}
|
||||
|
||||
Identifiers map[string]bool
|
||||
)
|
||||
|
||||
func (ri Identifiers) Add(ii ...string) Identifiers {
|
||||
for _, i := range ii {
|
||||
if len(i) > 0 {
|
||||
ri[i] = true
|
||||
}
|
||||
}
|
||||
|
||||
return ri
|
||||
}
|
||||
|
||||
func (ri Identifiers) Remove(ii ...string) Identifiers {
|
||||
for _, i := range ii {
|
||||
delete(ri, i)
|
||||
}
|
||||
|
||||
return ri
|
||||
}
|
||||
|
||||
func (ri Identifiers) HasAny(ii ...string) bool {
|
||||
for _, i := range ii {
|
||||
if ri[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (ri Identifiers) StringSlice() []string {
|
||||
ss := make([]string, 0, len(ri))
|
||||
for k := range ri {
|
||||
ss = append(ss, k)
|
||||
}
|
||||
return ss
|
||||
}
|
||||
|
||||
func (ss RefSet) FilterByResourceType(rt string) RefSet {
|
||||
rr := make(RefSet, 0, len(ss))
|
||||
|
||||
for _, s := range ss {
|
||||
if s.ResourceType == rt {
|
||||
rr = append(rr, s)
|
||||
}
|
||||
}
|
||||
|
||||
return rr
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user