Files
Packer-Cn/test/mini_plugin/datasource/sleeper/data.go
T
Lucas Bajolet 1f4f2aebca test: add mini plugin for testing
The mini plugin implemented here is a minimal Packer plugin that we use
for acceptance testing Packer core.

There's a few components exposed so we can write templates using it, and
make sure Packer interprets it all as it should, and runs/errors as we
expect it.
2024-06-07 14:38:20 -04:00

64 lines
1.4 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,DatasourceOutput
package sleeper
import (
"log"
"time"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/zclconf/go-cty/cty"
)
type Config struct {
Duration string `mapstructure:"duration" required:"true"`
}
type Datasource struct {
config Config
durationTime time.Duration
}
type DatasourceOutput struct {
Status bool `mapstructure:"status"`
}
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
}
dt, err := time.ParseDuration(d.config.Duration)
if err != nil {
return err
}
d.durationTime = dt
return nil
}
func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec()
}
func (d *Datasource) Execute() (cty.Value, error) {
log.Printf("[sleeper] Sleeping for %s", d.config.Duration)
time.Sleep(d.durationTime)
log.Printf("[sleeper] Done sleeping!")
output := DatasourceOutput{
Status: true,
}
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
}