Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better endpoints management #474

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions outscale/framework_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
72 changes: 26 additions & 46 deletions outscale/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,21 +25,25 @@ type frameworkProvider struct {
accessKeyId types.String
secretKeyId types.String
region types.String
endpoints map[string]interface{}
endpoints []Endpoints
x509CertPath string
x509KeyPath string
insecure bool
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 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) {
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
},
}
}
35 changes: 13 additions & 22 deletions outscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 != "" {
Expand Down
Loading