Skip to content

Commit

Permalink
Handle secrete deletion outside TF
Browse files Browse the repository at this point in the history
fixes #55
  • Loading branch information
paultyng committed Mar 12, 2018
1 parent d609eae commit 2d85cd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vault/resource_generic_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func genericSecretResourceRead(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("error reading from Vault: %s", err)
}
if secret == nil {
log.Printf("[WARN] secret (%s) not found, removing from state", path)
d.SetId("")
return nil
}

log.Printf("[DEBUG] secret: %#v", secret)

Expand Down
25 changes: 25 additions & 0 deletions vault/resource_generic_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ func TestResourceGenericSecret(t *testing.T) {
})
}

func TestResourceGenericSecret_deleted(t *testing.T) {
path := acctest.RandomWithPrefix("secret/test")
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
resource.TestStep{
Config: testResourceGenericSecret_initialConfig(path),
Check: testResourceGenericSecret_initialCheck(path),
},
resource.TestStep{
PreConfig: func() {
client := testProvider.Meta().(*api.Client)
_, err := client.Logical().Delete(path)
if err != nil {
t.Fatalf("unable to manually delete the secret via the SDK: %s", err)
}
},
Config: testResourceGenericSecret_initialConfig(path),
Check: testResourceGenericSecret_initialCheck(path),
},
},
})
}

func testResourceGenericSecret_initialConfig(path string) string {
return fmt.Sprintf(`
resource "vault_generic_secret" "test" {
Expand Down

0 comments on commit 2d85cd4

Please sign in to comment.