mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-24 23:19:39 -05:00
AB#2094 cloud provider specific configs (#151)
add argument to generate cloud specific configuration file
This commit is contained in:
parent
54e2e492df
commit
7c2d1c3490
@ -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: |
|
||||
|
@ -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,10 +12,15 @@ 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),
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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))
|
||||
|
@ -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.
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user