From 7c2d1c34907590e8446f3e86bb658bb3d98813d6 Mon Sep 17 00:00:00 2001 From: Fabian Kammel Date: Wed, 18 May 2022 11:39:14 +0200 Subject: [PATCH] AB#2094 cloud provider specific configs (#151) add argument to generate cloud specific configuration file --- .../actions/constellation_create/action.yml | 2 +- cli/cmd/configgenerate.go | 35 ++++++++++++---- cli/cmd/configgenerate_test.go | 27 ++++++++++-- internal/config/config.go | 19 +++++++++ internal/config/config_test.go | 42 +++++++++++++++++++ 5 files changed, 113 insertions(+), 12 deletions(-) diff --git a/.github/actions/constellation_create/action.yml b/.github/actions/constellation_create/action.yml index 742bd0c12..0110e4091 100644 --- a/.github/actions/constellation_create/action.yml +++ b/.github/actions/constellation_create/action.yml @@ -29,7 +29,7 @@ runs: shell: bash - name: Constellation config generate run: | - constellation config generate + constellation config generate ${{ inputs.cloudProvider }} shell: bash - name: Constellation create run: | diff --git a/cli/cmd/configgenerate.go b/cli/cmd/configgenerate.go index 156cc638f..1ec30c50c 100644 --- a/cli/cmd/configgenerate.go +++ b/cli/cmd/configgenerate.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/edgelesssys/constellation/cli/cloudprovider" "github.com/edgelesssys/constellation/internal/config" "github.com/edgelesssys/constellation/internal/constants" "github.com/edgelesssys/constellation/internal/file" @@ -11,11 +12,16 @@ import ( func newConfigGenerateCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "generate", + Use: "generate {aws|azure|gcp}", Short: "Generate a default configuration file", Long: "Generate a default configuration file for your selected cloud provider.", - Args: cobra.ExactArgs(0), - RunE: runConfigGenerate, + Args: cobra.MatchAll( + cobra.ExactArgs(1), + isCloudProvider(0), + warnAWS(0), + ), + ValidArgsFunction: generateCompletion, + RunE: runConfigGenerate, } cmd.Flags().StringP("file", "f", constants.ConfigFilename, "output file") @@ -28,17 +34,21 @@ type generateFlags struct { func runConfigGenerate(cmd *cobra.Command, args []string) error { fileHandler := file.NewHandler(afero.NewOsFs()) - return configGenerate(cmd, fileHandler) + provider := cloudprovider.FromString(args[0]) + return configGenerate(cmd, fileHandler, provider) } -func configGenerate(cmd *cobra.Command, fileHandler file.Handler) error { +func configGenerate(cmd *cobra.Command, fileHandler file.Handler, provider cloudprovider.Provider) error { flags, err := parseGenerateFlags(cmd) if err != nil { return err } + conf := config.Default() + conf.RemoveProviderExcept(provider) + if flags.file == "-" { - content, err := encoder.NewEncoder(config.Default()).Encode() + content, err := encoder.NewEncoder(conf).Encode() if err != nil { return err } @@ -46,7 +56,7 @@ func configGenerate(cmd *cobra.Command, fileHandler file.Handler) error { return err } - return fileHandler.WriteYAML(flags.file, config.Default(), 0o644) + return fileHandler.WriteYAML(flags.file, conf, 0o644) } func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) { @@ -58,3 +68,14 @@ func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) { file: file, }, nil } + +// createCompletion handles the completion of the create command. It is frequently called +// while the user types arguments of the command to suggest completion. +func generateCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + switch len(args) { + case 0: + return []string{"aws", "gcp", "azure"}, cobra.ShellCompDirectiveNoFileComp + default: + return []string{}, cobra.ShellCompDirectiveError + } +} diff --git a/cli/cmd/configgenerate_test.go b/cli/cmd/configgenerate_test.go index 6577e1bab..cb1d7190d 100644 --- a/cli/cmd/configgenerate_test.go +++ b/cli/cmd/configgenerate_test.go @@ -4,6 +4,7 @@ import ( "bytes" "testing" + "github.com/edgelesssys/constellation/cli/cloudprovider" "github.com/edgelesssys/constellation/internal/config" "github.com/edgelesssys/constellation/internal/constants" "github.com/edgelesssys/constellation/internal/file" @@ -20,7 +21,7 @@ func TestConfigGenerateDefault(t *testing.T) { fileHandler := file.NewHandler(afero.NewMemMapFs()) cmd := newConfigGenerateCmd() - require.NoError(configGenerate(cmd, fileHandler)) + require.NoError(configGenerate(cmd, fileHandler, cloudprovider.Unknown)) var readConfig config.Config err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig) @@ -28,6 +29,24 @@ func TestConfigGenerateDefault(t *testing.T) { assert.Equal(*config.Default(), readConfig) } +func TestConfigGenerateDefaultGCPSpecific(t *testing.T) { + assert := assert.New(t) + require := require.New(t) + + wantConf := config.Default() + wantConf.RemoveProviderExcept(cloudprovider.GCP) + + fileHandler := file.NewHandler(afero.NewMemMapFs()) + cmd := newConfigGenerateCmd() + + require.NoError(configGenerate(cmd, fileHandler, cloudprovider.GCP)) + + var readConfig config.Config + err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig) + assert.NoError(err) + assert.Equal(*wantConf, readConfig) +} + func TestConfigGenerateDefaultExists(t *testing.T) { require := require.New(t) @@ -35,7 +54,7 @@ func TestConfigGenerateDefaultExists(t *testing.T) { require.NoError(fileHandler.Write(constants.ConfigFilename, []byte("foobar"), file.OptNone)) cmd := newConfigGenerateCmd() - require.Error(configGenerate(cmd, fileHandler)) + require.Error(configGenerate(cmd, fileHandler, cloudprovider.Unknown)) } func TestConfigGenerateFileFlagRemoved(t *testing.T) { @@ -45,7 +64,7 @@ func TestConfigGenerateFileFlagRemoved(t *testing.T) { cmd := newConfigGenerateCmd() cmd.ResetFlags() - require.Error(configGenerate(cmd, fileHandler)) + require.Error(configGenerate(cmd, fileHandler, cloudprovider.Unknown)) } func TestConfigGenerateStdOut(t *testing.T) { @@ -59,7 +78,7 @@ func TestConfigGenerateStdOut(t *testing.T) { cmd.SetOut(&outBuffer) require.NoError(cmd.Flags().Set("file", "-")) - require.NoError(configGenerate(cmd, fileHandler)) + require.NoError(configGenerate(cmd, fileHandler, cloudprovider.Unknown)) var readConfig config.Config require.NoError(yaml.NewDecoder(&outBuffer).Decode(&readConfig)) diff --git a/internal/config/config.go b/internal/config/config.go index dff521ede..423e47b14 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,6 +8,7 @@ import ( "fmt" "io/fs" + "github.com/edgelesssys/constellation/cli/cloudprovider" "github.com/edgelesssys/constellation/internal/constants" "github.com/edgelesssys/constellation/internal/file" ) @@ -217,6 +218,24 @@ func Default() *Config { } } +// RemoveProviderExcept removes all provider specific configurations, i.e., +// sets them to nil, except the one specified. +// If an unknown provider is passed, the same configuration is returned. +func (c *Config) RemoveProviderExcept(provider cloudprovider.Provider) { + currentProviderConfigs := c.Provider + c.Provider = ProviderConfig{} + switch provider { + case cloudprovider.Azure: + c.Provider.Azure = currentProviderConfigs.Azure + case cloudprovider.GCP: + c.Provider.GCP = currentProviderConfigs.GCP + case cloudprovider.QEMU: + c.Provider.QEMU = currentProviderConfigs.QEMU + default: + c.Provider = currentProviderConfigs + } +} + // FromFile returns config file with `name` read from `fileHandler` by parsing // it as YAML. // If name is empty, the default configuration is returned. diff --git a/internal/config/config_test.go b/internal/config/config_test.go index fd3684b98..60294537b 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -3,6 +3,7 @@ package config import ( "testing" + "github.com/edgelesssys/constellation/cli/cloudprovider" "github.com/edgelesssys/constellation/internal/constants" "github.com/edgelesssys/constellation/internal/file" "github.com/spf13/afero" @@ -92,3 +93,44 @@ func TestFromFile(t *testing.T) { }) } } + +func TestConfigRemoveProviderExcept(t *testing.T) { + testCases := map[string]struct { + removeExcept cloudprovider.Provider + wantAzure *AzureConfig + wantGCP *GCPConfig + wantQEMU *QEMUConfig + }{ + "except azure": { + removeExcept: cloudprovider.Azure, + wantAzure: Default().Provider.Azure, + }, + "except gcp": { + removeExcept: cloudprovider.GCP, + wantGCP: Default().Provider.GCP, + }, + "except qemu": { + removeExcept: cloudprovider.QEMU, + wantQEMU: Default().Provider.QEMU, + }, + "unknown provider": { + removeExcept: cloudprovider.Unknown, + wantAzure: Default().Provider.Azure, + wantGCP: Default().Provider.GCP, + wantQEMU: Default().Provider.QEMU, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + assert := assert.New(t) + + conf := Default() + conf.RemoveProviderExcept(tc.removeExcept) + + assert.Equal(tc.wantAzure, conf.Provider.Azure) + assert.Equal(tc.wantGCP, conf.Provider.GCP) + assert.Equal(tc.wantQEMU, conf.Provider.QEMU) + }) + } +}