From d91d273ac73825cbf3e71f8e5be844172ab5ceae Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Thu, 17 Oct 2024 15:25:41 -0400 Subject: [PATCH] Use ProfileConf rather than NodeConf for profile commands This removes node-only attributes, like a profile list, from profile commands. Signed-off-by: Jonathon Anderson --- internal/app/wwctl/profile/add/root.go | 4 ++-- internal/app/wwctl/profile/edit/main.go | 6 ++--- internal/app/wwctl/profile/set/root.go | 4 ++-- internal/pkg/node/checkconf.go | 32 ++++++++++++++++--------- internal/pkg/node/flags.go | 4 ++++ internal/pkg/node/methods.go | 6 +++++ 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/internal/app/wwctl/profile/add/root.go b/internal/app/wwctl/profile/add/root.go index c40fa684..b1faed36 100644 --- a/internal/app/wwctl/profile/add/root.go +++ b/internal/app/wwctl/profile/add/root.go @@ -12,7 +12,7 @@ import ( type variables struct { netName string - profileConf node.NodeConf + profileConf node.ProfileConf SetNetDevDel string SetNodeAll bool SetYes bool @@ -25,7 +25,7 @@ type variables struct { // GetRootCommand returns the root cobra.Command for the application. func GetCommand() *cobra.Command { vars := variables{} - vars.profileConf = node.NewNode("") + vars.profileConf = node.NewProfile("") baseCmd := &cobra.Command{ DisableFlagsInUseLine: true, Use: "add PROFILE", diff --git a/internal/app/wwctl/profile/edit/main.go b/internal/app/wwctl/profile/edit/main.go index d8d2a517..e00c849b 100644 --- a/internal/app/wwctl/profile/edit/main.go +++ b/internal/app/wwctl/profile/edit/main.go @@ -41,7 +41,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { Output: args, } profileListMsg := apiprofile.FilteredProfiles(&filterList) - profileMap := make(map[string]*node.NodeConf) + profileMap := make(map[string]*node.ProfileConf) // got proper yaml back _ = yaml.Unmarshal([]byte(profileListMsg.NodeConfMapYaml), profileMap) file, err := os.CreateTemp(os.TempDir(), "ww4ProfileEdit*.yaml") @@ -49,7 +49,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Error("Could not create temp file:%s \n", err) } defer os.Remove(file.Name()) - yamlTemplate := node.UnmarshalConf(node.NodeConf{}, []string{"tagsdel"}) + yamlTemplate := node.UnmarshalConf(node.ProfileConf{}, []string{"tagsdel"}) for { _ = file.Truncate(0) _, _ = file.Seek(0, 0) @@ -77,7 +77,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Debug("Hashes are before %s and after %s\n", sum1, sum2) if sum1 != sum2 { wwlog.Debug("Profiles were modified") - modifiedProfileMap := make(map[string]*node.NodeConf) + modifiedProfileMap := make(map[string]*node.ProfileConf) _, _ = file.Seek(0, 0) // ignore error as only may occurs under strange circumstances buffer, _ := io.ReadAll(file) diff --git a/internal/app/wwctl/profile/set/root.go b/internal/app/wwctl/profile/set/root.go index 896ba7b4..332fd0d5 100644 --- a/internal/app/wwctl/profile/set/root.go +++ b/internal/app/wwctl/profile/set/root.go @@ -21,14 +21,14 @@ type variables struct { partName string diskName string fsName string - profileConf node.NodeConf + profileConf node.ProfileConf profileDel node.NodeConfDel profileAdd node.NodeConfAdd } func GetCommand() *cobra.Command { vars := variables{} - vars.profileConf = node.NewNode("") + vars.profileConf = node.NewProfile("") baseCmd := &cobra.Command{ Use: "set [OPTIONS] [PROFILE ...]", diff --git a/internal/pkg/node/checkconf.go b/internal/pkg/node/checkconf.go index 89941a24..c04fd075 100644 --- a/internal/pkg/node/checkconf.go +++ b/internal/pkg/node/checkconf.go @@ -17,19 +17,29 @@ Checks if for NodeConf all values can be parsed according to their type. func (nodeConf *NodeConf) Check() (err error) { nodeInfoType := reflect.TypeOf(nodeConf) nodeInfoVal := reflect.ValueOf(nodeConf) + return check(nodeInfoType, nodeInfoVal) +} + +func (profileConf *ProfileConf) Check() (err error) { + profileInfoType := reflect.TypeOf(profileConf) + profileInfoVal := reflect.ValueOf(profileConf) + return check(profileInfoType, profileInfoVal) +} + +func check(infoType reflect.Type, infoVal reflect.Value) (err error) { // now iterate of every field - for i := 0; i < nodeInfoVal.Elem().NumField(); i++ { - //wwlog.Debug("checking field: %s type: %s", nodeInfoType.Elem().Field(i).Name, nodeInfoVal.Elem().Field(i).Type()) - if nodeInfoType.Elem().Field(i).Type.Kind() == reflect.String { - newFmt, err := checker(nodeInfoVal.Elem().Field(i).Interface().(string), nodeInfoType.Elem().Field(i).Tag.Get("type")) + 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 { - return fmt.Errorf("field: %s value:%s err: %s", nodeInfoType.Elem().Field(i).Name, nodeInfoVal.Elem().Field(i).String(), err) + return fmt.Errorf("field: %s value:%s err: %s", infoType.Elem().Field(i).Name, infoVal.Elem().Field(i).String(), err) } else if newFmt != "" { - nodeInfoVal.Elem().Field(i).SetString(newFmt) + infoVal.Elem().Field(i).SetString(newFmt) } - } else if nodeInfoType.Elem().Field(i).Type.Kind() == reflect.Ptr && !nodeInfoVal.Elem().Field(i).IsNil() { - nestType := reflect.TypeOf(nodeInfoVal.Elem().Field(i).Interface()) - nestVal := reflect.ValueOf(nodeInfoVal.Elem().Field(i).Interface()) + } else if infoType.Elem().Field(i).Type.Kind() == reflect.Ptr && !infoVal.Elem().Field(i).IsNil() { + nestType := reflect.TypeOf(infoVal.Elem().Field(i).Interface()) + 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")) @@ -41,8 +51,8 @@ func (nodeConf *NodeConf) Check() (err error) { } } } - } else if nodeInfoType.Elem().Field(i).Type == reflect.TypeOf(map[string]*NetDevs(nil)) { - netMap := nodeInfoVal.Elem().Field(i).Interface().(map[string]*NetDevs) + } else if infoType.Elem().Field(i).Type == reflect.TypeOf(map[string]*NetDevs(nil)) { + netMap := infoVal.Elem().Field(i).Interface().(map[string]*NetDevs) for _, val := range netMap { netType := reflect.TypeOf(val) netVal := reflect.ValueOf(val) diff --git a/internal/pkg/node/flags.go b/internal/pkg/node/flags.go index c286ccb7..4210f650 100644 --- a/internal/pkg/node/flags.go +++ b/internal/pkg/node/flags.go @@ -35,6 +35,10 @@ func (nodeConf *NodeConf) CreateFlags(baseCmd *cobra.Command) { recursiveCreateFlags(nodeConf, baseCmd) } +func (profileConf *ProfileConf) CreateFlags(baseCmd *cobra.Command) { + recursiveCreateFlags(profileConf, baseCmd) +} + func (del *NodeConfDel) CreateDelFlags(baseCmd *cobra.Command) { recursiveCreateFlags(del, baseCmd) diff --git a/internal/pkg/node/methods.go b/internal/pkg/node/methods.go index 00b2a74e..6d968fbe 100644 --- a/internal/pkg/node/methods.go +++ b/internal/pkg/node/methods.go @@ -92,6 +92,12 @@ func NewNode(id string) (nodeconf NodeConf) { return nodeconf } +func NewProfile(id string) (profileconf ProfileConf) { + profileconf = EmptyProfile() + profileconf.id = id + return profileconf +} + func EmptyNode() (nodeconf NodeConf) { nodeconf.Ipmi = new(IpmiConf) nodeconf.Ipmi.Tags = map[string]string{}