diff --git a/nomad/helper/import.go b/nomad/helper/import.go new file mode 100644 index 00000000..6a8c22a5 --- /dev/null +++ b/nomad/helper/import.go @@ -0,0 +1,26 @@ +package helper + +import ( + "context" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func NamespacedImporterContext(_ context.Context, d *schema.ResourceData, a any) ([]*schema.ResourceData, error) { + parts := strings.SplitN(d.Id(), " ", 2) + + var ns, jobID string + switch len(parts) { + case 1: + jobID = parts[0] + case 2: + ns = parts[0] + jobID = parts[1] + } + + d.Set("namespace", ns) + d.SetId(jobID) + + return []*schema.ResourceData{d}, nil +} diff --git a/nomad/resource_job.go b/nomad/resource_job.go index e3ac7c73..9df924e0 100644 --- a/nomad/resource_job.go +++ b/nomad/resource_job.go @@ -19,6 +19,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 { @@ -36,7 +37,7 @@ func resourceJob() *schema.Resource { }, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: helper.NamespacedImporterContext, }, Schema: map[string]*schema.Schema{