/azurerm/internal/services/kusto/tests/kusto_attached_database_configuration_resource_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 145 lines · 125 code · 19 blank · 1 comment · 12 complexity · 783d8dabe219a79978dfb87a88a265f9 MD5 · raw file

  1. package tests
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
  6. "github.com/hashicorp/terraform-plugin-sdk/terraform"
  7. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
  8. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
  10. )
  11. func TestAccAzureRMKustoAttachedDatabaseConfiguration_basic(t *testing.T) {
  12. data := acceptance.BuildTestData(t, "azurerm_kusto_attached_database_configuration", "test")
  13. resource.ParallelTest(t, resource.TestCase{
  14. PreCheck: func() { acceptance.PreCheck(t) },
  15. Providers: acceptance.SupportedProviders,
  16. CheckDestroy: testCheckAzureRMKustoAttachedDatabaseConfigurationDestroy,
  17. Steps: []resource.TestStep{
  18. {
  19. Config: testAccAzureRMKustoAttachedDatabaseConfiguration_basic(data),
  20. Check: resource.ComposeTestCheckFunc(
  21. testCheckAzureRMKustoAttachedDatabaseConfigurationExists(data.ResourceName),
  22. ),
  23. },
  24. data.ImportStep(),
  25. },
  26. })
  27. }
  28. func testAccAzureRMKustoAttachedDatabaseConfiguration_basic(data acceptance.TestData) string {
  29. return fmt.Sprintf(`
  30. provider "azurerm" {
  31. features {}
  32. }
  33. resource "azurerm_resource_group" "rg" {
  34. name = "acctestRG-%d"
  35. location = "%s"
  36. }
  37. resource "azurerm_kusto_cluster" "cluster1" {
  38. name = "acctestkc1%s"
  39. location = azurerm_resource_group.rg.location
  40. resource_group_name = azurerm_resource_group.rg.name
  41. sku {
  42. name = "Dev(No SLA)_Standard_D11_v2"
  43. capacity = 1
  44. }
  45. }
  46. resource "azurerm_kusto_cluster" "cluster2" {
  47. name = "acctestkc2%s"
  48. location = azurerm_resource_group.rg.location
  49. resource_group_name = azurerm_resource_group.rg.name
  50. sku {
  51. name = "Dev(No SLA)_Standard_D11_v2"
  52. capacity = 1
  53. }
  54. }
  55. resource "azurerm_kusto_database" "followed_database" {
  56. name = "acctestkd-%d"
  57. resource_group_name = azurerm_resource_group.rg.name
  58. location = azurerm_resource_group.rg.location
  59. cluster_name = azurerm_kusto_cluster.cluster1.name
  60. }
  61. resource "azurerm_kusto_attached_database_configuration" "configuration1" {
  62. name = "acctestka-%d"
  63. resource_group_name = azurerm_resource_group.rg.name
  64. location = azurerm_resource_group.rg.location
  65. cluster_name = azurerm_kusto_cluster.cluster1.name
  66. cluster_resource_id = azurerm_kusto_cluster.cluster2.id
  67. database_name = "*"
  68. }
  69. `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, data.RandomInteger)
  70. }
  71. func testCheckAzureRMKustoAttachedDatabaseConfigurationDestroy(s *terraform.State) error {
  72. client := acceptance.AzureProvider.Meta().(*clients.Client).Kusto.AttachedDatabaseConfigurationsClient
  73. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  74. for _, rs := range s.RootModule().Resources {
  75. if rs.Type != "azurerm_kusto_attached_database_configuration" {
  76. continue
  77. }
  78. resourceGroup := rs.Primary.Attributes["resource_group_name"]
  79. clusterName := rs.Primary.Attributes["cluster_name"]
  80. name := rs.Primary.Attributes["name"]
  81. resp, err := client.Get(ctx, resourceGroup, clusterName, name)
  82. if err != nil {
  83. if utils.ResponseWasNotFound(resp.Response) {
  84. return nil
  85. }
  86. return err
  87. }
  88. return nil
  89. }
  90. return nil
  91. }
  92. func testCheckAzureRMKustoAttachedDatabaseConfigurationExists(resourceName string) resource.TestCheckFunc {
  93. return func(s *terraform.State) error {
  94. client := acceptance.AzureProvider.Meta().(*clients.Client).Kusto.AttachedDatabaseConfigurationsClient
  95. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  96. // Ensure we have enough information in state to look up in API
  97. rs, ok := s.RootModule().Resources[resourceName]
  98. if !ok {
  99. return fmt.Errorf("Not found: %s", resourceName)
  100. }
  101. configurationName := rs.Primary.Attributes["name"]
  102. resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
  103. if !hasResourceGroup {
  104. return fmt.Errorf("Bad: no resource group found in state for Kusto Attached Database Configuration: %s", configurationName)
  105. }
  106. clusterName, hasClusterName := rs.Primary.Attributes["cluster_name"]
  107. if !hasClusterName {
  108. return fmt.Errorf("Bad: no resource group found in state for Kusto Attached Database Configuration: %s", configurationName)
  109. }
  110. resp, err := client.Get(ctx, resourceGroup, clusterName, configurationName)
  111. if err != nil {
  112. if utils.ResponseWasNotFound(resp.Response) {
  113. return fmt.Errorf("Bad: Kusto Attached Database Configuration %q (resource group: %q, cluster: %q) does not exist", configurationName, resourceGroup, clusterName)
  114. }
  115. return fmt.Errorf("Bad: Get on AttachedDatabaseConfigurationsClient: %+v", err)
  116. }
  117. return nil
  118. }
  119. }