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

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 382 lines · 340 code · 42 blank · 0 comment · 39 complexity · 5ac2c9c35c8442ae22722949ccd01d63 MD5 · raw file

  1. package tests
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage"
  7. "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
  8. "github.com/hashicorp/terraform-plugin-sdk/terraform"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
  10. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  11. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
  12. )
  13. func TestAccAzureRMStorageTable_basic(t *testing.T) {
  14. data := acceptance.BuildTestData(t, "azurerm_storage_table", "test")
  15. resource.ParallelTest(t, resource.TestCase{
  16. PreCheck: func() { acceptance.PreCheck(t) },
  17. Providers: acceptance.SupportedProviders,
  18. CheckDestroy: testCheckAzureRMStorageTableDestroy,
  19. Steps: []resource.TestStep{
  20. {
  21. Config: testAccAzureRMStorageTable_basic(data),
  22. Check: resource.ComposeTestCheckFunc(
  23. testCheckAzureRMStorageTableExists(data.ResourceName),
  24. ),
  25. },
  26. data.ImportStep(),
  27. },
  28. })
  29. }
  30. func TestAccAzureRMStorageTable_requiresImport(t *testing.T) {
  31. data := acceptance.BuildTestData(t, "azurerm_storage_table", "test")
  32. resource.ParallelTest(t, resource.TestCase{
  33. PreCheck: func() { acceptance.PreCheck(t) },
  34. Providers: acceptance.SupportedProviders,
  35. CheckDestroy: testCheckAzureRMStorageTableDestroy,
  36. Steps: []resource.TestStep{
  37. {
  38. Config: testAccAzureRMStorageTable_basic(data),
  39. Check: resource.ComposeTestCheckFunc(
  40. testCheckAzureRMStorageTableExists(data.ResourceName),
  41. ),
  42. },
  43. {
  44. Config: testAccAzureRMStorageTable_requiresImport(data),
  45. ExpectError: acceptance.RequiresImportError("azurerm_storage_table"),
  46. },
  47. },
  48. })
  49. }
  50. func TestAccAzureRMStorageTable_disappears(t *testing.T) {
  51. data := acceptance.BuildTestData(t, "azurerm_storage_table", "test")
  52. resource.ParallelTest(t, resource.TestCase{
  53. PreCheck: func() { acceptance.PreCheck(t) },
  54. Providers: acceptance.SupportedProviders,
  55. CheckDestroy: testCheckAzureRMStorageTableDestroy,
  56. Steps: []resource.TestStep{
  57. {
  58. Config: testAccAzureRMStorageTable_basic(data),
  59. Check: resource.ComposeTestCheckFunc(
  60. testCheckAzureRMStorageTableExists("azurerm_storage_table.test"),
  61. testAccARMStorageTableDisappears("azurerm_storage_table.test"),
  62. ),
  63. ExpectNonEmptyPlan: true,
  64. },
  65. },
  66. })
  67. }
  68. func TestAccAzureRMStorageTable_acl(t *testing.T) {
  69. data := acceptance.BuildTestData(t, "azurerm_storage_table", "test")
  70. resource.ParallelTest(t, resource.TestCase{
  71. PreCheck: func() { acceptance.PreCheck(t) },
  72. Providers: acceptance.SupportedProviders,
  73. CheckDestroy: testCheckAzureRMStorageTableDestroy,
  74. Steps: []resource.TestStep{
  75. {
  76. Config: testAccAzureRMStorageTable_acl(data),
  77. Check: resource.ComposeTestCheckFunc(
  78. testCheckAzureRMStorageTableExists(data.ResourceName),
  79. ),
  80. },
  81. data.ImportStep(),
  82. {
  83. Config: testAccAzureRMStorageTable_aclUpdated(data),
  84. Check: resource.ComposeTestCheckFunc(
  85. testCheckAzureRMStorageTableExists(data.ResourceName),
  86. ),
  87. },
  88. data.ImportStep(),
  89. },
  90. })
  91. }
  92. func testCheckAzureRMStorageTableExists(resourceName string) resource.TestCheckFunc {
  93. return func(s *terraform.State) error {
  94. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  95. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  96. rs, ok := s.RootModule().Resources[resourceName]
  97. if !ok {
  98. return fmt.Errorf("Not found: %s", resourceName)
  99. }
  100. tableName := rs.Primary.Attributes["name"]
  101. accountName := rs.Primary.Attributes["storage_account_name"]
  102. account, err := storageClient.FindAccount(ctx, accountName)
  103. if err != nil {
  104. return fmt.Errorf("Error retrieving Account %q for Table %q: %s", accountName, tableName, err)
  105. }
  106. if account == nil {
  107. return fmt.Errorf("Unable to locate Storage Account %q!", accountName)
  108. }
  109. client, err := storageClient.TablesClient(ctx, *account)
  110. if err != nil {
  111. return fmt.Errorf("Error building Table Client: %s", err)
  112. }
  113. props, err := client.Exists(ctx, accountName, tableName)
  114. if err != nil {
  115. if utils.ResponseWasNotFound(props) {
  116. return fmt.Errorf("Table %q doesn't exist in Account %q!", tableName, accountName)
  117. }
  118. return fmt.Errorf("Error retrieving Table %q: %s", tableName, accountName)
  119. }
  120. return nil
  121. }
  122. }
  123. func testAccARMStorageTableDisappears(resourceName string) resource.TestCheckFunc {
  124. return func(s *terraform.State) error {
  125. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  126. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  127. rs, ok := s.RootModule().Resources[resourceName]
  128. if !ok {
  129. return fmt.Errorf("Not found: %s", resourceName)
  130. }
  131. tableName := rs.Primary.Attributes["name"]
  132. accountName := rs.Primary.Attributes["storage_account_name"]
  133. account, err := storageClient.FindAccount(ctx, accountName)
  134. if err != nil {
  135. return fmt.Errorf("Error retrieving Account %q for Table %q: %s", accountName, tableName, err)
  136. }
  137. if account == nil {
  138. return fmt.Errorf("Unable to locate Storage Account %q!", accountName)
  139. }
  140. client, err := storageClient.TablesClient(ctx, *account)
  141. if err != nil {
  142. return fmt.Errorf("Error building Table Client: %s", err)
  143. }
  144. props, err := client.Exists(ctx, accountName, tableName)
  145. if err != nil {
  146. if utils.ResponseWasNotFound(props) {
  147. return fmt.Errorf("Table %q doesn't exist in Account %q so it can't be deleted!", tableName, accountName)
  148. }
  149. return fmt.Errorf("Error retrieving Table %q: %s", tableName, accountName)
  150. }
  151. if _, err := client.Delete(ctx, accountName, tableName); err != nil {
  152. return fmt.Errorf("Error deleting Table %q: %s", tableName, err)
  153. }
  154. return nil
  155. }
  156. }
  157. func testCheckAzureRMStorageTableDestroy(s *terraform.State) error {
  158. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  159. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  160. for _, rs := range s.RootModule().Resources {
  161. if rs.Type != "azurerm_storage_table" {
  162. continue
  163. }
  164. tableName := rs.Primary.Attributes["name"]
  165. accountName := rs.Primary.Attributes["storage_account_name"]
  166. account, err := storageClient.FindAccount(ctx, accountName)
  167. if err != nil {
  168. return fmt.Errorf("Error retrieving Account %q for Table %q: %s", accountName, tableName, err)
  169. }
  170. if account == nil {
  171. return nil
  172. }
  173. client, err := storageClient.TablesClient(ctx, *account)
  174. if err != nil {
  175. return fmt.Errorf("Error building Table Client: %s", err)
  176. }
  177. props, err := client.Exists(ctx, accountName, tableName)
  178. if err != nil {
  179. return nil
  180. }
  181. return fmt.Errorf("Table still exists: %+v", props)
  182. }
  183. return nil
  184. }
  185. func TestValidateArmStorageTableName(t *testing.T) {
  186. validNames := []string{
  187. "mytable01",
  188. "mytable",
  189. "myTable",
  190. "MYTABLE",
  191. "tbl",
  192. strings.Repeat("w", 63),
  193. }
  194. for _, v := range validNames {
  195. _, errors := storage.ValidateArmStorageTableName(v, "name")
  196. if len(errors) != 0 {
  197. t.Fatalf("%q should be a valid Storage Table Name: %q", v, errors)
  198. }
  199. }
  200. invalidNames := []string{
  201. "table",
  202. "-invalidname1",
  203. "invalid_name",
  204. "invalid!",
  205. "ww",
  206. strings.Repeat("w", 64),
  207. }
  208. for _, v := range invalidNames {
  209. _, errors := storage.ValidateArmStorageTableName(v, "name")
  210. if len(errors) == 0 {
  211. t.Fatalf("%q should be an invalid Storage Table Name", v)
  212. }
  213. }
  214. }
  215. func testAccAzureRMStorageTable_basic(data acceptance.TestData) string {
  216. return fmt.Sprintf(`
  217. provider "azurerm" {
  218. features {}
  219. }
  220. resource "azurerm_resource_group" "test" {
  221. name = "acctestRG-%d"
  222. location = "%s"
  223. }
  224. resource "azurerm_storage_account" "test" {
  225. name = "acctestacc%s"
  226. resource_group_name = azurerm_resource_group.test.name
  227. location = azurerm_resource_group.test.location
  228. account_tier = "Standard"
  229. account_replication_type = "LRS"
  230. tags = {
  231. environment = "staging"
  232. }
  233. }
  234. resource "azurerm_storage_table" "test" {
  235. name = "acctestst%d"
  236. storage_account_name = azurerm_storage_account.test.name
  237. }
  238. `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
  239. }
  240. func testAccAzureRMStorageTable_requiresImport(data acceptance.TestData) string {
  241. template := testAccAzureRMStorageTable_basic(data)
  242. return fmt.Sprintf(`
  243. %s
  244. resource "azurerm_storage_table" "import" {
  245. name = azurerm_storage_table.test.name
  246. storage_account_name = azurerm_storage_table.test.storage_account_name
  247. }
  248. `, template)
  249. }
  250. func testAccAzureRMStorageTable_acl(data acceptance.TestData) string {
  251. return fmt.Sprintf(`
  252. provider "azurerm" {
  253. features {}
  254. }
  255. resource "azurerm_resource_group" "test" {
  256. name = "acctestRG-%d"
  257. location = "%s"
  258. }
  259. resource "azurerm_storage_account" "test" {
  260. name = "acctestacc%s"
  261. resource_group_name = azurerm_resource_group.test.name
  262. location = azurerm_resource_group.test.location
  263. account_tier = "Standard"
  264. account_replication_type = "LRS"
  265. tags = {
  266. environment = "staging"
  267. }
  268. }
  269. resource "azurerm_storage_table" "test" {
  270. name = "acctestst%d"
  271. storage_account_name = azurerm_storage_account.test.name
  272. acl {
  273. id = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI"
  274. access_policy {
  275. permissions = "raud"
  276. start = "2020-11-26T08:49:37.0000000Z"
  277. expiry = "2020-11-27T08:49:37.0000000Z"
  278. }
  279. }
  280. }
  281. `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
  282. }
  283. func testAccAzureRMStorageTable_aclUpdated(data acceptance.TestData) string {
  284. return fmt.Sprintf(`
  285. provider "azurerm" {
  286. features {}
  287. }
  288. resource "azurerm_resource_group" "test" {
  289. name = "acctestRG-%d"
  290. location = "%s"
  291. }
  292. resource "azurerm_storage_account" "test" {
  293. name = "acctestacc%s"
  294. resource_group_name = azurerm_resource_group.test.name
  295. location = azurerm_resource_group.test.location
  296. account_tier = "Standard"
  297. account_replication_type = "LRS"
  298. tags = {
  299. environment = "staging"
  300. }
  301. }
  302. resource "azurerm_storage_table" "test" {
  303. name = "acctestst%d"
  304. storage_account_name = azurerm_storage_account.test.name
  305. acl {
  306. id = "AAAANDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI"
  307. access_policy {
  308. permissions = "raud"
  309. start = "2020-11-26T08:49:37.0000000Z"
  310. expiry = "2020-11-27T08:49:37.0000000Z"
  311. }
  312. }
  313. acl {
  314. id = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI"
  315. access_policy {
  316. permissions = "raud"
  317. start = "2019-07-02T09:38:21.0000000Z"
  318. expiry = "2019-07-02T10:38:21.0000000Z"
  319. }
  320. }
  321. }
  322. `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
  323. }