Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_data_factory - Add support for public_network_enabled for Data Factories #9605

Merged
merged 2 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions azurerm/internal/services/datafactory/data_factory_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand All @@ -23,6 +24,15 @@ func resourceArmDataFactory() *schema.Resource {
Update: resourceArmDataFactoryCreateUpdate,
Delete: resourceArmDataFactoryDelete,

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: migration.DataFactoryUpgradeV0Schema().CoreConfigSchema().ImpliedType(),
Upgrade: migration.DataFactoryUpgradeV0ToV1,
Version: 0,
},
},

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -151,6 +161,12 @@ func resourceArmDataFactory() *schema.Resource {
},
},

"public_network_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -180,8 +196,15 @@ func resourceArmDataFactoryCreateUpdate(d *schema.ResourceData, meta interface{}
}

dataFactory := datafactory.Factory{
Location: &location,
Tags: tags.Expand(t),
Location: &location,
FactoryProperties: &datafactory.FactoryProperties{},
Tags: tags.Expand(t),
}

dataFactory.PublicNetworkAccess = datafactory.PublicNetworkAccessEnabled
enabled := d.Get("public_network_enabled").(bool)
if !enabled {
dataFactory.FactoryProperties.PublicNetworkAccess = datafactory.PublicNetworkAccessDisabled
}

if v, ok := d.GetOk("identity.0.type"); ok {
Expand Down Expand Up @@ -269,6 +292,11 @@ func resourceArmDataFactoryRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error flattening `identity`: %+v", err)
}

// This variable isn't returned from the API if it hasn't been passed in first but we know the default is `true`
if resp.PublicNetworkAccess != "" {
d.Set("public_network_enabled", resp.PublicNetworkAccess == datafactory.PublicNetworkAccessEnabled)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package datafactory

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func ResourceDataFactoryMigrateState(
v int, is *terraform.InstanceState, _ interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found AzureRM DataFactory State v0; migrating to v1")
return migrateDataFactoryStateV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateDataFactoryStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}

log.Printf("[DEBUG] ARM Data Factory Attributes before Migration: %#v", is.Attributes)

is.Attributes["public_network_enabled"] = string(datafactory.PublicNetworkAccessEnabled)

log.Printf("[DEBUG] ARM Data Factory Attributes after State Migration: %#v", is.Attributes)

return is, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ func TestAccAzureRMDataFactory_github(t *testing.T) {
})
}

func TestAccAzureRMDataFactory_publicNetworkDisabled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_data_factory", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMDataFactoryDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMDataFactory_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDataFactoryExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMDataFactory_publicNetworkDisabled(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDataFactoryExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMDataFactory_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDataFactoryExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMDataFactoryExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).DataFactory.FactoriesClient
Expand Down Expand Up @@ -385,3 +418,24 @@ resource "azurerm_data_factory" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMDataFactory_publicNetworkDisabled(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

public_network_enabled = false
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
143 changes: 143 additions & 0 deletions azurerm/internal/services/datafactory/migration/data_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package migration

import (
"log"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
)

func DataFactoryUpgradeV0Schema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataFactoryName(),
},

"location": azure.SchemaLocation(),

// There's a bug in the Azure API where this is returned in lower-case
// BUG: https://github.com/Azure/azure-rest-api-specs/issues/5788
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"identity": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"SystemAssigned",
}, false),
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"github_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"vsts_configuration"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"branch_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"git_url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"repository_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"root_folder": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"vsts_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"github_configuration"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"branch_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"project_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"repository_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"root_folder": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"tenant_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
},
},
},
"tags": tags.Schema(),
},
}
}

func DataFactoryUpgradeV0ToV1(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
log.Printf("[DEBUG] Updating `public_network_enabled` to %q", datafactory.PublicNetworkAccessEnabled)

rawState["public_network_enabled"] = true

return rawState, nil
}
2 changes: 2 additions & 0 deletions website/docs/r/data_factory.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ The following arguments are supported:

* `vsts_configuration` - (Optional) A `vsts_configuration` block as defined below.

* `public_network_enabled` - (Optional) Is the Data Factory visible to the public network? Defaults to `true`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down