2018-12-21 19:23:21 +00:00
|
|
|
package hclutils
|
2018-08-09 20:20:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-07-19 17:04:39 +00:00
|
|
|
"errors"
|
2018-08-09 20:20:10 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2020-05-19 16:50:47 +00:00
|
|
|
hcl "github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
|
|
|
hjson "github.com/hashicorp/hcl/v2/json"
|
2018-08-09 20:20:10 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"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
|
2019-02-02 20:19:16 +00:00
|
|
|
// body and return the interpolated value. Vars may be nil if there are no
|
|
|
|
// variables to interpolate.
|
2019-07-19 17:04:39 +00:00
|
|
|
func ParseHclInterface(val interface{}, spec hcldec.Spec, vars map[string]cty.Value) (cty.Value, hcl.Diagnostics, []error) {
|
2019-02-02 20:19:16 +00:00
|
|
|
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
|
2019-07-19 17:04:39 +00:00
|
|
|
errorMessage := fmt.Sprintf("Label encoding failed: %v", err)
|
|
|
|
return cty.NilVal,
|
|
|
|
hcl.Diagnostics([]*hcl.Diagnostic{{
|
2018-08-09 20:20:10 +00:00
|
|
|
Severity: hcl.DiagError,
|
2019-07-19 17:04:39 +00:00
|
|
|
Summary: "Failed to encode label value",
|
|
|
|
Detail: errorMessage,
|
|
|
|
}}),
|
|
|
|
[]error{errors.New(errorMessage)}
|
2018-08-09 20:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the json as hcl2
|
|
|
|
hclFile, diag := hjson.Parse(buf.Bytes(), "")
|
|
|
|
if diag.HasErrors() {
|
2019-07-19 17:04:39 +00:00
|
|
|
return cty.NilVal, diag, formattedDiagnosticErrors(diag)
|
2018-08-09 20:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 20:19:16 +00:00
|
|
|
value, decDiag := hcldec.Decode(hclFile.Body, spec, evalCtx)
|
2018-08-09 20:20:10 +00:00
|
|
|
diag = diag.Extend(decDiag)
|
|
|
|
if diag.HasErrors() {
|
2019-07-19 17:04:39 +00:00
|
|
|
return cty.NilVal, diag, formattedDiagnosticErrors(diag)
|
2018-08-09 20:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 17:04:39 +00:00
|
|
|
return value, diag, nil
|
2018-08-09 20:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 17:04:39 +00:00
|
|
|
|
|
|
|
// TODO: update hcl2 library with better diagnostics formatting for streamed configs
|
|
|
|
// - should be arbitrary labels not JSON https://github.com/hashicorp/hcl2/blob/4fba5e1a75e382aed7f7a7993f2c4836a5e1cd52/hcl/json/structure.go#L66
|
|
|
|
// - should not print diagnostic subject https://github.com/hashicorp/hcl2/blob/4fba5e1a75e382aed7f7a7993f2c4836a5e1cd52/hcl/diagnostic.go#L77
|
|
|
|
func formattedDiagnosticErrors(diag hcl.Diagnostics) []error {
|
|
|
|
var errs []error
|
|
|
|
for _, d := range diag {
|
|
|
|
if d.Summary == "Extraneous JSON object property" {
|
|
|
|
d.Summary = "Invalid label"
|
|
|
|
}
|
2020-12-09 19:05:18 +00:00
|
|
|
err := fmt.Errorf("%s: %s", d.Summary, d.Detail)
|
2019-07-19 17:04:39 +00:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|