in internal/provider/provider.go [76:206]
func (p *backupdrProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Info(ctx, "Configuring BackupDR client")
// Retrieve provider data from configuration
var config backupdrProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if config.Endpoint.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("endpoint"),
"Unknown BackupDR API Endpoint",
"The provider cannot create the BackupDR API client as there is an unknown configuration value for the BackupDR API Endpoint. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the BACKUPDR_ENDPOINT environment variable.",
)
}
if config.GcpAccessToken.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("user"),
"Unknown BackupDR API AccessToken",
"The provider cannot create the BackupDR API client as there is an unknown configuration value for the BackupDR API AcessToken. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the BACKUPDR_ACCESS_TOKEN environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
// Default values to environment variables, but override
// with Terraform configuration value if set.
endpoint := os.Getenv("BACKUPDR_ENDPOINT")
accessToken := os.Getenv("BACKUPDR_ACCESS_TOKEN")
if !config.Endpoint.IsNull() {
endpoint = config.Endpoint.ValueString()
}
if !config.GcpAccessToken.IsNull() {
accessToken = config.GcpAccessToken.ValueString()
}
// If any of the expected configurations are missing, return
// errors with provider-specific guidance.
if endpoint == "" {
resp.Diagnostics.AddAttributeError(
path.Root("endpoint"),
"Missing BackupDR API endpoint",
"The provider cannot create the BackupDR API client as there is a missing or empty value for the BackupDR API endpoint. "+
"Set the endpoint value in the configuration or use the BACKUPDR_ENDPOINT environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if accessToken == "" {
resp.Diagnostics.AddAttributeError(
path.Root("access_token"),
"Unknown BackupDR API AccessToken",
"The provider cannot create the BackupDR API client as there is a missing or empty value for the BackupDR API access_token. "+
"Set the access_token value in the configuration or use the BACKUPDR_ACCESS_TOKEN environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if resp.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "BACKUPDR_ENDPOINT", endpoint)
ctx = tflog.SetField(ctx, "BACKUPDR_ACCESS_TOKEN", accessToken)
tflog.Debug(ctx, "Creating BackupDR client")
// define auth context with accessToken
authCtx := context.WithValue(context.Background(), backupdr.ContextAccessToken, accessToken)
// define client configuration object
cfg := backupdr.NewConfiguration()
cfg.Host = endpoint
// define backupdr client using configuration object
client := *backupdr.NewAPIClient(cfg)
sessionObj, res, err := client.UserSessionApi.Login(authCtx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Session for BackupDR API Client",
"An unexpected error occurred when creating the BackupDR API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"BackupDR Client Error: "+err.Error(),
)
return
}
if res.StatusCode != 200 {
resp.Diagnostics.AddError(
"Unable to Create Session for BackupDR API Client",
"An unexpected error occurred when creating the BackupDR API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"BackupDR Client Error: "+res.Status,
)
}
if sessionObj.SessionId == "" {
resp.Diagnostics.AddError(
"Unable to Create SessionID for BackupDR API Client",
"An unexpected error occurred when creating the BackupDR API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"BackupDR SessionID "+fmt.Sprint(sessionObj),
)
return
}
p.authCtx = context.WithValue(authCtx, backupdr.ContextAPIKey, backupdr.APIKey{
Key: sessionObj.SessionId,
Prefix: "Actifio",
})
p.client = &client
// // Make the BackupDR client available during DataSource and Resource
// // type Configure methods.
resp.DataSourceData = p
resp.ResourceData = p
tflog.Info(ctx, "Configured BackupDR client", map[string]any{"success": true})
}