From 4c64606434b4e8eaf549f9dae3e4f04cfbbcd019 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 25 Sep 2020 14:33:52 +0900 Subject: [PATCH] Create a node component from the network routes when the node component does not exist. --- config/config.go | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index 8b01cce6..95ebf8b5 100644 --- a/config/config.go +++ b/config/config.go @@ -262,6 +262,15 @@ func (cfg *Config) LoadRealNodesFile(path string) error { return cfg.LoadRealNodes(buf) } +func (cfg *Config) FindNode(name string) (*Node, error) { + for _, n := range cfg.Nodes { + if strings.EqualFold(n.FullName(), name) { + return n, nil + } + } + return nil, fmt.Errorf("node not found: %s", name) +} + func (cfg *Config) FindComponent(name string) (*Component, error) { var components []*Component switch sepCount(name) { @@ -299,6 +308,7 @@ func (cfg *Config) buildComponents() error { // global components for _, c := range gc.Keys() { + // create global component from network route cfg.globalComponents = append(cfg.globalComponents, &Component{ Name: c.(string), }) @@ -309,18 +319,29 @@ func (cfg *Config) buildComponents() error { cfg.nodeComponents = append(cfg.nodeComponents, n.Components...) } - belongTo := false for _, c := range nc.Keys() { - for _, n := range cfg.Nodes { - for _, com := range n.Components { - if strings.EqualFold(com.FullName(), c.(string)) { - belongTo = true - } + belongTo := false + splitted := sepSplit(c.(string)) + nodeName := splitted[0] + comName := splitted[1] + n, err := cfg.FindNode(nodeName) + if err != nil { + return fmt.Errorf("node '%s' not found: %s", nodeName, c) + } + for _, com := range n.Components { + if strings.EqualFold(com.FullName(), c.(string)) { + belongTo = true + break } } if !belongTo { - splitted := sepSplit(c.(string)) - return fmt.Errorf("node '%s' not found: %s", splitted[0], c) + // create node component from network route + component := &Component{ + Name: comName, + Node: n, + } + n.Components = append(n.Components, component) + cfg.nodeComponents = append(cfg.nodeComponents, component) } } @@ -332,6 +353,7 @@ func (cfg *Config) buildComponents() error { belongTo := false for _, cl := range cfg.Clusters() { if strings.EqualFold(cl.FullName(), clName) { + // create cluster component from network route com := &Component{ Cluster: cl, Name: comName,