From 8b2a735576d8c3fba1f3ca242622f512a703c8cf Mon Sep 17 00:00:00 2001 From: Wai Lau <59835087+Waiwait@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:19:21 +0100 Subject: [PATCH] [Fix] Recreate missing system schema (#4068) ## Changes Addresses https://github.com/databricks/terraform-provider-databricks/issues/4066. Makes read function more in line with rest of module to ignore non `enabled` schemas. ## Tests - [x] `make test` run locally - [ ] ~relevant change in `docs/` folder~ - [ ] covered with integration tests in `internal/acceptance` - [x] relevant acceptance tests are passing - [ ] ~using Go SDK~ --------- Co-authored-by: wai Lau Co-authored-by: Alex Ott --- catalog/resource_system_schema.go | 11 +++++ catalog/resource_system_schema_test.go | 68 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/catalog/resource_system_schema.go b/catalog/resource_system_schema.go index 06b1ff24d3..6c033fab89 100644 --- a/catalog/resource_system_schema.go +++ b/catalog/resource_system_schema.go @@ -93,10 +93,21 @@ func ResourceSystemSchema() common.Resource { if err != nil { return err } + // only track enabled/legacy schemas + if schema.State != catalog.SystemSchemaInfoStateEnableCompleted && + schema.State != catalog.SystemSchemaInfoStateEnableInitialized && + schema.State != catalog.SystemSchemaInfoStateUnavailable { + log.Printf("[WARN] %s is not enabled, ignoring it", schemaName) + d.SetId("") + return nil + } + d.Set("full_name", fmt.Sprintf("system.%s", schemaName)) return nil } } + log.Printf("[WARN] %s does not exist, ignoring it", schemaName) + d.SetId("") return nil }, Update: createOrUpdate, diff --git a/catalog/resource_system_schema_test.go b/catalog/resource_system_schema_test.go index e576eec072..8973d8727a 100644 --- a/catalog/resource_system_schema_test.go +++ b/catalog/resource_system_schema_test.go @@ -239,6 +239,74 @@ func TestSystemSchemaRead_Error(t *testing.T) { assert.Equal(t, "abc|access", d.Id(), "Id should not be empty for error reads") } +func TestSystemSchemaRead_NotEnabled(t *testing.T) { + d, err := qa.ResourceFixture{ + Fixtures: []qa.HTTPFixture{ + { + Method: http.MethodGet, + Resource: "/api/2.1/unity-catalog/metastore_summary", + Response: catalog.GetMetastoreSummaryResponse{ + MetastoreId: "abc", + }, + }, + { + Method: http.MethodGet, + Resource: "/api/2.1/unity-catalog/metastores/abc/systemschemas?", + Response: catalog.ListSystemSchemasResponse{ + Schemas: []catalog.SystemSchemaInfo{ + { + Schema: "access", + State: catalog.SystemSchemaInfoStateAvailable, + }, + { + Schema: "billing", + State: catalog.SystemSchemaInfoStateEnableCompleted, + }, + }, + }, + }, + }, + Resource: ResourceSystemSchema(), + Read: true, + Removed: true, + ID: "abc|access", + }.Apply(t) + assert.NoError(t, err) + assert.Equal(t, "", d.Id(), "Id should be empty if a schema is not enabled") +} + +func TestSystemSchemaRead_NotExists(t *testing.T) { + d, err := qa.ResourceFixture{ + Fixtures: []qa.HTTPFixture{ + { + Method: http.MethodGet, + Resource: "/api/2.1/unity-catalog/metastore_summary", + Response: catalog.GetMetastoreSummaryResponse{ + MetastoreId: "abc", + }, + }, + { + Method: http.MethodGet, + Resource: "/api/2.1/unity-catalog/metastores/abc/systemschemas?", + Response: catalog.ListSystemSchemasResponse{ + Schemas: []catalog.SystemSchemaInfo{ + { + Schema: "billing", + State: catalog.SystemSchemaInfoStateEnableCompleted, + }, + }, + }, + }, + }, + Resource: ResourceSystemSchema(), + Read: true, + Removed: true, + ID: "abc|access", + }.Apply(t) + assert.NoError(t, err) + assert.Equal(t, "", d.Id(), "Id should be empty if a schema does not exist") +} + func TestSystemSchemaDelete(t *testing.T) { d, err := qa.ResourceFixture{ Fixtures: []qa.HTTPFixture{