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

Enable json jobs as jobpecs #42

Merged
merged 2 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nomad

import (
"encoding/json"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -46,6 +47,12 @@ func resourceJob() *schema.Resource {
Default: true,
Type: schema.TypeBool,
},

"json": {
Description: "If true, the `jobspec` will be parsed as json instead of HCL.",
Optional: true,
Type: schema.TypeBool,
},
},
}
}
Expand All @@ -58,9 +65,18 @@ func resourceJobRegister(d *schema.ResourceData, meta interface{}) error {
jobspecRaw := d.Get("jobspec").(string)

// Parse it
job, err := jobspec.Parse(strings.NewReader(jobspecRaw))
if err != nil {
return fmt.Errorf("error parsing jobspec: %s", err)
var job *api.Job
if is_json := d.Get("json").(bool); is_json {
err := json.Unmarshal([]byte(jobspecRaw), &job)
if err != nil {
return fmt.Errorf("error parsing jobspec: %s", err)
}
} else {
var err error
job, err = jobspec.Parse(strings.NewReader(jobspecRaw))
if err != nil {
return fmt.Errorf("error parsing jobspec: %s", err)
}
}

// Inject the Vault token
Expand Down Expand Up @@ -97,7 +113,7 @@ func resourceJobRegister(d *schema.ResourceData, meta interface{}) error {
}

// Register the job
_, _, err = client.Jobs().RegisterOpts(job, &api.RegisterOptions{
_, _, err := client.Jobs().RegisterOpts(job, &api.RegisterOptions{
PolicyOverride: d.Get("policy_override").(bool),
}, nil)
if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions nomad/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ func TestResourceJob_v086(t *testing.T) {
})
}

func TestResourceJob_json(t *testing.T) {
r.Test(t, r.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
{
Config: testResourceJob_jsonConfig,
Check: testResourceJob_initialCheck,
},
},

CheckDestroy: testResourceJob_checkDestroy("foo-json"),
})
}

func TestResourceJob_refresh(t *testing.T) {
r.Test(t, r.TestCase{
Providers: testProviders,
Expand Down Expand Up @@ -227,6 +242,42 @@ resource "nomad_job" "test" {
}
`

var testResourceJob_jsonConfig = `
resource "nomad_job" "json_test" {
json = true
jobspec = <<EOT
{
"Datacenters": [ "dc1" ],
"ID": "foo-json",
"Name": "foo-json",
"Type": "service",
"TaskGroups": [
{
"Name": "foo",
"Tasks": [{
"Config": {
"command": "/bin/sleep",
"args": [ "1" ]
},
"Driver": "raw_exec",
"Leader": true,
"LogConfig": {
"MaxFileSizeMB": 10,
"MaxFiles": 3
},
"Name": "foo",
"Resources": {
"CPU": 100,
"MemoryMB": 10
}
}
]
}
]
}
EOT
}
`
var testResourceJob_noDestroy = `
resource "nomad_job" "test" {
deregister_on_destroy = false
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ The following arguments are supported:

- `policy_override` `(bool: false)` - Determines if the job will override any
soft-mandatory Sentinel policies and register even if they fail.

- `json` `(bool: false)` - Set this to true if your jobspec is stractured with
JSON instead of the default HCL.