diff --git a/azurerm/internal/services/databricks/resource_arm_databricks_workspace.go b/azurerm/internal/services/databricks/resource_arm_databricks_workspace.go index 23f608829f4b..bf3b0bcc17e7 100644 --- a/azurerm/internal/services/databricks/resource_arm_databricks_workspace.go +++ b/azurerm/internal/services/databricks/resource_arm_databricks_workspace.go @@ -69,6 +69,32 @@ func resourceArmDatabricksWorkspace() *schema.Resource { ValidateFunc: validate.NoEmptyStrings, }, + "custom_parameters": { + Type: schema.TypeList, + Optional: true, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "public_subnet_name": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + }, + "private_subnet_name": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + }, + "virtual_network_id": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + }, + }, + }, + }, + "managed_resource_group_id": { Type: schema.TypeString, Computed: true, @@ -125,6 +151,7 @@ func resourceArmDatabricksWorkspaceCreateUpdate(d *schema.ResourceData, meta int Location: utils.String(location), WorkspaceProperties: &databricks.WorkspaceProperties{ ManagedResourceGroupID: &managedResourceGroupID, + Parameters: expandWorkspaceCustomParameters(d), }, Tags: expandedTags, } @@ -193,6 +220,7 @@ func resourceArmDatabricksWorkspaceRead(d *schema.ResourceData, meta interface{} } d.Set("managed_resource_group_id", props.ManagedResourceGroupID) d.Set("managed_resource_group_name", managedResourceGroupID.ResourceGroup) + d.Set("custom_parameters", flattenWorkspaceCustomParameters(props.Parameters)) } return tags.FlattenAndSet(d, resp.Tags) @@ -225,6 +253,62 @@ func resourceArmDatabricksWorkspaceDelete(d *schema.ResourceData, meta interface return nil } +func flattenWorkspaceCustomParameters(p *databricks.WorkspaceCustomParameters) []interface{} { + if p == nil { + return nil + } + + parameters := make(map[string]interface{}) + if privateSubnet := p.CustomPrivateSubnetName; privateSubnet != nil { + if privateSubnet.Value != nil { + parameters["private_subnet_name"] = *privateSubnet.Value + } + } + + if publicSubnet := p.CustomPublicSubnetName; publicSubnet != nil { + if publicSubnet.Value != nil { + parameters["public_subnet_name"] = *publicSubnet.Value + } + } + + if vnetID := p.CustomVirtualNetworkID; vnetID != nil { + if vnetID.Value != nil { + parameters["virtual_network_id"] = *vnetID.Value + } + } + + return []interface{}{parameters} +} + +func expandWorkspaceCustomParameters(d *schema.ResourceData) *databricks.WorkspaceCustomParameters { + configList, ok := d.GetOkExists("custom_parameters") + if !ok { + return nil + } + config := configList.([]interface{})[0].(map[string]interface{}) + parameters := databricks.WorkspaceCustomParameters{} + + if v := config["public_subnet_name"].(string); v != "" { + parameters.CustomPublicSubnetName = &databricks.WorkspaceCustomStringParameter{ + Value: &v, + } + } + + if v := config["private_subnet_name"].(string); v != "" { + parameters.CustomPrivateSubnetName = &databricks.WorkspaceCustomStringParameter{ + Value: &v, + } + } + + if v := config["virtual_network_id"].(string); v != "" { + parameters.CustomVirtualNetworkID = &databricks.WorkspaceCustomStringParameter{ + Value: &v, + } + } + + return ¶meters +} + func ValidateDatabricksWorkspaceName(i interface{}, k string) (warnings []string, errors []error) { v, ok := i.(string) if !ok { diff --git a/azurerm/internal/services/databricks/tests/resource_arm_databricks_workspace_test.go b/azurerm/internal/services/databricks/tests/resource_arm_databricks_workspace_test.go index 27e9938e3141..2f72e28e6bc0 100644 --- a/azurerm/internal/services/databricks/tests/resource_arm_databricks_workspace_test.go +++ b/azurerm/internal/services/databricks/tests/resource_arm_databricks_workspace_test.go @@ -131,6 +131,9 @@ func TestAccAzureRMDatabricksWorkspace_complete(t *testing.T) { testCheckAzureRMDatabricksWorkspaceExists(data.ResourceName), resource.TestCheckResourceAttrSet(data.ResourceName, "managed_resource_group_id"), resource.TestCheckResourceAttrSet(data.ResourceName, "managed_resource_group_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "custom_parameters.0.virtual_network_id"), + resource.TestCheckResourceAttr(data.ResourceName, "custom_parameters.0.public_subnet_name", "public"), + resource.TestCheckResourceAttr(data.ResourceName, "custom_parameters.0.private_subnet_name", "private"), resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "2"), resource.TestCheckResourceAttr(data.ResourceName, "tags.Environment", "Production"), resource.TestCheckResourceAttr(data.ResourceName, "tags.Pricing", "Standard"), @@ -142,6 +145,9 @@ func TestAccAzureRMDatabricksWorkspace_complete(t *testing.T) { testCheckAzureRMDatabricksWorkspaceExists(data.ResourceName), resource.TestCheckResourceAttrSet(data.ResourceName, "managed_resource_group_id"), resource.TestCheckResourceAttrSet(data.ResourceName, "managed_resource_group_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "custom_parameters.0.virtual_network_id"), + resource.TestCheckResourceAttr(data.ResourceName, "custom_parameters.0.public_subnet_name", "public"), + resource.TestCheckResourceAttr(data.ResourceName, "custom_parameters.0.private_subnet_name", "private"), resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"), resource.TestCheckResourceAttr(data.ResourceName, "tags.Pricing", "Standard"), ), @@ -242,6 +248,79 @@ resource "azurerm_resource_group" "test" { location = "%s" } +resource "azurerm_virtual_network" "test" { + name = "test" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + address_space = ["10.0.0.0/16"] +} + +resource "azurerm_subnet" "public" { + name = "public" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.1.0/24" + + delegation { + name = "acctest" + + service_delegation { + name = "Microsoft.Databricks/workspaces" + + actions = [ + "Microsoft.Network/virtualNetworks/subnets/join/action", + "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action", + "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action", + ] + } + } + + lifecycle { + ignore_changes = ["network_security_group_id"] + } +} + +resource "azurerm_subnet" "private" { + name = "private" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" + + delegation { + name = "acctest" + + service_delegation { + name = "Microsoft.Databricks/workspaces" + + actions = [ + "Microsoft.Network/virtualNetworks/subnets/join/action", + "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action", + "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action", + ] + } + } + + lifecycle { + ignore_changes = ["network_security_group_id"] + } +} + +resource "azurerm_network_security_group" "nsg" { + name = "private-nsg" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +resource "azurerm_subnet_network_security_group_association" "public" { + subnet_id = "${azurerm_subnet.public.id}" + network_security_group_id = "${azurerm_network_security_group.nsg.id}" +} + +resource "azurerm_subnet_network_security_group_association" "private" { + subnet_id = "${azurerm_subnet.private.id}" + network_security_group_id = "${azurerm_network_security_group.nsg.id}" +} + resource "azurerm_databricks_workspace" "test" { name = "acctestdbw-%d" resource_group_name = "${azurerm_resource_group.test.name}" @@ -249,6 +328,12 @@ resource "azurerm_databricks_workspace" "test" { sku = "standard" managed_resource_group_name = "acctestRG-%d-managed" + custom_parameters { + public_subnet_name = "${azurerm_subnet.public.name}" + private_subnet_name = "${azurerm_subnet.private.name}" + virtual_network_id = "${azurerm_virtual_network.test.id}" + } + tags = { Environment = "Production" Pricing = "Standard" diff --git a/website/docs/r/databricks_workspace.html.markdown b/website/docs/r/databricks_workspace.html.markdown index ddf7493bff74..31ac5684c5e2 100644 --- a/website/docs/r/databricks_workspace.html.markdown +++ b/website/docs/r/databricks_workspace.html.markdown @@ -46,8 +46,22 @@ The following arguments are supported: ~> **NOTE** Azure requires that this Resource Group does not exist in this Subscription (and that the Azure API creates it) - otherwise the deployment will fail. +* `custom_parameters` - (Optional) A `custom_parameters` block as documented below. + * `tags` - (Optional) A mapping of tags to assign to the resource. +--- + +`custom_parameters` supports the following: + +* `public_subnet_name` - (Optional) The name of the Public Subnet within the Virtual Network. Required if `virtual_network_id` is set. + +* `private_subnet_name` - (Optional) The name of the Private Subnet within the Virtual Network. Required if `virtual_network_id` is set. + +* `virtual_network_id` - (Optional) The ID of a Virtual Network where this Databricks Cluster should be created. + +~> **NOTE** Databricks requires that a network security group is associated with public and private subnets when `virtual_network_id` is set. + ## Attributes Reference The following attributes are exported: