airshipctl/cmd/config/set_management_configuration.go
Ruslan Aliev 63b3501a53 Refactor config* commands
All the config commands was refactored for usage with new config
factory. Config object now is being initialized in pkg module on demand,
which provides more flexibility and cleaner code.

Change-Id: Ibad0985e30c16efa96109a49edf2840c60305df8
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #327
2020-09-03 23:52:49 -05:00

110 lines
3.2 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/remote/redfish"
)
const (
flagInsecure = "insecure"
flagInsecureDescription = "Ignore SSL certificate verification on out-of-band management requests"
flagManagementType = "management-type"
flagManagementTypeDescription = "Set the out-of-band management type"
flagUseProxy = "use-proxy"
flagUseProxyDescription = "Use the proxy configuration specified in the local environment"
)
// NewSetManagementConfigCommand creates a command for creating and modifying clusters
// in the airshipctl config file.
func NewSetManagementConfigCommand(cfgFactory config.Factory) *cobra.Command {
var insecure bool
var managementType string
var useProxy bool
cmd := &cobra.Command{
Use: "set-management-config NAME",
Short: "Modify an out-of-band management configuration",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
name := args[0]
managementCfg, err := cfg.GetManagementConfiguration(name)
if err != nil {
return err
}
var modified bool
if cmd.Flags().Changed(flagInsecure) && insecure != managementCfg.Insecure {
modified = true
managementCfg.Insecure = insecure
fmt.Fprintf(cmd.OutOrStdout(),
"Option 'insecure' set to '%t' for management configuration '%s'.\n",
managementCfg.Insecure, name)
}
if cmd.Flags().Changed(flagManagementType) && managementType != managementCfg.Type {
modified = true
if err = managementCfg.SetType(managementType); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(),
"Option 'type' set to '%s' for management configuration '%s'.\n",
managementCfg.Type, name)
}
if cmd.Flags().Changed(flagUseProxy) && useProxy != managementCfg.UseProxy {
modified = true
managementCfg.UseProxy = useProxy
fmt.Fprintf(cmd.OutOrStdout(),
"Option 'useproxy' set to '%t' for management configuration '%s'\n",
managementCfg.UseProxy, name)
}
if !modified {
fmt.Fprintf(cmd.OutOrStdout(),
"Management configuration '%s' not modified. No new settings.\n", name)
return nil
}
if err = cfg.PersistConfig(true); err != nil {
return err
}
return nil
},
}
flags := cmd.Flags()
flags.BoolVar(&insecure, flagInsecure, false, flagInsecureDescription)
flags.StringVar(&managementType, flagManagementType, redfish.ClientType, flagManagementTypeDescription)
flags.BoolVar(&useProxy, flagUseProxy, true, flagUseProxyDescription)
return cmd
}