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
This commit is contained in:
Ruslan Aliev 2020-08-28 18:32:33 -05:00
parent 5a94f9f99c
commit 63b3501a53
18 changed files with 121 additions and 87 deletions

View File

@ -17,6 +17,7 @@ package config
import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
)
@ -39,18 +40,21 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
},
}
configRootCmd.AddCommand(NewGetContextCommand(rootSettings))
configRootCmd.AddCommand(NewSetContextCommand(rootSettings))
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
configRootCmd.AddCommand(NewGetManagementConfigCommand(rootSettings))
configRootCmd.AddCommand(NewSetManagementConfigCommand(rootSettings))
configRootCmd.AddCommand(NewGetContextCommand(cfgFactory))
configRootCmd.AddCommand(NewSetContextCommand(cfgFactory))
configRootCmd.AddCommand(NewImportCommand(rootSettings))
configRootCmd.AddCommand(NewGetManagementConfigCommand(cfgFactory))
configRootCmd.AddCommand(NewSetManagementConfigCommand(cfgFactory))
configRootCmd.AddCommand(NewImportCommand(cfgFactory))
configRootCmd.AddCommand(NewUseContextCommand(cfgFactory))
configRootCmd.AddCommand(NewGetManifestCommand(cfgFactory))
configRootCmd.AddCommand(NewSetManifestCommand(cfgFactory))
// Init will have different factory
configRootCmd.AddCommand(NewInitCommand(rootSettings))
configRootCmd.AddCommand(NewUseContextCommand(rootSettings))
configRootCmd.AddCommand(NewGetManifestCommand(rootSettings))
configRootCmd.AddCommand(NewSetManifestCommand(rootSettings))
return configRootCmd
}

View File

@ -20,6 +20,7 @@ import (
"testing"
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil"
)
@ -28,7 +29,7 @@ func TestConfig(t *testing.T) {
{
Name: "config-cmd-with-help",
CmdLine: "--help",
Cmd: cmd.NewConfigCommand(nil),
Cmd: cmd.NewConfigCommand(&environment.AirshipCTLSettings{}),
},
}

View File

