Skip to content

Commit

Permalink
"azurerm_data_factory" - supports property "managed_virtual_network_e…
Browse files Browse the repository at this point in the history
…nabled" (#12343)

fix #10542
  • Loading branch information
njuCZ authored Jul 15, 2021
1 parent 20d7e14 commit 10aa825
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 12 deletions.
29 changes: 17 additions & 12 deletions azurerm/internal/services/datafactory/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
)

type Client struct {
DatasetClient *datafactory.DatasetsClient
FactoriesClient *datafactory.FactoriesClient
IntegrationRuntimesClient *datafactory.IntegrationRuntimesClient
LinkedServiceClient *datafactory.LinkedServicesClient
PipelinesClient *datafactory.PipelinesClient
TriggersClient *datafactory.TriggersClient
DatasetClient *datafactory.DatasetsClient
FactoriesClient *datafactory.FactoriesClient
IntegrationRuntimesClient *datafactory.IntegrationRuntimesClient
LinkedServiceClient *datafactory.LinkedServicesClient
ManagedVirtualNetworksClient *datafactory.ManagedVirtualNetworksClient
PipelinesClient *datafactory.PipelinesClient
TriggersClient *datafactory.TriggersClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -27,18 +28,22 @@ func NewClient(o *common.ClientOptions) *Client {
LinkedServiceClient := datafactory.NewLinkedServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&LinkedServiceClient.Client, o.ResourceManagerAuthorizer)

ManagedVirtualNetworksClient := datafactory.NewManagedVirtualNetworksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagedVirtualNetworksClient.Client, o.ResourceManagerAuthorizer)

PipelinesClient := datafactory.NewPipelinesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&PipelinesClient.Client, o.ResourceManagerAuthorizer)

TriggersClient := datafactory.NewTriggersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&TriggersClient.Client, o.ResourceManagerAuthorizer)

return &Client{
DatasetClient: &DatasetClient,
FactoriesClient: &FactoriesClient,
IntegrationRuntimesClient: &IntegrationRuntimesClient,
LinkedServiceClient: &LinkedServiceClient,
PipelinesClient: &PipelinesClient,
TriggersClient: &TriggersClient,
DatasetClient: &DatasetClient,
FactoriesClient: &FactoriesClient,
IntegrationRuntimesClient: &IntegrationRuntimesClient,
LinkedServiceClient: &LinkedServiceClient,
ManagedVirtualNetworksClient: &ManagedVirtualNetworksClient,
PipelinesClient: &PipelinesClient,
TriggersClient: &TriggersClient,
}
}
49 changes: 49 additions & 0 deletions azurerm/internal/services/datafactory/data_factory_resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datafactory

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -207,6 +208,11 @@ func resourceDataFactory() *pluginsdk.Resource {
},
},

"managed_virtual_network_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"public_network_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand All @@ -227,6 +233,7 @@ func resourceDataFactory() *pluginsdk.Resource {

func resourceDataFactoryCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.FactoriesClient
managedVirtualNetworksClient := meta.(*clients.Client).DataFactory.ManagedVirtualNetworksClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -316,13 +323,32 @@ func resourceDataFactoryCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
}
}

managedVirtualNetworkEnabled := d.Get("managed_virtual_network_enabled").(bool)
// only pass datafactory.ManagedVirtualNetworkResource{} will cause rest api error
resource := datafactory.ManagedVirtualNetworkResource{
Properties: &datafactory.ManagedVirtualNetwork{},
}
if d.IsNewResource() && managedVirtualNetworkEnabled {
if _, err := managedVirtualNetworksClient.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, "default", resource, ""); err != nil {
return fmt.Errorf("creating virtual network for %s: %+v", id, err)
}
} else if !d.IsNewResource() && d.HasChange("managed_virtual_network_enabled") {
if !managedVirtualNetworkEnabled {
return fmt.Errorf("updating %s: once Managed Virtual Network has been Enabled it's not possible to disable it", id)
}
if _, err := managedVirtualNetworksClient.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, "default", resource, ""); err != nil {
return fmt.Errorf("creating virtual network for %s: %+v", id, err)
}
}

d.SetId(id.ID())

return resourceDataFactoryRead(d, meta)
}

func resourceDataFactoryRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.FactoriesClient
managedVirtualNetworksClient := meta.(*clients.Client).DataFactory.ManagedVirtualNetworksClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -393,6 +419,16 @@ func resourceDataFactoryRead(d *pluginsdk.ResourceData, meta interface{}) error
d.Set("public_network_enabled", resp.PublicNetworkAccess == datafactory.PublicNetworkAccessEnabled)
}

managedVirtualNetworkEnabled := false
managedVirtualNetworkName, err := getManagedVirtualNetworkName(ctx, managedVirtualNetworksClient, id.ResourceGroup, id.FactoryName)
if err != nil {
return err
}
if managedVirtualNetworkName != nil {
managedVirtualNetworkEnabled = true
}
d.Set("managed_virtual_network_enabled", managedVirtualNetworkEnabled)

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

Expand Down Expand Up @@ -577,3 +613,16 @@ func flattenDataFactoryGlobalParameters(input map[string]*datafactory.GlobalPara
}
return result
}

// Only one VNet is allowed per factory
func getManagedVirtualNetworkName(ctx context.Context, client *datafactory.ManagedVirtualNetworksClient, resourceGroup, factoryName string) (*string, error) {
resp, err := client.ListByFactory(ctx, resourceGroup, factoryName)
if err != nil {
return nil, err
}
if len(resp.Values()) == 0 {
return nil, nil
}
managedVirtualNetwork := resp.Values()[0]
return managedVirtualNetwork.Name, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,43 @@ func TestAccDataFactory_globalParameterUpdate(t *testing.T) {
})
}

func TestAccDataFactory_managedVirtualNetwork(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_data_factory", "test")
r := DataFactoryResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.managedVirtualNetwork(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccDataFactory_managedVirtualNetworkUpdated(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_data_factory", "test")
r := DataFactoryResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.managedVirtualNetwork(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t DataFactoryResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.DataFactoryID(state.ID)
if err != nil {
Expand Down Expand Up @@ -602,3 +639,23 @@ resource "azurerm_data_factory" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (DataFactoryResource) managedVirtualNetwork(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
managed_virtual_network_enabled = true
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
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 @@ -43,6 +43,8 @@ The following arguments are supported:

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

* `managed_virtual_network_enabled` - (Optional) Is Managed Virtual Network enabled?

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

* `customer_managed_key_id` - (Optional) Specifies the Azure Key Vault Key ID to be used as the Customer Managed Key (CMK) for double encryption. Required with user assigned identity.
Expand Down

0 comments on commit 10aa825

Please sign in to comment.