* Allow backend or state_store config to be passed via BackendOpts from calling code
* Update messages sent to view: make message specific to state storage mechanism in use
* Add nil pointer check
* Fix typos
* Pivot to `Len` method approach of nil check
* Pivot to the point of pirouetting
_=ctx// prevent staticcheck from complaining to avoid a maintenence hazard of having the wrong ctx in scope here
_=ctx// prevent staticcheck from complaining to avoid a maintenance hazard of having the wrong ctx in scope here
deferspan.End()
view.Output(views.InitializingBackendMessage)
ifroot.StateStore!=nil{
view.Output(views.InitializingStateStoreMessage)
}else{
view.Output(views.InitializingBackendMessage)
}
varopts*BackendOpts
switch{
caseroot.StateStore!=nil&&root.Backend!=nil:
// We expect validation during config parsing to prevent mutually exclusive backend and state_store blocks,
// but checking here just in case.
diags=diags.Append(&hcl.Diagnostic{
Severity:hcl.DiagError,
Summary:"Conflicting backend and state_store configurations present during init",
Detail:fmt.Sprintf("When initializing the backend there was configuration data present for both backend %q and state store %q. This is a bug in Terraform and should be reported.",
root.Backend.Type,
root.StateStore.Type,
),
Subject:&root.Backend.TypeRange,
})
returnnil,true,diags
caseroot.StateStore!=nil:
// state_store config present
// Access provider factories
ctxOpts,err:=c.contextOpts()
iferr!=nil{
diags=diags.Append(err)
returnnil,true,diags
}
ifroot.StateStore.ProviderAddr.IsZero(){
// This should not happen; this data is populated when parsing config,
// even for builtin providers
panic(fmt.Sprintf("unknown provider while beginning to initialize state store %q from provider %q",
Detail:fmt.Sprintf("The provider %s (%q) is required to initialize the %q state store, but the matching provider factory is missing. This is a bug in Terraform and should be reported.",
root.StateStore.Provider.Name,
root.StateStore.ProviderAddr,
root.StateStore.Type,
),
Subject:&root.Backend.TypeRange,
})
returnnil,true,diags
}
// If overrides supplied by -backend-config CLI flag, process them
varconfigOverridehcl.Body
if!extraConfig.Empty(){
// We need to launch an instance of the provider to get the config of the state store for processing any overrides.
provider,err:=factory()
deferprovider.Close()// Stop the child process once we're done with it here.
iferr!=nil{
diags=diags.Append(fmt.Errorf("error when obtaining provider instance during state store initialization: %w",err))
returnnil,true,diags
}
resp:=provider.GetProviderSchema()
varbackendConfig*configs.Backend
varbackendConfigOverridehcl.Body
ifroot.Backend!=nil{
iflen(resp.StateStores)==0{
diags=diags.Append(&hcl.Diagnostic{
Severity:hcl.DiagError,
Summary:"Provider does not support pluggable state storage",
Detail:fmt.Sprintf("There are no state stores implemented by provider %s (%q)",