Skip to content

Commit 51ec786

Browse files
hcl2template: simplify startDatasource function
1 parent d9259d6 commit 51ec786

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

hcl2template/types.datasource.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/hashicorp/hcl/v2/hclsyntax"
1111
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
1212
hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
13-
"github.com/hashicorp/packer/packer"
1413
"github.com/zclconf/go-cty/cty"
1514
)
1615

@@ -65,31 +64,33 @@ func (ds *Datasources) Values() (map[string]cty.Value, hcl.Diagnostics) {
6564
return res, diags
6665
}
6766

68-
func (cfg *PackerConfig) startDatasource(dataSourceStore packer.DatasourceStore, ref DatasourceRef, secondaryEvaluation bool) (packersdk.Datasource, hcl.Diagnostics) {
67+
func (cfg *PackerConfig) startDatasource(ds DatasourceBlock) (packersdk.Datasource, hcl.Diagnostics) {
6968
var diags hcl.Diagnostics
70-
block := cfg.Datasources[ref].block
69+
block := ds.block
70+
71+
dataSourceStore := cfg.parser.PluginConfig.DataSources
7172

7273
if dataSourceStore == nil {
7374
diags = append(diags, &hcl.Diagnostic{
74-
Summary: "Unknown " + dataSourceLabel + " type " + ref.Type,
75+
Summary: "Unknown " + dataSourceLabel + " type " + ds.Type,
7576
Subject: block.LabelRanges[0].Ptr(),
7677
Detail: "packer does not currently know any data source.",
7778
Severity: hcl.DiagError,
7879
})
7980
return nil, diags
8081
}
8182

82-
if !dataSourceStore.Has(ref.Type) {
83+
if !dataSourceStore.Has(ds.Type) {
8384
diags = append(diags, &hcl.Diagnostic{
84-
Summary: "Unknown " + dataSourceLabel + " type " + ref.Type,
85+
Summary: "Unknown " + dataSourceLabel + " type " + ds.Type,
8586
Subject: block.LabelRanges[0].Ptr(),
8687
Detail: fmt.Sprintf("known data sources: %v", dataSourceStore.List()),
8788
Severity: hcl.DiagError,
8889
})
8990
return nil, diags
9091
}
9192

92-
datasource, err := dataSourceStore.Start(ref.Type)
93+
datasource, err := dataSourceStore.Start(ds.Type)
9394
if err != nil {
9495
diags = append(diags, &hcl.Diagnostic{
9596
Summary: err.Error(),
@@ -99,7 +100,7 @@ func (cfg *PackerConfig) startDatasource(dataSourceStore packer.DatasourceStore,
99100
}
100101
if datasource == nil {
101102
diags = append(diags, &hcl.Diagnostic{
102-
Summary: fmt.Sprintf("failed to start datasource plugin %q.%q", ref.Type, ref.Name),
103+
Summary: fmt.Sprintf("failed to start datasource plugin %q.%q", ds.Type, ds.Name),
103104
Subject: &block.DefRange,
104105
Severity: hcl.DiagError,
105106
})

hcl2template/types.packer_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func (cfg *PackerConfig) recursivelyEvaluateDatasources(ref DatasourceRef, depen
381381
// If we've gotten here, then it means ref doesn't seem to have any further
382382
// dependencies we need to evaluate first. Evaluate it, with the cfg's full
383383
// data source context.
384-
datasource, startDiags := cfg.startDatasource(cfg.parser.PluginConfig.DataSources, ref, true)
384+
datasource, startDiags := cfg.startDatasource(ds)
385385
if startDiags.HasErrors() {
386386
diags = append(diags, startDiags...)
387387
return dependencies, diags

0 commit comments

Comments
 (0)