diff --git a/CHANGELOG.md b/CHANGELOG.md index 4149bba1..5c47d107 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,11 @@ IMPROVEMENTS: * provider: update Go to 1.21.5 ([#399](https://github.com/hashicorp/terraform-provider-nomad/pull/399)) * resource/resource_acl_auth_method: add support for `token_name_format` ([#403](https://github.com/hashicorp/terraform-provider-nomad/pull/403)) * resource/nomad_csi_volume: changes to `capacity_min` or `capacity_max` may now expand the volume instead of forcing replacement, +* resource/nomad_csi_volume: update import key to be `@` to allow importing volumes from namespaces other than `default` ([#408](https://github.com/hashicorp/terraform-provider-nomad/pull/408)) +* resource/nomad_csi_volume_registration: update import key to be `@` to allow importing volume registrations from namespaces other than `default` ([#408](https://github.com/hashicorp/terraform-provider-nomad/pull/408)) on Nomad version 1.6.3 or later, if the CSI plugin supports it ([#382](https://github.com/hashicorp/terraform-provider-nomad/pull/382)) * resource/nomad_job: Add `rerun_if_dead` attribute to allow forcing a job to run again if it's marked as `dead`. ([#407](https://github.com/hashicorp/terraform-provider-nomad/pull/407)) +* resource/nomad_job: update import key to be `@` to allow importing jobs from namespaces other than `default` ([#408](https://github.com/hashicorp/terraform-provider-nomad/pull/408)) BUG FIXES: * resource/nomad_acl_policy: fixed a bug where the namespace would be incorrectly calculated from a job identity ([#396](https://github.com/hashicorp/terraform-provider-nomad/pull/396)) diff --git a/nomad/helper/import.go b/nomad/helper/import.go new file mode 100644 index 00000000..03d3c09c --- /dev/null +++ b/nomad/helper/import.go @@ -0,0 +1,42 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package helper + +import ( + "context" + "errors" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +var ( + missingNamespaceImportErr = errors.New("missing namespace, the import ID should follow the pattern @") + missingIDImportErr = errors.New("missing resource ID, the import ID should follow the pattern @") +) + +// NamespacedImporterContext imports a namespaced resource that doesn't have +// its namespace as part of the Terraform resource ID. +func NamespacedImporterContext(_ context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { + namespacedID := d.Id() + sepIdx := strings.LastIndex(namespacedID, "@") + if sepIdx == -1 { + return nil, missingNamespaceImportErr + } + + ns := namespacedID[sepIdx+1:] + if len(ns) == 0 { + return nil, missingNamespaceImportErr + } + + id := namespacedID[:sepIdx] + if len(id) == 0 { + return nil, missingIDImportErr + } + + d.SetId(id) + d.Set("namespace", ns) + + return []*schema.ResourceData{d}, nil +} diff --git a/nomad/resource_csi_volume.go b/nomad/resource_csi_volume.go index fcb05ed0..d0f75a1e 100644 --- a/nomad/resource_csi_volume.go +++ b/nomad/resource_csi_volume.go @@ -34,7 +34,7 @@ func resourceCSIVolume() *schema.Resource { }, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: helper.NamespacedImporterContext, }, Schema: map[string]*schema.Schema{ diff --git a/nomad/resource_csi_volume_registration.go b/nomad/resource_csi_volume_registration.go index a25c58ca..0c85ad81 100644 --- a/nomad/resource_csi_volume_registration.go +++ b/nomad/resource_csi_volume_registration.go @@ -35,7 +35,7 @@ func resourceCSIVolumeRegistration() *schema.Resource { }, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: helper.NamespacedImporterContext, }, Schema: map[string]*schema.Schema{ diff --git a/nomad/resource_job.go b/nomad/resource_job.go index 792a2d35..c5521502 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{ diff --git a/website/docs/r/csi_volume.html.markdown b/website/docs/r/csi_volume.html.markdown index e18016f3..11fa58db 100644 --- a/website/docs/r/csi_volume.html.markdown +++ b/website/docs/r/csi_volume.html.markdown @@ -137,5 +137,22 @@ configuration options. - `create` `(string: "10m")` - Timeout when creating or updating a new CSI volume. - `delete` `(string: "10m")` - Timeout when deleting a CSI volume. +## Importing CSI Volumes + +CSI volumes are imported using the pattern `@` . + +```console +$ terraform import nomad_csi_volume.mysql mysql@my-namespace +nomad_csi_volume.mysql: Importing from ID "mysql@my-namespace"... +nomad_csi_volume.mysql: Import prepared! + Prepared nomad_csi_volume for import +nomad_csi_volume.mysql: Refreshing state... [id=mysql@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. +``` + [tf_docs_timeouts]: https://www.terraform.io/docs/configuration/blocks/resources/syntax.html#operation-timeouts [tf_docs_prevent_destroy]: https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#prevent_destroy diff --git a/website/docs/r/csi_volume_registration.html.markdown b/website/docs/r/csi_volume_registration.html.markdown index 0670ac53..2e864116 100644 --- a/website/docs/r/csi_volume_registration.html.markdown +++ b/website/docs/r/csi_volume_registration.html.markdown @@ -130,4 +130,22 @@ can be referenced: - `create` `(string: "10m")` - Timeout when registering a new CSI volume. - `delete` `(string: "10m")` - Timeout when deregistering a CSI volume. +## Importing CSI Volume Registrations + +CSI volume registrations are imported using the pattern +`@`. + +```console +$ terraform import nomad_csi_volume.mysql mysql@my-namespace +nomad_csi_volume_registration.mysql: Importing from ID "mysql@my-namespace"... +nomad_csi_volume_registration.mysql: Import prepared! + Prepared nomad_csi_volume_registration for import +nomad_csi_volume_registration.mysql: Refreshing state... [id=mysql@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. +``` + [tf_docs_timeouts]: https://www.terraform.io/docs/configuration/blocks/resources/syntax.html#operation-timeouts diff --git a/website/docs/r/job.html.markdown b/website/docs/r/job.html.markdown index c8221d45..0840b60c 100644 --- a/website/docs/r/job.html.markdown +++ b/website/docs/r/job.html.markdown @@ -300,6 +300,23 @@ 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 + +Jobs are imported using the pattern `@`. + +```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. +``` + [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