Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_key_vault_key - handling the parent Key Vault being deleted #1535

Merged
merged 2 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions azurerm/resource_arm_key_vault_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func resourceArmKeyVaultCertificateRead(d *schema.ResourceData, meta interface{}

if err != nil {
if utils.ResponseWasNotFound(cert.Response) {
log.Printf("[DEBUG] Certificate %q was not found in Key Vault at URI %q - removing from state", id.Name, id.KeyVaultBaseUrl)
d.SetId("")
return nil
}
Expand Down
69 changes: 69 additions & 0 deletions azurerm/resource_arm_key_vault_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@ func TestAccAzureRMKeyVaultCertificate_basicImportPFX(t *testing.T) {
})
}

func TestAccAzureRMKeyVaultCertificate_disappears(t *testing.T) {
resourceName := "azurerm_key_vault_certificate.test"
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultCertificate_basicGenerate(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultCertificateDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultCertificateExists(resourceName),
testCheckAzureRMKeyVaultCertificateDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMKeyVaultCertificate_disappearsWhenParentKeyVaultDeleted(t *testing.T) {
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultCertificate_basicGenerate(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultCertificateDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultCertificateExists("azurerm_key_vault_certificate.test"),
testCheckAzureRMKeyVaultDisappears("azurerm_key_vault.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMKeyVaultCertificate_basicGenerate(t *testing.T) {
resourceName := "azurerm_key_vault_certificate.test"
rs := acctest.RandString(6)
Expand Down Expand Up @@ -129,6 +172,32 @@ func testCheckAzureRMKeyVaultCertificateExists(name string) resource.TestCheckFu
}
}

func testCheckAzureRMKeyVaultCertificateDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
name := rs.Primary.Attributes["name"]
vaultBaseUrl := rs.Primary.Attributes["vault_uri"]

client := testAccProvider.Meta().(*ArmClient).keyVaultManagementClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.DeleteCertificate(ctx, vaultBaseUrl, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("Bad: Delete on keyVaultManagementClient: %+v", err)
}

return nil
}
}

func testAccAzureRMKeyVaultCertificate_basicImportPFX(rString string, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down
6 changes: 6 additions & 0 deletions azurerm/resource_arm_key_vault_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ func resourceArmKeyVaultKeyRead(d *schema.ResourceData, meta interface{}) error

resp, err := client.GetKey(ctx, id.KeyVaultBaseUrl, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Key %q was not found in Key Vault at URI %q - removing from state", id.Name, id.KeyVaultBaseUrl)
d.SetId("")
return nil
}

return err
}

Expand Down
70 changes: 70 additions & 0 deletions azurerm/resource_arm_key_vault_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ func TestAccAzureRMKeyVaultKey_update(t *testing.T) {
})
}

func TestAccAzureRMKeyVaultKey_disappears(t *testing.T) {
resourceName := "azurerm_key_vault_key.test"
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultKey_basicEC(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultKeyDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultKeyExists(resourceName),
testCheckAzureRMKeyVaultKeyDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMKeyVaultKey_disappearsWhenParentKeyVaultDeleted(t *testing.T) {
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultKey_basicEC(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultKeyDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultKeyExists("azurerm_key_vault_key.test"),
testCheckAzureRMKeyVaultDisappears("azurerm_key_vault.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testCheckAzureRMKeyVaultKeyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).keyVaultManagementClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -176,6 +219,33 @@ func testCheckAzureRMKeyVaultKeyExists(name string) resource.TestCheckFunc {
}
}

func testCheckAzureRMKeyVaultKeyDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

name := rs.Primary.Attributes["name"]
vaultBaseUrl := rs.Primary.Attributes["vault_uri"]

client := testAccProvider.Meta().(*ArmClient).keyVaultManagementClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.DeleteKey(ctx, vaultBaseUrl, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("Bad: Delete on keyVaultManagementClient: %+v", err)
}

return nil
}
}

func testAccAzureRMKeyVaultKey_basicEC(rString string, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down
1 change: 1 addition & 0 deletions azurerm/resource_arm_key_vault_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func resourceArmKeyVaultSecretRead(d *schema.ResourceData, meta interface{}) err
resp, err := client.GetSecret(ctx, id.KeyVaultBaseUrl, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Secret %q was not found in Key Vault at URI %q - removing from state", id.Name, id.KeyVaultBaseUrl)
d.SetId("")
return nil
}
Expand Down
69 changes: 69 additions & 0 deletions azurerm/resource_arm_key_vault_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@ func TestAccAzureRMKeyVaultSecret_basic(t *testing.T) {
})
}

func TestAccAzureRMKeyVaultSecret_disappears(t *testing.T) {
resourceName := "azurerm_key_vault_secret.test"
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultSecret_basic(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultSecretDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultSecretExists(resourceName),
testCheckAzureRMKeyVaultSecretDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMKeyVaultSecret_disappearsWhenParentKeyVaultDeleted(t *testing.T) {
rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultSecret_basic(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultSecretDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultSecretExists("azurerm_key_vault_secret.test"),
testCheckAzureRMKeyVaultDisappears("azurerm_key_vault.test"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMKeyVaultSecret_complete(t *testing.T) {
resourceName := "azurerm_key_vault_secret.test"
rs := acctest.RandString(6)
Expand Down Expand Up @@ -135,6 +178,32 @@ func testCheckAzureRMKeyVaultSecretExists(name string) resource.TestCheckFunc {
}
}

func testCheckAzureRMKeyVaultSecretDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
name := rs.Primary.Attributes["name"]
vaultBaseUrl := rs.Primary.Attributes["vault_uri"]

client := testAccProvider.Meta().(*ArmClient).keyVaultManagementClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.DeleteSecret(ctx, vaultBaseUrl, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("Bad: Delete on keyVaultManagementClient: %+v", err)
}

return nil
}
}

func testAccAzureRMKeyVaultSecret_basic(rString string, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down
53 changes: 53 additions & 0 deletions azurerm/resource_arm_key_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -87,6 +88,28 @@ func TestAccAzureRMKeyVault_basic(t *testing.T) {
})
}

func TestAccAzureRMKeyVault_disappears(t *testing.T) {
resourceName := "azurerm_key_vault.test"
ri := acctest.RandInt()
config := testAccAzureRMKeyVault_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultExists(resourceName),
testCheckAzureRMKeyVaultDisappears(resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAzureRMKeyVault_complete(t *testing.T) {
resourceName := "azurerm_key_vault.test"
ri := acctest.RandInt()
Expand Down Expand Up @@ -199,6 +222,36 @@ func testCheckAzureRMKeyVaultExists(name string) resource.TestCheckFunc {
}
}

func testCheckAzureRMKeyVaultDisappears(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

vaultName := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for vault: %s", vaultName)
}

client := testAccProvider.Meta().(*ArmClient).keyVaultClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := client.Delete(ctx, resourceGroup, vaultName)
if err != nil {
if response.WasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("Bad: Delete on keyVaultClient: %+v", err)
}

return nil
}
}

func testAccAzureRMKeyVault_basic(rInt int, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
Expand Down