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

r/databricks_workspace: support for workspace parameters #3889

Merged
merged 2 commits into from
Jan 20, 2020
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
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