/azurerm/internal/services/recoveryservices/tests/resource_arm_backup_container_storage_account_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 147 lines · 123 code · 21 blank · 3 comment · 18 complexity · 34d8078b581e686f7696a05ecab8d10a MD5 · raw file

  1. package tests
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
  7. "github.com/hashicorp/terraform-plugin-sdk/terraform"
  8. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
  10. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  11. )
  12. func TestAccAzureRMBackupProtectionContainerStorageAccount_basic(t *testing.T) {
  13. data := acceptance.BuildTestData(t, "azurerm_backup_container_storage_account", "test")
  14. resource.ParallelTest(t, resource.TestCase{
  15. PreCheck: func() { acceptance.PreCheck(t) },
  16. Providers: acceptance.SupportedProviders,
  17. CheckDestroy: testCheckAzureRMBackupProtectionContainerStorageAccountDestroy,
  18. Steps: []resource.TestStep{
  19. {
  20. Config: testAccAzureRMBackupProtectionContainerStorageAccount_basic(data),
  21. Check: resource.ComposeTestCheckFunc(
  22. testCheckAzureRMBackupProtectionContainerStorageAccountExists(data.ResourceName),
  23. ),
  24. },
  25. data.ImportStep(),
  26. },
  27. })
  28. }
  29. func testAccAzureRMBackupProtectionContainerStorageAccount_basic(data acceptance.TestData) string {
  30. return fmt.Sprintf(`
  31. provider "azurerm" {
  32. features {}
  33. }
  34. resource "azurerm_resource_group" "test" {
  35. name = "acctestRG-backup-%d"
  36. location = "%s"
  37. }
  38. resource "azurerm_recovery_services_vault" "testvlt" {
  39. name = "acctest-vault-%d"
  40. location = azurerm_resource_group.test.location
  41. resource_group_name = azurerm_resource_group.test.name
  42. sku = "Standard"
  43. soft_delete_enabled = false
  44. }
  45. resource "azurerm_storage_account" "test" {
  46. name = "unlikely23exst2acct%s"
  47. resource_group_name = azurerm_resource_group.test.name
  48. location = azurerm_resource_group.test.location
  49. account_tier = "Standard"
  50. account_replication_type = "LRS"
  51. }
  52. resource "azurerm_backup_container_storage_account" "test" {
  53. resource_group_name = azurerm_resource_group.test.name
  54. recovery_vault_name = azurerm_recovery_services_vault.testvlt.name
  55. storage_account_id = azurerm_storage_account.test.id
  56. }
  57. `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString)
  58. }
  59. func testCheckAzureRMBackupProtectionContainerStorageAccountExists(resourceName string) resource.TestCheckFunc {
  60. return func(s *terraform.State) error {
  61. client := acceptance.AzureProvider.Meta().(*clients.Client).RecoveryServices.BackupProtectionContainersClient
  62. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  63. // Ensure we have enough information in state to look up in API
  64. state, ok := s.RootModule().Resources[resourceName]
  65. if !ok {
  66. return fmt.Errorf("Not found: %s", resourceName)
  67. }
  68. resourceGroupName := state.Primary.Attributes["resource_group_name"]
  69. vaultName := state.Primary.Attributes["recovery_vault_name"]
  70. storageAccountID := state.Primary.Attributes["storage_account_id"]
  71. parsedStorageAccountID, err := azure.ParseAzureResourceID(storageAccountID)
  72. if err != nil {
  73. return fmt.Errorf("Bad: Unable to parse storage_account_id '%s': %+v", storageAccountID, err)
  74. }
  75. accountName, hasName := parsedStorageAccountID.Path["storageAccounts"]
  76. if !hasName {
  77. return fmt.Errorf("Bad: Parsed storage_account_id '%s' doesn't contain 'storageAccounts'", storageAccountID)
  78. }
  79. containerName := fmt.Sprintf("StorageContainer;storage;%s;%s", parsedStorageAccountID.ResourceGroup, accountName)
  80. // Ensure container exists in API
  81. resp, err := client.Get(ctx, vaultName, resourceGroupName, "Azure", containerName)
  82. if err != nil {
  83. return fmt.Errorf("Bad: Get on protection container: %+v", err)
  84. }
  85. if resp.Response.StatusCode == http.StatusNotFound {
  86. return fmt.Errorf("Bad: container: %q does not exist", containerName)
  87. }
  88. return nil
  89. }
  90. }
  91. func testCheckAzureRMBackupProtectionContainerStorageAccountDestroy(s *terraform.State) error {
  92. client := acceptance.AzureProvider.Meta().(*clients.Client).RecoveryServices.BackupProtectionContainersClient
  93. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  94. for _, rs := range s.RootModule().Resources {
  95. if rs.Type != "azurerm_backup_container_storage_account" {
  96. continue
  97. }
  98. resourceGroupName := rs.Primary.Attributes["resource_group_name"]
  99. vaultName := rs.Primary.Attributes["recovery_vault_name"]
  100. storageAccountID := rs.Primary.Attributes["storage_account_id"]
  101. parsedStorageAccountID, err := azure.ParseAzureResourceID(storageAccountID)
  102. if err != nil {
  103. return fmt.Errorf("Bad: Unable to parse storage_account_id '%s': %+v", storageAccountID, err)
  104. }
  105. accountName, hasName := parsedStorageAccountID.Path["storageAccounts"]
  106. if !hasName {
  107. return fmt.Errorf("Bad: Parsed storage_account_id '%s' doesn't contain 'storageAccounts'", storageAccountID)
  108. }
  109. containerName := fmt.Sprintf("StorageContainer;storage;%s;%s", parsedStorageAccountID.ResourceGroup, accountName)
  110. // Ensure container exists in API
  111. resp, err := client.Get(ctx, vaultName, resourceGroupName, "Azure", containerName)
  112. if err != nil {
  113. return nil
  114. }
  115. if resp.StatusCode != http.StatusNotFound {
  116. return fmt.Errorf("Backup Container Storage Account still exists:\n%#v", resp.Properties)
  117. }
  118. }
  119. return nil
  120. }