/azurerm/internal/services/storage/tests/resource_arm_storage_table_entity_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 254 lines · 223 code · 29 blank · 2 comment · 24 complexity · 64f4587de614f8fab65afa70d72d52c1 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/internal/acceptance"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  10. "github.com/tombuildsstuff/giovanni/storage/2018-11-09/table/entities"
  11. )
  12. func TestAccAzureRMTableEntity_basic(t *testing.T) {
  13. data := acceptance.BuildTestData(t, "azurerm_storage_table_entity", "test")
  14. resource.ParallelTest(t, resource.TestCase{
  15. PreCheck: func() { acceptance.PreCheck(t) },
  16. Providers: acceptance.SupportedProviders,
  17. CheckDestroy: testCheckAzureRMTableEntityDestroy,
  18. Steps: []resource.TestStep{
  19. {
  20. Config: testAccAzureRMTableEntity_basic(data),
  21. Check: resource.ComposeTestCheckFunc(
  22. testCheckAzureRMTableEntityExists(data.ResourceName),
  23. ),
  24. },
  25. data.ImportStep(),
  26. },
  27. })
  28. }
  29. func TestAccAzureRMTableEntity_requiresImport(t *testing.T) {
  30. data := acceptance.BuildTestData(t, "azurerm_storage_table_entity", "test")
  31. resource.ParallelTest(t, resource.TestCase{
  32. PreCheck: func() { acceptance.PreCheck(t) },
  33. Providers: acceptance.SupportedProviders,
  34. CheckDestroy: testCheckAzureRMTableEntityDestroy,
  35. Steps: []resource.TestStep{
  36. {
  37. Config: testAccAzureRMTableEntity_basic(data),
  38. Check: resource.ComposeTestCheckFunc(
  39. testCheckAzureRMTableEntityExists(data.ResourceName),
  40. ),
  41. },
  42. {
  43. Config: testAccAzureRMTableEntity_requiresImport(data),
  44. ExpectError: acceptance.RequiresImportError("azurerm_storage_table_entity"),
  45. },
  46. },
  47. })
  48. }
  49. func TestAccAzureRMTableEntity_update(t *testing.T) {
  50. data := acceptance.BuildTestData(t, "azurerm_storage_table_entity", "test")
  51. resource.ParallelTest(t, resource.TestCase{
  52. PreCheck: func() { acceptance.PreCheck(t) },
  53. Providers: acceptance.SupportedProviders,
  54. CheckDestroy: testCheckAzureRMTableEntityDestroy,
  55. Steps: []resource.TestStep{
  56. {
  57. Config: testAccAzureRMTableEntity_basic(data),
  58. Check: resource.ComposeTestCheckFunc(
  59. testCheckAzureRMTableEntityExists(data.ResourceName),
  60. ),
  61. },
  62. data.ImportStep(),
  63. {
  64. Config: testAccAzureRMTableEntity_updated(data),
  65. Check: resource.ComposeTestCheckFunc(
  66. testCheckAzureRMTableEntityExists(data.ResourceName),
  67. ),
  68. },
  69. data.ImportStep(),
  70. },
  71. })
  72. }
  73. func testCheckAzureRMTableEntityExists(resourceName string) resource.TestCheckFunc {
  74. return func(s *terraform.State) error {
  75. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  76. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  77. // Ensure we have enough information in state to look up in API
  78. rs, ok := s.RootModule().Resources[resourceName]
  79. if !ok {
  80. return fmt.Errorf("Not found: %s", resourceName)
  81. }
  82. tableName := rs.Primary.Attributes["table_name"]
  83. accountName := rs.Primary.Attributes["storage_account_name"]
  84. partitionKey := rs.Primary.Attributes["partition_key"]
  85. rowKey := rs.Primary.Attributes["row_key"]
  86. account, err := storageClient.FindAccount(ctx, accountName)
  87. if err != nil {
  88. return fmt.Errorf("Error locating Resource Group for Storage Table Entity (Partition Key %q / Row Key %q) (Table %q / Account %q): %v", partitionKey, rowKey, tableName, accountName, err)
  89. }
  90. if account == nil {
  91. return fmt.Errorf("Storage Account %q was not found!", accountName)
  92. }
  93. client, err := storageClient.TableEntityClient(ctx, *account)
  94. if err != nil {
  95. return fmt.Errorf("Error building Table Entity Client: %s", err)
  96. }
  97. input := entities.GetEntityInput{
  98. PartitionKey: partitionKey,
  99. RowKey: rowKey,
  100. MetaDataLevel: entities.NoMetaData,
  101. }
  102. resp, err := client.Get(ctx, accountName, tableName, input)
  103. if err != nil {
  104. return fmt.Errorf("Bad: Get on Table EntityClient: %+v", err)
  105. }
  106. if resp.StatusCode == http.StatusNotFound {
  107. return fmt.Errorf("Bad: Entity (Partition Key %q / Row Key %q) (Table %q / Account %q / Resource Group %q) does not exist", partitionKey, rowKey, tableName, accountName, account.ResourceGroup)
  108. }
  109. return nil
  110. }
  111. }
  112. func testCheckAzureRMTableEntityDestroy(s *terraform.State) error {
  113. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  114. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  115. for _, rs := range s.RootModule().Resources {
  116. if rs.Type != "azurerm_storage_table_entity" {
  117. continue
  118. }
  119. tableName := rs.Primary.Attributes["table_name"]
  120. accountName := rs.Primary.Attributes["storage_account_name"]
  121. partitionKey := rs.Primary.Attributes["parititon_key"]
  122. rowKey := rs.Primary.Attributes["row_key"]
  123. account, err := storageClient.FindAccount(ctx, accountName)
  124. if err != nil {
  125. return fmt.Errorf("Error locating Resource Group for Storage Table Entity (Partition Key %q / Row Key %q) (Table %q / Account %q): %v", partitionKey, rowKey, tableName, accountName, err)
  126. }
  127. // not found, the account's gone
  128. if account == nil {
  129. return nil
  130. }
  131. client, err := storageClient.TableEntityClient(ctx, *account)
  132. if err != nil {
  133. return fmt.Errorf("Error building TableEntity Client: %s", err)
  134. }
  135. input := entities.GetEntityInput{
  136. PartitionKey: partitionKey,
  137. RowKey: rowKey,
  138. }
  139. resp, err := client.Get(ctx, accountName, tableName, input)
  140. if err != nil {
  141. return fmt.Errorf("Bad: Get on Table Entity: %+v", err)
  142. }
  143. if resp.StatusCode != http.StatusNotFound {
  144. return fmt.Errorf("Table Entity still exists:\n%#v", resp)
  145. }
  146. }
  147. return nil
  148. }
  149. func testAccAzureRMTableEntity_basic(data acceptance.TestData) string {
  150. template := testAccAzureRMTableEntity_template(data)
  151. return fmt.Sprintf(`
  152. %s
  153. resource "azurerm_storage_table_entity" "test" {
  154. storage_account_name = azurerm_storage_account.test.name
  155. table_name = azurerm_storage_table.test.name
  156. partition_key = "test_partition%d"
  157. row_key = "test_row%d"
  158. entity = {
  159. Foo = "Bar"
  160. }
  161. }
  162. `, template, data.RandomInteger, data.RandomInteger)
  163. }
  164. func testAccAzureRMTableEntity_requiresImport(data acceptance.TestData) string {
  165. template := testAccAzureRMTableEntity_basic(data)
  166. return fmt.Sprintf(`
  167. %s
  168. resource "azurerm_storage_table_entity" "import" {
  169. storage_account_name = azurerm_storage_account.test.name
  170. table_name = azurerm_storage_table.test.name
  171. partition_key = "test_partition%d"
  172. row_key = "test_row%d"
  173. entity = {
  174. Foo = "Bar"
  175. }
  176. }
  177. `, template, data.RandomInteger, data.RandomInteger)
  178. }
  179. func testAccAzureRMTableEntity_updated(data acceptance.TestData) string {
  180. template := testAccAzureRMTableEntity_template(data)
  181. return fmt.Sprintf(`
  182. %s
  183. resource "azurerm_storage_table_entity" "test" {
  184. storage_account_name = azurerm_storage_account.test.name
  185. table_name = azurerm_storage_table.test.name
  186. partition_key = "test_partition%d"
  187. row_key = "test_row%d"
  188. entity = {
  189. Foo = "Bar"
  190. Test = "Updated"
  191. }
  192. }
  193. `, template, data.RandomInteger, data.RandomInteger)
  194. }
  195. func testAccAzureRMTableEntity_template(data acceptance.TestData) string {
  196. return fmt.Sprintf(`
  197. provider "azurerm" {
  198. features {}
  199. }
  200. resource "azurerm_resource_group" "test" {
  201. name = "acctestRG-%d"
  202. location = "%s"
  203. }
  204. resource "azurerm_storage_account" "test" {
  205. name = "acctestsa%s"
  206. resource_group_name = azurerm_resource_group.test.name
  207. location = azurerm_resource_group.test.location
  208. account_tier = "Standard"
  209. account_replication_type = "LRS"
  210. }
  211. resource "azurerm_storage_table" "test" {
  212. name = "acctestst%d"
  213. storage_account_name = azurerm_storage_account.test.name
  214. }
  215. `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
  216. }