diff --git a/internal/app/wwctl/container/exec/child/main.go b/internal/app/wwctl/container/exec/child/main.go index bfd500b4..6fd00eb7 100644 --- a/internal/app/wwctl/container/exec/child/main.go +++ b/internal/app/wwctl/container/exec/child/main.go @@ -102,7 +102,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) { if err != nil { return fmt.Errorf("could not get node list: %s", err) } - nodes = node.FilterByName(nodes, []string{nodename}) + nodes = node.FilterNodeListByName(nodes, []string{nodename}) if len(nodes) != 1 { return fmt.Errorf("no single node idendified with %s", nodename) } diff --git a/internal/app/wwctl/node/add/main_test.go b/internal/app/wwctl/node/add/main_test.go index ab19e7d2..01a2811c 100644 --- a/internal/app/wwctl/node/add/main_test.go +++ b/internal/app/wwctl/node/add/main_test.go @@ -296,7 +296,6 @@ nodes: path: /var `}, } - // wwlog.SetLogLevel(wwlog.DEBUG) warewulfd.SetNoDaemon() for _, tt := range tests { env := testenv.New(t) diff --git a/internal/app/wwctl/node/console/main.go b/internal/app/wwctl/node/console/main.go index 5d4686da..bbc2d319 100644 --- a/internal/app/wwctl/node/console/main.go +++ b/internal/app/wwctl/node/console/main.go @@ -27,7 +27,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { args = hostlist.Expand(args) if len(args) > 0 { - nodes = node.FilterByName(nodes, args) + nodes = node.FilterNodeListByName(nodes, args) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/node/list/main_test.go b/internal/app/wwctl/node/list/main_test.go index 389410c8..19517eb6 100644 --- a/internal/app/wwctl/node/list/main_test.go +++ b/internal/app/wwctl/node/list/main_test.go @@ -364,7 +364,6 @@ n01 RuntimeOverlay p1+nop1,rop1,rop2 t.Run(tt.name, func(t *testing.T) { baseCmd := GetCommand() baseCmd.SetArgs(tt.args) - //verifyOutput(t, baseCmd, tt.stdout) buf := new(bytes.Buffer) wwlog.SetLogWriter(buf) wwlog.SetLogWriterErr(buf) diff --git a/internal/app/wwctl/node/sensors/main.go b/internal/app/wwctl/node/sensors/main.go index 08f31d40..00e382d4 100644 --- a/internal/app/wwctl/node/sensors/main.go +++ b/internal/app/wwctl/node/sensors/main.go @@ -28,7 +28,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { args = hostlist.Expand(args) if len(args) > 0 { - nodes = node.FilterByName(nodes, args) + nodes = node.FilterNodeListByName(nodes, args) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/overlay/build/main.go b/internal/app/wwctl/overlay/build/main.go index 71e24b3c..8335ebd4 100644 --- a/internal/app/wwctl/overlay/build/main.go +++ b/internal/app/wwctl/overlay/build/main.go @@ -29,7 +29,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { if len(args) > 0 { args = hostlist.Expand(args) - db = node.FilterByName(db, args) + db = node.FilterNodeListByName(db, args) if len(db) < len(args) { return errors.New("failed to find nodes") diff --git a/internal/app/wwctl/power/cycle/main.go b/internal/app/wwctl/power/cycle/main.go index 18894b3b..0018f857 100644 --- a/internal/app/wwctl/power/cycle/main.go +++ b/internal/app/wwctl/power/cycle/main.go @@ -26,7 +26,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/power/off/main.go b/internal/app/wwctl/power/off/main.go index 28bf4924..278c08f2 100644 --- a/internal/app/wwctl/power/off/main.go +++ b/internal/app/wwctl/power/off/main.go @@ -26,7 +26,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/power/on/main.go b/internal/app/wwctl/power/on/main.go index c660d936..9bf926d5 100644 --- a/internal/app/wwctl/power/on/main.go +++ b/internal/app/wwctl/power/on/main.go @@ -26,7 +26,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/power/reset/main.go b/internal/app/wwctl/power/reset/main.go index 1bbeb87b..b3550944 100644 --- a/internal/app/wwctl/power/reset/main.go +++ b/internal/app/wwctl/power/reset/main.go @@ -26,7 +26,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/power/soft/main.go b/internal/app/wwctl/power/soft/main.go index 52e785c6..9eede24a 100644 --- a/internal/app/wwctl/power/soft/main.go +++ b/internal/app/wwctl/power/soft/main.go @@ -26,7 +26,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/power/status/main.go b/internal/app/wwctl/power/status/main.go index af95dc20..6bfe4d15 100644 --- a/internal/app/wwctl/power/status/main.go +++ b/internal/app/wwctl/power/status/main.go @@ -26,7 +26,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/app/wwctl/profile/list/main_test.go b/internal/app/wwctl/profile/list/main_test.go index 203a1d58..a919c5c1 100644 --- a/internal/app/wwctl/profile/list/main_test.go +++ b/internal/app/wwctl/profile/list/main_test.go @@ -2,11 +2,14 @@ package list import ( "bytes" - "strings" + "io" + "os" "testing" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" - "github.com/warewulf/warewulf/internal/pkg/testenv" + warewulfconf "github.com/warewulf/warewulf/internal/pkg/config" + "github.com/warewulf/warewulf/internal/pkg/node" "github.com/warewulf/warewulf/internal/pkg/warewulfd" "github.com/warewulf/warewulf/internal/pkg/wwlog" ) @@ -30,13 +33,127 @@ nodes: n01: profiles: - default -`}, +`, + }, { name: "profile list returns multiple profiles", args: []string{"default,test"}, stdout: `PROFILE NAME COMMENT/DESCRIPTION - default - test`, + default + test`, + inDb: `WW_INTERNAL: 45 +nodeprofiles: + default: {} + test: {} +nodes: + n01: + profiles: + - default +`, + }, + { + name: "profile list returns one profile", + args: []string{"test,"}, + stdout: `PROFILE NAME COMMENT/DESCRIPTION + test`, + inDb: `WW_INTERNAL: 45 +nodeprofiles: + default: {} + test: {} +nodes: + n01: + profiles: + - default +`, + }, + { + name: "profile list returns all profiles", + args: []string{","}, + stdout: `PROFILE NAME COMMENT/DESCRIPTION + default + test`, + inDb: `WW_INTERNAL: 45 +nodeprofiles: + default: {} + test: {} +nodes: + n01: + profiles: + - default +`, + }, + } + + conf_yml := `WW_INTERNAL: 0` + tempWarewulfConf, warewulfConfErr := os.CreateTemp("", "warewulf.conf-") + assert.NoError(t, warewulfConfErr) + defer os.Remove(tempWarewulfConf.Name()) + _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) + assert.NoError(t, warewulfConfErr) + assert.NoError(t, tempWarewulfConf.Sync()) + assert.NoError(t, warewulfconf.New().Read(tempWarewulfConf.Name())) + + tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") + assert.NoError(t, nodesConfErr) + defer os.Remove(tempNodeConf.Name()) + node.ConfigFile = tempNodeConf.Name() + warewulfd.SetNoDaemon() + for _, tt := range tests { + var err error + _, err = tempNodeConf.Seek(0, 0) + assert.NoError(t, err) + assert.NoError(t, tempNodeConf.Truncate(0)) + _, err = tempNodeConf.Write([]byte(tt.inDb)) + assert.NoError(t, err) + assert.NoError(t, tempNodeConf.Sync()) + assert.NoError(t, err) + + t.Run(tt.name, func(t *testing.T) { + baseCmd := GetCommand() + baseCmd.SetArgs(tt.args) + verifyOutput(t, baseCmd, tt.stdout) + }) + } +} + +func TestListMultipleFormats(t *testing.T) { + t.Skip("temporally skip this test") + tests := []struct { + name string + args []string + output []string + inDb string + }{ + { + name: "single profile list yaml output", + args: []string{"-y"}, + output: []string{"default:\n AssetKey: \"\"\n ClusterName: \"\"\n Comment: \"\"\n ContainerName: \"\"\n Discoverable: \"\"\n Disks: {}\n FileSystems: {}\n Grub: \"\"\n Id: |\n Source: explicit\n Value: default\n Init: \"\"\n Ipmi:\n EscapeChar: \"\"\n Gateway: \"\"\n Interface: \"\"\n Ipaddr: \"\"\n Netmask: \"\"\n Password: \"\"\n Port: \"\"\n Tags: null\n UserName: \"\"\n Write: \"\"\n Ipxe: \"\"\n Kernel:\n Args: \"\"\n Override: \"\"\n NetDevs: {}\n PrimaryNetDev: \"\"\n Profiles: \"\"\n Root: \"\"\n RuntimeOverlay: \"\"\n SystemOverlay: \"\"\n Tags: {}\n"}, + inDb: `WW_INTERNAL: 43 +nodeprofiles: + default: {} +nodes: + n01: + profiles: + - default +`, + }, + { + name: "single profile list json output", + args: []string{"-j"}, + output: []string{"{\"default\":{\"Id\":\"Source: explicit\\nValue: default\\n\",\"Comment\":\"\",\"ClusterName\":\"\",\"ContainerName\":\"\",\"Ipxe\":\"\",\"Grub\":\"\",\"RuntimeOverlay\":\"\",\"SystemOverlay\":\"\",\"Root\":\"\",\"Discoverable\":\"\",\"Init\":\"\",\"AssetKey\":\"\",\"Kernel\":{\"Override\":\"\",\"Args\":\"\"},\"Ipmi\":{\"Ipaddr\":\"\",\"Netmask\":\"\",\"Port\":\"\",\"Gateway\":\"\",\"UserName\":\"\",\"Password\":\"\",\"Interface\":\"\",\"EscapeChar\":\"\",\"Write\":\"\",\"Tags\":null},\"Profiles\":\"\",\"PrimaryNetDev\":\"\",\"NetDevs\":{},\"Tags\":{},\"Disks\":{},\"FileSystems\":{}}}\n"}, + inDb: `WW_INTERNAL: 43 +nodeprofiles: + default: {} +nodes: + n01: + profiles: + - default +`, + }, + { + name: "multiple profiles list yaml output", + args: []string{"-y"}, + output: []string{"default", "test"}, inDb: `WW_INTERNAL: 43 nodeprofiles: default: {} @@ -46,57 +163,83 @@ nodes: profiles: - default `, - }, /* - { - name: "profile list returns one profiles", - args: []string{"test,"}, - stdout: `PROFILE NAME COMMENT/DESCRIPTION - test --`, - inDb: `WW_INTERNAL: 43 - nodeprofiles: - default: {} - test: {} - nodes: - n01: - profiles: - - default - `, - }, - { - name: "profile list returns all profiles", - args: []string{","}, - stdout: `PROFILE NAME COMMENT/DESCRIPTION - default -- - test --`, - inDb: `WW_INTERNAL: 43 - nodeprofiles: - default: {} - test: {} - nodes: - n01: - profiles: - - default - `, - },*/ + }, + { + name: "multiple profiles list json output", + args: []string{"-j"}, + output: []string{"default", "test"}, + inDb: `WW_INTERNAL: 43 +nodeprofiles: + default: {} + test: {} +nodes: + n01: + profiles: + - default +`, + }, } + conf_yml := `WW_INTERNAL: 0` + tempWarewulfConf, warewulfConfErr := os.CreateTemp("", "warewulf.conf-") + assert.NoError(t, warewulfConfErr) + defer os.Remove(tempWarewulfConf.Name()) + _, warewulfConfErr = tempWarewulfConf.Write([]byte(conf_yml)) + assert.NoError(t, warewulfConfErr) + assert.NoError(t, tempWarewulfConf.Sync()) + assert.NoError(t, warewulfconf.New().Read(tempWarewulfConf.Name())) + + tempNodeConf, nodesConfErr := os.CreateTemp("", "nodes.conf-") + assert.NoError(t, nodesConfErr) + defer os.Remove(tempNodeConf.Name()) + node.ConfigFile = tempNodeConf.Name() warewulfd.SetNoDaemon() - //wwlog.SetLogLevel(wwlog.DEBUG) for _, tt := range tests { - env := testenv.New(t) - env.WriteFile(t, "etc/warewulf/nodes.conf", - tt.inDb) + var err error + _, err = tempNodeConf.Seek(0, 0) + assert.NoError(t, err) + assert.NoError(t, tempNodeConf.Truncate(0)) + _, err = tempNodeConf.Write([]byte(tt.inDb)) + assert.NoError(t, err) + assert.NoError(t, tempNodeConf.Sync()) + t.Run(tt.name, func(t *testing.T) { baseCmd := GetCommand() baseCmd.SetArgs(tt.args) + buf := new(bytes.Buffer) baseCmd.SetOut(buf) baseCmd.SetErr(buf) wwlog.SetLogWriter(buf) - assert.NoError(t, baseCmd.Execute()) - assert.Equal(t, - strings.Join(strings.Fields(tt.stdout), ""), - strings.Join(strings.Fields(buf.String()), "")) + err := baseCmd.Execute() + assert.NoError(t, err) + for _, output := range tt.output { + assert.Contains(t, buf.String(), output) + } }) } } + +func verifyOutput(t *testing.T, baseCmd *cobra.Command, content string) { + stdoutR, stdoutW, _ := os.Pipe() + oriout := os.Stdout + os.Stdout = stdoutW + wwlog.SetLogWriter(os.Stdout) + baseCmd.SetOut(os.Stdout) + baseCmd.SetErr(os.Stdout) + err := baseCmd.Execute() + assert.NoError(t, err) + + stdoutC := make(chan string) + go func() { + var buf bytes.Buffer + _, _ = io.Copy(&buf, stdoutR) + stdoutC <- buf.String() + }() + stdoutW.Close() + os.Stdout = oriout + + stdout := <-stdoutC + assert.NotEmpty(t, stdout) + assert.Contains(t, stdout, content) +} diff --git a/internal/app/wwctl/ssh/main.go b/internal/app/wwctl/ssh/main.go index e91302a5..cf743e63 100644 --- a/internal/app/wwctl/ssh/main.go +++ b/internal/app/wwctl/ssh/main.go @@ -32,7 +32,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if len(args) > 0 { - nodes = node.FilterByName(nodes, hostlist.Expand(args)) + nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args)) } else { //nolint:errcheck cmd.Usage() diff --git a/internal/pkg/api/node/delete.go b/internal/pkg/api/node/delete.go index bf17217e..e7f2f222 100644 --- a/internal/pkg/api/node/delete.go +++ b/internal/pkg/api/node/delete.go @@ -37,7 +37,6 @@ func NodeDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) { if err != nil { wwlog.Error("%s", err) } else { - //count++ wwlog.Verbose("Deleting node: %s\n", n.Id()) } } diff --git a/internal/pkg/api/node/edit.go b/internal/pkg/api/node/edit.go index fe777681..b28ead94 100644 --- a/internal/pkg/api/node/edit.go +++ b/internal/pkg/api/node/edit.go @@ -39,7 +39,7 @@ func FilteredNodes(nodeList *wwapiv1.NodeList) *wwapiv1.NodeYaml { os.Exit(1) } nodeMap, _ := nodeDB.FindAllNodes() - nodeMap = node.FilterByName(nodeMap, nodeList.Output) + nodeMap = node.FilterNodeListByName(nodeMap, nodeList.Output) buffer, _ := yaml.Marshal(nodeMap) retVal := wwapiv1.NodeYaml{ NodeConfMapYaml: string(buffer), diff --git a/internal/pkg/api/node/list.go b/internal/pkg/api/node/list.go index 6ff21107..b2018e3d 100644 --- a/internal/pkg/api/node/list.go +++ b/internal/pkg/api/node/list.go @@ -32,7 +32,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro if nodeGet.Type == wwapiv1.GetNodeList_Simple { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s", "NODE NAME", "PROFILES", "NETWORK")) - for _, n := range node.FilterByName(nodes, nodeGet.Nodes) { + for _, n := range node.FilterNodeListByName(nodes, nodeGet.Nodes) { var netNames []string for k := range n.NetDevs { netNames = append(netNames, k) @@ -44,7 +44,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro } else if nodeGet.Type == wwapiv1.GetNodeList_Network { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s:=:%s:=:%s:=:%s", "NODE", "NETWORK", "HWADDR", "IPADDR", "GATEWAY", "DEVICE")) - for _, n := range node.FilterByName(nodes, nodeGet.Nodes) { + for _, n := range node.FilterNodeListByName(nodes, nodeGet.Nodes) { if len(n.NetDevs) > 0 { for name := range n.NetDevs { nodeList.Output = append(nodeList.Output, @@ -62,7 +62,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro } else if nodeGet.Type == wwapiv1.GetNodeList_Ipmi { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s:=:%s:=:%s", "NODE", "IPMI IPADDR", "IPMI PORT", "IPMI USERNAME", "IPMI INTERFACE")) - for _, n := range node.FilterByName(nodes, nodeGet.Nodes) { + for _, n := range node.FilterNodeListByName(nodes, nodeGet.Nodes) { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s:=:%s:=:%s:=:%s", n.Id(), n.Ipmi.Ipaddr.String(), @@ -74,7 +74,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro } else if nodeGet.Type == wwapiv1.GetNodeList_Long { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s:=:%s", "NODE NAME", "KERNEL OVERRIDE", "CONTAINER", "OVERLAYS (S/R)")) - for _, n := range node.FilterByName(nodes, nodeGet.Nodes) { + for _, n := range node.FilterNodeListByName(nodes, nodeGet.Nodes) { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id(), n.Kernel.Override, @@ -82,7 +82,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro strings.Join(n.SystemOverlay, ",")+"/"+strings.Join(n.RuntimeOverlay, ","))) } } else if nodeGet.Type == wwapiv1.GetNodeList_All { - for _, n := range node.FilterByName(nodes, nodeGet.Nodes) { + for _, n := range node.FilterNodeListByName(nodes, nodeGet.Nodes) { nodeList.Output = append(nodeList.Output, fmt.Sprintf("%s:=:%s:=:%s:=:%s", "NODE", "FIELD", "PROFILE", "VALUE")) fields := nodeDB.GetFields(n) @@ -92,7 +92,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro } } } else if nodeGet.Type == wwapiv1.GetNodeList_YAML || nodeGet.Type == wwapiv1.GetNodeList_JSON { - filterNodes := node.FilterByName(nodes, nodeGet.Nodes) + filterNodes := node.FilterNodeListByName(nodes, nodeGet.Nodes) var buf []byte if nodeGet.Type == wwapiv1.GetNodeList_JSON { buf, _ = json.MarshalIndent(filterNodes, "", " ") diff --git a/internal/pkg/api/node/set.go b/internal/pkg/api/node/set.go index 9c212e47..e6f4c142 100644 --- a/internal/pkg/api/node/set.go +++ b/internal/pkg/api/node/set.go @@ -41,7 +41,6 @@ func NodeSetParameterCheck(set *wwapiv1.ConfSetParameter) (nodeDB node.NodeYaml, wwlog.Error("Could not open configuration: %s", err) return } - //func AbstractSetParameterCheck(set *wwapiv1.ConfSetParameter, confMap map[string]*node.NodeConf, confs []string) (count uint, err error) { if set == nil { err = fmt.Errorf("node set parameter is nil") return diff --git a/internal/pkg/api/profile/edit.go b/internal/pkg/api/profile/edit.go index 56458dce..cbd06d0a 100644 --- a/internal/pkg/api/profile/edit.go +++ b/internal/pkg/api/profile/edit.go @@ -38,9 +38,9 @@ func FilteredProfiles(profileList *wwapiv1.NodeList) *wwapiv1.NodeYaml { wwlog.Error("Could not open nodeDB: %s\n", err) os.Exit(1) } - profileMap, _ := nodeDB.FindAllProfiles() - //profileMap = node.FilterProfilesByName(profileMap, profileList.Output) - buffer, _ := yaml.Marshal(profileMap) + profiles, _ := nodeDB.FindAllProfiles() + profiles = node.FilterProfileListByName(profiles, profileList.Output) + buffer, _ := yaml.Marshal(profiles) retVal := wwapiv1.NodeYaml{ NodeConfMapYaml: string(buffer), } diff --git a/internal/pkg/api/profile/list.go b/internal/pkg/api/profile/list.go index 6eb7a13a..536f9b18 100644 --- a/internal/pkg/api/profile/list.go +++ b/internal/pkg/api/profile/list.go @@ -23,7 +23,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi if err != nil { return } - //profiles = node.FilterByName(profiles, ShowOpt.Profiles) + profiles = node.FilterProfileListByName(profiles, ShowOpt.Profiles) sort.Slice(profiles, func(i, j int) bool { return profiles[i].Id() < profiles[j].Id() }) diff --git a/internal/pkg/node/checkconf.go b/internal/pkg/node/checkconf.go index c04fd075..623f078f 100644 --- a/internal/pkg/node/checkconf.go +++ b/internal/pkg/node/checkconf.go @@ -29,7 +29,6 @@ func (profileConf *ProfileConf) Check() (err error) { func check(infoType reflect.Type, infoVal reflect.Value) (err error) { // now iterate of every field for i := 0; i < infoVal.Elem().NumField(); i++ { - //wwlog.Debug("checking field: %s type: %s", infoType.Elem().Field(i).Name, infoVal.Elem().Field(i).Type()) if infoType.Elem().Field(i).Type.Kind() == reflect.String { newFmt, err := checker(infoVal.Elem().Field(i).Interface().(string), infoType.Elem().Field(i).Tag.Get("type")) if err != nil { @@ -42,7 +41,6 @@ func check(infoType reflect.Type, infoVal reflect.Value) (err error) { nestVal := reflect.ValueOf(infoVal.Elem().Field(i).Interface()) for j := 0; j < nestType.Elem().NumField(); j++ { if nestType.Elem().Field(j).Type.Kind() == reflect.String { - //wwlog.Debug("checking field: %s type: %s", nestType.Elem().Field(j).Name, nestType.Elem().Field(j).Tag.Get("type")) newFmt, err := checker(nestVal.Elem().Field(j).Interface().(string), nestType.Elem().Field(j).Tag.Get("type")) if err != nil { return fmt.Errorf("field: %s value:%s err: %s", nestType.Elem().Field(j).Name, nestVal.Elem().Field(j).String(), err) @@ -74,7 +72,6 @@ func checker(value string, valType string) (niceValue string, err error) { if valType == "" || value == "" || util.InSlice(wwtype.GetUnsetVerbs(), value) { return "", nil } - //wwlog.Debug("checker: %s is %s", value, valType) switch valType { case "": return "", nil diff --git a/internal/pkg/node/constructors.go b/internal/pkg/node/constructors.go index 148983a2..88fec481 100644 --- a/internal/pkg/node/constructors.go +++ b/internal/pkg/node/constructors.go @@ -87,43 +87,6 @@ func (config *NodeYaml) GetNode(id string) (node NodeConf, err error) { return node, err } } - // err = mergo.Merge(&node, config.nodes[id], mergo.WithOverride, mergo.WithoutDereference) - // err = mergo.Merge(&node, config.nodes[id], mergo.WithOverride) - // err = mergo.Merge(&node, config.nodes[id]) - /* - node = EmptyNode() - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - dec := gob.NewDecoder(&buf) - includedProfile, err := config.GetProfile(p) - appendStringSlices(&node, &includedProfile) - if err != nil { - return node, err - } - err = enc.Encode(includedProfile) - if err != nil { - return node, err - } - err = dec.Decode(&node) - if err != nil { - return node, err - } - wwlog.Debug("merged in profile: %s", p) - } - var bufNode bytes.Buffer - encNode := gob.NewEncoder(&bufNode) - decNode := gob.NewDecoder(&bufNode) - appendStringSlices(&node, &config.nodes[id].ProfileConf) - - err = encNode.Encode(config.nodes[id]) - if err != nil { - return node, err - } - err = decNode.Decode(&node) - if err != nil { - return node, err - } - */ // finally set no exported values node.id = id node.valid = true diff --git a/internal/pkg/node/constuctors_test.go b/internal/pkg/node/constuctors_test.go index 6cb64ff3..ec0171cf 100644 --- a/internal/pkg/node/constuctors_test.go +++ b/internal/pkg/node/constuctors_test.go @@ -60,7 +60,6 @@ nodes: } func Test_Primary_Network(t *testing.T) { - //wwlog.SetLogLevel(wwlog.DEBUG) c := newConstructorPrimaryNetworkTest(t) test_node1, err := c.GetNode("test_node1") assert.NoError(t, err) diff --git a/internal/pkg/node/methods.go b/internal/pkg/node/methods.go index 8e5da8a3..ad7e4b22 100644 --- a/internal/pkg/node/methods.go +++ b/internal/pkg/node/methods.go @@ -10,11 +10,17 @@ import ( "github.com/warewulf/warewulf/internal/pkg/util" ) -type sortByName []NodeConf +type nodeList []NodeConf -func (a sortByName) Len() int { return len(a) } -func (a sortByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a sortByName) Less(i, j int) bool { return a[i].id < a[j].id } +func (a nodeList) Len() int { return len(a) } +func (a nodeList) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a nodeList) Less(i, j int) bool { return a[i].id < a[j].id } + +type profileList []ProfileConf + +func (a profileList) Len() int { return len(a) } +func (a profileList) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a profileList) Less(i, j int) bool { return a[i].id < a[j].id } /********** * @@ -26,7 +32,7 @@ func (a sortByName) Less(i, j int) bool { return a[i].id < a[j].id } Filter a given slice of NodeConf against a given regular expression */ -func FilterByName(set []NodeConf, searchList []string) []NodeConf { +func FilterNodeListByName(set []NodeConf, searchList []string) []NodeConf { var ret []NodeConf unique := make(map[string]NodeConf) @@ -45,14 +51,41 @@ func FilterByName(set []NodeConf, searchList []string) []NodeConf { ret = set } - sort.Sort(sortByName(ret)) + sort.Sort(nodeList(ret)) + return ret +} + +/* +Filter a given slice of ProfileConf against a given +regular expression +*/ +func FilterProfileListByName(set []ProfileConf, searchList []string) []ProfileConf { + var ret []ProfileConf + unique := make(map[string]ProfileConf) + + if len(searchList) > 0 { + for _, search := range searchList { + for _, entry := range set { + if match, _ := regexp.MatchString("^"+search+"$", entry.id); match { + unique[entry.id] = entry + } + } + } + for _, n := range unique { + ret = append(ret, n) + } + } else { + ret = set + } + + sort.Sort(profileList(ret)) return ret } /* Filter a given map of NodeConf against given regular expression. */ -func FilterNodesByName(inputMap map[string]*NodeConf, searchList []string) (retMap map[string]*NodeConf) { +func FilterNodeMapByName(inputMap map[string]*NodeConf, searchList []string) (retMap map[string]*NodeConf) { retMap = map[string]*NodeConf{} if len(searchList) > 0 { for _, search := range searchList { @@ -67,9 +100,9 @@ func FilterNodesByName(inputMap map[string]*NodeConf, searchList []string) (retM } /* -Filter a given map of NodeConf against given regular expression. +Filter a given map of ProfileConf against given regular expression. */ -func FilterProfilesByName(inputMap map[string]*ProfileConf, searchList []string) (retMap map[string]*ProfileConf) { +func FilterProfileMapByName(inputMap map[string]*ProfileConf, searchList []string) (retMap map[string]*ProfileConf) { retMap = map[string]*ProfileConf{} if len(searchList) > 0 { for _, search := range searchList { diff --git a/internal/pkg/node/util.go b/internal/pkg/node/util.go index bf44a928..c3924da1 100644 --- a/internal/pkg/node/util.go +++ b/internal/pkg/node/util.go @@ -72,7 +72,6 @@ func ObjectIsEmpty(obj interface{}) bool { } } else if varType.Field(i).Type == reflect.TypeOf(map[string]string{}) { if varVal.Field(i).Len() != 0 { - // if len(varVal.Field(i).Interface().(map[string]string)) != 0 { return false } } else if varType.Field(i).Type.Kind() == reflect.Ptr {