-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
job: import jobs from non-default namespace (#408)
* import: add helper to import namespaced resources `nomad_job`, `nomad_csi_volume`, and `nomad_csi_volume_registration` do not have the namespace as part of their state ID. This makes it impossible to import them if they are registered in a non-default namespace since only the ID is provided to the import function. This commit adds a new helper that imports using an ID with the format `<id>@<namespace>`. Since `@` is not a valid character for namespaces, the last `@` character (if found) represents the separator. * changelog: add entry for #408 * chore: add missing copyright header * provider: fix namespaced importer Importing namespaced resources require a namespace to be passed. Without this requirement jobs such as `a@b` become ambiguous as it's not possible to determine if the user intention was to import the job named `a` from namespace `b` or job `a@b` on namespace `default`. By requiring a namespace to be set this ambiguity is solved. Additionally, Terraform automatically refreshes the state after an import, so there is no need to call the `Read` method manually on import. Doing so can also have unintended consequences, as the provider deletes resources from state in case of a `404`, resulting in a Terraform error when trying to import resources that don't exist. * apply code review
- Loading branch information
Showing
8 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <id>@<namespace>") | ||
missingIDImportErr = errors.New("missing resource ID, the import ID should follow the pattern <id>@<namespace>") | ||
) | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters