in internal/provider/provider.go [82:178]
func (p *AlzProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Debug(ctx, "Provider configuration started")
if p.data != nil {
tflog.Debug(ctx, "Provider AlzLib already present, skipping configuration")
resp.DataSourceData = p.data
resp.ResourceData = p.data
return
}
tflog.Debug(ctx, "Provider AlzLib not present, beginning configuration")
var data gen.AlzModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Read the environment variables and set in data
// if the data is not already set and the environment variable is set.
configureFromEnvironment(&data)
// Set the go sdk's azidentity specific environment variables
configureAzIdentityEnvironment(&data)
// Configure aux tenant ids from config and environment.
if resp.Diagnostics = append(resp.Diagnostics, configureAuxTenants(ctx, &data)...); resp.Diagnostics.HasError() {
return
}
// Set the default values if not already set in the config or by environment.
configureDefaults(ctx, &data)
// Get a token credential.
cred, diags := getTokenCredential(data)
resp.Diagnostics = append(resp.Diagnostics, diags...)
if resp.Diagnostics.HasError() {
return
}
// Create the clients
clients, diags := getClients(cred, data, fmt.Sprintf("%s/%s", userAgentBase, p.version))
resp.Diagnostics = append(resp.Diagnostics, diags...)
if resp.Diagnostics.HasError() {
return
}
// Create the AlzLib.
alz, diags := configureAlzLib(cred, data, fmt.Sprintf("%s/%s", userAgentBase, p.version))
resp.Diagnostics = append(resp.Diagnostics, diags...)
if resp.Diagnostics.HasError() {
return
}
// Convert the supplied libraries to alzlib.LibraryReferences
libRefs, diags := generateLibraryDefinitions(ctx, &data)
resp.Diagnostics = append(resp.Diagnostics, diags...)
if resp.Diagnostics.HasError() {
return
}
// Fetch the library dependencies if enabled.
// If not, the refs passed to alzlib.Init() will be fetched on demand without dependencies.
if data.LibraryFetchDependencies.ValueBool() {
var err error
tflog.Debug(ctx, "Begin fetch library dependencies", map[string]interface{}{
"library_references": libRefs,
})
libRefs, err = libRefs.FetchWithDependencies(ctx)
if err != nil {
resp.Diagnostics.AddError("Failed to fetch library dependencies", err.Error())
return
}
tflog.Debug(ctx, "End fetch library dependencies", map[string]interface{}{
"library_references": libRefs,
})
}
// Init alzlib
if err := alz.Init(ctx, libRefs...); err != nil {
resp.Diagnostics.AddError("Failed to initialize AlzLib", err.Error())
return
}
// Store the alz pointer in the provider struct so we don't have to do all this work every time `.Configure` is called.
// Due to fetch from Azure, it takes approx 30 seconds each time and is called 4-5 time during a single acceptance test.
p.data = &alzProviderData{
AlzLib: alz,
mu: &sync.Mutex{},
clients: clients,
suppressWarningPolicyRoleAssignments: data.SuppressWarningPolicyRoleAssignments.ValueBool(),
}
resp.DataSourceData = p.data
resp.ResourceData = p.data
tflog.Debug(ctx, "Provider configuration finished")
}