-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from terraform-providers/paddy_import_generic_…
…secret Make generic secrets importable.
- Loading branch information
Showing
6 changed files
with
225 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package vault | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccGenericSecret_importBasic(t *testing.T) { | ||
path := acctest.RandomWithPrefix("secret/test-") | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testResourceGenericSecret_initialConfig(path), | ||
Check: testResourceGenericSecret_initialCheck(path), | ||
}, | ||
{ | ||
ResourceName: "vault_generic_secret.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
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,36 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceGenericSecretMigrateState(v int, s *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
if s.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return s, nil | ||
} | ||
|
||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found Vault Generic Secret state v0; migrating to v1") | ||
s, err := migrateGenericSecretStateV0toV1(s) | ||
return s, err | ||
default: | ||
return s, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateGenericSecretStateV0toV1(s *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
log.Printf("[DEBUG] Attributes before migration: %#v", s.Attributes) | ||
|
||
disabledRead := s.Attributes["allow_read"] != "true" | ||
if disabledRead { | ||
s.Attributes["disable_read"] = "true" | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes after migration: %#v:", s.Attributes) | ||
return s, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package vault | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestGenericSecretMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
Attributes map[string]string | ||
Expected map[string]string | ||
}{ | ||
"unset allow_read to disable_read": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"data_json": `{"hello": "world"}`, | ||
"path": "secret/test-123", | ||
}, | ||
Expected: map[string]string{ | ||
"data_json": `{"hello": "world"}`, | ||
"path": "secret/test-123", | ||
}, | ||
}, | ||
"allow_read false to disable_read": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"data_json": `{"hello": "world"}`, | ||
"path": "secret/test-123", | ||
"allow_read": "false", | ||
}, | ||
Expected: map[string]string{ | ||
"data_json": `{"hello": "world"}`, | ||
"path": "secret/test-123", | ||
"disable_read": "true", | ||
}, | ||
}, | ||
"allow_read true to disable_read": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"data_json": `{"hello": "world"}`, | ||
"path": "secret/test-123", | ||
"allow_read": "true", | ||
}, | ||
Expected: map[string]string{ | ||
"data_json": `{"hello": "world"}`, | ||
"path": "secret/test-123", | ||
}, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
is, err := resourceGenericSecretMigrateState( | ||
tc.StateVersion, &terraform.InstanceState{ | ||
ID: tc.Attributes["path"], | ||
Attributes: tc.Attributes, | ||
}, nil) | ||
|
||
if err != nil { | ||
t.Fatalf("Unexpected error for migration %q: %+v", tn, err) | ||
} | ||
|
||
for k, v := range tc.Expected { | ||
if is.Attributes[k] != v { | ||
t.Fatalf("Expected %q to be %v for %q, got %v", k, v, tn, is.Attributes[k]) | ||
} | ||
} | ||
} | ||
} |
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