Skip to content

Commit

Permalink
Fix databricks_connection regression when creating without owner (#…
Browse files Browse the repository at this point in the history
…3186)

* fix connection creation

* fix empty map for options
  • Loading branch information
nkvuong authored Feb 1, 2024
1 parent 1940e1a commit fe54a55
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
23 changes: 13 additions & 10 deletions catalog/resource_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,19 @@ func ResourceConnection() common.Resource {
}
var createConnectionRequest catalog.CreateConnection
common.DataToStructPointer(d, s, &createConnectionRequest)
_, err = w.Connections.Create(ctx, createConnectionRequest)
conn, err := w.Connections.Create(ctx, createConnectionRequest)
if err != nil {
return err
}
// Update owner if it is provided
if d.Get("owner") == "" {
return nil
}
var updateConnectionRequest catalog.UpdateConnection
common.DataToStructPointer(d, s, &updateConnectionRequest)
updateConnectionRequest.NameArg = updateConnectionRequest.Name
conn, err := w.Connections.Update(ctx, updateConnectionRequest)
if err != nil {
return err
if d.Get("owner") != "" {
var updateConnectionRequest catalog.UpdateConnection
common.DataToStructPointer(d, s, &updateConnectionRequest)
updateConnectionRequest.NameArg = updateConnectionRequest.Name
conn, err = w.Connections.Update(ctx, updateConnectionRequest)
if err != nil {
return err
}
}
d.Set("metastore_id", conn.MetastoreId)
pi.Pack(d)
Expand All @@ -91,6 +90,10 @@ func ResourceConnection() common.Resource {
// We need to preserve original sensitive options as API doesn't return them
var cOrig catalog.CreateConnection
common.DataToStructPointer(d, s, &cOrig)
// If there are no options returned, need to initialize the map
if conn.Options == nil {
conn.Options = map[string]string{}
}
for key, element := range cOrig.Options {
if slices.Contains(sensitiveOptions, key) {
conn.Options[key] = element
Expand Down
35 changes: 35 additions & 0 deletions internal/acceptance/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ func connectionTemplateWithOwner(host string, owner string) string {
}
`, host, owner)
}

func connectionTemplateWithoutOwner() string {
return `
resource "databricks_connection" "this" {
name = "name-{var.STICKY_RANDOM}"
connection_type = "BIGQUERY"
comment = "test"
options = {
GoogleServiceAccountKeyJson = <<-EOT
{
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL",
"universe_domain": "googleapis.com"
}
EOT
}
}
`
}
func TestUcAccConnectionsResourceFullLifecycle(t *testing.T) {
unityWorkspaceLevel(t, step{
Template: connectionTemplateWithOwner("test.mysql.database.azure.com", "account users"),
Expand All @@ -30,3 +57,11 @@ func TestUcAccConnectionsResourceFullLifecycle(t *testing.T) {
Template: connectionTemplateWithOwner("test.mysql.database.azure.com", "{env.TEST_METASTORE_ADMIN_GROUP_NAME}"),
})
}

func TestUcAccConnectionsWithoutOwnerResourceFullLifecycle(t *testing.T) {
unityWorkspaceLevel(t, step{
Template: connectionTemplateWithoutOwner(),
}, step{
Template: connectionTemplateWithoutOwner(),
})
}

0 comments on commit fe54a55

Please sign in to comment.