/azurerm/internal/services/datafactory/data_factory_dataset_json_resource_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 374 lines · 348 code · 25 blank · 1 comment · 12 complexity · 811ae88b8983727630f22cf08dc0fb14 MD5 · raw file

  1. package datafactory_test
  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/terraform-providers/terraform-provider-azurerm/azurerm/utils"
  11. )
  12. func TestAccAzureRMDataFactoryDatasetJSON_basic(t *testing.T) {
  13. data := acceptance.BuildTestData(t, "azurerm_data_factory_dataset_json", "test")
  14. resource.ParallelTest(t, resource.TestCase{
  15. PreCheck: func() { acceptance.PreCheck(t) },
  16. Providers: acceptance.SupportedProviders,
  17. CheckDestroy: testCheckAzureRMDataFactoryDatasetJSONDestroy,
  18. Steps: []resource.TestStep{
  19. {
  20. Config: testAccAzureRMDataFactoryDatasetJSON_basic(data),
  21. Check: resource.ComposeTestCheckFunc(
  22. testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
  23. ),
  24. },
  25. data.ImportStep(),
  26. },
  27. })
  28. }
  29. func TestAccAzureRMDataFactoryDatasetJSON_update(t *testing.T) {
  30. data := acceptance.BuildTestData(t, "azurerm_data_factory_dataset_json", "test")
  31. resource.ParallelTest(t, resource.TestCase{
  32. PreCheck: func() { acceptance.PreCheck(t) },
  33. Providers: acceptance.SupportedProviders,
  34. CheckDestroy: testCheckAzureRMDataFactoryDatasetJSONDestroy,
  35. Steps: []resource.TestStep{
  36. {
  37. Config: testAccAzureRMDataFactoryDatasetJSON_update1(data),
  38. Check: resource.ComposeTestCheckFunc(
  39. testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
  40. resource.TestCheckResourceAttr(data.ResourceName, "parameters.%", "2"),
  41. resource.TestCheckResourceAttr(data.ResourceName, "annotations.#", "3"),
  42. resource.TestCheckResourceAttr(data.ResourceName, "schema_column.#", "1"),
  43. resource.TestCheckResourceAttr(data.ResourceName, "additional_properties.%", "2"),
  44. resource.TestCheckResourceAttr(data.ResourceName, "description", "test description"),
  45. ),
  46. },
  47. data.ImportStep(),
  48. {
  49. Config: testAccAzureRMDataFactoryDatasetJSON_update2(data),
  50. Check: resource.ComposeTestCheckFunc(
  51. testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
  52. resource.TestCheckResourceAttr(data.ResourceName, "parameters.%", "3"),
  53. resource.TestCheckResourceAttr(data.ResourceName, "annotations.#", "2"),
  54. resource.TestCheckResourceAttr(data.ResourceName, "schema_column.#", "2"),
  55. resource.TestCheckResourceAttr(data.ResourceName, "additional_properties.%", "1"),
  56. resource.TestCheckResourceAttr(data.ResourceName, "description", "test description 2"),
  57. ),
  58. },
  59. data.ImportStep(),
  60. },
  61. })
  62. }
  63. func testCheckAzureRMDataFactoryDatasetJSONExists(name string) resource.TestCheckFunc {
  64. return func(s *terraform.State) error {
  65. client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.DatasetClient
  66. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  67. // Ensure we have enough information in state to look up in API
  68. rs, ok := s.RootModule().Resources[name]
  69. if !ok {
  70. return fmt.Errorf("Not found: %s", name)
  71. }
  72. name := rs.Primary.Attributes["name"]
  73. resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
  74. dataFactoryName := rs.Primary.Attributes["data_factory_name"]
  75. if !hasResourceGroup {
  76. return fmt.Errorf("Bad: no resource group found in state for Data Factory: %s", name)
  77. }
  78. resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
  79. if err != nil {
  80. return fmt.Errorf("Bad: Get on dataFactoryDatasetClient: %+v", err)
  81. }
  82. if utils.ResponseWasNotFound(resp.Response) {
  83. return fmt.Errorf("Bad: Data Factory Dataset JSON %q (data factory name: %q / resource group: %q) does not exist", name, dataFactoryName, resourceGroup)
  84. }
  85. return nil
  86. }
  87. }
  88. func testCheckAzureRMDataFactoryDatasetJSONDestroy(s *terraform.State) error {
  89. client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.DatasetClient
  90. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  91. for _, rs := range s.RootModule().Resources {
  92. if rs.Type != "azurerm_data_factory_dataset_json" {
  93. continue
  94. }
  95. name := rs.Primary.Attributes["name"]
  96. resourceGroup := rs.Primary.Attributes["resource_group_name"]
  97. dataFactoryName := rs.Primary.Attributes["data_factory_name"]
  98. resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
  99. if err != nil {
  100. return nil
  101. }
  102. if resp.StatusCode != http.StatusNotFound {
  103. return fmt.Errorf("Data Factory Dataset HTTP still exists:\n%#v", resp.Properties)
  104. }
  105. }
  106. return nil
  107. }
  108. func testAccAzureRMDataFactoryDatasetJSON_basic(data acceptance.TestData) string {
  109. return fmt.Sprintf(`
  110. provider "azurerm" {
  111. features {}
  112. }
  113. resource "azurerm_resource_group" "test" {
  114. name = "acctestRG-df-%d"
  115. location = "%s"
  116. }
  117. resource "azurerm_data_factory" "test" {
  118. name = "acctestdf%d"
  119. location = azurerm_resource_group.test.location
  120. resource_group_name = azurerm_resource_group.test.name
  121. }
  122. resource "azurerm_data_factory_linked_service_web" "test" {
  123. name = "acctestlsweb%d"
  124. resource_group_name = azurerm_resource_group.test.name
  125. data_factory_name = azurerm_data_factory.test.name
  126. authentication_type = "Anonymous"
  127. url = "http://www.bing.com"
  128. }
  129. resource "azurerm_data_factory_dataset_json" "test" {
  130. name = "acctestds%d"
  131. resource_group_name = azurerm_resource_group.test.name
  132. data_factory_name = azurerm_data_factory.test.name
  133. linked_service_name = azurerm_data_factory_linked_service_web.test.name
  134. http_server_location {
  135. relative_url = "/fizz/buzz/"
  136. path = "foo/bar/"
  137. filename = "foo.json"
  138. }
  139. encoding = "UTF-8"
  140. }
  141. `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
  142. }
  143. func testAccAzureRMDataFactoryDatasetJSON_update1(data acceptance.TestData) string {
  144. return fmt.Sprintf(`
  145. provider "azurerm" {
  146. features {}
  147. }
  148. resource "azurerm_resource_group" "test" {
  149. name = "acctestRG-df-%d"
  150. location = "%s"
  151. }
  152. resource "azurerm_data_factory" "test" {
  153. name = "acctestdf%d"
  154. location = azurerm_resource_group.test.location
  155. resource_group_name = azurerm_resource_group.test.name
  156. }
  157. resource "azurerm_data_factory_linked_service_web" "test" {
  158. name = "acctestlsweb%d"
  159. resource_group_name = azurerm_resource_group.test.name
  160. data_factory_name = azurerm_data_factory.test.name
  161. authentication_type = "Anonymous"
  162. url = "http://www.bing.com"
  163. }
  164. resource "azurerm_data_factory_dataset_json" "test" {
  165. name = "acctestds%d"
  166. resource_group_name = azurerm_resource_group.test.name
  167. data_factory_name = azurerm_data_factory.test.name
  168. linked_service_name = azurerm_data_factory_linked_service_web.test.name
  169. http_server_location {
  170. relative_url = "/fizz/buzz/"
  171. path = "foo/bar/"
  172. filename = "foo.json"
  173. }
  174. encoding = "UTF-8"
  175. description = "test description"
  176. annotations = ["test1", "test2", "test3"]
  177. folder = "testFolder"
  178. parameters = {
  179. foo = "test1"
  180. Bar = "Test2"
  181. }
  182. additional_properties = {
  183. foo = "test1"
  184. bar = "test2"
  185. }
  186. schema_column {
  187. name = "test1"
  188. type = "Byte"
  189. description = "description"
  190. }
  191. }
  192. `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
  193. }
  194. func testAccAzureRMDataFactoryDatasetJSON_update2(data acceptance.TestData) string {
  195. return fmt.Sprintf(`
  196. provider "azurerm" {
  197. features {}
  198. }
  199. resource "azurerm_resource_group" "test" {
  200. name = "acctestRG-df-%d"
  201. location = "%s"
  202. }
  203. resource "azurerm_data_factory" "test" {
  204. name = "acctestdf%d"
  205. location = azurerm_resource_group.test.location
  206. resource_group_name = azurerm_resource_group.test.name
  207. }
  208. resource "azurerm_data_factory_linked_service_web" "test" {
  209. name = "acctestlsweb%d"
  210. resource_group_name = azurerm_resource_group.test.name
  211. data_factory_name = azurerm_data_factory.test.name
  212. authentication_type = "Anonymous"
  213. url = "http://www.bing.com"
  214. }
  215. resource "azurerm_data_factory_dataset_json" "test" {
  216. name = "acctestds%d"
  217. resource_group_name = azurerm_resource_group.test.name
  218. data_factory_name = azurerm_data_factory.test.name
  219. linked_service_name = azurerm_data_factory_linked_service_web.test.name
  220. http_server_location {
  221. relative_url = "/fizz/buzz/"
  222. path = "foo/bar/"
  223. filename = "foo.json"
  224. }
  225. encoding = "UTF-8"
  226. description = "test description 2"
  227. annotations = ["test1", "test2"]
  228. folder = "testFolder"
  229. parameters = {
  230. foo = "test1"
  231. bar = "test2"
  232. buzz = "test3"
  233. }
  234. additional_properties = {
  235. foo = "test1"
  236. }
  237. schema_column {
  238. name = "test1"
  239. type = "Byte"
  240. description = "description"
  241. }
  242. schema_column {
  243. name = "test2"
  244. type = "Byte"
  245. description = "description"
  246. }
  247. }
  248. `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
  249. }
  250. func TestAccAzureRMDataFactoryDatasetJSON_blob(t *testing.T) {
  251. data := acceptance.BuildTestData(t, "azurerm_data_factory_dataset_json", "test")
  252. resource.ParallelTest(t, resource.TestCase{
  253. PreCheck: func() { acceptance.PreCheck(t) },
  254. Providers: acceptance.SupportedProviders,
  255. CheckDestroy: testCheckAzureRMDataFactoryDatasetJSONDestroy,
  256. Steps: []resource.TestStep{
  257. {
  258. Config: testAccAzureRMDataFactoryDatasetJSON_blob(data),
  259. Check: resource.ComposeTestCheckFunc(
  260. testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
  261. ),
  262. },
  263. data.ImportStep(),
  264. },
  265. })
  266. }
  267. func testAccAzureRMDataFactoryDatasetJSON_blob(data acceptance.TestData) string {
  268. return fmt.Sprintf(`
  269. provider "azurerm" {
  270. features {}
  271. }
  272. resource "azurerm_resource_group" "test" {
  273. name = "acctestRG-df-%d"
  274. location = "%s"
  275. }
  276. resource "azurerm_storage_account" "test" {
  277. name = "acctestdf%s"
  278. location = azurerm_resource_group.test.location
  279. resource_group_name = azurerm_resource_group.test.name
  280. account_tier = "Standard"
  281. account_replication_type = "GRS"
  282. }
  283. resource "azurerm_storage_container" "test" {
  284. name = "content"
  285. storage_account_name = azurerm_storage_account.test.name
  286. container_access_type = "private"
  287. }
  288. resource "azurerm_data_factory" "test" {
  289. name = "acctestdf%d"
  290. location = azurerm_resource_group.test.location
  291. resource_group_name = azurerm_resource_group.test.name
  292. }
  293. resource "azurerm_data_factory_linked_service_azure_blob_storage" "test" {
  294. name = "acctestlsblob%d"
  295. resource_group_name = azurerm_resource_group.test.name
  296. data_factory_name = azurerm_data_factory.test.name
  297. connection_string = azurerm_storage_account.test.primary_connection_string
  298. }
  299. resource "azurerm_data_factory_dataset_json" "test" {
  300. name = "acctestds%d"
  301. resource_group_name = azurerm_resource_group.test.name
  302. data_factory_name = azurerm_data_factory.test.name
  303. linked_service_name = azurerm_data_factory_linked_service_azure_blob_storage.test.name
  304. azure_blob_storage_location {
  305. container = azurerm_storage_container.test.name
  306. path = "foo/bar/"
  307. filename = "foo.txt"
  308. }
  309. encoding = "UTF-8"
  310. }
  311. `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger, data.RandomInteger, data.RandomInteger)
  312. }