in internal/provider/provider.go [71:172]
func (p *devlakeProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Info(ctx, "Configuring Devlake client")
// Retrieve provider data from configuration
var config devlakeProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// If practitioner provided a configuration value for any of the
// attributes, it must be a known value.
if config.Host.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("host"),
"Unknown Devlake API Host",
"The provider cannot create the Devlake API client as there is an unknown configuration value for the Devlake API host. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the DEVLAKE_HOST environment variable.",
)
}
if config.Token.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Unknown Devlake API Token",
"The provider cannot create the Devlake API client as there is an unknown configuration value for the Devlake API token. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the DEVLAKE_TOKEN environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
// Default values to environment variables, but override
// with Terraform configuration value if set.
host := os.Getenv("DEVLAKE_HOST")
token := os.Getenv("DEVLAKE_TOKEN")
if !config.Host.IsNull() {
host = config.Host.ValueString()
}
if !config.Token.IsNull() {
token = config.Token.ValueString()
}
// If any of the expected configurations are missing, return
// errors with provider-specific guidance.
if host == "" {
resp.Diagnostics.AddAttributeError(
path.Root("host"),
"Missing Devlake API Host",
"The provider cannot create the Devlake API client as there is a missing or empty value for the Devlake API host. "+
"Set the host value in the configuration or use the DEVLAKE_HOST environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if token == "" {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Missing Devlake API Token",
"The provider cannot create the Devlake API client as there is a missing or empty value for the Devlake API token. "+
"Set the token value in the configuration or use the DEVLAKE_TOKEN environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if resp.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "devlake_host", host)
ctx = tflog.SetField(ctx, "devlake_token", token)
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "devlake_token")
tflog.Debug(ctx, "Creating Devlake client")
// Create a new Devlake client using the configuration values
client, err := client.NewClient(&host, &token)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Devlake API Client",
"An unexpected error occurred when creating the Devlake API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"Devlake Client Error: "+err.Error(),
)
return
}
// Make the Devlake client available during DataSource and Resource
// type Configure methods.
resp.DataSourceData = client
resp.ResourceData = client
tflog.Info(ctx, "Configured Devlake client", map[string]any{"success": true})
}