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

target nomad api 0.11.0-rc1 #99

Merged
merged 13 commits into from
Apr 8, 2020
Merged
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 1.5.0 (Unreleased)
## 1.4.5 (Unreleased)

IMPROVEMENTS:

* **Target Nomad 0.11.0**: updated the nomad client to support Nomad API version 0.11.0 ([#99](https://github.com/terraform-providers/terraform-provider-nomad/pull/99))

## 1.4.4 (February 10, 2020)

IMPROVEMENTS:
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/terraform-providers/terraform-provider-nomad

go 1.14

exclude (
github.com/Sirupsen/logrus v1.1.0
github.com/Sirupsen/logrus v1.1.1
Expand All @@ -14,7 +16,7 @@ require (
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-version v1.2.0
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
github.com/hashicorp/nomad/api v0.0.0-20200129172448-74e08d5af489
github.com/hashicorp/nomad/api v0.0.0-20200407142900-e7d563e9e04f
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/hashicorp/vault v0.10.4
github.com/mitchellh/mapstructure v1.1.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uP
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-plugin v1.0.1 h1:4OtAfUGbnKC6yS48p0CtMX2oFYtzFZVv6rok3cRWgnE=
github.com/hashicorp/go-plugin v1.0.1/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn3cQptSMzBuY=
github.com/hashicorp/go-rootcerts v1.0.0 h1:Rqb66Oo1X/eSV1x66xbDccZjhJigjg0+e82kpwzSwCI=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo=
github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
Expand All @@ -113,8 +113,8 @@ github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/nomad/api v0.0.0-20200129172448-74e08d5af489 h1:MCZpZRk0tN0ezSBjwtVzYZwOz2KBmNONI1rHz6XE39E=
github.com/hashicorp/nomad/api v0.0.0-20200129172448-74e08d5af489/go.mod h1:Kbx02dGxN6wnAHhSbTqeg/sdACnMMD20BFkVuAxJzds=
github.com/hashicorp/nomad/api v0.0.0-20200407142900-e7d563e9e04f h1:QosWOGkgR0wqmmcC4toVmsz4vAuiHxJo7NxG8fuLi+U=
github.com/hashicorp/nomad/api v0.0.0-20200407142900-e7d563e9e04f/go.mod h1:WKCL+tLVhN1D+APwH3JiTRZoxcdwRk86bWu1LVCUPaE=
github.com/hashicorp/terraform-config-inspect v0.0.0-20190821133035-82a99dc22ef4 h1:fTkL0YwjohGyN7AqsDhz6bwcGBpT+xBqi3Qhpw58Juw=
github.com/hashicorp/terraform-config-inspect v0.0.0-20190821133035-82a99dc22ef4/go.mod h1:JDmizlhaP5P0rYTTZB0reDMefAiJyfWPEtugV4in1oI=
github.com/hashicorp/terraform-plugin-sdk v1.0.0 h1:3AjuuV1LJKs1NlG+heUgqWN6/QCSx2kDhyS6K7F0fTw=
Expand Down
58 changes: 58 additions & 0 deletions nomad/core/helper/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package helper

import (
"fmt"
"reflect"
"strings"
"time"

multierror "github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -50,3 +52,59 @@ func CheckHCLKeys(node ast.Node, valid []string) error {

return result
}

// UnusedKeys returns a pretty-printed error if any `hcl:",unusedKeys"` is not empty
func UnusedKeys(obj interface{}) error {
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
return unusedKeysImpl([]string{}, val)
}

func unusedKeysImpl(path []string, val reflect.Value) error {
stype := val.Type()
for i := 0; i < stype.NumField(); i++ {
ftype := stype.Field(i)
fval := val.Field(i)
tags := strings.Split(ftype.Tag.Get("hcl"), ",")
name := tags[0]
tags = tags[1:]

if fval.Kind() == reflect.Ptr {
fval = reflect.Indirect(fval)
}

// struct? recurse. Add the struct's key to the path
if fval.Kind() == reflect.Struct {
err := unusedKeysImpl(append([]string{name}, path...), fval)
if err != nil {
return err
}
continue
}

// Search the hcl tags for "unusedKeys"
unusedKeys := false
for _, p := range tags {
if p == "unusedKeys" {
unusedKeys = true
break
}
}

if unusedKeys {
ks, ok := fval.Interface().([]string)
if ok && len(ks) != 0 {
ps := ""
if len(path) > 0 {
ps = strings.Join(path, ".") + " "
}
return fmt.Errorf("%sunexpected keys %s",
ps,
strings.Join(ks, ", "))
}
}
}
return nil
}
118 changes: 88 additions & 30 deletions nomad/core/jobspec/parse_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error {
"vault",
"migrate",
"spread",
"shutdown_delay",
"network",
"service",
"volume",
"scaling",
}
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf("'%s' ->", n))
Expand All @@ -63,6 +65,7 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error {
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}

delete(m, "constraint")
delete(m, "affinity")
delete(m, "meta")
Expand All @@ -76,11 +79,21 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error {
delete(m, "network")
delete(m, "service")
delete(m, "volume")
delete(m, "scaling")

// Build the group with the basic decode
var g api.TaskGroup
g.Name = helper.StringToPtr(n)
if err := mapstructure.WeakDecode(m, &g); err != nil {
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
WeaklyTypedInput: true,
Result: &g,
})

if err != nil {
return err
}
if err := dec.Decode(m); err != nil {
return err
}

Expand Down Expand Up @@ -170,6 +183,13 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error {
}
}

// Parse scaling policy
if o := listVal.Filter("scaling"); len(o.Items) > 0 {
if err := parseScalingPolicy(&g.Scaling, o); err != nil {
return multierror.Prefix(err, "scaling ->")
}
}

// Parse tasks
if o := listVal.Filter("task"); len(o.Items) > 0 {
if err := parseTasks(&g.Tasks, o); err != nil {
Expand Down Expand Up @@ -201,7 +221,6 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error {
return multierror.Prefix(err, fmt.Sprintf("'%s',", n))
}
}

collection = append(collection, &g)
}

Expand Down Expand Up @@ -285,41 +304,80 @@ func parseRestartPolicy(final **api.RestartPolicy, list *ast.ObjectList) error {
}

func parseVolumes(out *map[string]*api.VolumeRequest, list *ast.ObjectList) error {
volumes := make(map[string]*api.VolumeRequest, len(list.Items))
hcl.DecodeObject(out, list)

for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
valid := []string{
"type",
"read_only",
"hidden",
"source",
}
if err := helper.CheckHCLKeys(item.Val, valid); err != nil {
for k, v := range *out {
err := helper.UnusedKeys(v)
if err != nil {
return err
}
// This is supported by `hcl:",key"`, but that only works if we start at the
// parent ast.ObjectItem
v.Name = k
}

var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
return nil
}

var result api.VolumeRequest
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: &result,
})
if err != nil {
return err
}
if err := dec.Decode(m); err != nil {
return err
}
func parseScalingPolicy(out **api.ScalingPolicy, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) > 1 {
return fmt.Errorf("only one 'scaling' block allowed")
}

// Get our resource object
o := list.Items[0]

result.Name = n
volumes[n] = &result
// We need this later
var listVal *ast.ObjectList
if ot, ok := o.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return fmt.Errorf("should be an object")
}

valid := []string{
"min",
"max",
"policy",
"enabled",
}
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
}
delete(m, "policy")

var result api.ScalingPolicy
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: &result,
})
if err != nil {
return err
}
if err := dec.Decode(m); err != nil {
return err
}

// If we have policy, then parse that
if o := listVal.Filter("policy"); len(o.Items) > 0 {
for _, o := range o.Elem().Items {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Val); err != nil {
return err
}

if err := mapstructure.WeakDecode(m, &result.Policy); err != nil {
return err
}
}
}

*out = volumes
*out = &result
return nil
}
1 change: 1 addition & 0 deletions nomad/core/jobspec/parse_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func parseJob(result *api.Job, list *ast.ObjectList) error {
"update",
"vault",
"vault_token",
"consul_token",
}
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
return multierror.Prefix(err, "job:")
Expand Down
Loading