-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathprovider.go
339 lines (316 loc) · 12.9 KB
/
provider.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package databricks
import (
"encoding/base64"
"fmt"
"log"
"os"
"github.com/databrickslabs/databricks-terraform/client/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
homedir "github.com/mitchellh/go-homedir"
ini "gopkg.in/ini.v1"
)
// Provider returns the entire terraform provider object
func Provider(version string) terraform.ResourceProvider {
provider := &schema.Provider{
DataSourcesMap: map[string]*schema.Resource{
"databricks_default_user_roles": dataSourceDefaultUserRoles(),
"databricks_notebook": dataSourceNotebook(),
"databricks_notebook_paths": dataSourceNotebookPaths(),
"databricks_dbfs_file": dataSourceDBFSFile(),
"databricks_dbfs_file_paths": dataSourceDBFSFilePaths(),
"databricks_zones": dataSourceClusterZones(),
},
ResourcesMap: map[string]*schema.Resource{
"databricks_token": resourceToken(),
"databricks_secret_scope": resourceSecretScope(),
"databricks_secret": resourceSecret(),
"databricks_secret_acl": resourceSecretACL(),
"databricks_instance_pool": resourceInstancePool(),
"databricks_scim_user": resourceScimUser(),
"databricks_scim_group": resourceScimGroup(),
// Scim Group is split into multiple components for flexibility to pick and choose
"databricks_group": resourceGroup(),
"databricks_group_instance_profile": resourceGroupInstanceProfile(),
"databricks_group_member": resourceGroupMember(),
"databricks_notebook": resourceNotebook(),
"databricks_cluster": resourceCluster(),
"databricks_job": resourceJob(),
"databricks_dbfs_file": resourceDBFSFile(),
"databricks_dbfs_file_sync": resourceDBFSFileSync(),
"databricks_instance_profile": resourceInstanceProfile(),
"databricks_aws_s3_mount": resourceAWSS3Mount(),
"databricks_azure_blob_mount": resourceAzureBlobMount(),
"databricks_azure_adls_gen1_mount": resourceAzureAdlsGen1Mount(),
"databricks_azure_adls_gen2_mount": resourceAzureAdlsGen2Mount(),
// MWS (multiple workspaces) resources are only limited to AWS as azure already has a built in concept of MWS
"databricks_mws_credentials": resourceMWSCredentials(),
"databricks_mws_storage_configurations": resourceMWSStorageConfigurations(),
"databricks_mws_networks": resourceMWSNetworks(),
"databricks_mws_workspaces": resourceMWSWorkspaces(),
},
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_HOST", nil),
},
"token": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_TOKEN", nil),
ConflictsWith: []string{"basic_auth"},
},
"basic_auth": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_USERNAME", nil),
},
"password": {
Type: schema.TypeString,
Sensitive: true,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_PASSWORD", nil),
},
},
},
ConflictsWith: []string{"token"},
},
"config_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_CONFIG_FILE", "~/.databrickscfg"),
Description: "Location of the Databricks CLI credentials file, that is created\n" +
"by `databricks configure --token` command. By default, it is located\n" +
"in ~/.databrickscfg. Check https://docs.databricks.com/dev-tools/cli/index.html#set-up-authentication for docs. Config\n" +
"file credetials will only be used when host/token are not provided.",
},
"profile": {
Type: schema.TypeString,
Optional: true,
Default: "DEFAULT",
Description: "Connection profile specified within ~/.databrickscfg. Please check\n" +
"https://docs.databricks.com/dev-tools/cli/index.html#connection-profiles for documentation.",
},
"azure_auth": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"managed_resource_group": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_AZURE_MANAGED_RESOURCE_GROUP", nil),
},
"azure_region": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("AZURE_REGION", nil),
},
"workspace_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_AZURE_WORKSPACE_NAME", nil),
},
"resource_group": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("DATABRICKS_AZURE_RESOURCE_GROUP", nil),
},
"subscription_id": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"DATABRICKS_AZURE_SUBSCRIPTION_ID", "ARM_SUBSCRIPTION_ID"}, nil),
},
"client_secret": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"DATABRICKS_AZURE_CLIENT_SECRET", "ARM_CLIENT_SECRET"}, nil),
},
"client_id": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"DATABRICKS_AZURE_CLIENT_ID", "ARM_CLIENT_ID"}, nil),
},
"tenant_id": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"DATABRICKS_AZURE_TENANT_ID", "ARM_TENANT_ID"}, nil),
},
},
},
},
},
}
provider.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
//terraformVersion := provider.TerraformVersion
//if terraformVersion == "" {
// Terraform 0.12 introduced this field to the protocol
// We can therefore assume that if it's missing it's 0.10 or 0.11
//terraformVersion = "0.11+compatible"
//}
return providerConfigure(d, version)
}
return provider
}
func providerConfigureAzureClient(d *schema.ResourceData, config *service.DBApiClientConfig) (interface{}, error) {
log.Println("Creating db client via azure auth!")
azureAuth, _ := d.GetOk("azure_auth")
azureAuthMap := azureAuth.(map[string]interface{})
//azureAuth AzureAuth{}
tokenPayload := TokenPayload{}
// The if else is required for the reason that "azure_auth" schema object is not a block but a map
// Maps do not inherently auto populate defaults from environment variables unless we explicitly assign values
// This makes it very difficult to test
if managedResourceGroup, ok := azureAuthMap["managed_resource_group"].(string); ok {
tokenPayload.ManagedResourceGroup = managedResourceGroup
} else if os.Getenv("DATABRICKS_AZURE_MANAGED_RESOURCE_GROUP") != "" {
tokenPayload.ManagedResourceGroup = os.Getenv("DATABRICKS_AZURE_MANAGED_RESOURCE_GROUP")
}
if azureRegion, ok := azureAuthMap["azure_region"].(string); ok {
tokenPayload.AzureRegion = azureRegion
} else if os.Getenv("AZURE_REGION") != "" {
tokenPayload.AzureRegion = os.Getenv("AZURE_REGION")
}
if resourceGroup, ok := azureAuthMap["resource_group"].(string); ok {
tokenPayload.ResourceGroup = resourceGroup
} else if os.Getenv("DATABRICKS_AZURE_RESOURCE_GROUP") != "" {
tokenPayload.ResourceGroup = os.Getenv("DATABRICKS_AZURE_RESOURCE_GROUP")
}
if workspaceName, ok := azureAuthMap["workspace_name"].(string); ok {
tokenPayload.WorkspaceName = workspaceName
} else if os.Getenv("DATABRICKS_AZURE_WORKSPACE_NAME") != "" {
tokenPayload.WorkspaceName = os.Getenv("DATABRICKS_AZURE_WORKSPACE_NAME")
}
// This provider takes DATABRICKS_AZURE_* for client ID etc
// The azurerm provider uses ARM_* for the same values
// To make it easier to use the two providers together we use the following sources in order:
// - provider config
// - DATABRICKS_AZURE_* environment variables
// - ARM_* environment variables
subscriptionID, ok := azureAuthMap["subscription_id"].(string)
switch {
case ok:
tokenPayload.SubscriptionID = subscriptionID
case os.Getenv("DATABRICKS_AZURE_SUBSCRIPTION_ID") != "":
tokenPayload.SubscriptionID = os.Getenv("DATABRICKS_AZURE_SUBSCRIPTION_ID")
case os.Getenv("ARM_SUBSCRIPTION_ID") != "":
tokenPayload.SubscriptionID = os.Getenv("DATABRICKS_AZURE_SUBSCRIPTION_ID")
}
clientSecret, ok := azureAuthMap["client_secret"].(string)
switch {
case ok:
tokenPayload.ClientSecret = clientSecret
case os.Getenv("DATABRICKS_AZURE_CLIENT_SECRET") != "":
tokenPayload.ClientSecret = os.Getenv("DATABRICKS_AZURE_CLIENT_SECRET")
case os.Getenv("ARM_CLIENT_SECRET") != "":
tokenPayload.ClientSecret = os.Getenv("ARM_CLIENT_SECRET")
}
clientID, ok := azureAuthMap["client_id"].(string)
switch {
case ok:
tokenPayload.ClientID = clientID
case os.Getenv("DATABRICKS_AZURE_CLIENT_ID") != "":
tokenPayload.ClientID = os.Getenv("DATABRICKS_AZURE_CLIENT_ID")
case os.Getenv("ARM_CLIENT_ID") != "":
tokenPayload.ClientID = os.Getenv("ARM_CLIENT_ID")
}
tenantID, ok := azureAuthMap["tenant_id"].(string)
switch {
case ok:
tokenPayload.TenantID = tenantID
case os.Getenv("DATABRICKS_AZURE_TENANT_ID") != "":
tokenPayload.TenantID = os.Getenv("DATABRICKS_AZURE_TENANT_ID")
case os.Getenv("ARM_TENANT_ID") != "":
tokenPayload.TenantID = os.Getenv("ARM_TENANT_ID")
}
azureAuthSetup := AzureAuth{
TokenPayload: &tokenPayload,
ManagementToken: "",
AdbWorkspaceResourceID: "",
AdbAccessToken: "",
AdbPlatformToken: "",
}
// Setup the CustomAuthorizer Function to be called at API invoke rather than client invoke
config.CustomAuthorizer = azureAuthSetup.initWorkspaceAndGetClient
var dbClient service.DBApiClient
dbClient.SetConfig(config)
return &dbClient, nil
}
// tryDatabricksCliConfigFile sets Host and Token from ~/.databrickscfg file if it exists
func tryDatabricksCliConfigFile(d *schema.ResourceData, config *service.DBApiClientConfig) error {
configFile, err := homedir.Expand(d.Get("config_file").(string))
if err != nil {
return err
}
cfg, err := ini.Load(configFile)
if err != nil {
return fmt.Errorf("Authentication is not configured for provider. Please configure it\n"+
"through one of the following options:\n"+
"1. DATABRICKS_HOST + DATABRICKS_TOKEN environment variables.\n"+
"2. host + token provider arguments.\n"+
"3. Run `databricks configure --token` that will create %s file.\n\n"+
"Please check https://docs.databricks.com/dev-tools/cli/index.html#set-up-authentication for details", configFile)
}
if profile, ok := d.GetOk("profile"); ok {
dbcliConfig := cfg.Section(profile.(string))
token := dbcliConfig.Key("token").String()
if "" == token {
return fmt.Errorf("config file %s is corrupt: cannot find token in %s profile",
configFile, profile)
}
config.Token = token
host := dbcliConfig.Key("host").String()
if "" == host {
return fmt.Errorf("config file %s is corrupt: cannot find host in %s profile",
configFile, profile)
}
config.Host = host
}
return nil
}
func providerConfigure(d *schema.ResourceData, providerVersion string) (interface{}, error) {
var config service.DBApiClientConfig
// Call setup to configure retryable httpclient
config.Setup()
//version information from go-releaser using -ldflags to tell the golang linker to send semver info
config.UserAgent = fmt.Sprintf("databricks-tf-provider/%s", providerVersion)
if _, ok := d.GetOk("azure_auth"); !ok {
if host, ok := d.GetOk("host"); ok {
config.Host = host.(string)
}
if token, ok := d.GetOk("token"); ok {
config.Token = token.(string)
}
// Basic authentication setup via username and password
if _, ok := d.GetOk("basic_auth"); ok {
username, userOk := d.GetOk("basic_auth.0.username")
password, passOk := d.GetOk("basic_auth.0.password")
if userOk && passOk {
tokenUnB64 := fmt.Sprintf("%s:%s", username.(string), password.(string))
config.Token = base64.StdEncoding.EncodeToString([]byte(tokenUnB64))
config.AuthType = service.BasicAuth
}
}
// Final catch all in case basic_auth/token + host is not setup
if config.Host == "" || config.Token == "" {
if err := tryDatabricksCliConfigFile(d, &config); err != nil {
return nil, fmt.Errorf("failed to get credentials from config file; error msg: %w", err)
}
}
} else {
// Abstracted logic to another function that returns a interface{}, error to inject directly
// for the providers during cloud integration testing
return providerConfigureAzureClient(d, &config)
}
var dbClient service.DBApiClient
dbClient.SetConfig(&config)
return &dbClient, nil
}