Skip to content

Commit

Permalink
hcl2template: simplify startDatasource function
Browse files Browse the repository at this point in the history
  • Loading branch information
lbajolet-hashicorp committed Oct 3, 2023
1 parent f4dcae2 commit 5bae773
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions hcl2template/types.datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
"github.com/hashicorp/packer/packer"
"github.com/zclconf/go-cty/cty"
)

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

func (cfg *PackerConfig) startDatasource(dataSourceStore packer.DatasourceStore, ref DatasourceRef, secondaryEvaluation bool) (packersdk.Datasource, hcl.Diagnostics) {
func (cfg *PackerConfig) startDatasource(ds DatasourceBlock) (packersdk.Datasource, hcl.Diagnostics) {
var diags hcl.Diagnostics
block := cfg.Datasources[ref].block
block := ds.block

dataSourceStore := cfg.parser.PluginConfig.DataSources

if dataSourceStore == nil {
diags = append(diags, &hcl.Diagnostic{
Summary: "Unknown " + dataSourceLabel + " type " + ref.Type,
Summary: "Unknown " + dataSourceLabel + " type " + ds.Type,
Subject: block.LabelRanges[0].Ptr(),
Detail: "packer does not currently know any data source.",
Severity: hcl.DiagError,
})
return nil, diags
}

if !dataSourceStore.Has(ref.Type) {
if !dataSourceStore.Has(ds.Type) {
diags = append(diags, &hcl.Diagnostic{
Summary: "Unknown " + dataSourceLabel + " type " + ref.Type,
Summary: "Unknown " + dataSourceLabel + " type " + ds.Type,
Subject: block.LabelRanges[0].Ptr(),
Detail: fmt.Sprintf("known data sources: %v", dataSourceStore.List()),
Severity: hcl.DiagError,
})
return nil, diags
}

datasource, err := dataSourceStore.Start(ref.Type)
datasource, err := dataSourceStore.Start(ds.Type)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Summary: err.Error(),
Expand All @@ -99,7 +100,7 @@ func (cfg *PackerConfig) startDatasource(dataSourceStore packer.DatasourceStore,
}
if datasource == nil {
diags = append(diags, &hcl.Diagnostic{
Summary: fmt.Sprintf("failed to start datasource plugin %q.%q", ref.Type, ref.Name),
Summary: fmt.Sprintf("failed to start datasource plugin %q.%q", ds.Type, ds.Name),
Subject: &block.DefRange,
Severity: hcl.DiagError,
})
Expand Down
2 changes: 1 addition & 1 deletion hcl2template/types.packer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (cfg *PackerConfig) recursivelyEvaluateDatasources(ref DatasourceRef, depen
// If we've gotten here, then it means ref doesn't seem to have any further
// dependencies we need to evaluate first. Evaluate it, with the cfg's full
// data source context.
datasource, startDiags := cfg.startDatasource(cfg.parser.PluginConfig.DataSources, ref, true)
datasource, startDiags := cfg.startDatasource(ds)
if startDiags.HasErrors() {
diags = append(diags, startDiags...)
return dependencies, diags
Expand Down

0 comments on commit 5bae773

Please sign in to comment.