Merge branch 'RefactorFlags' into development

Signed-off-by: Christian Goll <cgoll@suse.de>
This commit is contained in:
Christian Goll
2023-03-23 11:24:47 +01:00
23 changed files with 940 additions and 184 deletions

View File

@@ -16,6 +16,12 @@ the required type is returned
*/
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
// run converters for different types
for _, c := range vars.converters {
if err := c(); err != nil {
return err
}
}
// remove the default network as all network values are assigned
// to this network
if _, ok := vars.nodeConf.NetDevs["default"]; ok && vars.netName != "" {

View File

@@ -16,6 +16,7 @@ func Test_Add(t *testing.T) {
args []string
wantErr bool
stdout string
chkout bool
outDb string
}{
{name: "single node add",
@@ -39,6 +40,51 @@ nodes:
n01:
profiles:
- foo
`},
{name: "single node add, discoverable true, explicit",
args: []string{"--discoverable=true", "n01"},
wantErr: false,
stdout: "",
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes:
n01:
discoverable: "true"
profiles:
- default
`},
{name: "single node add, discoverable true with yes",
args: []string{"--discoverable=yes", "n01"},
wantErr: false,
stdout: "",
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes:
n01:
discoverable: "true"
profiles:
- default
`},
{name: "single node add, discoverable wrong argument",
args: []string{"--discoverable=maybe", "n01"},
wantErr: true,
stdout: "",
chkout: false,
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes: {}
`},
{name: "single node add, discoverable false",
args: []string{"--discoverable=false", "n01"},
wantErr: false,
stdout: "",
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes:
n01:
discoverable: "false"
profiles:
- default
`},
{name: "single node add with Kernel args",
args: []string{"--kernelargs=foo", "n01"},
@@ -94,6 +140,15 @@ nodes:
network devices:
default:
ipaddr: 10.0.0.1
`},
{name: "single node with malformed ipaddr",
args: []string{"--ipaddr=10.0.1", "n01"},
wantErr: true,
stdout: "",
chkout: false,
outDb: `WW_INTERNAL: 43
nodeprofiles: {}
nodes: {}
`},
{name: "three nodes with ipaddr",
args: []string{"--ipaddr=10.10.0.1", "n[01-02,03]"},
@@ -212,7 +267,7 @@ WW_INTERNAL: 43
t.Errorf("DB dump is wrong, got:'%s'\nwant:'%s'", dump, tt.outDb)
t.FailNow()
}
if buf.String() != tt.stdout {
if tt.chkout && buf.String() != tt.stdout {
t.Errorf("Got wrong output, got:'%s'\nwant:'%s'", buf.String(), tt.stdout)
t.FailNow()
}

View File

@@ -12,8 +12,9 @@ import (
// Holds the variables which are needed in CobraRunE
type variables struct {
netName string
nodeConf node.NodeConf
netName string
nodeConf node.NodeConf
converters []func() error
}
// Returns the newly created command
@@ -28,9 +29,8 @@ func GetCommand() *cobra.Command {
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
}
vars.nodeConf.CreateFlags(baseCmd, []string{"tagdel", "nettagdel", "ipmitagdel"})
vars.converters = vars.nodeConf.CreateFlags(baseCmd, []string{"tagdel", "nettagdel", "ipmitagdel"})
baseCmd.PersistentFlags().StringVar(&vars.netName, "netname", "", "Set network name for network options")
// register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("container", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, _ := container.ListSources()

View File

@@ -77,17 +77,38 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
// ignore error as only may occurs under strange circumstances
buffer, _ := io.ReadAll(file)
err = yaml.Unmarshal(buffer, modifiedNodeMap)
if err == nil {
nodeList := make([]string, len(nodeMap))
i := 0
for key := range nodeMap {
nodeList[i] = key
i++
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedNodeMap)))
if !yes {
if err != nil {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", err))
if yes {
continue
} else {
break
}
}
var checkErrors []error
for nodeName, node := range modifiedNodeMap {
err = node.Check()
if err != nil {
checkErrors = append(checkErrors, fmt.Errorf("node: %s parse error: %s", nodeName, err))
}
}
if len(checkErrors) != 0 {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", checkErrors))
if yes {
continue
} else {
break
}
}
nodeList := make([]string, len(nodeMap))
i := 0
for key := range nodeMap {
nodeList[i] = key
i++
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedNodeMap)))
if yes {
err = apinode.NodeDelete(&wwapiv1.NodeDeleteParameter{NodeNames: nodeList, Force: true})
if err != nil {
wwlog.Verbose("Problem deleting nodes before modification %s")
@@ -99,11 +120,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
os.Exit(1)
}
break
} else {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", err))
if !yes {
break
}
}
} else {
break

View File

@@ -13,6 +13,12 @@ import (
)
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
// run converters for different types
for _, c := range Converters {
if err := c(); err != nil {
return err
}
}
// remove the default network as the all network values are assigned
// to this network
if NetName != "default" {

View File

@@ -38,11 +38,12 @@ var (
SetYes bool
SetForce bool
NodeConf node.NodeConf
Converters []func() error
)
func init() {
NodeConf = node.NewConf()
NodeConf.CreateFlags(baseCmd, []string{})
Converters = NodeConf.CreateFlags(baseCmd, []string{})
baseCmd.PersistentFlags().StringVarP(&SetNetDevDel, "netdel", "D", "", "Delete the node's network device")
baseCmd.PersistentFlags().StringVar(&NetName, "netname", "default", "Set network name for network options")
baseCmd.PersistentFlags().BoolVarP(&SetNodeAll, "all", "a", false, "Set all nodes")

View File

@@ -14,6 +14,12 @@ import (
)
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
// run converters for different types
for _, c := range Converters {
if err := c(); err != nil {
return err
}
}
// remove the default network as the all network values are assigned
// to this network
if NetName != "" {

View File

@@ -25,12 +25,13 @@ var (
SetForce bool
NetName string
ProfileConf node.NodeConf
Converters []func() error
)
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
ProfileConf = node.NewConf()
ProfileConf.CreateFlags(baseCmd,
Converters = ProfileConf.CreateFlags(baseCmd,
[]string{"ipaddr", "ipaddr6", "ipmiaddr", "profile"})
baseCmd.PersistentFlags().StringVar(&NetName, "netname", "", "Set network name for network options")
// register the command line completions

View File

@@ -71,24 +71,44 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
sum2 := hex.EncodeToString(hasher.Sum(nil))
wwlog.Debug("Hashes are before %s and after %s\n", sum1, sum2)
if sum1 != sum2 {
wwlog.Debug("Nodes were modified")
wwlog.Debug("Profiles were modified")
modifiedProfileMap := make(map[string]*node.NodeConf)
_, _ = file.Seek(0, 0)
// ignore error as only may occurs under strange circumstances
buffer, _ := io.ReadAll(file)
err = yaml.Unmarshal(buffer, modifiedProfileMap)
if err == nil {
nodeList := make([]string, len(profileMap))
i := 0
for key := range profileMap {
nodeList[i] = key
i++
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedProfileMap)))
if !yes {
if err != nil {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", err))
if yes {
continue
} else {
break
}
err = apiprofile.ProfileDelete(&wwapiv1.NodeDeleteParameter{NodeNames: nodeList, Force: true})
}
var checkErrors []error
for nodeName, node := range modifiedProfileMap {
err = node.Check()
if err != nil {
checkErrors = append(checkErrors, fmt.Errorf("profile: %s parse error: %s", nodeName, err))
}
}
if len(checkErrors) != 0 {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", checkErrors))
if yes {
continue
} else {
break
}
}
pList := make([]string, len(profileMap))
i := 0
for key := range profileMap {
pList[i] = key
i++
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedProfileMap)))
if yes {
err = apiprofile.ProfileDelete(&wwapiv1.NodeDeleteParameter{NodeNames: pList, Force: true})
if err != nil {
wwlog.Verbose("Problem deleting nodes before modification %s")
}
@@ -99,11 +119,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
os.Exit(1)
}
break
} else {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", err))
if !yes {
break
}
}
} else {
break

View File

@@ -38,11 +38,12 @@ var (
SetForce bool
NetName string
ProfileConf node.NodeConf
Converters []func() error
)
func init() {
ProfileConf = node.NewConf()
ProfileConf.CreateFlags(baseCmd,
Converters = ProfileConf.CreateFlags(baseCmd,
[]string{"ipaddr", "ipaddr6", "ipmiaddr", "profile"})
baseCmd.PersistentFlags().StringVar(&NetName, "netname", "default", "Set network name for network options")
baseCmd.PersistentFlags().StringVarP(&SetNetDevDel, "netdel", "D", "", "Delete the node's network device")