-
Notifications
You must be signed in to change notification settings - Fork 353
/
resource_databricks_azure_adls_gen1_mount.go
195 lines (183 loc) · 5.44 KB
/
resource_databricks_azure_adls_gen1_mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package databricks
import (
"fmt"
"github.com/databrickslabs/databricks-terraform/client/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"strings"
)
func resourceAzureAdlsGen1Mount() *schema.Resource {
return &schema.Resource{
Create: resourceAzureAdlsGen1Create,
Read: resourceAzureAdlsGen1Read,
Delete: resourceAzureAdlsGen1Delete,
Schema: map[string]*schema.Schema{
"cluster_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"storage_resource_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"spark_conf_prefix": {
Type: schema.TypeString,
Optional: true,
Default: "fs.adl",
ValidateFunc: validation.StringInSlice([]string{"fs.adl", "dfs.adls"}, false),
ForceNew: true,
},
"directory": {
Type: schema.TypeString,
Optional: true,
Computed: true,
//Default: "/",
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errors []error) {
directory := val.(string)
if strings.HasPrefix(directory, "/") {
return
}
errors = append(errors, fmt.Errorf("%s must start with /, got: %s", key, val))
return
},
},
"mount_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tenant_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_secret_scope": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_secret_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAzureAdlsGen1Create(d *schema.ResourceData, m interface{}) error {
client := m.(service.DBApiClient)
clusterID := d.Get("cluster_id").(string)
err := changeClusterIntoRunningState(clusterID, client)
if err != nil {
return err
}
storageResourceName := d.Get("storage_resource_name").(string)
sparkConfPrefix := d.Get("spark_conf_prefix").(string)
directory := d.Get("directory").(string)
mountName := d.Get("mount_name").(string)
tenantID := d.Get("tenant_id").(string)
clientID := d.Get("client_id").(string)
clientSecretScope := d.Get("client_secret_scope").(string)
clientSecretKey := d.Get("client_secret_key").(string)
adlsGen1Mount := NewAzureADLSGen1Mount(storageResourceName, directory, mountName,
sparkConfPrefix, clientID, tenantID, clientSecretScope, clientSecretKey)
err = adlsGen1Mount.Create(client, clusterID)
if err != nil {
return err
}
d.SetId(mountName)
err = d.Set("cluster_id", clusterID)
if err != nil {
return err
}
err = d.Set("mount_name", mountName)
if err != nil {
return err
}
err = d.Set("tenant_id", tenantID)
if err != nil {
return err
}
err = d.Set("client_id", clientID)
if err != nil {
return err
}
err = d.Set("spark_conf_prefix", sparkConfPrefix)
if err != nil {
return err
}
err = d.Set("client_secret_scope", clientSecretScope)
if err != nil {
return err
}
err = d.Set("client_secret_key", clientSecretKey)
if err != nil {
return err
}
return resourceAzureAdlsGen1Read(d, m)
}
func resourceAzureAdlsGen1Read(d *schema.ResourceData, m interface{}) error {
client := m.(service.DBApiClient)
clusterID := d.Get("cluster_id").(string)
err := changeClusterIntoRunningState(clusterID, client)
if err != nil {
return err
}
storageResourceName := d.Get("storage_resource_name").(string)
sparkConfPrefix := d.Get("spark_conf_prefix").(string)
directory := d.Get("directory").(string)
mountName := d.Get("mount_name").(string)
tenantID := d.Get("tenant_id").(string)
clientID := d.Get("client_id").(string)
clientSecretScope := d.Get("client_secret_scope").(string)
clientSecretKey := d.Get("client_secret_key").(string)
adlsGen1Mount := NewAzureADLSGen1Mount(storageResourceName, directory, mountName,
sparkConfPrefix, clientID, tenantID, clientSecretScope, clientSecretKey)
url, err := adlsGen1Mount.Read(client, clusterID)
if err != nil {
//Reset id in case of inability to find mount
if strings.Contains(err.Error(), "Unable to find mount point!") ||
strings.Contains(err.Error(), fmt.Sprintf("/mnt/%s does not exist.", mountName)) {
d.SetId("")
return nil
}
return err
}
storageResourceName, dir, err := ProcessAzureAdlsGen1Uri(url)
if err != nil {
return err
}
err = d.Set("storage_resource_name", storageResourceName)
if err != nil {
return err
}
err = d.Set("directory", dir)
return err
}
func resourceAzureAdlsGen1Delete(d *schema.ResourceData, m interface{}) error {
client := m.(service.DBApiClient)
clusterID := d.Get("cluster_id").(string)
err := changeClusterIntoRunningState(clusterID, client)
if err != nil {
return err
}
storageResourceName := d.Get("storage_resource_name").(string)
sparkConfPrefix := d.Get("spark_conf_prefix").(string)
directory := d.Get("directory").(string)
mountName := d.Get("mount_name").(string)
tenantID := d.Get("tenant_id").(string)
clientID := d.Get("client_id").(string)
clientSecretScope := d.Get("client_secret_scope").(string)
clientSecretKey := d.Get("client_secret_key").(string)
adlsGen1Mount := NewAzureADLSGen1Mount(storageResourceName, directory, mountName,
sparkConfPrefix, clientID, tenantID, clientSecretScope, clientSecretKey)
return adlsGen1Mount.Delete(client, clusterID)
}