/azurerm/internal/services/iothub/tests/iothub_fallback_route_resource_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 252 lines · 230 code · 20 blank · 2 comment · 24 complexity · 2831877f645ea3580dd5acb2284b837c 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/helpers/azure"
  8. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  10. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
  11. )
  12. // NOTE: this resource intentionally doesn't support Requires Import
  13. // since a fallback route is created by default
  14. func TestAccAzureRMIotHubFallbackRoute_basic(t *testing.T) {
  15. data := acceptance.BuildTestData(t, "azurerm_iothub_fallback_route", "test")
  16. resource.ParallelTest(t, resource.TestCase{
  17. PreCheck: func() { acceptance.PreCheck(t) },
  18. Providers: acceptance.SupportedProviders,
  19. CheckDestroy: testCheckAzureRMIotHubFallbackRouteDestroy,
  20. Steps: []resource.TestStep{
  21. {
  22. Config: testAccAzureRMIotHubFallbackRoute_basic(data),
  23. Check: resource.ComposeTestCheckFunc(
  24. testCheckAzureRMIotHubFallbackRouteExists(data.ResourceName),
  25. ),
  26. },
  27. data.ImportStep(),
  28. },
  29. })
  30. }
  31. func TestAccAzureRMIotHubFallbackRoute_update(t *testing.T) {
  32. data := acceptance.BuildTestData(t, "azurerm_iothub_fallback_route", "test")
  33. resource.ParallelTest(t, resource.TestCase{
  34. PreCheck: func() { acceptance.PreCheck(t) },
  35. Providers: acceptance.SupportedProviders,
  36. CheckDestroy: testCheckAzureRMIotHubFallbackRouteDestroy,
  37. Steps: []resource.TestStep{
  38. {
  39. Config: testAccAzureRMIotHubFallbackRoute_basic(data),
  40. Check: resource.ComposeTestCheckFunc(
  41. testCheckAzureRMIotHubFallbackRouteExists(data.ResourceName),
  42. ),
  43. },
  44. data.ImportStep(),
  45. {
  46. Config: testAccAzureRMIotHubFallbackRoute_update(data),
  47. Check: resource.ComposeTestCheckFunc(
  48. testCheckAzureRMIotHubFallbackRouteExists(data.ResourceName),
  49. ),
  50. },
  51. data.ImportStep(),
  52. },
  53. })
  54. }
  55. func testCheckAzureRMIotHubFallbackRouteDestroy(s *terraform.State) error {
  56. client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.ResourceClient
  57. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  58. for _, rs := range s.RootModule().Resources {
  59. if rs.Type != "azurerm_iothub_fallback_route" {
  60. continue
  61. }
  62. iothubName := rs.Primary.Attributes["iothub_name"]
  63. resourceGroup := rs.Primary.Attributes["resource_group_name"]
  64. iothub, err := client.Get(ctx, resourceGroup, iothubName)
  65. if err != nil {
  66. if utils.ResponseWasNotFound(iothub.Response) {
  67. return nil
  68. }
  69. return fmt.Errorf("Bad: Get on iothubResourceClient: %+v", err)
  70. }
  71. if iothub.Properties == nil || iothub.Properties.Routing == nil {
  72. return nil
  73. }
  74. if iothub.Properties.Routing.FallbackRoute != nil {
  75. return fmt.Errorf("Bad: fallback route still exists on IoTHb %s", iothubName)
  76. }
  77. }
  78. return nil
  79. }
  80. func testCheckAzureRMIotHubFallbackRouteExists(resourceName string) resource.TestCheckFunc {
  81. return func(s *terraform.State) error {
  82. client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.ResourceClient
  83. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  84. rs, ok := s.RootModule().Resources[resourceName]
  85. if !ok {
  86. return fmt.Errorf("Not found: %s", resourceName)
  87. }
  88. parsedIothubId, err := azure.ParseAzureResourceID(rs.Primary.ID)
  89. if err != nil {
  90. return err
  91. }
  92. iothubName := parsedIothubId.Path["IotHubs"]
  93. resourceGroup := parsedIothubId.ResourceGroup
  94. iothub, err := client.Get(ctx, resourceGroup, iothubName)
  95. if err != nil {
  96. if utils.ResponseWasNotFound(iothub.Response) {
  97. return fmt.Errorf("IotHub %q (Resource Group %q) was not found", iothubName, resourceGroup)
  98. }
  99. return fmt.Errorf("Error loading IotHub %q (Resource Group %q): %+v", iothubName, resourceGroup, err)
  100. }
  101. if iothub.Properties == nil || iothub.Properties.Routing == nil || iothub.Properties.Routing.FallbackRoute == nil {
  102. return fmt.Errorf("Bad: No fallbackroute defined for IotHub %s", iothubName)
  103. }
  104. return nil
  105. }
  106. }
  107. func testAccAzureRMIotHubFallbackRoute_basic(data acceptance.TestData) string {
  108. return fmt.Sprintf(`
  109. provider "azurerm" {
  110. features {}
  111. }
  112. resource "azurerm_resource_group" "test" {
  113. name = "acctestRG-iothub-%[1]d"
  114. location = "%[2]s"
  115. }
  116. resource "azurerm_storage_account" "test" {
  117. name = "acctestsa%[3]s"
  118. resource_group_name = azurerm_resource_group.test.name
  119. location = azurerm_resource_group.test.location
  120. account_tier = "Standard"
  121. account_replication_type = "LRS"
  122. }
  123. resource "azurerm_storage_container" "test" {
  124. name = "test-%[1]d"
  125. storage_account_name = azurerm_storage_account.test.name
  126. container_access_type = "private"
  127. }
  128. resource "azurerm_iothub" "test" {
  129. name = "acctestIoTHub-%[1]d"
  130. resource_group_name = azurerm_resource_group.test.name
  131. location = azurerm_resource_group.test.location
  132. sku {
  133. name = "S1"
  134. capacity = "1"
  135. }
  136. tags = {
  137. purpose = "testing"
  138. }
  139. }
  140. resource "azurerm_iothub_endpoint_storage_container" "test" {
  141. resource_group_name = azurerm_resource_group.test.name
  142. iothub_name = azurerm_iothub.test.name
  143. name = "acctest"
  144. connection_string = azurerm_storage_account.test.primary_blob_connection_string
  145. batch_frequency_in_seconds = 60
  146. max_chunk_size_in_bytes = 10485760
  147. container_name = azurerm_storage_container.test.name
  148. encoding = "Avro"
  149. file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}"
  150. }
  151. resource "azurerm_iothub_fallback_route" "test" {
  152. resource_group_name = azurerm_resource_group.test.name
  153. iothub_name = azurerm_iothub.test.name
  154. condition = "true"
  155. endpoint_names = [azurerm_iothub_endpoint_storage_container.test.name]
  156. enabled = true
  157. }
  158. `, data.RandomInteger, data.Locations.Primary, data.RandomString)
  159. }
  160. func testAccAzureRMIotHubFallbackRoute_update(data acceptance.TestData) string {
  161. return fmt.Sprintf(`
  162. provider "azurerm" {
  163. features {}
  164. }
  165. resource "azurerm_resource_group" "test" {
  166. name = "acctestRG-iothub-%[1]d"
  167. location = "%[2]s"
  168. }
  169. resource "azurerm_storage_account" "test" {
  170. name = "acctestsa%[3]s"
  171. resource_group_name = azurerm_resource_group.test.name
  172. location = azurerm_resource_group.test.location
  173. account_tier = "Standard"
  174. account_replication_type = "LRS"
  175. }
  176. resource "azurerm_storage_container" "test" {
  177. name = "test-%[1]d"
  178. storage_account_name = azurerm_storage_account.test.name
  179. container_access_type = "private"
  180. }
  181. resource "azurerm_iothub" "test" {
  182. name = "acctestIoTHub-%[1]d"
  183. resource_group_name = azurerm_resource_group.test.name
  184. location = azurerm_resource_group.test.location
  185. sku {
  186. name = "S1"
  187. capacity = "1"
  188. }
  189. tags = {
  190. purpose = "testing"
  191. }
  192. }
  193. resource "azurerm_iothub_endpoint_storage_container" "test" {
  194. resource_group_name = azurerm_resource_group.test.name
  195. iothub_name = azurerm_iothub.test.name
  196. name = "acctest"
  197. connection_string = azurerm_storage_account.test.primary_blob_connection_string
  198. batch_frequency_in_seconds = 60
  199. max_chunk_size_in_bytes = 10485760
  200. container_name = azurerm_storage_container.test.name
  201. encoding = "Avro"
  202. file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}"
  203. }
  204. resource "azurerm_iothub_fallback_route" "test" {
  205. resource_group_name = azurerm_resource_group.test.name
  206. iothub_name = azurerm_iothub.test.name
  207. condition = "true"
  208. endpoint_names = [azurerm_iothub_endpoint_storage_container.test.name]
  209. enabled = false
  210. }
  211. `, data.RandomInteger, data.Locations.Primary, data.RandomString)
  212. }