From 63f0dfcaac8cbe693e37a6c3924966ea4c807721 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 13 Dec 2024 09:09:52 +0000 Subject: [PATCH] Fix osc profile configuration --- outscale/config.go | 20 ++++++------ outscale/framework_config.go | 34 +++++++++----------- outscale/framework_provider.go | 44 ++++++++++++------------- outscale/framework_provider_test.go | 8 ++--- outscale/provider.go | 50 ++++++++++++++--------------- utils/utils.go | 1 + 6 files changed, 77 insertions(+), 80 deletions(-) diff --git a/outscale/config.go b/outscale/config.go index 81865ce3b..2886e56e1 100644 --- a/outscale/config.go +++ b/outscale/config.go @@ -13,16 +13,16 @@ import ( // Config ... type Config struct { - AccessKeyID string - SecretKeyID string - Region string - TokenID string - Endpoints map[string]interface{} - X509CertPath string - X509KeyPath string - Insecure bool - ConfigFilePath string - Profile string + AccessKeyID string + SecretKeyID string + Region string + TokenID string + Endpoints map[string]interface{} + X509CertPath string + X509KeyPath string + Insecure bool + ConfigFile string + Profile string } // OutscaleClient client diff --git a/outscale/framework_config.go b/outscale/framework_config.go index fb3a7a887..fa2d06a44 100644 --- a/outscale/framework_config.go +++ b/outscale/framework_config.go @@ -4,7 +4,6 @@ import ( "context" "crypto/tls" "fmt" - "io/ioutil" "net/http" "os" "strings" @@ -25,7 +24,7 @@ type OutscaleClient_fw struct { // Client ... func (c *frameworkProvider) Client_fw(ctx context.Context, data *ProviderModel, diags *diag.Diagnostics) (*OutscaleClient_fw, error) { - ok, err := IsProfileSet(data) + ok, err := isProfileSet(data) if err != nil { return nil, err } @@ -75,39 +74,38 @@ func (c *frameworkProvider) Client_fw(ctx context.Context, data *ProviderModel, return client, nil } -func IsProfileSet(data *ProviderModel) (bool, error) { +func isProfileSet(data *ProviderModel) (bool, error) { isProfSet := false if profileName, ok := os.LookupEnv("OSC_PROFILE"); ok || !data.Profile.IsNull() { if data.Profile.ValueString() != "" { profileName = data.Profile.ValueString() } - var profilePath string - if envPath, ok := os.LookupEnv("OSC_CONFIG_FILE"); ok || !data.ConfigFilePath.IsNull() { - if data.ConfigFilePath.ValueString() != "" { - profilePath = data.ConfigFilePath.ValueString() + var configFilePath string + if envPath, ok := os.LookupEnv("OSC_CONFIG_FILE"); ok || !data.ConfigFile.IsNull() { + if data.ConfigFile.ValueString() != "" { + configFilePath = data.ConfigFile.ValueString() } else { - profilePath = envPath + configFilePath = envPath } - if profilePath == "" { - homePath, err := os.UserHomeDir() - if err != nil { - return isProfSet, err - } - profilePath = homePath + "/.osc/config.json" + } else { + homePath, err := os.UserHomeDir() + if err != nil { + return isProfSet, err } + configFilePath = homePath + utils.SuffixConfigFilePath } - jsonFile, err := ioutil.ReadFile(profilePath) + jsonFile, err := os.ReadFile(configFilePath) if err != nil { - return isProfSet, err + return isProfSet, fmt.Errorf("%v \n Connot found configue file: %v", err, configFilePath) } profile := gjson.GetBytes(jsonFile, profileName) if !gjson.Valid(profile.String()) { - return isProfSet, fmt.Errorf("Invalid json profile file") + return isProfSet, fmt.Errorf("invalid json profile file") } if !profile.Get("access_key").Exists() || !profile.Get("secret_key").Exists() { - return isProfSet, fmt.Errorf("Profile 'access_key' or 'secret_key' are not defined!") + return isProfSet, fmt.Errorf("profile 'access_key' or 'secret_key' are not defined! ") } setProfile(data, profile) isProfSet = true diff --git a/outscale/framework_provider.go b/outscale/framework_provider.go index 8c2927ed8..cf66141a1 100644 --- a/outscale/framework_provider.go +++ b/outscale/framework_provider.go @@ -22,28 +22,28 @@ func New(version string) provider.Provider { } type frameworkProvider struct { - accessKeyId types.String - secretKeyId types.String - region types.String - endpoints []Endpoints - x509CertPath string - x509KeyPath string - configFilePath string - insecure bool - profile string - version string + accessKeyId types.String + secretKeyId types.String + region types.String + endpoints []Endpoints + x509CertPath string + x509KeyPath string + configFile string + insecure bool + profile string + version string } type ProviderModel struct { - AccessKeyId types.String `tfsdk:"access_key_id"` - SecretKeyId types.String `tfsdk:"secret_key_id"` - Region types.String `tfsdk:"region"` - Endpoints []Endpoints `tfsdk:"endpoints"` - X509CertPath types.String `tfsdk:"x509_cert_path"` - X509KeyPath types.String `tfsdk:"x509_key_path"` - ConfigFilePath types.String `tfsdk:"config_file_path"` - Profile types.String `tfsdk:"profile"` - Insecure types.Bool `tfsdk:"insecure"` + AccessKeyId types.String `tfsdk:"access_key_id"` + SecretKeyId types.String `tfsdk:"secret_key_id"` + Region types.String `tfsdk:"region"` + Endpoints []Endpoints `tfsdk:"endpoints"` + X509CertPath types.String `tfsdk:"x509_cert_path"` + X509KeyPath types.String `tfsdk:"x509_key_path"` + ConfigFile types.String `tfsdk:"config_file"` + Profile types.String `tfsdk:"profile"` + Insecure types.Bool `tfsdk:"insecure"` } type Endpoints struct { @@ -91,7 +91,7 @@ func (p *frameworkProvider) Schema(ctx context.Context, req provider.SchemaReque Optional: true, Description: "The path to your x509 key", }, - "config_file_path": schema.StringAttribute{ + "config_file": schema.StringAttribute{ Optional: true, Description: "The path to your configuration file in which you have defined your credentials.", }, @@ -161,9 +161,9 @@ func (p *frameworkProvider) Configure(ctx context.Context, req provider.Configur "Either target apply the source of the value first, set the value statically in the configuration, or use the 'OSC_X509_CLIENT_KEY or OUTSCALE_X509KEY' environment variable.", ) } - if config.ConfigFilePath.IsUnknown() { + if config.ConfigFile.IsUnknown() { resp.Diagnostics.AddAttributeError( - path.Root("config_file_path"), + path.Root("config_file"), "Unknown Outscale API ConfigFilePath", "The provider cannot create the Outscale API client as there is an unknown configuration value for the Outscale API profile. "+ "Either target apply the source of the value first, set the value statically in the configuration, or use the 'OSC_CONFIG_FILE' environment variable.", diff --git a/outscale/framework_provider_test.go b/outscale/framework_provider_test.go index 31250c838..3e5ea504b 100644 --- a/outscale/framework_provider_test.go +++ b/outscale/framework_provider_test.go @@ -91,10 +91,10 @@ func TestDataSource_UpgradeFromVersion(t *testing.T) { } const fwtestAccDataSourceOutscaleQuotaConfig = ` - data "outscale_quota" "lbuquota1" { - filter { - name = "quota_names" - values = ["lb_listeners_limit"] + data "outscale_quota" "lbuquota1" { + filter { + name = "quota_names" + values = ["lb_listeners_limit"] } } ` diff --git a/outscale/provider.go b/outscale/provider.go index d4a38f46f..7a9362bef 100644 --- a/outscale/provider.go +++ b/outscale/provider.go @@ -2,7 +2,6 @@ package outscale import ( "fmt" - "io/ioutil" "os" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -60,7 +59,7 @@ func Provider() *schema.Provider { Optional: true, Description: "The path to your x509 key", }, - "config_file_path": { + "config_file": { Type: schema.TypeString, Optional: true, Description: "The path to your configuration file in which you have defined your credentials.", @@ -229,15 +228,15 @@ func Provider() *schema.Provider { func providerConfigureClient(d *schema.ResourceData) (interface{}, error) { config := Config{ - AccessKeyID: d.Get("access_key_id").(string), - SecretKeyID: d.Get("secret_key_id").(string), - Region: d.Get("region").(string), - Endpoints: make(map[string]interface{}), - X509CertPath: d.Get("x509_cert_path").(string), - X509KeyPath: d.Get("x509_key_path").(string), - ConfigFilePath: d.Get("config_file_path").(string), - Profile: d.Get("profile").(string), - Insecure: d.Get("insecure").(bool), + AccessKeyID: d.Get("access_key_id").(string), + SecretKeyID: d.Get("secret_key_id").(string), + Region: d.Get("region").(string), + Endpoints: make(map[string]interface{}), + X509CertPath: d.Get("x509_cert_path").(string), + X509KeyPath: d.Get("x509_key_path").(string), + ConfigFile: d.Get("config_file").(string), + Profile: d.Get("profile").(string), + Insecure: d.Get("insecure").(bool), } endpointsSet := d.Get("endpoints").(*schema.Set) for _, endpointsSetI := range endpointsSet.List() { @@ -263,32 +262,31 @@ func IsOldProfileSet(conf *Config) (bool, error) { profileName = conf.Profile } - var profilePath string - if envPath, ok := os.LookupEnv("OSC_CONFIG_FILE"); ok || conf.ConfigFilePath != "" { - if conf.ConfigFilePath != "" { - profilePath = conf.ConfigFilePath + var configFilePath string + if envPath, ok := os.LookupEnv("OSC_CONFIG_FILE"); ok || conf.ConfigFile != "" { + if conf.ConfigFile != "" { + configFilePath = conf.ConfigFile } else { - profilePath = envPath + configFilePath = envPath } - if profilePath == "" { - homePath, err := os.UserHomeDir() - if err != nil { - return isProfSet, err - } - profilePath = homePath + "/.osc/config.json" + } else { + homePath, err := os.UserHomeDir() + if err != nil { + return isProfSet, err } + configFilePath = homePath + utils.SuffixConfigFilePath } - jsonFile, err := ioutil.ReadFile(profilePath) + jsonFile, err := os.ReadFile(configFilePath) if err != nil { - return isProfSet, err + return isProfSet, fmt.Errorf("%v \nConnot found configue file: %v", err, configFilePath) } profile := gjson.GetBytes(jsonFile, profileName) if !gjson.Valid(profile.String()) { - return isProfSet, fmt.Errorf("Invalid json profile file") + return isProfSet, fmt.Errorf("invalid json profile file") } if !profile.Get("access_key").Exists() || !profile.Get("secret_key").Exists() { - return isProfSet, fmt.Errorf("Profile 'access_key' or 'secret_key' are not defined!") + return isProfSet, fmt.Errorf("profile 'access_key' or 'secret_key' are not defined! ") } setOldProfile(conf, profile) isProfSet = true diff --git a/utils/utils.go b/utils/utils.go index 8dc31be3a..34c874bb4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -34,6 +34,7 @@ const ( MaxSize int = 14901 LinkedPolicyNotFound string = "5102" InvalidState string = "InvalidState" + SuffixConfigFilePath string = "/.osc/config.json" pathRegex string = "^(/[a-zA-Z0-9/_]+/)" pathError string = "path must begin and end with '/' and contain only alphanumeric characters and/or '/', '_' characters" VolumeIOPSError string = `