diff --git a/catalog/resource_connection.go b/catalog/resource_connection.go index 498cf177ff..978e213348 100644 --- a/catalog/resource_connection.go +++ b/catalog/resource_connection.go @@ -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) @@ -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 diff --git a/internal/acceptance/connection_test.go b/internal/acceptance/connection_test.go index 714ea33a53..7cf6ec5093 100644 --- a/internal/acceptance/connection_test.go +++ b/internal/acceptance/connection_test.go @@ -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"), @@ -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(), + }) +}