Skip to content

Commit

Permalink
handle external providers with preloaded schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Feb 13, 2025
1 parent 8b84cab commit d70c9ff
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
17 changes: 15 additions & 2 deletions internal/schemarepo/loadschemas/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ type Plugins struct {
provisionerFactories map[string]provisioners.Factory

preloadedProviderSchemas map[addrs.Provider]providers.ProviderSchema
preloadedIdentitySchemas map[addrs.Provider]providers.ResourceIdentitySchemas
}

func NewPlugins(
providerFactories map[addrs.Provider]providers.Factory,
provisionerFactories map[string]provisioners.Factory,
preloadedProviderSchemas map[addrs.Provider]providers.ProviderSchema,
preloadedIdentitySchemas map[addrs.Provider]providers.ResourceIdentitySchemas,
) *Plugins {
ret := &Plugins{
providerFactories: providerFactories,
provisionerFactories: provisionerFactories,
preloadedProviderSchemas: preloadedProviderSchemas,
preloadedIdentitySchemas: preloadedIdentitySchemas,
}
return ret
}
Expand All @@ -54,6 +57,11 @@ func (cp *Plugins) HasPreloadedSchemaForProvider(addr addrs.Provider) bool {
return ok
}

func (cp *Plugins) HasPreloadedIdentitySchemaForProvider(addr addrs.Provider) bool {
_, ok := cp.preloadedIdentitySchemas[addr]
return ok
}

func (cp *Plugins) NewProviderInstance(addr addrs.Provider) (providers.Interface, error) {
f, ok := cp.providerFactories[addr]
if !ok {
Expand Down Expand Up @@ -208,12 +216,17 @@ func (cp *Plugins) ResourceIdentitySchemas(addr addrs.Provider) (providers.Resou
return schemas, nil
}

// TODO preload identity schemas?
if is, ok := cp.preloadedIdentitySchemas[addr]; ok {
return is, nil
}

log.Printf("[TRACE] terraform.contextPlugins: Initializing provider %q to read its resource identity schemas", addr)
provider, err := cp.NewProviderInstance(addr)
if err != nil {
return schemas, fmt.Errorf("failed to instantiate provider %q to obtain resource identity schemas: %s", addr, err)
// A provider might have provided preloaded resource schemas but is missing the identity schemas.
// We will return an empty set of schemas in this case.
log.Printf("[TRACE] failed to instantiate provider %q to obtain resource identity schemas: %s", addr, err)
return providers.ResourceIdentitySchemas{}, nil
}
defer provider.Close()

Expand Down
7 changes: 4 additions & 3 deletions internal/terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type ContextOpts struct {
Providers map[addrs.Provider]providers.Factory
Provisioners map[string]provisioners.Factory

// PreloadedProviderSchemas is an optional map of provider schemas that
// were already loaded from providers by the caller. This is intended
// PreloadedProviderSchemas and PreloadedIdentitySchemas are optional maps of provider
// schemas that were already loaded from providers by the caller. This is intended
// to avoid redundant re-fetching of schemas when the caller has already
// loaded them for some other reason.
//
Expand All @@ -59,6 +59,7 @@ type ContextOpts struct {
// Callers must not access (read or write) the given map once it has
// been passed to Terraform Core using this field.
PreloadedProviderSchemas map[addrs.Provider]providers.ProviderSchema
PreloadedIdentitySchemas map[addrs.Provider]providers.ResourceIdentitySchemas

UIInput UIInput
}
Expand Down Expand Up @@ -147,7 +148,7 @@ func NewContext(opts *ContextOpts) (*Context, tfdiags.Diagnostics) {
par = 10
}

plugins := newContextPlugins(opts.Providers, opts.Provisioners, opts.PreloadedProviderSchemas)
plugins := newContextPlugins(opts.Providers, opts.Provisioners, opts.PreloadedProviderSchemas, opts.PreloadedIdentitySchemas)

log.Printf("[TRACE] terraform.NewContext: complete")

Expand Down
3 changes: 2 additions & 1 deletion internal/terraform/context_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func newContextPlugins(
providerFactories map[addrs.Provider]providers.Factory,
provisionerFactories map[string]provisioners.Factory,
preloadedProviderSchemas map[addrs.Provider]providers.ProviderSchema,
preloadedIdentitySchemas map[addrs.Provider]providers.ResourceIdentitySchemas,
) *loadschemas.Plugins {
return loadschemas.NewPlugins(providerFactories, provisionerFactories, preloadedProviderSchemas)
return loadschemas.NewPlugins(providerFactories, provisionerFactories, preloadedProviderSchemas, preloadedIdentitySchemas)
}

// Schemas is a deprecated old name for schemarepo.Schemas
Expand Down
1 change: 1 addition & 0 deletions internal/terraform/context_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func simpleMockPluginLibrary() *loadschemas.Plugins {
},
},
nil,
nil,
)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/eval_context_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestBuildingEvalContextInitProvider(t *testing.T) {
ctx.ProviderCache = make(map[string]providers.Interface)
ctx.Plugins = newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): providers.FactoryFixed(testP),
}, nil, nil)
}, nil, nil, nil)

providerAddrDefault := addrs.AbsProviderConfig{
Module: addrs.RootModule,
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/graph_builder_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func TestApplyGraphBuilder_withChecks(t *testing.T) {

plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): providers.FactoryFixed(awsProvider),
}, nil, nil)
}, nil, nil, nil)

b := &ApplyGraphBuilder{
Config: testModule(t, "apply-with-checks"),
Expand Down
8 changes: 4 additions & 4 deletions internal/terraform/graph_builder_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPlanGraphBuilder(t *testing.T) {
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): providers.FactoryFixed(awsProvider),
addrs.NewDefaultProvider("openstack"): providers.FactoryFixed(openstackProvider),
}, nil, nil)
}, nil, nil, nil)

b := &PlanGraphBuilder{
Config: testModule(t, "graph-builder-plan-basic"),
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestPlanGraphBuilder_dynamicBlock(t *testing.T) {
})
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): providers.FactoryFixed(provider),
}, nil, nil)
}, nil, nil, nil)

b := &PlanGraphBuilder{
Config: testModule(t, "graph-builder-plan-dynblock"),
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestPlanGraphBuilder_attrAsBlocks(t *testing.T) {
})
plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): providers.FactoryFixed(provider),
}, nil, nil)
}, nil, nil, nil)

b := &PlanGraphBuilder{
Config: testModule(t, "graph-builder-plan-attr-as-blocks"),
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestPlanGraphBuilder_forEach(t *testing.T) {

plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): providers.FactoryFixed(awsProvider),
}, nil, nil)
}, nil, nil, nil)

b := &PlanGraphBuilder{
Config: testModule(t, "plan-for-each"),
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ func schemaOnlyProvidersForTesting(schemas map[addrs.Provider]providers.Provider
}
}

return newContextPlugins(factories, nil, nil)
return newContextPlugins(factories, nil, nil, nil)
}

0 comments on commit d70c9ff

Please sign in to comment.