Add set-management-configuration command

This change adds a set-management-configuration command that allows the
user, typically CI, to change management configuration settings
imperatively.

Change-Id: I7a106a46f57fef0ea2a8c5dd8c8786d8a103305c
Signed-off-by: Drew Walters <andrew.walters@att.com>
This commit is contained in:
Drew Walters 2020-05-05 22:39:09 +00:00
parent 88542a66b5
commit 383cffed4d
16 changed files with 387 additions and 17 deletions

View File

@ -34,15 +34,21 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
rootSettings.InitConfig()
},
}
configRootCmd.AddCommand(NewSetClusterCommand(rootSettings))
configRootCmd.AddCommand(NewGetClusterCommand(rootSettings))
configRootCmd.AddCommand(NewSetContextCommand(rootSettings))
configRootCmd.AddCommand(NewGetContextCommand(rootSettings))
configRootCmd.AddCommand(NewInitCommand(rootSettings))
configRootCmd.AddCommand(NewSetAuthInfoCommand(rootSettings))
configRootCmd.AddCommand(NewGetAuthInfoCommand(rootSettings))
configRootCmd.AddCommand(NewUseContextCommand(rootSettings))
configRootCmd.AddCommand(NewSetAuthInfoCommand(rootSettings))
configRootCmd.AddCommand(NewGetClusterCommand(rootSettings))
configRootCmd.AddCommand(NewSetClusterCommand(rootSettings))
configRootCmd.AddCommand(NewGetContextCommand(rootSettings))
configRootCmd.AddCommand(NewSetContextCommand(rootSettings))
configRootCmd.AddCommand(NewSetManagementConfigCommand(rootSettings))
configRootCmd.AddCommand(NewImportCommand(rootSettings))
configRootCmd.AddCommand(NewInitCommand(rootSettings))
configRootCmd.AddCommand(NewUseContextCommand(rootSettings))
return configRootCmd
}

View File

