-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
schema: Account for short (Registry) module source address #94
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e | |
return nil, coreSchemaRequiredErr{} | ||
} | ||
|
||
if meta == nil || m.schemaReader == nil { | ||
if meta == nil { | ||
return m.coreSchema, nil | ||
} | ||
|
||
|
@@ -72,95 +72,97 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e | |
|
||
providerRefs := ProviderReferences(meta.ProviderReferences) | ||
|
||
for pAddr, pVersionCons := range meta.ProviderRequirements { | ||
pSchema, err := m.schemaReader.ProviderSchema(meta.Path, pAddr, pVersionCons) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
refs := providerRefs.ReferencesOfProvider(pAddr) | ||
for _, localRef := range refs { | ||
if pSchema.Provider != nil { | ||
mergedSchema.Blocks["provider"].DependentBody[schema.NewSchemaKey(schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: localRef.LocalName}, | ||
}, | ||
})] = pSchema.Provider | ||
} | ||
|
||
providerAddr := lang.Address{ | ||
lang.RootStep{Name: localRef.LocalName}, | ||
} | ||
if localRef.Alias != "" { | ||
providerAddr = append(providerAddr, lang.AttrStep{Name: localRef.Alias}) | ||
if m.schemaReader != nil { | ||
for pAddr, pVersionCons := range meta.ProviderRequirements { | ||
pSchema, err := m.schemaReader.ProviderSchema(meta.Path, pAddr, pVersionCons) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
for rName, rSchema := range pSchema.Resources { | ||
depKeys := schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: rName}, | ||
}, | ||
Attributes: []schema.AttributeDependent{ | ||
{ | ||
Name: "provider", | ||
Expr: schema.ExpressionValue{ | ||
Address: providerAddr, | ||
}, | ||
refs := providerRefs.ReferencesOfProvider(pAddr) | ||
for _, localRef := range refs { | ||
if pSchema.Provider != nil { | ||
mergedSchema.Blocks["provider"].DependentBody[schema.NewSchemaKey(schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: localRef.LocalName}, | ||
}, | ||
}, | ||
})] = pSchema.Provider | ||
} | ||
|
||
providerAddr := lang.Address{ | ||
lang.RootStep{Name: localRef.LocalName}, | ||
} | ||
if localRef.Alias != "" { | ||
providerAddr = append(providerAddr, lang.AttrStep{Name: localRef.Alias}) | ||
} | ||
mergedSchema.Blocks["resource"].DependentBody[schema.NewSchemaKey(depKeys)] = rSchema | ||
|
||
// No explicit association is required | ||
// if the resource prefix matches provider name | ||
if strings.HasPrefix(rName, localRef.LocalName+"_") { | ||
for rName, rSchema := range pSchema.Resources { | ||
depKeys := schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: rName}, | ||
}, | ||
Attributes: []schema.AttributeDependent{ | ||
{ | ||
Name: "provider", | ||
Expr: schema.ExpressionValue{ | ||
Address: providerAddr, | ||
}, | ||
}, | ||
}, | ||
} | ||
mergedSchema.Blocks["resource"].DependentBody[schema.NewSchemaKey(depKeys)] = rSchema | ||
} | ||
} | ||
|
||
for dsName, dsSchema := range pSchema.DataSources { | ||
depKeys := schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: dsName}, | ||
}, | ||
Attributes: []schema.AttributeDependent{ | ||
{ | ||
Name: "provider", | ||
Expr: schema.ExpressionValue{ | ||
Address: providerAddr, | ||
// No explicit association is required | ||
// if the resource prefix matches provider name | ||
if strings.HasPrefix(rName, localRef.LocalName+"_") { | ||
depKeys := schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: rName}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Add backend-related core bits of schema | ||
if isRemoteStateDataSource(pAddr, dsName) { | ||
dsSchema.Attributes["backend"].IsDepKey = true | ||
dsSchema.Attributes["backend"].Expr = backends.BackendTypesAsExprConstraints(m.terraformVersion) | ||
|
||
delete(dsSchema.Attributes, "config") | ||
depBodies := m.dependentBodyForRemoteStateDataSource(providerAddr, localRef) | ||
for key, depBody := range depBodies { | ||
mergedSchema.Blocks["data"].DependentBody[key] = depBody | ||
} | ||
mergedSchema.Blocks["resource"].DependentBody[schema.NewSchemaKey(depKeys)] = rSchema | ||
} | ||
} | ||
|
||
mergedSchema.Blocks["data"].DependentBody[schema.NewSchemaKey(depKeys)] = dsSchema | ||
|
||
// No explicit association is required | ||
// if the resource prefix matches provider name | ||
if strings.HasPrefix(dsName, localRef.LocalName+"_") { | ||
for dsName, dsSchema := range pSchema.DataSources { | ||
depKeys := schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: dsName}, | ||
}, | ||
Attributes: []schema.AttributeDependent{ | ||
{ | ||
Name: "provider", | ||
Expr: schema.ExpressionValue{ | ||
Address: providerAddr, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Add backend-related core bits of schema | ||
if isRemoteStateDataSource(pAddr, dsName) { | ||
dsSchema.Attributes["backend"].IsDepKey = true | ||
dsSchema.Attributes["backend"].Expr = backends.BackendTypesAsExprConstraints(m.terraformVersion) | ||
|
||
delete(dsSchema.Attributes, "config") | ||
depBodies := m.dependentBodyForRemoteStateDataSource(providerAddr, localRef) | ||
for key, depBody := range depBodies { | ||
mergedSchema.Blocks["data"].DependentBody[key] = depBody | ||
} | ||
} | ||
|
||
mergedSchema.Blocks["data"].DependentBody[schema.NewSchemaKey(depKeys)] = dsSchema | ||
|
||
// No explicit association is required | ||
// if the resource prefix matches provider name | ||
if strings.HasPrefix(dsName, localRef.LocalName+"_") { | ||
depKeys := schema.DependencyKeys{ | ||
Labels: []schema.LabelDependent{ | ||
{Index: 0, Value: dsName}, | ||
}, | ||
} | ||
mergedSchema.Blocks["data"].DependentBody[schema.NewSchemaKey(depKeys)] = dsSchema | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -176,17 +178,20 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e | |
} | ||
mergedSchema.Blocks["variable"].DependentBody = variableDependentBody(meta.Variables) | ||
} | ||
|
||
if m.moduleReader != nil { | ||
reader := m.moduleReader | ||
modules, err := reader.ModuleCalls(meta.Path) | ||
if err != nil { | ||
return mergedSchema, nil | ||
} | ||
|
||
for _, module := range modules { | ||
modMeta, err := reader.ModuleMeta(module.Path) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
depKeys := schema.DependencyKeys{ | ||
// Fetching based only on the source can cause conflicts for multiple versions of the same module | ||
// specially if they have different versions or the source of those modules have been modified | ||
|
@@ -205,6 +210,31 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e | |
if err == nil { | ||
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeys)] = depSchema | ||
} | ||
|
||
// There's likely more edge cases with how source address can be represented in config | ||
// vs in module manifest, but for now we at least account for the common case of TF Registry | ||
if strings.HasPrefix(module.SourceAddr, "registry.terraform.io/") { | ||
dbanck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shortName := strings.TrimPrefix(module.SourceAddr, "registry.terraform.io/") | ||
|
||
depKeys := schema.DependencyKeys{ | ||
// Fetching based only on the source can cause conflicts for multiple versions of the same module | ||
// specially if they have different versions or the source of those modules have been modified | ||
// inside the .terraform folder. This is a compromise that we made in this moment since it would impact only auto completion | ||
Attributes: []schema.AttributeDependent{ | ||
{ | ||
Name: "source", | ||
Expr: schema.ExpressionValue{ | ||
Static: cty.StringVal(shortName), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
depSchema, err := schemaForDependentModuleBlock(module.LocalName, modMeta) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the |
||
if err == nil { | ||
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeys)] = depSchema | ||
} | ||
} | ||
} | ||
} | ||
return mergedSchema, nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just makes testing a bit easier as less tests require provider schema.