From 2cf4dbcfe1191cb820e8dde369a3c5624ffb6fad Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> Date: Thu, 4 Apr 2024 13:46:40 +0200 Subject: [PATCH] Config: Remove unused field on config reload (#585) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description At config creation with exo cli here is the output: ```Toml [[accounts]] account = 'no longer used name foo' defaultZone = 'ch-gva-2' key = 'EXOxxx' name = 'foo' secret = 'xxx' ``` When you switch account with `exo config` here is the reloaded config account foo: ```Toml [[accounts]] account = 'no longer used name foo' clientTimeout = 20 defaultTemplate = 'Linux Ubuntu 22.04 LTS 64-bit' defaultZone = 'ch-gva-2' environment = 'api' key = 'EXOxxx' name = 'foo' secret = 'xxx' ``` I agree those config fields can be useful to be set by the user if needed but not auto generated on basic configuration profile. Here is what I Updated in the change, by default we create the config: ```Bash ✔ [+] API Key [none]: EXOxxx [+] Secret Key [none]: xxx [+] Name [none]: foo ``` No more Account string name is required or asked. Here is the generated config: ```Toml [[accounts]] defaultZone = 'ch-gva-2' key = 'EXOxxx' name = 'foo' secret = 'xxx' ``` When the config reloads, the config remains the same. If an attribute like `defaultTemplate = 'Linux Ubuntu 22.04 LTS 64-bit'` has been set or from the past config it will be kept same for other one like `environment`...etc The benefits of that, config are generated lighter and remain light, user can still personalize it like before...etc. Now, default value (`defaultTemplate`, `timeout`...etc) will be used from code `const` variable, and will be not ingested at config runtime, and will be no more generated at config reload. --------- Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> --- CHANGELOG.md | 9 +++++++++ README.md | 2 -- cmd/client.go | 6 +++++- cmd/cmd.go | 6 +++++- cmd/config.go | 30 +++++++++++++++++++----------- cmd/config_add.go | 13 +------------ cmd/root.go | 18 +++--------------- pkg/account/account.go | 3 ++- 8 files changed, 44 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d8e0ad8c..f82e72682 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## Unreleased + +### Features + +### Improvements + +### Deprecations +- Config: Remove unused field on config reload #585 + ## 1.77.0 ### Features diff --git a/README.md b/README.md index dcbd03814..3d62c562c 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,6 @@ The configuration parameters are then saved in a `exoscale.toml` file with the f defaultaccount = "account_name" [[accounts]] - account = "account_name" - endpoint = "https://api.exoscale.com/v1" key = "API_KEY" name = "account_name" secret = "API_SECRET" diff --git a/cmd/client.go b/cmd/client.go index bc89f2df8..9298c4ea4 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -62,10 +62,14 @@ func buildClient() { httpClient := &http.Client{Transport: newCLIRoundTripper(http.DefaultTransport, account.CurrentAccount.CustomHeaders)} + clientTimeout := account.CurrentAccount.ClientTimeout + if clientTimeout == 0 { + clientTimeout = defaultClientTimeout + } clientExoV2, err := exov2.NewClient( account.CurrentAccount.Key, account.CurrentAccount.APISecret(), - exov2.ClientOptWithTimeout(time.Minute*time.Duration(account.CurrentAccount.ClientTimeout)), + exov2.ClientOptWithTimeout(time.Minute*time.Duration(clientTimeout)), exov2.ClientOptWithHTTPClient(httpClient), exov2.ClientOptCond(func() bool { if v := os.Getenv("EXOSCALE_TRACE"); v != "" { diff --git a/cmd/cmd.go b/cmd/cmd.go index 675448944..89c6f7d3b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -81,7 +81,11 @@ func cmdSetZoneFlagFromDefault(cmd *cobra.Command) { // set once this function returns. func cmdSetTemplateFlagFromDefault(cmd *cobra.Command) { if cmd.Flag("template").Value.String() == "" { - cmd.Flag("template").Value.Set(account.CurrentAccount.DefaultTemplate) // nolint:errcheck + if account.CurrentAccount.DefaultTemplate != "" { + cmd.Flag("template").Value.Set(account.CurrentAccount.DefaultTemplate) // nolint:errcheck + } else { + cmd.Flag("template").Value.Set(defaultTemplate) // nolint:errcheck + } } } diff --git a/cmd/config.go b/cmd/config.go index d5a55a25e..2e98bb52f 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -113,23 +113,37 @@ func saveConfig(filePath string, newAccounts *account.Config) error { accounts[i]["name"] = acc.Name accounts[i]["key"] = acc.Key - accounts[i]["endpoint"] = acc.Endpoint accounts[i]["defaultZone"] = acc.DefaultZone - accounts[i]["defaultOutputFormat"] = acc.DefaultOutputFormat - accounts[i]["clientTimeout"] = acc.ClientTimeout - accounts[i]["environment"] = acc.Environment + if acc.ClientTimeout != 0 { + accounts[i]["clientTimeout"] = acc.ClientTimeout + } + if acc.DefaultOutputFormat != "" { + accounts[i]["defaultOutputFormat"] = acc.DefaultOutputFormat + } + // TODO(pej): This is a workaround to not propagate Environment 'api' on config reload. + // By default, acc.Environment is set to 'api' to be used for egoscale v2 in the whole codebase. + // We can not tweak it like DefaultTemplate by using the 'api' default const. + // Remove the environment when egoscale v3 will be fully integrated. + if acc.Environment != "" && acc.Environment != "api" { + accounts[i]["environment"] = acc.Environment + } + if acc.Endpoint != "" { + accounts[i]["endpoint"] = acc.Endpoint + } if acc.DefaultSSHKey != "" { accounts[i]["defaultSSHKey"] = acc.DefaultSSHKey } if acc.DefaultTemplate != "" { accounts[i]["defaultTemplate"] = acc.DefaultTemplate } + if acc.Account != "" { + accounts[i]["account"] = acc.Account + } if len(acc.SecretCommand) != 0 { accounts[i]["secretCommand"] = acc.SecretCommand } else { accounts[i]["secret"] = acc.Secret } - accounts[i]["account"] = acc.Account conf.Accounts = append(conf.Accounts, acc) } @@ -140,14 +154,8 @@ func saveConfig(filePath string, newAccounts *account.Config) error { accounts[accountsSize+i]["name"] = acc.Name accounts[accountsSize+i]["key"] = acc.Key - accounts[accountsSize+i]["endpoint"] = acc.Endpoint accounts[accountsSize+i]["secret"] = acc.Secret accounts[accountsSize+i]["defaultZone"] = acc.DefaultZone - accounts[accountsSize+i]["environment"] = acc.Environment - if acc.DefaultSSHKey != "" { - accounts[accountsSize+i]["defaultSSHKey"] = acc.DefaultSSHKey - } - accounts[accountsSize+i]["account"] = acc.Account conf.Accounts = append(conf.Accounts, acc) } } diff --git a/cmd/config_add.go b/cmd/config_add.go index 4d9422dfe..79337e51d 100644 --- a/cmd/config_add.go +++ b/cmd/config_add.go @@ -68,10 +68,7 @@ func promptAccountInformation() (*account.Account, error) { var client *exo.Client reader := bufio.NewReader(os.Stdin) - account := &account.Account{ - Key: "", - Secret: "", - } + account := &account.Account{} apiKey, err := readInput(reader, "API Key", account.Key) if err != nil { @@ -94,14 +91,6 @@ func promptAccountInformation() (*account.Account, error) { account.Secret = secretKey } - acc, err := readInput(reader, "Account name", account.Account) - if err != nil { - return nil, err - } - if acc != "" { - account.Account = acc - } - name, err := readInput(reader, "Name", account.Name) if err != nil { return nil, err diff --git a/cmd/root.go b/cmd/root.go index 40fd803c1..2660e7586 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -87,10 +87,9 @@ func Execute(version, commit string) { func init() { account.CurrentAccount = &account.Account{ - DefaultZone: defaultZone, - DefaultTemplate: defaultTemplate, - Environment: defaultEnvironment, - SosEndpoint: defaultSosEndpoint, + DefaultZone: defaultZone, + Environment: defaultEnvironment, + SosEndpoint: defaultSosEndpoint, } gConfig = viper.New() @@ -159,7 +158,6 @@ func initConfig() { //nolint:gocyclo if apiKeyFromEnv != "" && apiSecretFromEnv != "" { account.CurrentAccount.Name = "" gConfigFilePath = "" - account.CurrentAccount.Account = "unknown" account.CurrentAccount.Key = apiKeyFromEnv account.CurrentAccount.Secret = apiSecretFromEnv @@ -173,9 +171,6 @@ func initConfig() { //nolint:gocyclo if sosEndpointFromEnv != "" { account.CurrentAccount.SosEndpoint = sosEndpointFromEnv } - if account.CurrentAccount.ClientTimeout == 0 { - account.CurrentAccount.ClientTimeout = defaultClientTimeout - } account.GAllAccount = &account.Config{ DefaultAccount: account.CurrentAccount.Name, @@ -307,17 +302,10 @@ func initConfig() { //nolint:gocyclo } } - if account.CurrentAccount.DefaultTemplate == "" { - account.CurrentAccount.DefaultTemplate = defaultTemplate - } - if account.CurrentAccount.SosEndpoint == "" { account.CurrentAccount.SosEndpoint = defaultSosEndpoint } - if account.CurrentAccount.ClientTimeout == 0 { - account.CurrentAccount.ClientTimeout = defaultClientTimeout - } clientTimeoutFromEnv := readFromEnv("EXOSCALE_API_TIMEOUT") if clientTimeoutFromEnv != "" { if t, err := strconv.Atoi(clientTimeoutFromEnv); err == nil { diff --git a/pkg/account/account.go b/pkg/account/account.go index 3cc0909bf..d2118dd15 100644 --- a/pkg/account/account.go +++ b/pkg/account/account.go @@ -13,7 +13,8 @@ var ( ) type Account struct { - Name string + Name string + // TODO: deprecated field, will be deleted. Account string // TODO: remove it to replace it with the new API listZones. SosEndpoint string