Skip to content

Recognize tf query files #36929

New issue

Have a question about this project? Sign up for a free account to open an issue and contact its maintainers and the community.

By clicking “Sign up for ”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on ? Sign in to your account

Open
wants to merge 6 commits into
base: f-tf-search
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/addrs/resource.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,8 @@ func (r Resource) String() string {
return fmt.Sprintf("data.%s.%s", r.Type, r.Name)
case EphemeralResourceMode:
return fmt.Sprintf("ephemeral.%s.%s", r.Type, r.Name)
case ListResourceMode:
return fmt.Sprintf("list.%s.%s", r.Type, r.Name)
default:
// Should never happen, but we'll return a string here rather than
// crashing just in case it does.
Expand DownExpand Up@@ -511,6 +513,10 @@ const (
// EphemeralResourceMode indicates an ephemeral resource, as defined by
// "ephemeral" blocks in configuration.
EphemeralResourceMode ResourceMode = 'E'

// ListResourceMode indicates a list resource, as defined by
// "list" blocks in tfquery configuration.
ListResourceMode ResourceMode = 'L'
)

// AbsResourceInstanceObject represents one of the specific remote objects
Expand Down
9 changes: 6 additions & 3 deletions internal/addrs/resourcemode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on .

4 changes: 2 additions & 2 deletions internal/configs/configload/loader_load.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,8 +22,8 @@ import (
//
// LoadConfig performs the basic syntax and uniqueness validations that are
// required to process the individual modules
func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) {
return l.loadConfig(l.parser.LoadConfigDir(rootDir))
func (l *Loader) LoadConfig(rootDir string, parserOpts ...configs.Option) (*configs.Config, hcl.Diagnostics) {
return l.loadConfig(l.parser.LoadConfigDir(rootDir, parserOpts...))
}

// LoadConfigWithTests matches LoadConfig, except the configs.Config contains
Expand Down
73 changes: 73 additions & 0 deletions internal/configs/module.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,6 +49,7 @@ type Module struct {
ManagedResources map[string]*Resource
DataResources map[string]*Resource
EphemeralResources map[string]*Resource
ListResources map[string]*Resource

Moved []*Moved
Removed []*Removed
Expand DownExpand Up@@ -128,6 +129,7 @@ func NewModule(primaryFiles, overrideFiles []*File) (*Module, hcl.Diagnostics) {
ManagedResources: map[string]*Resource{},
EphemeralResources: map[string]*Resource{},
DataResources: map[string]*Resource{},
ListResources: map[string]*Resource{},
Checks: map[string]*Check{},
ProviderMetas: map[addrs.Provider]*ProviderMeta{},
Tests: map[string]*TestFile{},
Expand DownExpand Up@@ -197,6 +199,8 @@ func (m *Module) ResourceByAddr(addr addrs.Resource) *Resource {
return m.DataResources[key]
case addrs.EphemeralResourceMode:
return m.EphemeralResources[key]
case addrs.ListResourceMode:
return m.ListResources[key]
default:
return nil
}
Expand DownExpand Up@@ -494,6 +498,75 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
return diags
}

func (m *Module) appendQueryFile(file *QueryFile) hcl.Diagnostics {
var diags hcl.Diagnostics

for _, pc := range file.ProviderConfigs {
key := pc.moduleUniqueKey()
if existing, exists := m.ProviderConfigs[key]; exists {
if existing.Alias == "" {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate provider configuration",
Detail: fmt.Sprintf("A default (non-aliased) provider configuration for %q was already given at %s. If multiple configurations are required, set the \"alias\" argument for alternative configurations.", existing.Name, existing.DeclRange),
Subject: &pc.DeclRange,
})
} else {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate provider configuration",
Detail: fmt.Sprintf("A provider configuration for %q with alias %q was already given at %s. Each configuration for the same provider must have a distinct alias.", existing.Name, existing.Alias, existing.DeclRange),
Subject: &pc.DeclRange,
})
}
continue
}
m.ProviderConfigs[key] = pc
}

for _, v := range file.Variables {
if existing, exists := m.Variables[v.Name]; exists {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate variable declaration",
Detail: fmt.Sprintf("A variable named %q was already declared at %s. Variable names must be unique within a module.", existing.Name, existing.DeclRange),
Subject: &v.DeclRange,
})
}
m.Variables[v.Name] = v
}

for _, l := range file.Locals {
if existing, exists := m.Locals[l.Name]; exists {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate local value definition",
Detail: fmt.Sprintf("A local value named %q was already defined at %s. Local value names must be unique within a module.", existing.Name, existing.DeclRange),
Subject: &l.DeclRange,
})
}
m.Locals[l.Name] = l
}

for _, ql := range file.ListResources {
key := ql.moduleUniqueKey()
if existing, exists := m.ListResources[key]; exists {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Duplicate list %q configuration", existing.Type),
Detail: fmt.Sprintf("A %s list named %q was already declared at %s. List names must be unique per type in each module.", existing.Type, existing.Name, existing.DeclRange),
Subject: &ql.DeclRange,
})
continue
}
// set the provider FQN for the resource
m.ListResources[key] = ql
ql.Provider = m.ProviderForLocalConfig(ql.ProviderConfigAddr())
}

return diags
}

func (m *Module) mergeFile(file *File) hcl.Diagnostics {
var diags hcl.Diagnostics

Expand Down
11 changes: 11 additions & 0 deletions internal/configs/parser_config.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,6 +48,17 @@ func (p *Parser) LoadTestFile(path string) (*TestFile, hcl.Diagnostics) {
return test, diags
}

func (p *Parser) LoadQueryFile(path string) (*QueryFile, hcl.Diagnostics) {
body, diags := p.LoadHCLFile(path)
if body == nil {
return nil, diags
}

query, queryDiags := loadQueryFile(body)
diags = append(diags, queryDiags...)
return query, diags
}

// LoadMockDataFile reads the file at the given path and parses it as a
// Terraform mock data file.
//
Expand Down
Loading