Skip to content

Commit 796af80

Browse files
nkvuongalexott
andauthored
[Fix] handle auto-enabled errors withdatabricks_system_schema (#4547)
## Changes - Handle new error message `billing system schema can only be enabled by Databricks`. Resolves #4539 - Update documentation with warnings about auto-enabled system schemas. - Enable system schema integration test on GCP ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [x] relevant change in `docs/` folder - [x] covered with integration tests in `internal/acceptance` - [x] using Go SDK - [x] using TF Plugin Framework --------- Co-authored-by: Alex Ott <[email protected]>
1 parent 651c3ed commit 796af80

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

NEXT_CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
### Bug Fixes
88

9-
- Skip Read after Create in `databricks_secret_acl` to avoid errors([#4548](https://github.com/databricks/terraform-provider-databricks/pull/4548)).
9+
* Handle auto-enabled errors with `databricks_system_schema` [#4547](https://github.com/databricks/terraform-provider-databricks/pull/4547)
10+
* Skip Read after Create in `databricks_secret_acl` to avoid errors([#4548](https://github.com/databricks/terraform-provider-databricks/pull/4548)).
1011

1112
### Documentation
1213

catalog/resource_system_schema.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func ResourceSystemSchema() common.Resource {
2121
Type: schema.TypeString,
2222
Computed: true,
2323
}
24+
m["auto_enabled"] = &schema.Schema{
25+
Type: schema.TypeBool,
26+
Computed: true,
27+
}
2428
m["state"].Computed = true
2529
return m
2630
})
@@ -49,12 +53,20 @@ func ResourceSystemSchema() common.Resource {
4953
MetastoreId: metastoreSummary.MetastoreId,
5054
SchemaName: new,
5155
})
52-
//ignore "schema <schema-name> already exists" error
53-
if err != nil && !strings.Contains(err.Error(), "already exists") {
54-
return err
56+
if err != nil {
57+
//ignore "<schema-name> system schema can only be enabled by Databricks" error, also mark it to make delete no-op
58+
d.Set("auto_enabled", strings.Contains(err.Error(), "can only be enabled by Databricks"))
59+
//ignore "schema <schema-name> already exists" error
60+
if !strings.Contains(err.Error(), "already exists") && !strings.Contains(err.Error(), "can only be enabled by Databricks") {
61+
return err
62+
}
5563
}
5664
//disable old schemas if needed
5765
if old != "" {
66+
if d.Get("auto_enabled").(bool) {
67+
log.Printf("[WARN] %s is auto enabled, ignoring it", old)
68+
return nil
69+
}
5870
err = w.SystemSchemas.Disable(ctx, catalog.DisableRequest{
5971
MetastoreId: metastoreSummary.MetastoreId,
6072
SchemaName: old,
@@ -116,6 +128,10 @@ func ResourceSystemSchema() common.Resource {
116128
if err != nil {
117129
return err
118130
}
131+
if d.Get("auto_enabled").(bool) {
132+
log.Printf("[WARN] %s is auto enabled, ignoring it", schemaName)
133+
return nil
134+
}
119135
w, err := c.WorkspaceClient()
120136
if err != nil {
121137
return err

catalog/resource_system_schema_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package catalog
22

33
import (
4+
"errors"
45
"net/http"
56
"testing"
67

8+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
79
"github.com/databricks/databricks-sdk-go/service/catalog"
810
"github.com/databricks/terraform-provider-databricks/common"
911
"github.com/databricks/terraform-provider-databricks/qa"
1012
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/mock"
1114
)
1215

1316
func TestSystemSchemaCreate(t *testing.T) {
@@ -87,6 +90,40 @@ func TestSystemSchemaCreate_Error(t *testing.T) {
8790
assert.Equal(t, "", d.Id(), "Id should be empty for error creates")
8891
}
8992

93+
func TestSystemSchemaCreateAlreadyEnabled(t *testing.T) {
94+
qa.ResourceFixture{
95+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
96+
w.GetMockMetastoresAPI().EXPECT().Summary(mock.Anything).Return(&catalog.GetMetastoreSummaryResponse{
97+
MetastoreId: "abc",
98+
}, nil)
99+
e := w.GetMockSystemSchemasAPI().EXPECT()
100+
e.Enable(mock.Anything, catalog.EnableRequest{
101+
MetastoreId: "abc",
102+
SchemaName: "billing",
103+
}).Return(errors.New("billing system schema can only be enabled by Databricks"))
104+
e.ListByMetastoreId(mock.Anything, "abc").Return(&catalog.ListSystemSchemasResponse{
105+
Schemas: []catalog.SystemSchemaInfo{
106+
{
107+
Schema: "access",
108+
State: catalog.SystemSchemaInfoStateEnableCompleted,
109+
},
110+
{
111+
Schema: "billing",
112+
State: catalog.SystemSchemaInfoStateEnableCompleted,
113+
},
114+
},
115+
}, nil)
116+
},
117+
Resource: ResourceSystemSchema(),
118+
HCL: `schema = "billing"`,
119+
Create: true,
120+
}.ApplyAndExpectData(t, map[string]any{
121+
"schema": "billing",
122+
"id": "abc|billing",
123+
"full_name": "system.billing",
124+
})
125+
}
126+
90127
func TestSystemSchemaUpdate(t *testing.T) {
91128
d, err := qa.ResourceFixture{
92129
Fixtures: []qa.HTTPFixture{

catalog/system_schema_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package catalog_test
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/databricks/terraform-provider-databricks/internal/acceptance"
87
)
98

109
func TestUcAccResourceSystemSchema(t *testing.T) {
11-
if os.Getenv("GOOGLE_CREDENTIALS") != "" {
12-
t.Skipf("databricks_system_schema resource not available on GCP")
13-
}
1410
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
1511
Template: `
16-
resource "databricks_system_schema" "this" {
12+
resource "databricks_system_schema" "access" {
1713
schema = "access"
14+
}
15+
resource "databricks_system_schema" "billing" {
16+
schema = "billing"
1817
}`,
1918
})
2019
}

docs/resources/system_schema.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ subcategory: "Unity Catalog"
77

88
-> This resource can only be used with a workspace-level provider!
99

10+
-> Certain system schemas (such as `billing`) may be auto-enabled once GA and should not be manually declared in Terraform configurations.
11+
1012
Manages system tables enablement. System tables are a Databricks-hosted analytical store of your account’s operational data. System tables can be used for historical observability across your account. System tables must be enabled by an account admin.
1113

1214
## Example Usage

0 commit comments

Comments
 (0)