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

Add purge_on_delete to nomad_job resource #127

Merged
merged 1 commit into from
Jul 11, 2020
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
9 changes: 8 additions & 1 deletion nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ func resourceJob() *schema.Resource {
},
},
},

"purge_on_delete": {
Description: "Whether to purge the job when the resource is deleted.",
Optional: true,
Type: schema.TypeBool,
},
},
}
}
Expand Down Expand Up @@ -412,7 +418,8 @@ func resourceJobDeregister(d *schema.ResourceData, meta interface{}) error {
if opts.Namespace == "" {
opts.Namespace = "default"
}
_, _, err := client.Jobs().Deregister(id, false, opts)
purge := d.Get("purge_on_delete").(bool)
_, _, err := client.Jobs().Deregister(id, purge, opts)
if err != nil {
return fmt.Errorf("error deregistering job: %s", err)
}
Expand Down
59 changes: 59 additions & 0 deletions nomad/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,34 @@ func TestResourceJob_parameterizedJob(t *testing.T) {
})
}

func TestResourceJob_purgeOnDelete(t *testing.T) {
r.Test(t, r.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
// create the resource
{
Config: testResourceJob_purgeOnDelete,
Check: testResourceJob_initialCheck(t),
},
// make sure it is purged once deleted
{
Destroy: true,
Config: testResourceJob_purgeOnDelete,
Check: func(s *terraform.State) error {
providerConfig := testProvider.Meta().(ProviderConfig)
client := providerConfig.client
job, _, err := client.Jobs().Info("purge-test", nil)
if !assert.EqualError(t, err, "Unexpected response code: 404 (job not found)") {
return fmt.Errorf("Job found: %#v", job)
}
return nil
},
},
},
})
}

func testResourceJob_parameterizedCheck(s *terraform.State) error {
resourceState := s.Modules[0].Resources["nomad_job.parameterized"]
if resourceState == nil {
Expand Down Expand Up @@ -723,6 +751,37 @@ resource "nomad_job" "test" {
}
`

var testResourceJob_purgeOnDelete = `
resource "nomad_job" "test" {
purge_on_delete = true
jobspec = <<EOT
job "foo" {
datacenters = ["dc1"]
type = "service"
group "foo" {
task "foo" {
driver = "raw_exec"
config {
command = "/bin/sleep"
args = ["30"]
}

resources {
cpu = 100
memory = 10
}

logs {
max_files = 3
max_file_size = 10
}
}
}
}
EOT
}
`

func testResourceJob_initialCheck(t *testing.T) r.TestCheckFunc {
return testResourceJob_initialCheckNS(t, "default")
}
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 @@ -107,3 +107,6 @@ The following arguments are supported:

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

- `purge_on_delete` `(boolean: false)` - Set this to true if you want the job to
be purged when the resource is deleted.