Skip to content

Commit

Permalink
Fix osc profile configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Dec 19, 2024
1 parent b06f49c commit 63f0dfc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 80 deletions.
20 changes: 10 additions & 10 deletions outscale/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 16 additions & 18 deletions outscale/framework_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
44 changes: 22 additions & 22 deletions outscale/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.",
},
Expand Down Expand Up @@ -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.",
Expand Down
8 changes: 4 additions & 4 deletions outscale/framework_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
`
50 changes: 24 additions & 26 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package outscale

import (
"fmt"
"io/ioutil"
"os"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down

0 comments on commit 63f0dfc

Please sign in to comment.