added username/password or token as env variable

This commit is contained in:
Manuel Ganter 2025-11-11 15:41:07 +01:00
parent bb9214cd2c
commit b2ad30266a
No known key found for this signature in database
7 changed files with 559 additions and 27 deletions

View file

@ -2,6 +2,7 @@ package provider
import (
"context"
"os"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
@ -34,25 +35,29 @@ func (p *EdgeConnectProvider) Metadata(ctx context.Context, req provider.Metadat
func (p *EdgeConnectProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Provider for Edge Connect API. Supports authentication via token or username/password.",
MarkdownDescription: "Provider for Edge Connect API. Supports authentication via token or username/password. " +
"Configuration can be provided via attributes or environment variables.",
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
MarkdownDescription: "Edge Connect API endpoint",
Required: true,
MarkdownDescription: "Edge Connect API endpoint. Can also be set via `EDGE_CONNECT_ENDPOINT` environment variable.",
Optional: true,
},
"token": schema.StringAttribute{
MarkdownDescription: "Edge Connect API token. Required if username/password are not provided.",
Optional: true,
Sensitive: true,
MarkdownDescription: "Edge Connect API token. Required if username/password are not provided. " +
"Can also be set via `EDGE_CONNECT_TOKEN` environment variable.",
Optional: true,
Sensitive: true,
},
"username": schema.StringAttribute{
MarkdownDescription: "Edge Connect API username. Required if token is not provided.",
Optional: true,
MarkdownDescription: "Edge Connect API username. Required if token is not provided. " +
"Can also be set via `EDGE_CONNECT_USERNAME` environment variable.",
Optional: true,
},
"password": schema.StringAttribute{
MarkdownDescription: "Edge Connect API password. Required if token is not provided.",
Optional: true,
Sensitive: true,
MarkdownDescription: "Edge Connect API password. Required if token is not provided. " +
"Can also be set via `EDGE_CONNECT_PASSWORD` environment variable.",
Optional: true,
Sensitive: true,
},
},
}
@ -67,17 +72,34 @@ func (p *EdgeConnectProvider) Configure(ctx context.Context, req provider.Config
return
}
// Read configuration values, falling back to environment variables
endpoint := data.Endpoint.ValueString()
if endpoint == "" {
endpoint = os.Getenv("EDGE_CONNECT_ENDPOINT")
}
token := data.Token.ValueString()
if token == "" {
token = os.Getenv("EDGE_CONNECT_TOKEN")
}
username := data.Username.ValueString()
if username == "" {
username = os.Getenv("EDGE_CONNECT_USERNAME")
}
password := data.Password.ValueString()
if password == "" {
password = os.Getenv("EDGE_CONNECT_PASSWORD")
}
// Validate endpoint
if endpoint == "" {
resp.Diagnostics.AddAttributeError(
path.Root("endpoint"),
"Missing Edge Connect API Endpoint",
"The provider cannot create the Edge Connect API client as there is a missing or empty value for the Edge Connect API endpoint.",
"The provider cannot create the Edge Connect API client as there is a missing or empty value for the Edge Connect API endpoint. "+
"Set the endpoint value in the configuration or use the EDGE_CONNECT_ENDPOINT environment variable.",
)
}
@ -89,8 +111,8 @@ func (p *EdgeConnectProvider) Configure(ctx context.Context, req provider.Config
resp.Diagnostics.AddError(
"Missing Authentication Credentials",
"The provider requires authentication credentials. Please provide either:\n"+
" - token: API token for authentication, or\n"+
" - username and password: Username and password for authentication",
" - token: API token for authentication (via 'token' attribute or EDGE_CONNECT_TOKEN environment variable), or\n"+
" - username and password: Username and password for authentication (via 'username'/'password' attributes or EDGE_CONNECT_USERNAME/EDGE_CONNECT_PASSWORD environment variables)",
)
}