@ -0,0 +1,105 @@
/*
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/environment"
"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(rootSettings *environment.AirshipCTLSettings) *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 {
name := args[0]
managementCfg, err := rootSettings.Config.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 = rootSettings.Config.PersistConfig(); 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
}

View File

@ -0,0 +1,166 @@
/*
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_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
redfishdell "opendev.org/airship/airshipctl/pkg/remote/redfish/vendors/dell"
"opendev.org/airship/airshipctl/testutil"
)
func TestConfigSetManagementConfigurationCmd(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
}
cmdTests := []*testutil.CmdTest{
{
Name: "config-cmd-set-management-config-with-help",
CmdLine: "--help",
Cmd: cmd.NewSetManagementConfigCommand(nil),
},
{
Name: "config-cmd-set-management-config-no-args",
CmdLine: "",
Cmd: cmd.NewSetManagementConfigCommand(nil),
Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0),
},
{
Name: "config-cmd-set-management-config-excess-args",
CmdLine: "arg1 arg2",
Cmd: cmd.NewSetManagementConfigCommand(nil),
Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2),
},
{
Name: "config-cmd-set-management-config-not-found",
CmdLine: fmt.Sprintf("%s-test", config.AirshipDefaultContext),
Cmd: cmd.NewSetManagementConfigCommand(settings),
Error: config.ErrManagementConfigurationNotFound{
Name: fmt.Sprintf("%s-test", config.AirshipDefaultContext),
},
},
}
for _, tt := range cmdTests {
testutil.RunTest(t, tt)
}
}
func TestConfigSetManagementConfigurationInsecure(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
}
require.False(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Insecure)
testutil.RunTest(t, &testutil.CmdTest{
Name: "config-cmd-set-management-config-change-insecure",
CmdLine: fmt.Sprintf("%s --insecure=true", config.AirshipDefaultContext),
Cmd: cmd.NewSetManagementConfigCommand(settings),
})
assert.True(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Insecure)
}
func TestConfigSetManagementConfigurationType(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
}
require.NotEqual(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
cmdTests := []*testutil.CmdTest{
{
Name: "config-cmd-set-management-config-unknown-type",
CmdLine: fmt.Sprintf("%s --management-type=foo", config.AirshipDefaultContext),
Cmd: cmd.NewSetManagementConfigCommand(settings),
Error: config.ErrUnknownManagementType{Type: "foo"},
},
{
Name: "config-cmd-set-management-config-change-type",
CmdLine: fmt.Sprintf("%s --management-type=%s", config.AirshipDefaultContext,
redfishdell.ClientType),
Cmd: cmd.NewSetManagementConfigCommand(settings),
},
}
for _, tt := range cmdTests {
testutil.RunTest(t, tt)
}
assert.Equal(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
}
func TestConfigSetManagementConfigurationUseProxy(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
}
require.False(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
testutil.RunTest(t, &testutil.CmdTest{
Name: "config-cmd-set-management-config-change-use-proxy",
CmdLine: fmt.Sprintf("%s --use-proxy=true", config.AirshipDefaultContext),
Cmd: cmd.NewSetManagementConfigCommand(settings),
})
assert.True(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
}
func TestConfigSetManagementConfigurationMultipleOptions(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
}
require.NotEqual(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
require.False(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
testutil.RunTest(t, &testutil.CmdTest{
Name: "config-cmd-set-management-config-change-type",
CmdLine: fmt.Sprintf("%s --management-type=%s --use-proxy=true", config.AirshipDefaultContext,
redfishdell.ClientType),
Cmd: cmd.NewSetManagementConfigCommand(settings),
})
assert.Equal(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
assert.True(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
}

View File

@ -4,16 +4,17 @@ Usage:
config [command]
Available Commands:
get-cluster Get cluster information from the airshipctl config
get-context Get context information from the airshipctl config
get-credential Get user credentials from the airshipctl config
help Help about any command
import Merge information from a kubernetes config file
init Generate initial configuration files for airshipctl
set-cluster Manage clusters
set-context Manage contexts
set-credentials Manage user credentials
use-context Switch to a different context
get-cluster Get cluster information from the airshipctl config
get-context Get context information from the airshipctl config
get-credential Get user credentials from the airshipctl config
help Help about any command
import Merge information from a kubernetes config file
init Generate initial configuration files for airshipctl
set-cluster Manage clusters
set-context Manage contexts
set-credentials Manage user credentials
set-management-config Modify an out-of-band management configuration
use-context Switch to a different context
Flags:
-h, --help help for config

View File

@ -0,0 +1,10 @@
Error: accepts 1 arg(s), received 2
Usage:
set-management-config NAME [flags]
Flags:
-h, --help help for set-management-config
--insecure Ignore SSL certificate verification on out-of-band management requests
--management-type string Set the out-of-band management type (default "redfish")
--use-proxy Use the proxy configuration specified in the local environment (default true)

View File

@ -0,0 +1,10 @@
Error: accepts 1 arg(s), received 0
Usage:
set-management-config NAME [flags]
Flags:
-h, --help help for set-management-config
--insecure Ignore SSL certificate verification on out-of-band management requests
--management-type string Set the out-of-band management type (default "redfish")
--use-proxy Use the proxy configuration specified in the local environment (default true)

View File

@ -0,0 +1,10 @@
Error: Unknown management configuration 'default-test'.
Usage:
set-management-config NAME [flags]
Flags:
-h, --help help for set-management-config
--insecure Ignore SSL certificate verification on out-of-band management requests
--management-type string Set the out-of-band management type (default "redfish")
--use-proxy Use the proxy configuration specified in the local environment (default true)

View File

@ -0,0 +1,10 @@
Modify an out-of-band management configuration
Usage:
set-management-config NAME [flags]
Flags:
-h, --help help for set-management-config
--insecure Ignore SSL certificate verification on out-of-band management requests
--management-type string Set the out-of-band management type (default "redfish")
--use-proxy Use the proxy configuration specified in the local environment (default true)

View File

@ -0,0 +1 @@
Option 'insecure' set to 'true' for management configuration 'default'.

View File

@ -0,0 +1,2 @@
Option 'type' set to 'redfish-dell' for management configuration 'default'.
Option 'useproxy' set to 'true' for management configuration 'default'

View File

@ -0,0 +1 @@
Option 'type' set to 'redfish-dell' for management configuration 'default'.

View File

@ -0,0 +1,10 @@
Error: Unknown management type 'foo'. Known types include 'redfish' and 'redfish-dell'.
Usage:
set-management-config NAME [flags]
Flags:
-h, --help help for set-management-config
--insecure Ignore SSL certificate verification on out-of-band management requests
--management-type string Set the out-of-band management type (default "redfish")
--use-proxy Use the proxy configuration specified in the local environment (default true)

View File

@ -0,0 +1 @@
Option 'useproxy' set to 'true' for management configuration 'default'

View File

@ -568,6 +568,16 @@ func (c *Config) GetContexts() []*Context {
return contexts
}
// GetManagementConfiguration retrieves a management configuration by name.
func (c *Config) GetManagementConfiguration(name string) (*ManagementConfiguration, error) {
managementCfg, exists := c.ManagementConfiguration[name]
if !exists {
return nil, ErrManagementConfigurationNotFound{Name: name}
}
return managementCfg, nil
}
// AddContext creates a new context and returns the instance of
// newly created context
func (c *Config) AddContext(theContext *ContextOptions) *Context {

View File

@ -731,3 +731,20 @@ func TestImportErrors(t *testing.T) {
assert.Contains(t, err.Error(), "json parse error")
})
}
func TestManagementConfigurationByName(t *testing.T) {
conf, cleanupConfig := testutil.InitConfig(t)
defer cleanupConfig(t)
mgmtCfg, err := conf.GetManagementConfiguration(config.AirshipDefaultContext)
require.NoError(t, err)
assert.Equal(t, conf.ManagementConfiguration[config.AirshipDefaultContext], mgmtCfg)
}
func TestManagementConfigurationByNameDoesNotExist(t *testing.T) {
conf, cleanupConfig := testutil.InitConfig(t)
defer cleanupConfig(t)
_, err := conf.GetManagementConfiguration(fmt.Sprintf("%s-test", config.AirshipDefaultContext))
assert.Error(t, err)
}

View File

@ -106,6 +106,16 @@ func (e ErrConfigFailed) Error() string {
return "Configuration failed to complete."
}
// ErrManagementConfigurationNotFound describes a situation in which a user has attempted to reference a management
// configuration that cannot be referenced.
type ErrManagementConfigurationNotFound struct {
Name string
}
func (e ErrManagementConfigurationNotFound) Error() string {
return fmt.Sprintf("Unknown management configuration '%s'.", e.Name)
}
// ErrMissingCurrentContext returned in case --current used without setting current-context
type ErrMissingCurrentContext struct {
}