Skip to content

Commit

Permalink
r/databricks_workspace: support for workspace parameters (#3889)
Browse files Browse the repository at this point in the history
Allows for optional map of workspace parameter key/values (e.g. customPrivateSubnetName, customPublicSubnetName). docs.microsoft.com/en-us/rest/api/databricks/workspaces/createorupdate#workspace

The list of accepted parameter keys included in the doc updates was provided in a response through the sdk with a disallowed parameter. Can't find the list in any other documentation though.

Includes test updates.

Fixes #3001
  • Loading branch information
notchairmk authored and katbyte committed Jan 20, 2020
1 parent ac5cc8c commit bc9c706
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 &parameters
}

func ValidateDatabricksWorkspaceName(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
),
Expand Down Expand Up @@ -242,13 +248,92 @@ 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}"
location = "${azurerm_resource_group.test.location}"
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"
Expand Down
14 changes: 14 additions & 0 deletions website/docs/r/databricks_workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

1 comment on commit bc9c706

@trinpm20
Copy link

@trinpm20 trinpm20 commented on bc9c706 Jan 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @notchairmk: Thank you for this commit!

Will this commit be shipped with the next release? I have checked out Terraform v0.12.19, it looks like this enhancement is not there at this moment:

on main.tf line 64, in resource "azurerm_databricks_workspace" "mto-azurerm-databrickws":
  64:   custom_parameters {
Blocks of type "custom_parameters" are not expected here.

Update: I saw "Terraform v0.12.19" was release in January. 8. I am really excited for this commit to be in next release. Thank you again.

Please sign in to comment.