@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
)
const (
@ -44,7 +43,7 @@ airshipctl config get-context exampleContext
// NewGetContextCommand creates a command for viewing cluster information
// defined in the airshipctl config file.
func NewGetContextCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewGetContextCommand(cfgFactory config.Factory) *cobra.Command {
o := &config.ContextOptions{}
cmd := &cobra.Command{
Use: "get-context [NAME]",
@ -53,7 +52,10 @@ func NewGetContextCommand(rootSettings *environment.AirshipCTLSettings) *cobra.C
Example: getContextExample,
Aliases: []string{"get-contexts"},
RunE: func(cmd *cobra.Command, args []string) error {
airconfig := rootSettings.Config
airconfig, err := cfgFactory()
if err != nil {
return err
}
if len(args) == 1 {
o.Name = args[0]
}

View File

@ -24,7 +24,6 @@ import (
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil"
)
@ -36,15 +35,15 @@ const (
)
func TestGetContextCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{
Config: &config.Config{
settings := func() (*config.Config, error) {
return &config.Config{
Contexts: map[string]*config.Context{
fooContext: getNamedTestContext(fooContext),
barContext: getNamedTestContext(barContext),
bazContext: getNamedTestContext(bazContext),
},
CurrentContext: bazContext,
},
}, nil
}
cmdTests := []*testutil.CmdTest{
@ -85,7 +84,7 @@ func TestGetContextCmd(t *testing.T) {
}
func TestNoContextsGetContextCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{Config: new(config.Config)}
settings := func() (*config.Config, error) { return new(config.Config), nil }
cmdTest := &testutil.CmdTest{
Name: "no-contexts",
CmdLine: "",

View File

@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/config"
)
const getManagementConfigExample = `
@ -32,7 +32,7 @@ airshipctl config get-management-config default
`
// NewGetManagementConfigCommand creates a command that enables printing a management configuration to stdout.
func NewGetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewGetManagementConfigCommand(cfgFactory config.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "get-management-config [NAME]",
Short: "View a management config or all management configs defined in the airshipctl config",
@ -40,10 +40,14 @@ func NewGetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings)
Args: cobra.MaximumNArgs(1),
Aliases: []string{"get-management-configs"},
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
if len(args) == 1 {
name := args[0]
config, err := rootSettings.Config.GetManagementConfiguration(name)
config, err := cfg.GetManagementConfiguration(name)
if err != nil {
return err
}
@ -53,21 +57,21 @@ func NewGetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings)
return nil
}
if len(rootSettings.Config.ManagementConfiguration) == 0 {
if len(cfg.ManagementConfiguration) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No management configurations defined.")
return nil
}
// Print all of the management configurations in order by name
keys := make([]string, 0, len(rootSettings.Config.ManagementConfiguration))
for key := range rootSettings.Config.ManagementConfiguration {
keys := make([]string, 0, len(cfg.ManagementConfiguration))
for key := range cfg.ManagementConfiguration {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
config := rootSettings.Config.ManagementConfiguration[key]
config := cfg.ManagementConfiguration[key]
fmt.Fprintf(cmd.OutOrStdout(), "name: %s\n%s\n", key, config.String())
}

View File

@ -21,24 +21,23 @@ import (
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 TestGetManagementConfigCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{
Config: &config.Config{
settings := func() (*config.Config, error) {
return &config.Config{
ManagementConfiguration: map[string]*config.ManagementConfiguration{
config.AirshipDefaultContext: testutil.DummyManagementConfiguration(),
"test": {
Type: redfishdell.ClientType,
},
},
},
}, nil
}
emptySettings := &environment.AirshipCTLSettings{
Config: &config.Config{},
emptySettings := func() (*config.Config, error) {
return &config.Config{}, nil
}
cmdTests := []*testutil.CmdTest{

View File

@ -20,7 +20,6 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
)
const (
@ -39,7 +38,7 @@ airshipctl config get-manifest e2e
// NewGetManifestCommand creates a command for viewing the manifest information
// defined in the airshipctl config file.
func NewGetManifestCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewGetManifestCommand(cfgFactory config.Factory) *cobra.Command {
o := &config.ManifestOptions{}
cmd := &cobra.Command{
Use: "get-manifest NAME",
@ -49,7 +48,10 @@ func NewGetManifestCommand(rootSettings *environment.AirshipCTLSettings) *cobra.
Args: cobra.MaximumNArgs(1),
Aliases: []string{"get-manifests"},
RunE: func(cmd *cobra.Command, args []string) error {
airconfig := rootSettings.Config
airconfig, err := cfgFactory()
if err != nil {
return err
}
if len(args) == 1 {
o.Name = args[0]
manifest, exists := airconfig.Manifests[o.Name]

View File

@ -20,22 +20,23 @@ import (
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil"
)
func TestGetManifestConfigCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{
Config: &config.Config{
settings := func() (*config.Config, error) {
return &config.Config{
Manifests: map[string]*config.Manifest{
"fooManifestConfig": getTestManifest("foo"),
"barManifestConfig": getTestManifest("bar"),
"bazManifestConfig": getTestManifest("baz"),
},
},
}, nil
}
noConfigSettings := &environment.AirshipCTLSettings{Config: new(config.Config)}
noConfigSettings := func() (*config.Config, error) {
return new(config.Config), nil
}
cmdTests := []*testutil.CmdTest{
{

View File

@ -19,7 +19,7 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/config"
)
const (
@ -35,7 +35,7 @@ airshipctl config import $HOME/.kube/config
// NewImportCommand creates a command that merges clusters, contexts, and
// users from a kubeConfig file into the airshipctl config file.
func NewImportCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewImportCommand(cfgFactory config.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "import <kubeConfig>",
Short: "Merge information from a kubernetes config file",
@ -43,8 +43,12 @@ func NewImportCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
Example: useImportExample,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
kubeConfigPath := args[0]
err := rootSettings.Config.ImportFromKubeConfig(kubeConfigPath)
err = cfg.ImportFromKubeConfig(kubeConfigPath)
if err != nil {
return err
}

View File

@ -20,12 +20,14 @@ import (
"testing"
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/testutil"
)
func TestConfigImport(t *testing.T) {
settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
settings := func() (*config.Config, error) {
return testutil.DummyConfig(), nil
}
cmdTests := []*testutil.CmdTest{
{

View File

@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
)
const (
@ -47,7 +46,7 @@ airshipctl config set-context \
// NewSetContextCommand creates a command for creating and modifying contexts
// in the airshipctl config
func NewSetContextCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewSetContextCommand(cfgFactory config.Factory) *cobra.Command {
o := &config.ContextOptions{}
cmd := &cobra.Command{
Use: "set-context NAME",
@ -66,7 +65,11 @@ func NewSetContextCommand(rootSettings *environment.AirshipCTLSettings) *cobra.C
return nil
}
modified, err := config.RunSetContext(o, rootSettings.Config, true)
cfg, err := cfgFactory()
if err != nil {
return err
}
modified, err := config.RunSetContext(o, cfg, true)
if err != nil {
return err

View File

@ -26,7 +26,6 @@ import (
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil"
)
@ -127,12 +126,14 @@ func TestSetContext(t *testing.T) {
func (test setContextTest) run(t *testing.T) {
// Get the Environment
settings := &environment.AirshipCTLSettings{Config: test.givenConfig}
settings := func() (*config.Config, error) {
return test.givenConfig, nil
}
test.cmdTest.Cmd = cmd.NewSetContextCommand(settings)
testutil.RunTest(t, test.cmdTest)
afterRunConf := settings.Config
afterRunConf := test.givenConfig
// Find the Context Created or Modified
afterRunContext, err := afterRunConf.GetContext(test.contextName)

View File

@ -19,7 +19,7 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/remote/redfish"
)
@ -36,7 +36,7 @@ const (
// NewSetManagementConfigCommand creates a command for creating and modifying clusters
// in the airshipctl config file.
func NewSetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewSetManagementConfigCommand(cfgFactory config.Factory) *cobra.Command {
var insecure bool
var managementType string
var useProxy bool
@ -46,8 +46,12 @@ func NewSetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings)
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 := rootSettings.Config.GetManagementConfiguration(name)
managementCfg, err := cfg.GetManagementConfiguration(name)
if err != nil {
return err
}
@ -88,7 +92,7 @@ func NewSetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings)
return nil
}
if err = rootSettings.Config.PersistConfig(true); err != nil {
if err = cfg.PersistConfig(true); err != nil {
return err
}

View File

@ -23,7 +23,6 @@ import (
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"
)
@ -32,8 +31,8 @@ func TestConfigSetManagementConfigurationCmd(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
settings := func() (*config.Config, error) {
return conf, nil
}
cmdTests := []*testutil.CmdTest{
@ -73,11 +72,11 @@ func TestConfigSetManagementConfigurationInsecure(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
settings := func() (*config.Config, error) {
return conf, nil
}
require.False(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Insecure)
require.False(t, conf.ManagementConfiguration[config.AirshipDefaultContext].Insecure)
testutil.RunTest(t, &testutil.CmdTest{
Name: "config-cmd-set-management-config-change-insecure",
@ -90,19 +89,19 @@ func TestConfigSetManagementConfigurationInsecure(t *testing.T) {
Cmd: cmd.NewSetManagementConfigCommand(settings),
})
assert.True(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Insecure)
assert.True(t, conf.ManagementConfiguration[config.AirshipDefaultContext].Insecure)
}
func TestConfigSetManagementConfigurationType(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
settings := func() (*config.Config, error) {
return conf, nil
}
require.NotEqual(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
conf.ManagementConfiguration[config.AirshipDefaultContext].Type)
cmdTests := []*testutil.CmdTest{
{
@ -124,18 +123,18 @@ func TestConfigSetManagementConfigurationType(t *testing.T) {
}
assert.Equal(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
conf.ManagementConfiguration[config.AirshipDefaultContext].Type)
}
func TestConfigSetManagementConfigurationUseProxy(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
settings := func() (*config.Config, error) {
return conf, nil
}
require.False(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
require.False(t, conf.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
testutil.RunTest(t, &testutil.CmdTest{
Name: "config-cmd-set-management-config-change-use-proxy",
@ -143,20 +142,20 @@ func TestConfigSetManagementConfigurationUseProxy(t *testing.T) {
Cmd: cmd.NewSetManagementConfigCommand(settings),
})
assert.True(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
assert.True(t, conf.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
}
func TestConfigSetManagementConfigurationMultipleOptions(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
settings := &environment.AirshipCTLSettings{
Config: conf,
settings := func() (*config.Config, error) {
return conf, nil
}
require.NotEqual(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
require.False(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
conf.ManagementConfiguration[config.AirshipDefaultContext].Type)
require.False(t, conf.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
testutil.RunTest(t, &testutil.CmdTest{
Name: "config-cmd-set-management-config-change-type",
@ -166,6 +165,6 @@ func TestConfigSetManagementConfigurationMultipleOptions(t *testing.T) {
})
assert.Equal(t, redfishdell.ClientType,
settings.Config.ManagementConfiguration[config.AirshipDefaultContext].Type)
assert.True(t, settings.Config.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
conf.ManagementConfiguration[config.AirshipDefaultContext].Type)
assert.True(t, conf.ManagementConfiguration[config.AirshipDefaultContext].UseProxy)
}

View File

@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log"
)
@ -58,7 +57,7 @@ airshipctl config set-manifest e2e \
// NewSetManifestCommand creates a command for creating and modifying manifests
// in the airshipctl config file.
func NewSetManifestCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewSetManifestCommand(cfgFactory config.Factory) *cobra.Command {
o := &config.ManifestOptions{}
cmd := &cobra.Command{
Use: "set-manifest NAME",
@ -67,8 +66,12 @@ func NewSetManifestCommand(rootSettings *environment.AirshipCTLSettings) *cobra.
Example: setManifestsExample,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
o.Name = args[0]
modified, err := config.RunSetManifest(o, rootSettings.Config, true)
modified, err := config.RunSetManifest(o, cfg, true)
// Check if URL flag is passed with empty value
if cmd.Flags().Changed("url") && o.URL == "" {
log.Fatal("Repository URL cannot be empty.")

View File

@ -24,7 +24,6 @@ import (
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil"
)
@ -125,11 +124,13 @@ func TestSetManifest(t *testing.T) {
}
func (test setManifestTest) run(t *testing.T) {
settings := &environment.AirshipCTLSettings{Config: test.inputConfig}
settings := func() (*config.Config, error) {
return test.inputConfig, nil
}
test.cmdTest.Cmd = cmd.NewSetManifestCommand(settings)
testutil.RunTest(t, test.cmdTest)
afterRunConf := settings.Config
afterRunConf := test.inputConfig
// Find the Manifest Created or Modified
afterRunManifest, _ := afterRunConf.Manifests[test.manifestName]
require.NotNil(t, afterRunManifest)

View File

@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
)
const (
@ -38,7 +37,7 @@ airshipctl config use-context exampleContext
)
// NewUseContextCommand creates a command for switching to a defined airshipctl context.
func NewUseContextCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
func NewUseContextCommand(cfgFactory config.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "use-context NAME",
Short: "Switch to a different context",
@ -46,8 +45,12 @@ func NewUseContextCommand(rootSettings *environment.AirshipCTLSettings) *cobra.C
Example: useContextExample,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := cfgFactory()
if err != nil {
return err
}
contextName := args[0]
err := config.RunUseContext(contextName, rootSettings.Config)
err = config.RunUseContext(contextName, cfg)
if err != nil {
return err
}

View File

@ -21,12 +21,14 @@ import (
"testing"
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/testutil"
)
func TestConfigUseContext(t *testing.T) {
settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
settings := func() (*config.Config, error) {
return testutil.DummyConfig(), nil
}
cmdTests := []*testutil.CmdTest{
{
Name: "config-use-context",