From ecd535be6789cc0256c9975d6f8d67aedccae1fd Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Wed, 11 Sep 2024 09:58:42 +0000 Subject: [PATCH] Better endpoints management --- outscale/framework_config.go | 10 ++--- outscale/framework_provider.go | 72 ++++++++++++---------------------- outscale/provider.go | 35 ++++++----------- 3 files changed, 44 insertions(+), 73 deletions(-) diff --git a/outscale/framework_config.go b/outscale/framework_config.go index 8093fa186..77c13939d 100644 --- a/outscale/framework_config.go +++ b/outscale/framework_config.go @@ -46,8 +46,8 @@ func (c *frameworkProvider) Client_fw(ctx context.Context, data *ProviderModel, oscConfig := oscgo.NewConfiguration() basePath := fmt.Sprintf("api.%s.outscale.com", data.Region.ValueString()) - if endpoint, ok := data.Endpoints["api"]; ok { - basePath = endpoint.(string) + if len(data.Endpoints) > 0 { + basePath = data.Endpoints[0].API.ValueString() if strings.Contains(basePath, "://") { if scheme, host, found := strings.Cut(basePath, "://"); found { oscConfig.Scheme = scheme @@ -97,9 +97,9 @@ func setDefaultEnv(data *ProviderModel) { } if len(data.Endpoints) == 0 { if endpoints := utils.GetEnvVariableValue([]string{"OSC_ENDPOINT_API", "OUTSCALE_OAPI_URL"}); endpoints != "" { - endpointsAttributes := make(map[string]interface{}) - endpointsAttributes["api"] = endpoints - data.Endpoints = endpointsAttributes + endp := make([]Endpoints, 1) + endp[0].API = types.StringValue(endpoints) + data.Endpoints = endp } } } diff --git a/outscale/framework_provider.go b/outscale/framework_provider.go index 602e1f616..979e34846 100644 --- a/outscale/framework_provider.go +++ b/outscale/framework_provider.go @@ -12,16 +12,9 @@ import ( ) var ( - _ provider.Provider = &frameworkProvider{} - endpointFwServiceNames []string + _ provider.Provider = &frameworkProvider{} ) -func init() { - endpointFwServiceNames = []string{ - "api", - } -} - func New(version string) provider.Provider { return &frameworkProvider{ version: version, @@ -32,7 +25,7 @@ type frameworkProvider struct { accessKeyId types.String secretKeyId types.String region types.String - endpoints map[string]interface{} + endpoints []Endpoints x509CertPath string x509KeyPath string insecure bool @@ -40,13 +33,17 @@ type frameworkProvider struct { } type ProviderModel struct { - AccessKeyId types.String `tfsdk:"access_key_id"` - SecretKeyId types.String `tfsdk:"secret_key_id"` - Region types.String `tfsdk:"region"` - Endpoints map[string]interface{} `tfsdk:"endpoints"` - X509CertPath types.String `tfsdk:"x509_cert_path"` - X509KeyPath types.String `tfsdk:"x509_key_path"` - 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"` + Insecure types.Bool `tfsdk:"insecure"` +} + +type Endpoints struct { + API types.String `tfsdk:"api"` } func (p *frameworkProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { @@ -56,6 +53,19 @@ func (p *frameworkProvider) Metadata(ctx context.Context, req provider.MetadataR func (p *frameworkProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { resp.Schema = schema.Schema{ + Blocks: map[string]schema.Block{ + "endpoints": schema.SetNestedBlock{ + NestedObject: schema.NestedBlockObject{ + Attributes: map[string]schema.Attribute{ + "api": schema.StringAttribute{ + Optional: true, + Description: "The Endpoints for API operations.", + }, + }, + }, + }, + }, + Attributes: map[string]schema.Attribute{ "access_key_id": schema.StringAttribute{ Optional: true, @@ -82,23 +92,9 @@ func (p *frameworkProvider) Schema(ctx context.Context, req provider.SchemaReque Description: "tls insecure connection", }, }, - Blocks: map[string]schema.Block{ - "endpoints": endpointsFwSchema(), - }, } } -/* - func (p *frameworkProvider) MetaSchema(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) { - resp.Schema = metaschema.Schema{ - Attributes: map[string]metaschema.Attribute{ - "module_name": metaschema.StringAttribute{ - Optional: true, - }, - }, - } - } -*/ func (p *frameworkProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { var config ProviderModel @@ -183,19 +179,3 @@ func (p *frameworkProvider) Resources(ctx context.Context) []func() resource.Res }*/ return nil } - -func endpointsFwSchema() schema.SetNestedBlock { - endpointsAttributes := make(map[string]schema.Attribute) - - for _, serviceKey := range endpointFwServiceNames { - endpointsAttributes[serviceKey] = schema.StringAttribute{ - Optional: true, - Description: "Use this to override the default service endpoint URL", - } - } - return schema.SetNestedBlock{ - NestedObject: schema.NestedBlockObject{ - Attributes: endpointsAttributes, - }, - } -} diff --git a/outscale/provider.go b/outscale/provider.go index 8e8757a2a..25ed9a4a0 100644 --- a/outscale/provider.go +++ b/outscale/provider.go @@ -32,7 +32,19 @@ func Provider() *schema.Provider { Optional: true, Description: "The Region for API operations.", }, - "endpoints": endpointsSchema(), + "endpoints": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "api": { + Type: schema.TypeString, + Optional: true, + Description: "The Endpoints for API operations.", + }, + }, + }, + }, "x509_cert_path": { Type: schema.TypeString, Optional: true, @@ -209,27 +221,6 @@ func providerConfigureClient(d *schema.ResourceData) (interface{}, error) { return config.Client() } -func endpointsSchema() *schema.Schema { - endpointsAttributes := make(map[string]*schema.Schema) - - for _, endpointServiceName := range endpointServiceNames { - endpointsAttributes[endpointServiceName] = &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "", - Description: "Use this to override the default service endpoint URL", - } - } - - return &schema.Schema{ - Type: schema.TypeSet, - Optional: true, - Elem: &schema.Resource{ - Schema: endpointsAttributes, - }, - } -} - func setProviderDefaultEnv(conf *Config) { if conf.AccessKeyID == "" { if accessKeyId := utils.GetEnvVariableValue([]string{"OSC_ACCESS_KEY", "OUTSCALE_ACCESSKEYID"}); accessKeyId != "" {