2015-09-15 00:43:42 +00:00
|
|
|
package jobspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-23 20:44:08 +00:00
|
|
|
"regexp"
|
2015-10-22 23:37:20 +00:00
|
|
|
"strconv"
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2015-09-15 00:43:42 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2015-11-09 06:57:39 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2017-02-06 19:48:28 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
2015-09-15 00:43:42 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2016-03-10 18:16:35 +00:00
|
|
|
var reDynamicPorts = regexp.MustCompile("^[a-zA-Z0-9_]+$")
|
2015-11-15 08:10:48 +00:00
|
|
|
var errPortLabel = fmt.Errorf("Port label does not conform to naming requirements %s", reDynamicPorts.String())
|
2015-11-09 06:57:39 +00:00
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Parse parses the job spec from the given io.Reader.
|
|
|
|
//
|
|
|
|
// Due to current internal limitations, the entire contents of the
|
|
|
|
// io.Reader will be copied into memory first before parsing.
|
2017-02-06 19:48:28 +00:00
|
|
|
func Parse(r io.Reader) (*api.Job, error) {
|
2015-09-15 00:43:42 +00:00
|
|
|
// Copy the reader into an in-memory buffer first since HCL requires it.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := io.Copy(&buf, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the buffer
|
2015-11-09 06:57:39 +00:00
|
|
|
root, err := hcl.Parse(buf.String())
|
2015-09-15 00:43:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing: %s", err)
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Top-level item should be a list
|
|
|
|
list, ok := root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: root should be an object")
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:16:35 +00:00
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"job",
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
if err := helper.CheckHCLKeys(list, valid); err != nil {
|
2016-03-10 18:16:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
var job api.Job
|
2015-09-15 00:43:42 +00:00
|
|
|
|
|
|
|
// Parse the job out
|
2015-11-09 06:57:39 +00:00
|
|
|
matches := list.Filter("job")
|
|
|
|
if len(matches.Items) == 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
return nil, fmt.Errorf("'job' stanza not found")
|
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := parseJob(&job, matches); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'job': %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &job, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFile parses the given path as a job spec.
|
2017-02-06 19:48:28 +00:00
|
|
|
func ParseFile(path string) (*api.Job, error) {
|
2015-09-15 00:43:42 +00:00
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return Parse(f)
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:49:01 +00:00
|
|
|
func parseReschedulePolicy(final **api.ReschedulePolicy, list *ast.ObjectList) error {
|
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return fmt.Errorf("only one 'reschedule' block allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our job object
|
|
|
|
obj := list.Items[0]
|
|
|
|
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"attempts",
|
|
|
|
"interval",
|
2018-03-02 00:16:01 +00:00
|
|
|
"unlimited",
|
|
|
|
"delay",
|
2018-03-13 15:06:26 +00:00
|
|
|
"max_delay",
|
2018-03-02 00:16:01 +00:00
|
|
|
"delay_function",
|
2018-01-18 20:49:01 +00:00
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(obj.Val, valid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, obj.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result api.ReschedulePolicy
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &result,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dec.Decode(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*final = &result
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
func parseConstraints(result *[]*api.Constraint, list *ast.ObjectList) error {
|
2015-11-09 06:57:39 +00:00
|
|
|
for _, o := range list.Elem().Items {
|
2016-03-10 18:16:35 +00:00
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"attribute",
|
2017-03-07 22:20:02 +00:00
|
|
|
"distinct_hosts",
|
|
|
|
"distinct_property",
|
2016-03-10 18:16:35 +00:00
|
|
|
"operator",
|
|
|
|
"regexp",
|
2016-10-19 20:06:28 +00:00
|
|
|
"set_contains",
|
2017-03-07 22:20:02 +00:00
|
|
|
"value",
|
|
|
|
"version",
|
2016-03-10 18:16:35 +00:00
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
2016-03-10 18:16:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-10 18:16:35 +00:00
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
m["LTarget"] = m["attribute"]
|
|
|
|
m["RTarget"] = m["value"]
|
|
|
|
m["Operand"] = m["operator"]
|
|
|
|
|
2015-10-11 19:20:58 +00:00
|
|
|
// If "version" is provided, set the operand
|
|
|
|
// to "version" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if constraint, ok := m[api.ConstraintVersion]; ok {
|
|
|
|
m["Operand"] = api.ConstraintVersion
|
2015-10-11 19:20:58 +00:00
|
|
|
m["RTarget"] = constraint
|
2015-10-11 19:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If "regexp" is provided, set the operand
|
|
|
|
// to "regexp" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if constraint, ok := m[api.ConstraintRegex]; ok {
|
|
|
|
m["Operand"] = api.ConstraintRegex
|
2015-10-11 19:37:50 +00:00
|
|
|
m["RTarget"] = constraint
|
2015-10-11 19:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:06:28 +00:00
|
|
|
// If "set_contains" is provided, set the operand
|
|
|
|
// to "set_contains" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if constraint, ok := m[api.ConstraintSetContains]; ok {
|
|
|
|
m["Operand"] = api.ConstraintSetContains
|
2016-10-19 20:06:28 +00:00
|
|
|
m["RTarget"] = constraint
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:23:49 +00:00
|
|
|
if value, ok := m[api.ConstraintDistinctHosts]; ok {
|
2015-11-25 20:33:56 +00:00
|
|
|
enabled, err := parseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("distinct_hosts should be set to true or false; %v", err)
|
2015-10-22 23:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it is not enabled, skip the constraint.
|
|
|
|
if !enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:23:49 +00:00
|
|
|
m["Operand"] = api.ConstraintDistinctHosts
|
2015-10-22 23:37:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:23:49 +00:00
|
|
|
if property, ok := m[api.ConstraintDistinctProperty]; ok {
|
|
|
|
m["Operand"] = api.ConstraintDistinctProperty
|
2017-03-07 22:20:02 +00:00
|
|
|
m["LTarget"] = property
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Build the constraint
|
2017-02-06 19:48:28 +00:00
|
|
|
var c api.Constraint
|
2015-09-15 00:43:42 +00:00
|
|
|
if err := mapstructure.WeakDecode(m, &c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.Operand == "" {
|
|
|
|
c.Operand = "="
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = append(*result, &c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-16 13:30:58 +00:00
|
|
|
func parseAffinities(result *[]*api.Affinity, list *ast.ObjectList) error {
|
|
|
|
for _, o := range list.Elem().Items {
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"attribute",
|
|
|
|
"operator",
|
|
|
|
"regexp",
|
2018-07-18 19:36:02 +00:00
|
|
|
"set_contains",
|
2018-07-16 13:30:58 +00:00
|
|
|
"set_contains_any",
|
|
|
|
"set_contains_all",
|
|
|
|
"value",
|
|
|
|
"version",
|
|
|
|
"weight",
|
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m["LTarget"] = m["attribute"]
|
|
|
|
m["RTarget"] = m["value"]
|
|
|
|
m["Operand"] = m["operator"]
|
|
|
|
|
|
|
|
// If "version" is provided, set the operand
|
|
|
|
// to "version" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if affinity, ok := m[api.ConstraintVersion]; ok {
|
|
|
|
m["Operand"] = api.ConstraintVersion
|
2018-07-16 13:30:58 +00:00
|
|
|
m["RTarget"] = affinity
|
|
|
|
}
|
|
|
|
|
|
|
|
// If "regexp" is provided, set the operand
|
|
|
|
// to "regexp" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if affinity, ok := m[api.ConstraintRegex]; ok {
|
|
|
|
m["Operand"] = api.ConstraintRegex
|
2018-07-16 13:30:58 +00:00
|
|
|
m["RTarget"] = affinity
|
|
|
|
}
|
|
|
|
|
|
|
|
// If "set_contains_any" is provided, set the operand
|
|
|
|
// to "set_contains_any" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if affinity, ok := m[api.ConstraintSetContainsAny]; ok {
|
|
|
|
m["Operand"] = api.ConstraintSetContainsAny
|
2018-07-16 13:30:58 +00:00
|
|
|
m["RTarget"] = affinity
|
|
|
|
}
|
|
|
|
|
|
|
|
// If "set_contains_all" is provided, set the operand
|
|
|
|
// to "set_contains_all" and the value to the "RTarget"
|
2019-05-09 12:23:49 +00:00
|
|
|
if affinity, ok := m[api.ConstraintSetContainsAll]; ok {
|
|
|
|
m["Operand"] = api.ConstraintSetContainsAll
|
2018-07-16 13:30:58 +00:00
|
|
|
m["RTarget"] = affinity
|
|
|
|
}
|
|
|
|
|
2018-07-18 19:36:02 +00:00
|
|
|
// set_contains is a synonym of set_contains_all
|
2019-05-09 12:23:49 +00:00
|
|
|
if affinity, ok := m[api.ConstraintSetContains]; ok {
|
|
|
|
m["Operand"] = api.ConstraintSetContains
|
2018-07-18 19:36:02 +00:00
|
|
|
m["RTarget"] = affinity
|
|
|
|
}
|
|
|
|
|
2018-07-16 13:30:58 +00:00
|
|
|
// Build the affinity
|
|
|
|
var a api.Affinity
|
|
|
|
if err := mapstructure.WeakDecode(m, &a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a.Operand == "" {
|
|
|
|
a.Operand = "="
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = append(*result, &a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:53:03 +00:00
|
|
|
func parseSpread(result *[]*api.Spread, list *ast.ObjectList) error {
|
|
|
|
for _, o := range list.Elem().Items {
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"attribute",
|
|
|
|
"weight",
|
|
|
|
"target",
|
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := o.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("spread should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(m, "target")
|
|
|
|
// Build spread
|
|
|
|
var s api.Spread
|
|
|
|
if err := mapstructure.WeakDecode(m, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse spread target
|
|
|
|
if o := listVal.Filter("target"); len(o.Items) > 0 {
|
|
|
|
if err := parseSpreadTarget(&s.SpreadTarget, o); err != nil {
|
2018-10-08 23:09:41 +00:00
|
|
|
return multierror.Prefix(err, fmt.Sprintf("target ->"))
|
2018-07-18 15:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = append(*result, &s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSpreadTarget(result *[]*api.SpreadTarget, list *ast.ObjectList) error {
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for _, item := range list.Items {
|
2018-10-08 23:09:41 +00:00
|
|
|
if len(item.Keys) != 1 {
|
|
|
|
return fmt.Errorf("missing spread target")
|
|
|
|
}
|
2018-07-18 15:53:03 +00:00
|
|
|
n := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
// Make sure we haven't already found this
|
|
|
|
if _, ok := seen[n]; ok {
|
|
|
|
return fmt.Errorf("target '%s' defined more than once", n)
|
|
|
|
}
|
|
|
|
seen[n] = struct{}{}
|
|
|
|
|
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("target should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"percent",
|
|
|
|
"value",
|
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
|
|
|
|
return multierror.Prefix(err, fmt.Sprintf("'%s' ->", n))
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, item.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode spread target
|
|
|
|
var g api.SpreadTarget
|
|
|
|
g.Value = n
|
|
|
|
if err := mapstructure.WeakDecode(m, &g); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*result = append(*result, &g)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-25 20:33:56 +00:00
|
|
|
// parseBool takes an interface value and tries to convert it to a boolean and
|
|
|
|
// returns an error if the type can't be converted.
|
|
|
|
func parseBool(value interface{}) (bool, error) {
|
|
|
|
var enabled bool
|
|
|
|
var err error
|
|
|
|
switch value.(type) {
|
|
|
|
case string:
|
|
|
|
enabled, err = strconv.ParseBool(value.(string))
|
|
|
|
case bool:
|
|
|
|
enabled = value.(bool)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("%v couldn't be converted to boolean value", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return enabled, err
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
func parseUpdate(result **api.UpdateStrategy, list *ast.ObjectList) error {
|
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return fmt.Errorf("only one 'update' block allowed")
|
2015-11-09 06:57:39 +00:00
|
|
|
}
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
// Get our resource object
|
|
|
|
o := list.Items[0]
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-30 22:51:39 +00:00
|
|
|
|
2016-03-10 18:16:35 +00:00
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"stagger",
|
|
|
|
"max_parallel",
|
2017-05-09 00:44:26 +00:00
|
|
|
"health_check",
|
|
|
|
"min_healthy_time",
|
|
|
|
"healthy_deadline",
|
2018-03-23 17:56:00 +00:00
|
|
|
"progress_deadline",
|
2017-05-09 00:44:26 +00:00
|
|
|
"auto_revert",
|
2019-05-09 13:42:18 +00:00
|
|
|
"auto_promote",
|
2017-05-09 00:44:26 +00:00
|
|
|
"canary",
|
2016-03-10 18:16:35 +00:00
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
2016-03-10 18:16:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: result,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
return dec.Decode(m)
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
2015-12-01 00:51:56 +00:00
|
|
|
|
2018-01-24 00:47:00 +00:00
|
|
|
func parseMigrate(result **api.MigrateStrategy, list *ast.ObjectList) error {
|
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return fmt.Errorf("only one 'migrate' block allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our resource object
|
|
|
|
o := list.Items[0]
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"max_parallel",
|
|
|
|
"health_check",
|
|
|
|
"min_healthy_time",
|
|
|
|
"healthy_deadline",
|
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: result,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return dec.Decode(m)
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
func parseVault(result *api.Vault, list *ast.ObjectList) error {
|
2016-08-09 23:07:45 +00:00
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return fmt.Errorf("only one 'vault' block allowed per task")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our resource object
|
|
|
|
o := list.Items[0]
|
|
|
|
|
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := o.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("vault: should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"policies",
|
2016-09-20 20:22:29 +00:00
|
|
|
"env",
|
2016-10-11 22:25:49 +00:00
|
|
|
"change_mode",
|
|
|
|
"change_signal",
|
2016-08-09 23:07:45 +00:00
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
|
2016-08-09 23:07:45 +00:00
|
|
|
return multierror.Prefix(err, "vault ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|