Skip to content

Commit 55bd34f

Browse files
authored
Merge branch 'master' into master
2 parents 3e5d40d + 3cb1004 commit 55bd34f

File tree

4 files changed

+195
-9
lines changed

4 files changed

+195
-9
lines changed

databricks/resource_databricks_azure_blob_mount.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ func resourceAzureBlobMountRead(d *schema.ResourceData, m interface{}) error {
139139
url, err := blobMount.Read(client, clusterID)
140140
if err != nil {
141141
//Reset id in case of inability to find mount
142-
if strings.Contains(err.Error(), "Unable to find mount point!") {
142+
if strings.Contains(err.Error(), "Unable to find mount point!") ||
143+
strings.Contains(err.Error(), fmt.Sprintf("/mnt/%s does not exist.", mountName)) {
143144
d.SetId("")
144145
return nil
145146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package databricks
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/databrickslabs/databricks-terraform/client/model"
9+
"github.com/databrickslabs/databricks-terraform/client/service"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestAccAzureBlobMount_correctly_mounts(t *testing.T) {
16+
terraformToApply := testAccAzureBlobMount_correctly_mounts()
17+
var clusterInfo model.ClusterInfo
18+
var azureBlobMount AzureBlobMount
19+
20+
resource.Test(t, resource.TestCase{
21+
Providers: testAccProviders,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: terraformToApply,
25+
Check: resource.ComposeTestCheckFunc(
26+
testAccAzureBlobMount_cluster_exists("databricks_cluster.cluster", &clusterInfo),
27+
testAccAzureBlobMount_mount_exists("databricks_azure_blob_mount.mount", &azureBlobMount, &clusterInfo),
28+
),
29+
},
30+
{
31+
PreConfig: func() {
32+
client := testAccProvider.Meta().(service.DBApiClient)
33+
err := azureBlobMount.Delete(client, clusterInfo.ClusterID)
34+
assert.NoError(t, err, "TestAccAzureBlobMount_correctly_mounts: Failed to remove the mount.")
35+
},
36+
Config: terraformToApply,
37+
Check: resource.ComposeTestCheckFunc(
38+
testAccAzureBlobMount_mount_exists("databricks_azure_blob_mount.mount", &azureBlobMount, &clusterInfo),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
func testAccAzureBlobMount_cluster_exists(n string, clusterInfo *model.ClusterInfo) resource.TestCheckFunc {
46+
return func(s *terraform.State) error {
47+
// find the corresponding state object
48+
rs, ok := s.RootModule().Resources[n]
49+
if !ok {
50+
return fmt.Errorf("Not found: %s", n)
51+
}
52+
53+
// retrieve the configured client from the test setup
54+
client := testAccProvider.Meta().(service.DBApiClient)
55+
resp, err := client.Clusters().Get(rs.Primary.ID)
56+
if err != nil {
57+
return err
58+
}
59+
60+
// If no error, assign the response Widget attribute to the widget pointer
61+
*clusterInfo = resp
62+
return nil
63+
}
64+
}
65+
66+
func testAccAzureBlobMount_mount_exists(n string, azureBlobMount *AzureBlobMount, clusterInfo *model.ClusterInfo) resource.TestCheckFunc {
67+
return func(s *terraform.State) error {
68+
// find the corresponding state object
69+
rs, ok := s.RootModule().Resources[n]
70+
if !ok {
71+
return fmt.Errorf("Not found in tfstate: %s", n)
72+
}
73+
74+
authType := rs.Primary.Attributes["auth_type"]
75+
containerName := rs.Primary.Attributes["container_name"]
76+
storageAccountName := rs.Primary.Attributes["storage_account_name"]
77+
directory := rs.Primary.Attributes["directory"]
78+
mountName := rs.Primary.Attributes["mount_name"]
79+
tokenSecretScope := rs.Primary.Attributes["token_secret_scope"]
80+
tokenSecretKey := rs.Primary.Attributes["token_secret_key"]
81+
82+
blobMount := NewAzureBlobMount(containerName, storageAccountName, directory, mountName, authType,
83+
tokenSecretScope, tokenSecretKey)
84+
85+
client := testAccProvider.Meta().(service.DBApiClient)
86+
cluster_id := clusterInfo.ClusterID
87+
88+
message, err := blobMount.Read(client, cluster_id)
89+
if err != nil {
90+
return fmt.Errorf("Error reading the mount %s: error %s", message, err)
91+
}
92+
93+
*azureBlobMount = *blobMount
94+
return nil
95+
96+
}
97+
}
98+
99+
func testAccAzureBlobMount_correctly_mounts() string {
100+
clientID := os.Getenv("ARM_CLIENT_ID")
101+
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
102+
tenantID := os.Getenv("ARM_TENANT_ID")
103+
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
104+
workspaceName := os.Getenv("TEST_WORKSPACE_NAME")
105+
resourceGroupName := os.Getenv("TEST_RESOURCE_GROUP")
106+
managedResourceGroupName := os.Getenv("TEST_MANAGED_RESOURCE_GROUP")
107+
location := os.Getenv("TEST_LOCATION")
108+
blobAccountKey := os.Getenv("TEST_STORAGE_ACCOUNT_KEY")
109+
blobAccountName := os.Getenv("TEST_STORAGE_ACCOUNT_NAME")
110+
111+
definition := fmt.Sprintf(`
112+
provider "databricks" {
113+
azure_auth = {
114+
client_id = "%[1]s"
115+
client_secret = "%[2]s"
116+
tenant_id = "%[3]s"
117+
subscription_id = "%[4]s"
118+
119+
workspace_name = "%[5]s"
120+
resource_group = "%[6]s"
121+
managed_resource_group = "%[7]s"
122+
azure_region = "%[8]s"
123+
}
124+
}
125+
126+
resource "databricks_cluster" "cluster" {
127+
num_workers = 1
128+
spark_version = "6.4.x-scala2.11"
129+
node_type_id = "Standard_D3_v2"
130+
# Don't spend too much, turn off cluster after 15mins
131+
autotermination_minutes = 15
132+
}
133+
134+
resource "databricks_secret_scope" "terraform" {
135+
# Add the cluster ID into the secret scope to ensure
136+
# it doesn't clash with one used by another test
137+
name = "terraform${databricks_cluster.cluster.cluster_id}"
138+
initial_manage_principal = "users"
139+
}
140+
141+
resource "databricks_secret" "storage_key" {
142+
key = "blob_storage_key"
143+
string_value = "%[10]s"
144+
scope = databricks_secret_scope.terraform.name
145+
}
146+
147+
resource "databricks_azure_blob_mount" "mount" {
148+
cluster_id = databricks_cluster.cluster.id
149+
container_name = "dev" # Created by prereqs.tf
150+
storage_account_name = "%[9]s"
151+
mount_name = "dev"
152+
auth_type = "ACCESS_KEY"
153+
token_secret_scope = databricks_secret_scope.terraform.name
154+
token_secret_key = databricks_secret.storage_key.key
155+
}
156+
157+
`, clientID, clientSecret, tenantID, subscriptionID, workspaceName, resourceGroupName, managedResourceGroupName, location, blobAccountName, blobAccountKey)
158+
return definition
159+
}

integration-environment-azure/prereqs.tf

+32-7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ resource "azurerm_databricks_workspace" "example" {
2626
managed_resource_group_name = "workspace${random_string.naming.result}"
2727
}
2828

29-
# Create container in storage acc and container for use by mount tests
30-
resource "azurerm_storage_account" "account" {
29+
# Create container in storage acc and container for use by adls gen2 mount tests
30+
resource "azurerm_storage_account" "adlsaccount" {
3131
name = "${random_string.naming.result}datalake"
3232
resource_group_name = azurerm_resource_group.example.name
3333
location = azurerm_resource_group.example.location
@@ -37,19 +37,35 @@ resource "azurerm_storage_account" "account" {
3737
is_hns_enabled = "true"
3838
}
3939

40+
# Create container in storage acc and container for use by blob mount tests
41+
resource "azurerm_storage_account" "blobaccount" {
42+
name = "${random_string.naming.result}blob"
43+
resource_group_name = azurerm_resource_group.example.name
44+
location = azurerm_resource_group.example.location
45+
account_tier = "Standard"
46+
account_replication_type = "LRS"
47+
account_kind = "StorageV2"
48+
}
49+
4050
data "azurerm_client_config" "current" {
4151
}
4252

4353
resource "azurerm_role_assignment" "datalake" {
44-
scope = azurerm_storage_account.account.id
54+
scope = azurerm_storage_account.adlsaccount.id
4555
#https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#storage-blob-data-contributor
4656
role_definition_name = "Storage Blob Data Contributor"
4757
principal_id = data.azurerm_client_config.current.object_id
4858
}
4959

50-
resource "azurerm_storage_container" "example" {
60+
resource "azurerm_storage_container" "adlsexample" {
61+
name = "dev"
62+
storage_account_name = azurerm_storage_account.adlsaccount.name
63+
container_access_type = "private"
64+
}
65+
66+
resource "azurerm_storage_container" "blobexample" {
5167
name = "dev"
52-
storage_account_name = azurerm_storage_account.account.name
68+
storage_account_name = azurerm_storage_account.blobaccount.name
5369
container_access_type = "private"
5470
}
5571

@@ -62,13 +78,22 @@ output "workspace_name" {
6278
}
6379

6480
output "gen2_adal_name" {
65-
value = azurerm_storage_account.account.name
81+
value = azurerm_storage_account.adlsaccount.name
6682
}
6783

6884
output "location" {
69-
value = azurerm_storage_account.account.location
85+
value = azurerm_storage_account.adlsaccount.location
7086
}
7187

7288
output "rg_name" {
7389
value = azurerm_resource_group.example.name
7490
}
91+
92+
output "blob_storage_key" {
93+
value = azurerm_storage_account.blobaccount.primary_access_key
94+
sensitive = true
95+
}
96+
97+
output "blob_storage_name" {
98+
value = azurerm_storage_account.blobaccount.name
99+
}

integration-environment-azure/run.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export TEST_WORKSPACE_NAME=$(terraform output workspace_name)
3737
export TEST_GEN2_ADAL_NAME=$(terraform output gen2_adal_name)
3838
export TEST_MANAGED_RESOURCE_GROUP=$(terraform output workspace_managed_rg_name)
3939
export TEST_LOCATION=$(terraform output location)
40-
40+
export TEST_STORAGE_ACCOUNT_KEY=$(terraform output blob_storage_key)
41+
export TEST_STORAGE_ACCOUNT_NAME=$(terraform output blob_storage_name)
4142

4243
echo -e "----> Running Azure Acceptance Tests \n\n"
4344
# Output debug log to file while tests run

0 commit comments

Comments
 (0)