providers/nomad: suppress diff for identical jobspecs

This commit is contained in:
Mitchell Hashimoto 2016-11-09 18:25:03 -08:00
parent bb5f6498e2
commit f35996cd23
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
1 changed files with 28 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/gob"
"fmt"
"log"
"reflect"
"strings"
"github.com/hashicorp/nomad/api"
@ -23,9 +24,10 @@ func resourceJob() *schema.Resource {
Schema: map[string]*schema.Schema{
"jobspec": {
Description: "Job specification. If you want to point to a file use the file() function.",
Required: true,
Type: schema.TypeString,
Description: "Job specification. If you want to point to a file use the file() function.",
Required: true,
Type: schema.TypeString,
DiffSuppressFunc: jobspecDiffSuppress,
},
"deregister_on_destroy": {
@ -169,3 +171,26 @@ func convertStructJob(in *structs.Job) (*api.Job, error) {
}
return apiJob, nil
}
// jobspecDiffSuppress is the DiffSuppressFunc used by the schema to
// check if two jobspecs are equal.
func jobspecDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// Parse the old job
oldJob, err := jobspec.Parse(strings.NewReader(old))
if err != nil {
return false
}
// Parse the new job
newJob, err := jobspec.Parse(strings.NewReader(new))
if err != nil {
return false
}
// Init
oldJob.Canonicalize()
newJob.Canonicalize()
// Check for jobspec equality
return reflect.DeepEqual(oldJob, newJob)
}