Skip to content

Commit

Permalink
job: import jobs from non-default namespace
Browse files Browse the repository at this point in the history
Since the `nomad_job` resource does not have a `namespace` attribute,
it's currently impossible to import jobs from a non-default namespace.

This commit adds a new helper that imports jobs using the
`<job>@<namespace>` ID syntax. Since `@` is not a valid character for
namespaces, the last `@` character (if found) represents the separator.
  • Loading branch information
lgfa29 committed Dec 16, 2023
1 parent a81b5f7 commit 87f853b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions nomad/helper/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package helper

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// NamespacedImporterContext allows importing resources under a namespace where
// the namespace is not passed as an attribute.
func NamespacedImporterContext(_ context.Context, d *schema.ResourceData, a any) ([]*schema.ResourceData, error) {
namespacedID := d.Id()
sepIdx := strings.LastIndex(d.Id(), "@")
if sepIdx == -1 {
return []*schema.ResourceData{d}, nil
}

d.SetId(namespacedID[:sepIdx])
d.Set("namespace", namespacedID[sepIdx+1:])
return []*schema.ResourceData{d}, nil
}
3 changes: 2 additions & 1 deletion nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/nomad/jobspec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-nomad/nomad/helper"
)

func resourceJob() *schema.Resource {
Expand All @@ -37,7 +38,7 @@ func resourceJob() *schema.Resource {
},

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: helper.NamespacedImporterContext,
},

Schema: map[string]*schema.Schema{
Expand Down
21 changes: 21 additions & 0 deletions website/docs/r/job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,27 @@ options when [`detach`](#detach) is set to `false`:
- `create` `(string: "5m")` - Timeout when registering a new job.
- `update` `(string: "5m")` - Timeout when updating an existing job.

## Importing Jobs

You can use the ID format `<job ID>@<namespace>` to import jobs from a
non-default namespace.

```console
$ terraform import nomad_job.example example@my-namespace
nomad_job.example: Importing from ID "example@my-namespace"...
nomad_job.example: Import prepared!
Prepared nomad_job for import
nomad_job.example: Refreshing state... [id=example@my-namespace]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
```

The `@<namespace>` component my be omitted if the job is registered in the
`default` namespace.

[tf_docs_timeouts]: https://www.terraform.io/docs/configuration/blocks/resources/syntax.html#operation-timeouts
[tf_docs_templatefile]: https://www.terraform.io/docs/configuration/functions/templatefile.html
[tf_docs_string_template]: https://www.terraform.io/language/expressions/strings#string-templates

0 comments on commit 87f853b

Please sign in to comment.