Skip to content
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

datasource: add file datasource #12397

Open
wants to merge 1 commit into
base: main
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
2 changes: 2 additions & 0 deletions command/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

filebuilder "github.com/hashicorp/packer/builder/file"
nullbuilder "github.com/hashicorp/packer/builder/null"
filedatasource "github.com/hashicorp/packer/datasource/file"
hcppackerimagedatasource "github.com/hashicorp/packer/datasource/hcp-packer-image"
hcppackeriterationdatasource "github.com/hashicorp/packer/datasource/hcp-packer-iteration"
httpdatasource "github.com/hashicorp/packer/datasource/http"
Expand Down Expand Up @@ -63,6 +64,7 @@ var PostProcessors = map[string]packersdk.PostProcessor{
}

var Datasources = map[string]packersdk.Datasource{
"file": new(filedatasource.Datasource),
"hcp-packer-image": new(hcppackerimagedatasource.Datasource),
"hcp-packer-iteration": new(hcppackeriterationdatasource.Datasource),
"http": new(httpdatasource.Datasource),
Expand Down
104 changes: 104 additions & 0 deletions datasource/file/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate packer-sdc struct-markdown
//go:generate packer-sdc mapstructure-to-hcl2 -type DatasourceOutput,Config
package file

import (
"fmt"
"os"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/zclconf/go-cty/cty"
)

type Config struct {
common.PackerConfig `mapstructure:",squash"`
// The contents of the file to create
//
// This is useful especially for files that involve templating so that
// Packer can dynamically create files and expose them for later importing
// as attributes in another entity.
//
// If no contents are specified, the resulting file will be empty.
Contents string `mapstructure:"contents" required:"false"`
// The file to write the contents to.
Destination string `mapstructure:"destination" required:"true"`
// Erase the destination if it exists.
//
// Default: `false`
Force bool `mapstructure:"force" required:"false"`
}

type Datasource struct {
config Config
}

type DatasourceOutput struct {
// The path of the file created
Path string `mapstructure:"path"`
}

func (d *Datasource) ConfigSpec() hcldec.ObjectSpec {
return d.config.FlatMapstructure().HCL2Spec()
}

func (d *Datasource) Configure(raws ...interface{}) error {
err := config.Decode(&d.config, nil, raws...)
if err != nil {
return err
}

if d.config.Destination == "" {
return fmt.Errorf("The `destination` must be specified.")
}

return nil
}

func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec()
}

func (d *Datasource) Execute() (cty.Value, error) {
nulVal := cty.NullVal(cty.EmptyObject)

_, err := os.Stat(d.config.Destination)
if err == nil {
if !d.config.Force {
return nulVal, fmt.Errorf("destination file %q already exists", d.config.Destination)
}
}

dest, err := os.OpenFile(d.config.Destination, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return nulVal, fmt.Errorf("failed to create destination %q: %s", d.config.Destination, err)
}

defer dest.Close()

written, err := dest.Write([]byte(d.config.Contents))
if err != nil {
defer os.Remove(d.config.Destination)
return nulVal, fmt.Errorf("failed to write contents to %q: %s", d.config.Destination, err)
}

if written != len(d.config.Contents) {
defer os.Remove(d.config.Destination)
return nulVal, fmt.Errorf(
"failed to write contents to %q: expected to write %d bytes, but wrote %d instead",
d.config.Destination,
len(d.config.Contents),
written)
}

output := DatasourceOutput{
Path: d.config.Destination,
}

return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
}
74 changes: 74 additions & 0 deletions datasource/file/data.hcl2spec.go

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

180 changes: 180 additions & 0 deletions datasource/file/data_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package file

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/packer-plugin-sdk/acctest"
)

func TestFileDataSource(t *testing.T) {
tests := []struct {
name string
template string
createOutput bool
expectError bool
expectOutput string
}{
{
"Success - write empty file",
basicEmptyFileWrite,
false,
false,
"",
},
{
"Fail - write empty file, pre-existing output",
basicEmptyFileWrite,
true,
true,
"",
},
{
"Success - write empty file, pre-existing output",
basicEmptyFileWriteForce,
true,
false,
"",
},
{
"Success - write template to output",
basicFileWithTemplateContents,
false,
false,
"contents are 12345\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testCase := &acctest.PluginTestCase{
Name: tt.name,
Setup: func() error {
return nil
},
Teardown: func() error {
return nil
},
Template: tt.template,
Type: "http",
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 && !tt.expectError {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
if tt.expectError && buildCommand.ProcessState.ExitCode() == 0 {
return fmt.Errorf("Expected an error but succeeded.")
}
}

if tt.expectError {
return nil
}

outFile, err := os.ReadFile("output")
if err != nil {
return fmt.Errorf("failed to read output file: %s", err)
}

diff := cmp.Diff(string(outFile), tt.expectOutput)
if diff != "" {
return fmt.Errorf("diff found in output: %s", diff)
}

return nil
},
}

os.RemoveAll("output")
if tt.createOutput {
err := os.WriteFile("output", []byte{}, 0644)
if err != nil {
t.Fatalf("failed to pre-create output file: %s", err)
}
}

acctest.TestPlugin(t, testCase)

os.RemoveAll("output")
})
}
}

var basicEmptyFileWrite string = `
source "null" "test" {
communicator = "none"
}

data "file" "empty" {
destination = "output"
}

build {
sources = [
"source.null.test"
]

provisioner "shell-local" {
inline = [
"set -ex",
"test -f ${data.file.empty.path}",
]
}
}
`

var basicEmptyFileWriteForce string = `
source "null" "test" {
communicator = "none"
}

data "file" "empty" {
destination = "output"
force = true
}

build {
sources = [
"source.null.test"
]

provisioner "shell-local" {
inline = [
"set -ex",
"test -f ${data.file.empty.path}",
]
}
}
`

var basicFileWithTemplateContents string = `
source "null" "test" {
communicator = "none"
}

data "file" "empty" {
contents = templatefile("test-fixtures/template.pkrtpl.hcl", {
"value" = "12345",
})
destination = "output"
}

build {
sources = [
"source.null.test"
]

provisioner "shell-local" {
inline = [
"set -ex",
"test -f ${data.file.empty.path}",
]
}
}
`
1 change: 1 addition & 0 deletions datasource/file/test-fixtures/template.pkrtpl.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contents are ${value}
Loading