Skip to content

Commit babef36

Browse files
author
Dave Storey
committed
mounts now handle deleted cluster correctly
1 parent 6f6fc0b commit babef36

7 files changed

+149
-23
lines changed

databricks/resource_databricks_azure_adls_gen2_mount.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package databricks
22

33
import (
44
"fmt"
5+
"log"
6+
"strings"
7+
58
"github.com/databrickslabs/databricks-terraform/client/service"
69
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7-
"strings"
810
)
911

1012
func resourceAzureAdlsGen2Mount() *schema.Resource {
@@ -129,10 +131,6 @@ func resourceAzureAdlsGen2Create(d *schema.ResourceData, m interface{}) error {
129131
func resourceAzureAdlsGen2Read(d *schema.ResourceData, m interface{}) error {
130132
client := m.(*service.DBApiClient)
131133
clusterID := d.Get("cluster_id").(string)
132-
err := changeClusterIntoRunningState(clusterID, client)
133-
if err != nil {
134-
return err
135-
}
136134
containerName := d.Get("container_name").(string)
137135
storageAccountName := d.Get("storage_account_name").(string)
138136
directory := d.Get("directory").(string)
@@ -143,6 +141,16 @@ func resourceAzureAdlsGen2Read(d *schema.ResourceData, m interface{}) error {
143141
clientSecretKey := d.Get("client_secret_key").(string)
144142
initializeFileSystem := d.Get("initialize_file_system").(bool)
145143

144+
err := changeClusterIntoRunningState(clusterID, client)
145+
if err != nil {
146+
if isClusterMissing(err.Error(), clusterID) {
147+
log.Printf("Unable to refresh mount '%s' as cluster '%s' is missing", mountName, clusterID)
148+
d.SetId("")
149+
return nil
150+
}
151+
return err
152+
}
153+
146154
adlsGen2Mount := NewAzureADLSGen2Mount(containerName, storageAccountName, directory, mountName, clientID, tenantID,
147155
clientSecretScope, clientSecretKey, initializeFileSystem)
148156

databricks/resource_databricks_azure_adls_gen2_mount_test.go

+50-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import (
66
"regexp"
77
"testing"
88

9+
"github.com/databrickslabs/databricks-terraform/client/model"
10+
"github.com/databrickslabs/databricks-terraform/client/service"
911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
13+
"github.com/stretchr/testify/assert"
1014
)
1115

1216
func TestAccAzureAdlsGen2Mount_correctly_mounts(t *testing.T) {
1317
terraformToApply := testAccAzureAdlsGen2Mount_correctly_mounts()
1418

1519
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
1621
Providers: testAccProviders,
1722
Steps: []resource.TestStep{
1823
{
@@ -22,6 +27,29 @@ func TestAccAzureAdlsGen2Mount_correctly_mounts(t *testing.T) {
2227
})
2328
}
2429

30+
func TestAccAzureAdlsGen2Mount_cluster_deleted_correctly_mounts(t *testing.T) {
31+
terraformToApply := testAccAzureAdlsGen2Mount_correctly_mounts()
32+
var cluster model.ClusterInfo
33+
34+
resource.Test(t, resource.TestCase{
35+
Providers: testAccProviders,
36+
Steps: []resource.TestStep{
37+
{
38+
Config: terraformToApply,
39+
Check: testClusterResourceExists("databricks_cluster.cluster", &cluster, t),
40+
},
41+
{
42+
PreConfig: func() {
43+
client := testAccProvider.Meta().(*service.DBApiClient)
44+
err := client.Clusters().Delete(cluster.ClusterID)
45+
assert.NoError(t, err, err)
46+
},
47+
Config: terraformToApply,
48+
},
49+
},
50+
})
51+
}
52+
2553
func TestAccAzureAdlsGen2Mount_capture_error(t *testing.T) {
2654
terraformToApply := testAccAzureAdlsGen2Mount_capture_error()
2755

@@ -39,7 +67,7 @@ func TestAccAzureAdlsGen2Mount_capture_error(t *testing.T) {
3967
}
4068

4169
func testAccAzureAdlsGen2Mount_correctly_mounts() string {
42-
clientID := os.Getenv("ARM_CLIENT_ID")
70+
clientID := os.Getenv("DATABRICKS_AZURE_CLIENT_ID")
4371
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
4472
tenantID := os.Getenv("ARM_TENANT_ID")
4573
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
@@ -103,7 +131,7 @@ func testAccAzureAdlsGen2Mount_correctly_mounts() string {
103131
}
104132

105133
func testAccAzureAdlsGen2Mount_capture_error() string {
106-
clientID := os.Getenv("ARM_CLIENT_ID")
134+
clientID := os.Getenv("DATABRICKS_AZURE_CLIENT_ID")
107135
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
108136
tenantID := os.Getenv("ARM_TENANT_ID")
109137
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
@@ -165,3 +193,23 @@ func testAccAzureAdlsGen2Mount_capture_error() string {
165193
`, clientID, clientSecret, tenantID, subscriptionID, workspaceName, resourceGroupName, managedResourceGroupName, location, gen2AdalName)
166194
return definition
167195
}
196+
197+
// testClusterResourceExists queries the API and retrieves the matching Cluster.
198+
func testClusterResourceExists(n string, cluster *model.ClusterInfo, t *testing.T) resource.TestCheckFunc {
199+
return func(s *terraform.State) error {
200+
// find the corresponding state object
201+
rs, ok := s.RootModule().Resources[n]
202+
if !ok {
203+
return fmt.Errorf("Not found: %s", n)
204+
}
205+
206+
conn := testAccProvider.Meta().(*service.DBApiClient)
207+
resp, err := conn.Clusters().Get(rs.Primary.ID)
208+
if err != nil {
209+
return err
210+
}
211+
212+
*cluster = resp
213+
return nil
214+
}
215+
}

databricks/resource_databricks_azure_blob_mount.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package databricks
22

33
import (
44
"fmt"
5+
"log"
6+
"strings"
7+
58
"github.com/databrickslabs/databricks-terraform/client/service"
69
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
710
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
8-
"strings"
911
)
1012

1113
func resourceAzureBlobMount() *schema.Resource {
@@ -121,10 +123,6 @@ func resourceAzureBlobMountCreate(d *schema.ResourceData, m interface{}) error {
121123
func resourceAzureBlobMountRead(d *schema.ResourceData, m interface{}) error {
122124
client := m.(*service.DBApiClient)
123125
clusterID := d.Get("cluster_id").(string)
124-
err := changeClusterIntoRunningState(clusterID, client)
125-
if err != nil {
126-
return err
127-
}
128126
containerName := d.Get("container_name").(string)
129127
storageAccountName := d.Get("storage_account_name").(string)
130128
directory := d.Get("directory").(string)
@@ -133,6 +131,16 @@ func resourceAzureBlobMountRead(d *schema.ResourceData, m interface{}) error {
133131
tokenSecretScope := d.Get("token_secret_scope").(string)
134132
tokenSecretKey := d.Get("token_secret_key").(string)
135133

134+
err := changeClusterIntoRunningState(clusterID, client)
135+
if err != nil {
136+
if isClusterMissing(err.Error(), clusterID) {
137+
log.Printf("Unable to refresh mount '%s' as cluster '%s' is missing", mountName, clusterID)
138+
d.SetId("")
139+
return nil
140+
}
141+
return err
142+
}
143+
136144
blobMount := NewAzureBlobMount(containerName, storageAccountName, directory, mountName, authType,
137145
tokenSecretScope, tokenSecretKey)
138146

databricks/resource_databricks_azure_blob_mount_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ func TestAccAzureBlobMount_correctly_mounts(t *testing.T) {
4242
})
4343
}
4444

45+
func TestAccAzureBlobMount_cluster_deleted_correctly_mounts(t *testing.T) {
46+
terraformToApply := testAccAzureBlobMount_correctly_mounts()
47+
var clusterInfo model.ClusterInfo
48+
var azureBlobMount AzureBlobMount
49+
50+
resource.Test(t, resource.TestCase{
51+
Providers: testAccProviders,
52+
Steps: []resource.TestStep{
53+
{
54+
Config: terraformToApply,
55+
Check: resource.ComposeTestCheckFunc(
56+
testAccAzureBlobMount_cluster_exists("databricks_cluster.cluster", &clusterInfo),
57+
testAccAzureBlobMount_mount_exists("databricks_azure_blob_mount.mount", &azureBlobMount, &clusterInfo),
58+
),
59+
},
60+
{
61+
PreConfig: func() {
62+
client := testAccProvider.Meta().(*service.DBApiClient)
63+
err := client.Clusters().Delete(clusterInfo.ClusterID)
64+
assert.NoError(t, err, err)
65+
},
66+
Config: terraformToApply,
67+
Check: resource.ComposeTestCheckFunc(
68+
testAccAzureBlobMount_mount_exists("databricks_azure_blob_mount.mount", &azureBlobMount, &clusterInfo),
69+
),
70+
},
71+
},
72+
})
73+
}
74+
4575
func testAccAzureBlobMount_cluster_exists(n string, clusterInfo *model.ClusterInfo) resource.TestCheckFunc {
4676
return func(s *terraform.State) error {
4777
// find the corresponding state object

databricks/resource_databricks_cluster.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package databricks
22

33
import (
44
"errors"
5-
"fmt"
6-
"github.com/databrickslabs/databricks-terraform/client/model"
7-
"github.com/databrickslabs/databricks-terraform/client/service"
8-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
95
"log"
106
"strings"
117
"time"
8+
9+
"github.com/databrickslabs/databricks-terraform/client/model"
10+
"github.com/databrickslabs/databricks-terraform/client/service"
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1212
)
1313

1414
func resourceCluster() *schema.Resource {
@@ -1453,8 +1453,3 @@ func getMapFromOneItemSet(input interface{}) map[string]interface{} {
14531453
}
14541454
return nil
14551455
}
1456-
1457-
func isClusterMissing(errorMsg, resourceID string) bool {
1458-
return strings.Contains(errorMsg, "INVALID_PARAMETER_VALUE") &&
1459-
strings.Contains(errorMsg, fmt.Sprintf("Cluster %s does not exist", resourceID))
1460-
}

databricks/utils.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package databricks
22

33
import (
44
"fmt"
5-
"github.com/databrickslabs/databricks-terraform/client/model"
6-
"github.com/databrickslabs/databricks-terraform/client/service"
75
"strings"
86
"time"
7+
8+
"github.com/databrickslabs/databricks-terraform/client/model"
9+
"github.com/databrickslabs/databricks-terraform/client/service"
910
)
1011

1112
func changeClusterIntoRunningState(clusterID string, client *service.DBApiClient) error {
@@ -68,3 +69,8 @@ func changeClusterIntoRunningState(clusterID string, client *service.DBApiClient
6869
return fmt.Errorf("cluster is in a non recoverable state: %s", currentState)
6970

7071
}
72+
73+
func isClusterMissing(errorMsg, resourceID string) bool {
74+
return strings.Contains(errorMsg, "INVALID_PARAMETER_VALUE") &&
75+
strings.Contains(errorMsg, fmt.Sprintf("Cluster %s does not exist", resourceID))
76+
}

databricks/utils_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package databricks
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsClusterMissingTrueWhenClusterIdSpecifiedPresent(t *testing.T) {
10+
errorMessage := "{\"error_code\":\"INVALID_PARAMETER_VALUE\",\"message\":\"Cluster 123 does not exist\"}"
11+
12+
result := isClusterMissing(errorMessage, "123")
13+
14+
assert.True(t, result)
15+
}
16+
17+
func TestIsClusterMissingFalseWhenClusterIdSpecifiedNotPresent(t *testing.T) {
18+
errorMessage := "{\"error_code\":\"INVALID_PARAMETER_VALUE\",\"message\":\"Cluster 123 does not exist\"}"
19+
20+
result := isClusterMissing(errorMessage, "xyz")
21+
22+
assert.False(t, result)
23+
}
24+
25+
func TestIsClusterMissingFalseWhenErrorNotInCorrectFormat(t *testing.T) {
26+
errorMessage := "{\"error_code\":\"INVALID_PARAMETER_VALUE\",\"message\":\"Something random went bang xyz\"}"
27+
28+
result := isClusterMissing(errorMessage, "xyz")
29+
30+
assert.False(t, result)
31+
}

0 commit comments

Comments
 (0)