Skip to content

Commit

Permalink
wip: namespaced importer
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Aug 21, 2023
1 parent 3b2c30e commit 6d5a960
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 26 additions & 0 deletions nomad/helper/import.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,7 +37,7 @@ func resourceJob() *schema.Resource {
},

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

Schema: map[string]*schema.Schema{
Expand Down

0 comments on commit 6d5a960

Please sign in to comment.