open-nomad/helper/pluginutils/hclutils/util.go

75 lines
2.0 KiB
Go
Raw Normal View History

2018-12-21 19:23:21 +00:00
package hclutils
2018-08-09 20:20:10 +00:00
import (
"bytes"
"fmt"
"github.com/hashicorp/hcl2/hcl"
2018-08-09 20:20:10 +00:00
hjson "github.com/hashicorp/hcl2/hcl/json"
"github.com/hashicorp/hcl2/hcldec"
2018-08-09 20:20:10 +00:00
"github.com/hashicorp/nomad/nomad/structs"
"github.com/ugorji/go/codec"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
)
// ParseHclInterface is used to convert an interface value representing a hcl2
// body and return the interpolated value. Vars may be nil if there are no
// variables to interpolate.
func ParseHclInterface(val interface{}, spec hcldec.Spec, vars map[string]cty.Value) (cty.Value, hcl.Diagnostics) {
evalCtx := &hcl.EvalContext{
Variables: vars,
Functions: GetStdlibFuncs(),
}
2018-08-09 20:20:10 +00:00
// Encode to json
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, structs.JsonHandle)
err := enc.Encode(val)
if err != nil {
// Convert to a hcl diagnostics message
return cty.NilVal, hcl.Diagnostics([]*hcl.Diagnostic{
{
Severity: hcl.DiagError,
Summary: "Failed to JSON encode value",
Detail: fmt.Sprintf("JSON encoding failed: %v", err),
}})
}
// Parse the json as hcl2
hclFile, diag := hjson.Parse(buf.Bytes(), "")
if diag.HasErrors() {
return cty.NilVal, diag
}
value, decDiag := hcldec.Decode(hclFile.Body, spec, evalCtx)
2018-08-09 20:20:10 +00:00
diag = diag.Extend(decDiag)
if diag.HasErrors() {
return cty.NilVal, diag
}
return value, diag
}
// GetStdlibFuncs returns the set of stdlib functions.
func GetStdlibFuncs() map[string]function.Function {
return map[string]function.Function{
"abs": stdlib.AbsoluteFunc,
"coalesce": stdlib.CoalesceFunc,
"concat": stdlib.ConcatFunc,
"hasindex": stdlib.HasIndexFunc,
"int": stdlib.IntFunc,
"jsondecode": stdlib.JSONDecodeFunc,
"jsonencode": stdlib.JSONEncodeFunc,
"length": stdlib.LengthFunc,
"lower": stdlib.LowerFunc,
"max": stdlib.MaxFunc,
"min": stdlib.MinFunc,
"reverse": stdlib.ReverseFunc,
"strlen": stdlib.StrlenFunc,
"substr": stdlib.SubstrFunc,
"upper": stdlib.UpperFunc,
}
}