Skip to content

Commit fba816f

Browse files
authored
Merge pull request #78 from databrickslabs/fix-azure-workspace-creation-bug
Fix azure workspace creation bug
2 parents c5782d4 + 3a2fac1 commit fba816f

File tree

53 files changed

+233
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+233
-189
lines changed

client/service/apis.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ func (c *DBApiClient) SetConfig(clientConfig *DBApiClientConfig) DBApiClient {
1717
}
1818

1919
// Clusters returns an instance of ClustersAPI
20-
func (c DBApiClient) Clusters() ClustersAPI {
20+
func (c *DBApiClient) Clusters() ClustersAPI {
2121
return ClustersAPI{Client: c}
2222
}
2323

2424
// Secrets returns an instance of SecretsAPI
25-
func (c DBApiClient) Secrets() SecretsAPI {
25+
func (c *DBApiClient) Secrets() SecretsAPI {
2626
return SecretsAPI{Client: c}
2727
}
2828

2929
// SecretScopes returns an instance of SecretScopesAPI
30-
func (c DBApiClient) SecretScopes() SecretScopesAPI {
30+
func (c *DBApiClient) SecretScopes() SecretScopesAPI {
3131
return SecretScopesAPI{Client: c}
3232
}
3333

3434
// SecretAcls returns an instance of SecretAclsAPI
35-
func (c DBApiClient) SecretAcls() SecretAclsAPI {
35+
func (c *DBApiClient) SecretAcls() SecretAclsAPI {
3636
return SecretAclsAPI{Client: c}
3737
}
3838

@@ -42,50 +42,54 @@ func (c *DBApiClient) Tokens() TokensAPI {
4242
}
4343

4444
// Users returns an instance of UsersAPI
45-
func (c DBApiClient) Users() UsersAPI {
45+
func (c *DBApiClient) Users() UsersAPI {
4646
return UsersAPI{Client: c}
4747
}
4848

4949
// Groups returns an instance of GroupsAPI
50-
func (c DBApiClient) Groups() GroupsAPI {
50+
func (c *DBApiClient) Groups() GroupsAPI {
5151
return GroupsAPI{Client: c}
5252
}
5353

5454
// Notebooks returns an instance of NotebooksAPI
55-
func (c DBApiClient) Notebooks() NotebooksAPI {
55+
func (c *DBApiClient) Notebooks() NotebooksAPI {
5656
return NotebooksAPI{Client: c}
5757
}
5858

5959
// Jobs returns an instance of JobsAPI
60-
func (c DBApiClient) Jobs() JobsAPI {
60+
func (c *DBApiClient) Jobs() JobsAPI {
6161
return JobsAPI{Client: c}
6262
}
6363

6464
// DBFS returns an instance of DBFSAPI
65-
func (c DBApiClient) DBFS() DBFSAPI {
65+
func (c *DBApiClient) DBFS() DBFSAPI {
6666
return DBFSAPI{Client: c}
6767
}
6868

6969
// Libraries returns an instance of LibrariesAPI
70-
func (c DBApiClient) Libraries() LibrariesAPI {
70+
func (c *DBApiClient) Libraries() LibrariesAPI {
7171
return LibrariesAPI{Client: c}
7272
}
7373

7474
// InstancePools returns an instance of InstancePoolsAPI
75-
func (c DBApiClient) InstancePools() InstancePoolsAPI {
75+
func (c *DBApiClient) InstancePools() InstancePoolsAPI {
7676
return InstancePoolsAPI{Client: c}
7777
}
7878

7979
// InstanceProfiles returns an instance of InstanceProfilesAPI
80-
func (c DBApiClient) InstanceProfiles() InstanceProfilesAPI {
80+
func (c *DBApiClient) InstanceProfiles() InstanceProfilesAPI {
8181
return InstanceProfilesAPI{Client: c}
8282
}
8383

8484
// Commands returns an instance of CommandsAPI
85-
func (c DBApiClient) Commands() CommandsAPI {
85+
func (c *DBApiClient) Commands() CommandsAPI {
8686
return CommandsAPI{Client: c}
8787
}
8888

89-
func (c DBApiClient) performQuery(method, path string, apiVersion string, headers map[string]string, data interface{}, secretsMask *SecretsMask) ([]byte, error) {
89+
func (c *DBApiClient) performQuery(method, path string, apiVersion string, headers map[string]string, data interface{}, secretsMask *SecretsMask) ([]byte, error) {
90+
err := c.Config.getOrCreateToken()
91+
if err != nil {
92+
return []byte(""), err
93+
}
9094
return PerformQuery(c.Config, method, path, apiVersion, headers, true, false, data, secretsMask)
9195
}

client/service/client.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"reflect"
1414
"strings"
15+
"sync"
1516
"time"
1617

1718
"github.com/google/go-querystring/query"
@@ -56,6 +57,8 @@ const (
5657
BasicAuth AuthType = "BASIC"
5758
)
5859

60+
var clientAuthorizerMutex sync.Mutex
61+
5962
// DBApiClientConfig is used to configure the DataBricks Client
6063
type DBApiClientConfig struct {
6164
Host string
@@ -65,12 +68,14 @@ type DBApiClientConfig struct {
6568
DefaultHeaders map[string]string
6669
InsecureSkipVerify bool
6770
TimeoutSeconds int
71+
CustomAuthorizer func(*DBApiClientConfig) error
6872
client *retryablehttp.Client
6973
}
7074

7175
var transientErrorStringMatches []string = []string{ // TODO: Should we make these regexes to match more of the message or is this sufficient?
7276
"com.databricks.backend.manager.util.UnknownWorkerEnvironmentException",
7377
"does not have any associated worker environments",
78+
"There is no worker environment with id",
7479
}
7580

7681
// Setup initializes the client
@@ -123,7 +128,6 @@ func checkHTTPRetry(ctx context.Context, resp *http.Response, err error) (bool,
123128
if err != nil {
124129
return false, err
125130
}
126-
127131
var errorBody DBApiErrorBody
128132
err = json.Unmarshal(body, &errorBody)
129133
if err != nil {
@@ -145,7 +149,22 @@ func checkHTTPRetry(ctx context.Context, resp *http.Response, err error) (bool,
145149
return false, nil
146150
}
147151

148-
func (c *DBApiClientConfig) getAuthHeader() map[string]string {
152+
func (c *DBApiClientConfig) getOrCreateToken() error {
153+
if c.CustomAuthorizer != nil {
154+
// Lock incase terraform tries to getOrCreateToken from multiple go routines on the same client ptr.
155+
clientAuthorizerMutex.Lock()
156+
defer clientAuthorizerMutex.Unlock()
157+
if reflect.ValueOf(c.Token).IsZero() {
158+
log.Println("NOT AUTHORIZED SO ATTEMPTING TO AUTHORIZE")
159+
return c.CustomAuthorizer(c)
160+
}
161+
log.Println("ALREADY AUTHORIZED")
162+
return nil
163+
}
164+
return nil
165+
}
166+
167+
func (c DBApiClientConfig) getAuthHeader() map[string]string {
149168
auth := make(map[string]string)
150169
if c.AuthType == BasicAuth {
151170
auth["Authorization"] = "Basic " + c.Token

client/service/clusters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// ClustersAPI is a struct that contains the Databricks api client to perform queries
1313
type ClustersAPI struct {
14-
Client DBApiClient
14+
Client *DBApiClient
1515
}
1616

1717
// Create creates a new Spark cluster

client/service/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// CommandsAPI exposes the Context & Commands API
1414
type CommandsAPI struct {
15-
Client DBApiClient
15+
Client *DBApiClient
1616
}
1717

1818
// Execute creates a spark context and executes a command and then closes context

client/service/dbfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// DBFSAPI exposes the DBFS API
1212
type DBFSAPI struct {
13-
Client DBApiClient
13+
Client *DBApiClient
1414
}
1515

1616
// Create creates a file in DBFS given data string in base64

client/service/groups.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// GroupsAPI exposes the scim groups API
1414
type GroupsAPI struct {
15-
Client DBApiClient
15+
Client *DBApiClient
1616
}
1717

1818
// Create creates a scim group in the Databricks workspace

client/service/instance_pools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// InstancePoolsAPI exposes the instance pools api
1010
type InstancePoolsAPI struct {
11-
Client DBApiClient
11+
Client *DBApiClient
1212
}
1313

1414
// Create creates the instance pool to given the instance pool configuration

client/service/instance_profiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// InstanceProfilesAPI exposes the instance profiles api on the AWS deployment of Databricks
1111
type InstanceProfilesAPI struct {
12-
Client DBApiClient
12+
Client *DBApiClient
1313
}
1414

1515
// Create creates an instance profile record on Databricks

client/service/jobs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// JobsAPI exposes the Jobs API
1010
type JobsAPI struct {
11-
Client DBApiClient
11+
Client *DBApiClient
1212
}
1313

1414
// Create creates a job on the workspace given the job settings

client/service/libraries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// LibrariesAPI exposes the Library API
1010
type LibrariesAPI struct {
11-
Client DBApiClient
11+
Client *DBApiClient
1212
}
1313

1414
// Create installs the list of libraries given a cluster id

0 commit comments

Comments
 (0)