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 <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-10-17 15:25:41 -04:00
parent 6460ca827a
commit d91d273ac7
6 changed files with 38 additions and 18 deletions

View File

@@ -12,7 +12,7 @@ import (
type variables struct { type variables struct {
netName string netName string
profileConf node.NodeConf profileConf node.ProfileConf
SetNetDevDel string SetNetDevDel string
SetNodeAll bool SetNodeAll bool
SetYes bool SetYes bool
@@ -25,7 +25,7 @@ type variables struct {
// GetRootCommand returns the root cobra.Command for the application. // GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command { func GetCommand() *cobra.Command {
vars := variables{} vars := variables{}
vars.profileConf = node.NewNode("") vars.profileConf = node.NewProfile("")
baseCmd := &cobra.Command{ baseCmd := &cobra.Command{
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Use: "add PROFILE", Use: "add PROFILE",

View File

@@ -41,7 +41,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
Output: args, Output: args,
} }
profileListMsg := apiprofile.FilteredProfiles(&filterList) profileListMsg := apiprofile.FilteredProfiles(&filterList)
profileMap := make(map[string]*node.NodeConf) profileMap := make(map[string]*node.ProfileConf)
// got proper yaml back // got proper yaml back
_ = yaml.Unmarshal([]byte(profileListMsg.NodeConfMapYaml), profileMap) _ = yaml.Unmarshal([]byte(profileListMsg.NodeConfMapYaml), profileMap)
file, err := os.CreateTemp(os.TempDir(), "ww4ProfileEdit*.yaml") 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) wwlog.Error("Could not create temp file:%s \n", err)
} }
defer os.Remove(file.Name()) defer os.Remove(file.Name())
yamlTemplate := node.UnmarshalConf(node.NodeConf{}, []string{"tagsdel"}) yamlTemplate := node.UnmarshalConf(node.ProfileConf{}, []string{"tagsdel"})
for { for {
_ = file.Truncate(0) _ = file.Truncate(0)
_, _ = file.Seek(0, 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) wwlog.Debug("Hashes are before %s and after %s\n", sum1, sum2)
if sum1 != sum2 { if sum1 != sum2 {
wwlog.Debug("Profiles were modified") wwlog.Debug("Profiles were modified")
modifiedProfileMap := make(map[string]*node.NodeConf) modifiedProfileMap := make(map[string]*node.ProfileConf)
_, _ = file.Seek(0, 0) _, _ = file.Seek(0, 0)
// ignore error as only may occurs under strange circumstances // ignore error as only may occurs under strange circumstances
buffer, _ := io.ReadAll(file) buffer, _ := io.ReadAll(file)

View File

@@ -21,14 +21,14 @@ type variables struct {
partName string partName string
diskName string diskName string
fsName string fsName string
profileConf node.NodeConf profileConf node.ProfileConf
profileDel node.NodeConfDel profileDel node.NodeConfDel
profileAdd node.NodeConfAdd profileAdd node.NodeConfAdd
} }
func GetCommand() *cobra.Command { func GetCommand() *cobra.Command {
vars := variables{} vars := variables{}
vars.profileConf = node.NewNode("") vars.profileConf = node.NewProfile("")
baseCmd := &cobra.Command{ baseCmd := &cobra.Command{
Use: "set [OPTIONS] [PROFILE ...]", Use: "set [OPTIONS] [PROFILE ...]",

View File

@@ -17,19 +17,29 @@ Checks if for NodeConf all values can be parsed according to their type.
func (nodeConf *NodeConf) Check() (err error) { func (nodeConf *NodeConf) Check() (err error) {
nodeInfoType := reflect.TypeOf(nodeConf) nodeInfoType := reflect.TypeOf(nodeConf)
nodeInfoVal := reflect.ValueOf(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 // now iterate of every field
for i := 0; i < nodeInfoVal.Elem().NumField(); i++ { for i := 0; i < infoVal.Elem().NumField(); i++ {
//wwlog.Debug("checking field: %s type: %s", nodeInfoType.Elem().Field(i).Name, nodeInfoVal.Elem().Field(i).Type()) //wwlog.Debug("checking field: %s type: %s", infoType.Elem().Field(i).Name, infoVal.Elem().Field(i).Type())
if nodeInfoType.Elem().Field(i).Type.Kind() == reflect.String { if infoType.Elem().Field(i).Type.Kind() == reflect.String {
newFmt, err := checker(nodeInfoVal.Elem().Field(i).Interface().(string), nodeInfoType.Elem().Field(i).Tag.Get("type")) newFmt, err := checker(infoVal.Elem().Field(i).Interface().(string), infoType.Elem().Field(i).Tag.Get("type"))
if err != nil { 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 != "" { } 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() { } else if infoType.Elem().Field(i).Type.Kind() == reflect.Ptr && !infoVal.Elem().Field(i).IsNil() {
nestType := reflect.TypeOf(nodeInfoVal.Elem().Field(i).Interface()) nestType := reflect.TypeOf(infoVal.Elem().Field(i).Interface())
nestVal := reflect.ValueOf(nodeInfoVal.Elem().Field(i).Interface()) nestVal := reflect.ValueOf(infoVal.Elem().Field(i).Interface())
for j := 0; j < nestType.Elem().NumField(); j++ { for j := 0; j < nestType.Elem().NumField(); j++ {
if nestType.Elem().Field(j).Type.Kind() == reflect.String { 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")) //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)) { } else if infoType.Elem().Field(i).Type == reflect.TypeOf(map[string]*NetDevs(nil)) {
netMap := nodeInfoVal.Elem().Field(i).Interface().(map[string]*NetDevs) netMap := infoVal.Elem().Field(i).Interface().(map[string]*NetDevs)
for _, val := range netMap { for _, val := range netMap {
netType := reflect.TypeOf(val) netType := reflect.TypeOf(val)
netVal := reflect.ValueOf(val) netVal := reflect.ValueOf(val)

View File

@@ -35,6 +35,10 @@ func (nodeConf *NodeConf) CreateFlags(baseCmd *cobra.Command) {
recursiveCreateFlags(nodeConf, baseCmd) recursiveCreateFlags(nodeConf, baseCmd)
} }
func (profileConf *ProfileConf) CreateFlags(baseCmd *cobra.Command) {
recursiveCreateFlags(profileConf, baseCmd)
}
func (del *NodeConfDel) CreateDelFlags(baseCmd *cobra.Command) { func (del *NodeConfDel) CreateDelFlags(baseCmd *cobra.Command) {
recursiveCreateFlags(del, baseCmd) recursiveCreateFlags(del, baseCmd)

View File

@@ -92,6 +92,12 @@ func NewNode(id string) (nodeconf NodeConf) {
return nodeconf return nodeconf
} }
func NewProfile(id string) (profileconf ProfileConf) {
profileconf = EmptyProfile()
profileconf.id = id
return profileconf
}
func EmptyNode() (nodeconf NodeConf) { func EmptyNode() (nodeconf NodeConf) {
nodeconf.Ipmi = new(IpmiConf) nodeconf.Ipmi = new(IpmiConf)
nodeconf.Ipmi.Tags = map[string]string{} nodeconf.Ipmi.Tags = map[string]string{}