/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
- package datafactory_test
- import (
- "fmt"
- "net/http"
- "testing"
- "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
- "github.com/hashicorp/terraform-plugin-sdk/terraform"
- "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
- "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
- "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
- )
- func TestAccAzureRMDataFactoryDatasetJSON_basic(t *testing.T) {
- data := acceptance.BuildTestData(t, "azurerm_data_factory_dataset_json", "test")
- resource.ParallelTest(t, resource.TestCase{
- PreCheck: func() { acceptance.PreCheck(t) },
- Providers: acceptance.SupportedProviders,
- CheckDestroy: testCheckAzureRMDataFactoryDatasetJSONDestroy,
- Steps: []resource.TestStep{
- {
- Config: testAccAzureRMDataFactoryDatasetJSON_basic(data),
- Check: resource.ComposeTestCheckFunc(
- testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
- ),
- },
- data.ImportStep(),
- },
- })
- }
- func TestAccAzureRMDataFactoryDatasetJSON_update(t *testing.T) {
- data := acceptance.BuildTestData(t, "azurerm_data_factory_dataset_json", "test")
- resource.ParallelTest(t, resource.TestCase{
- PreCheck: func() { acceptance.PreCheck(t) },
- Providers: acceptance.SupportedProviders,
- CheckDestroy: testCheckAzureRMDataFactoryDatasetJSONDestroy,
- Steps: []resource.TestStep{
- {
- Config: testAccAzureRMDataFactoryDatasetJSON_update1(data),
- Check: resource.ComposeTestCheckFunc(
- testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
- resource.TestCheckResourceAttr(data.ResourceName, "parameters.%", "2"),
- resource.TestCheckResourceAttr(data.ResourceName, "annotations.#", "3"),
- resource.TestCheckResourceAttr(data.ResourceName, "schema_column.#", "1"),
- resource.TestCheckResourceAttr(data.ResourceName, "additional_properties.%", "2"),
- resource.TestCheckResourceAttr(data.ResourceName, "description", "test description"),
- ),
- },
- data.ImportStep(),
- {
- Config: testAccAzureRMDataFactoryDatasetJSON_update2(data),
- Check: resource.ComposeTestCheckFunc(
- testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
- resource.TestCheckResourceAttr(data.ResourceName, "parameters.%", "3"),
- resource.TestCheckResourceAttr(data.ResourceName, "annotations.#", "2"),
- resource.TestCheckResourceAttr(data.ResourceName, "schema_column.#", "2"),
- resource.TestCheckResourceAttr(data.ResourceName, "additional_properties.%", "1"),
- resource.TestCheckResourceAttr(data.ResourceName, "description", "test description 2"),
- ),
- },
- data.ImportStep(),
- },
- })
- }
- func testCheckAzureRMDataFactoryDatasetJSONExists(name string) resource.TestCheckFunc {
- return func(s *terraform.State) error {
- client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.DatasetClient
- ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
- // Ensure we have enough information in state to look up in API
- rs, ok := s.RootModule().Resources[name]
- if !ok {
- return fmt.Errorf("Not found: %s", name)
- }
- name := rs.Primary.Attributes["name"]
- resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
- dataFactoryName := rs.Primary.Attributes["data_factory_name"]
- if !hasResourceGroup {
- return fmt.Errorf("Bad: no resource group found in state for Data Factory: %s", name)
- }
- resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
- if err != nil {
- return fmt.Errorf("Bad: Get on dataFactoryDatasetClient: %+v", err)
- }
- if utils.ResponseWasNotFound(resp.Response) {
- return fmt.Errorf("Bad: Data Factory Dataset JSON %q (data factory name: %q / resource group: %q) does not exist", name, dataFactoryName, resourceGroup)
- }
- return nil
- }
- }
- func testCheckAzureRMDataFactoryDatasetJSONDestroy(s *terraform.State) error {
- client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.DatasetClient
- ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
- for _, rs := range s.RootModule().Resources {
- if rs.Type != "azurerm_data_factory_dataset_json" {
- continue
- }
- name := rs.Primary.Attributes["name"]
- resourceGroup := rs.Primary.Attributes["resource_group_name"]
- dataFactoryName := rs.Primary.Attributes["data_factory_name"]
- resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
- if err != nil {
- return nil
- }
- if resp.StatusCode != http.StatusNotFound {
- return fmt.Errorf("Data Factory Dataset HTTP still exists:\n%#v", resp.Properties)
- }
- }
- return nil
- }
- func testAccAzureRMDataFactoryDatasetJSON_basic(data acceptance.TestData) string {
- return fmt.Sprintf(`
- provider "azurerm" {
- features {}
- }
- resource "azurerm_resource_group" "test" {
- name = "acctestRG-df-%d"
- location = "%s"
- }
- resource "azurerm_data_factory" "test" {
- name = "acctestdf%d"
- location = azurerm_resource_group.test.location
- resource_group_name = azurerm_resource_group.test.name
- }
- resource "azurerm_data_factory_linked_service_web" "test" {
- name = "acctestlsweb%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- authentication_type = "Anonymous"
- url = "http://www.bing.com"
- }
- resource "azurerm_data_factory_dataset_json" "test" {
- name = "acctestds%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- linked_service_name = azurerm_data_factory_linked_service_web.test.name
- http_server_location {
- relative_url = "/fizz/buzz/"
- path = "foo/bar/"
- filename = "foo.json"
- }
- encoding = "UTF-8"
- }
- `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
- }
- func testAccAzureRMDataFactoryDatasetJSON_update1(data acceptance.TestData) string {
- return fmt.Sprintf(`
- provider "azurerm" {
- features {}
- }
- resource "azurerm_resource_group" "test" {
- name = "acctestRG-df-%d"
- location = "%s"
- }
- resource "azurerm_data_factory" "test" {
- name = "acctestdf%d"
- location = azurerm_resource_group.test.location
- resource_group_name = azurerm_resource_group.test.name
- }
- resource "azurerm_data_factory_linked_service_web" "test" {
- name = "acctestlsweb%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- authentication_type = "Anonymous"
- url = "http://www.bing.com"
- }
- resource "azurerm_data_factory_dataset_json" "test" {
- name = "acctestds%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- linked_service_name = azurerm_data_factory_linked_service_web.test.name
- http_server_location {
- relative_url = "/fizz/buzz/"
- path = "foo/bar/"
- filename = "foo.json"
- }
- encoding = "UTF-8"
- description = "test description"
- annotations = ["test1", "test2", "test3"]
- folder = "testFolder"
- parameters = {
- foo = "test1"
- Bar = "Test2"
- }
- additional_properties = {
- foo = "test1"
- bar = "test2"
- }
- schema_column {
- name = "test1"
- type = "Byte"
- description = "description"
- }
- }
- `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
- }
- func testAccAzureRMDataFactoryDatasetJSON_update2(data acceptance.TestData) string {
- return fmt.Sprintf(`
- provider "azurerm" {
- features {}
- }
- resource "azurerm_resource_group" "test" {
- name = "acctestRG-df-%d"
- location = "%s"
- }
- resource "azurerm_data_factory" "test" {
- name = "acctestdf%d"
- location = azurerm_resource_group.test.location
- resource_group_name = azurerm_resource_group.test.name
- }
- resource "azurerm_data_factory_linked_service_web" "test" {
- name = "acctestlsweb%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- authentication_type = "Anonymous"
- url = "http://www.bing.com"
- }
- resource "azurerm_data_factory_dataset_json" "test" {
- name = "acctestds%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- linked_service_name = azurerm_data_factory_linked_service_web.test.name
- http_server_location {
- relative_url = "/fizz/buzz/"
- path = "foo/bar/"
- filename = "foo.json"
- }
- encoding = "UTF-8"
- description = "test description 2"
- annotations = ["test1", "test2"]
- folder = "testFolder"
- parameters = {
- foo = "test1"
- bar = "test2"
- buzz = "test3"
- }
- additional_properties = {
- foo = "test1"
- }
- schema_column {
- name = "test1"
- type = "Byte"
- description = "description"
- }
- schema_column {
- name = "test2"
- type = "Byte"
- description = "description"
- }
- }
- `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
- }
- func TestAccAzureRMDataFactoryDatasetJSON_blob(t *testing.T) {
- data := acceptance.BuildTestData(t, "azurerm_data_factory_dataset_json", "test")
- resource.ParallelTest(t, resource.TestCase{
- PreCheck: func() { acceptance.PreCheck(t) },
- Providers: acceptance.SupportedProviders,
- CheckDestroy: testCheckAzureRMDataFactoryDatasetJSONDestroy,
- Steps: []resource.TestStep{
- {
- Config: testAccAzureRMDataFactoryDatasetJSON_blob(data),
- Check: resource.ComposeTestCheckFunc(
- testCheckAzureRMDataFactoryDatasetJSONExists(data.ResourceName),
- ),
- },
- data.ImportStep(),
- },
- })
- }
- func testAccAzureRMDataFactoryDatasetJSON_blob(data acceptance.TestData) string {
- return fmt.Sprintf(`
- provider "azurerm" {
- features {}
- }
- resource "azurerm_resource_group" "test" {
- name = "acctestRG-df-%d"
- location = "%s"
- }
- resource "azurerm_storage_account" "test" {
- name = "acctestdf%s"
- location = azurerm_resource_group.test.location
- resource_group_name = azurerm_resource_group.test.name
- account_tier = "Standard"
- account_replication_type = "GRS"
- }
- resource "azurerm_storage_container" "test" {
- name = "content"
- storage_account_name = azurerm_storage_account.test.name
- container_access_type = "private"
- }
- resource "azurerm_data_factory" "test" {
- name = "acctestdf%d"
- location = azurerm_resource_group.test.location
- resource_group_name = azurerm_resource_group.test.name
- }
- resource "azurerm_data_factory_linked_service_azure_blob_storage" "test" {
- name = "acctestlsblob%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- connection_string = azurerm_storage_account.test.primary_connection_string
- }
- resource "azurerm_data_factory_dataset_json" "test" {
- name = "acctestds%d"
- resource_group_name = azurerm_resource_group.test.name
- data_factory_name = azurerm_data_factory.test.name
- linked_service_name = azurerm_data_factory_linked_service_azure_blob_storage.test.name
- azure_blob_storage_location {
- container = azurerm_storage_container.test.name
- path = "foo/bar/"
- filename = "foo.txt"
- }
- encoding = "UTF-8"
- }
- `, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger, data.RandomInteger, data.RandomInteger)
- }