Skip to content

Commit 60d5333

Browse files
committed
Pull code for obtaining provider factory into a separate method
1 parent 1cb7d18 commit 60d5333

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

internal/command/init.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,34 +187,9 @@ func (c *InitCommand) initBackend(ctx context.Context, root *configs.Module, ext
187187
return nil, true, diags
188188
case root.StateStore != nil:
189189
// state_store config present
190-
// Access provider factories
191-
ctxOpts, err := c.contextOpts()
192-
if err != nil {
193-
diags = diags.Append(err)
194-
return nil, true, diags
195-
}
196-
197-
if root.StateStore.ProviderAddr.IsZero() {
198-
// This should not happen; this data is populated when parsing config,
199-
// even for builtin providers
200-
panic(fmt.Sprintf("unknown provider while beginning to initialize state store %q from provider %q",
201-
root.StateStore.Type,
202-
root.StateStore.Provider.Name))
203-
}
204-
205-
var exists bool
206-
factory, exists := ctxOpts.Providers[root.StateStore.ProviderAddr]
207-
if !exists {
208-
diags = diags.Append(&hcl.Diagnostic{
209-
Severity: hcl.DiagError,
210-
Summary: "Provider unavailable",
211-
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.",
212-
root.StateStore.Provider.Name,
213-
root.StateStore.ProviderAddr,
214-
root.StateStore.Type,
215-
),
216-
Subject: &root.StateStore.TypeRange,
217-
})
190+
factory, fDiags := c.Meta.getStateStoreProviderFactory(root.StateStore)
191+
diags = diags.Append(fDiags)
192+
if fDiags.HasErrors() {
218193
return nil, true, diags
219194
}
220195

internal/command/meta_backend.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,41 @@ func (m *Meta) assertSupportedCloudInitOptions(mode cloud.ConfigChangeMode) tfdi
17461746
return diags
17471747
}
17481748

1749+
func (m *Meta) getStateStoreProviderFactory(config *configs.StateStore) (providers.Factory, tfdiags.Diagnostics) {
1750+
var diags tfdiags.Diagnostics
1751+
1752+
if config.ProviderAddr.IsZero() {
1753+
// This should not happen; this data is populated when parsing config,
1754+
// even for builtin providers
1755+
panic(fmt.Sprintf("unknown provider while beginning to initialize state store %q from provider %q",
1756+
config.Type,
1757+
config.Provider.Name))
1758+
}
1759+
1760+
ctxOpts, err := m.contextOpts()
1761+
if err != nil {
1762+
diags = diags.Append(err)
1763+
return nil, diags
1764+
}
1765+
1766+
factory, exists := ctxOpts.Providers[config.ProviderAddr]
1767+
if !exists {
1768+
diags = diags.Append(&hcl.Diagnostic{
1769+
Severity: hcl.DiagError,
1770+
Summary: "Provider unavailable",
1771+
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.",
1772+
config.Provider.Name,
1773+
config.ProviderAddr,
1774+
config.Type,
1775+
),
1776+
Subject: &config.TypeRange,
1777+
})
1778+
return nil, diags
1779+
}
1780+
1781+
return factory, diags
1782+
}
1783+
17491784
//-------------------------------------------------------------------
17501785
// Output constants and initialization code
17511786
//-------------------------------------------------------------------

0 commit comments

Comments
 (0)