Skip to content

Commit

Permalink
Add force_destroy flag to databricks_schema & databricks_catalog (
Browse files Browse the repository at this point in the history
  • Loading branch information
nkvuong committed Sep 2, 2022
1 parent fe46f57 commit 85ffea6
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
30 changes: 27 additions & 3 deletions catalog/resource_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,32 @@ func (a CatalogsAPI) getCatalog(name string) (ci CatalogInfo, err error) {
}

func (a CatalogsAPI) deleteCatalog(name string) error {
// TODO: force_destroy attribute
return a.client.Delete(a.context, "/unity-catalog/catalogs/"+name+"?force=true", nil)
return a.client.Delete(a.context, "/unity-catalog/catalogs/"+name, nil)
}

func (a CatalogsAPI) forceDeleteCatalog(name string) error {
schemasAPI := NewSchemasAPI(a.context, a.client)
schemas, err := schemasAPI.listByCatalog(name)
if err != nil {
return err
}
for _, s := range schemas.Schemas {
if s.Name != name+".information_schema" {
schemasAPI.forceDeleteSchema(s.FullName)
}
}

return a.client.Delete(a.context, "/unity-catalog/catalogs/"+name, nil)
}

func ResourceCatalog() *schema.Resource {
catalogSchema := common.StructToSchema(CatalogInfo{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["force_destroy"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
}
return m
})
update := updateFunctionFactory("/unity-catalog/catalogs", []string{"owner", "comment", "properties"})
Expand All @@ -77,7 +96,12 @@ func ResourceCatalog() *schema.Resource {
},
Update: update,
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
return NewCatalogsAPI(ctx, c).deleteCatalog(d.Id())
force := d.Get("force_destroy").(bool)
if force {
return NewCatalogsAPI(ctx, c).forceDeleteCatalog(d.Id())
} else {
return NewCatalogsAPI(ctx, c).deleteCatalog(d.Id())
}
},
}.ToResource()
}
64 changes: 64 additions & 0 deletions catalog/resource_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,67 @@ func TestUpdateCatalog(t *testing.T) {
`,
}.ApplyNoError(t)
}

func TestForceDeleteCatalog(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/schemas?catalog_name=b",
Response: Schemas{
Schemas: []SchemaInfo{
{
Name: "a",
FullName: "b.a",
},
},
},
},
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/tables/?catalog_name=b&schema_name=a",
Response: Tables{
Tables: []TableInfo{
{
CatalogName: "b",
SchemaName: "a",
Name: "c",
TableType: "MANAGED",
},
{
CatalogName: "b",
SchemaName: "a",
Name: "d",
TableType: "VIEW",
},
},
},
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/tables/b.a.c",
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/tables/b.a.d",
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/schemas/b.a",
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/catalogs/b",
},
},
Resource: ResourceCatalog(),
Delete: true,
ID: "b",
HCL: `
name = "b"
comment = "c"
owner = "administrators"
force_destroy = true
`,
}.ApplyNoError(t)
}
22 changes: 22 additions & 0 deletions catalog/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package catalog

import (
"context"
"strings"

"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -50,10 +51,27 @@ func (a SchemasAPI) deleteSchema(name string) error {
return a.client.Delete(a.context, "/unity-catalog/schemas/"+name, nil)
}

func (a SchemasAPI) forceDeleteSchema(name string) error {
tablesAPI := NewTablesAPI(a.context, a.client)
tables, err := tablesAPI.listTables(strings.Split(name, ".")[0], strings.Split(name, ".")[1])
if err != nil {
return err
}
for _, v := range tables.Tables {
tablesAPI.deleteTable(v.FullName())
}
return a.client.Delete(a.context, "/unity-catalog/schemas/"+name, nil)
}

func ResourceSchema() *schema.Resource {
s := common.StructToSchema(SchemaInfo{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
delete(m, "full_name")
m["force_destroy"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
}
return m
})
update := updateFunctionFactory("/unity-catalog/schemas", []string{"owner", "comment", "properties"})
Expand All @@ -77,6 +95,10 @@ func ResourceSchema() *schema.Resource {
},
Update: update,
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
force := d.Get("force_destroy").(bool)
if force {
return NewSchemasAPI(ctx, c).forceDeleteSchema(d.Id())
}
return NewSchemasAPI(ctx, c).deleteSchema(d.Id())
},
}.ToResource()
Expand Down
49 changes: 49 additions & 0 deletions catalog/resource_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,52 @@ func TestUpdateSchema(t *testing.T) {
`,
}.ApplyNoError(t)
}

func TestForceDeleteSchema(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/tables/?catalog_name=b&schema_name=a",
Response: Tables{
Tables: []TableInfo{
{
CatalogName: "b",
SchemaName: "a",
Name: "c",
TableType: "MANAGED",
},
{
CatalogName: "b",
SchemaName: "a",
Name: "d",
TableType: "VIEW",
},
},
},
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/tables/b.a.c",
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/tables/b.a.d",
},
{
Method: "DELETE",
Resource: "/api/2.1/unity-catalog/schemas/b.a",
},
},
Resource: ResourceSchema(),
Delete: true,
ID: "b.a",
HCL: `
name = "a"
catalog_name = "b"
comment = "c"
owner = "administrators"
force_destroy = true
`,
}.ApplyNoError(t)
}
1 change: 1 addition & 0 deletions docs/resources/catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The following arguments are required:
* `owner` - (Optional) Username/groupname/sp application_id of the catalog owner.
* `comment` - (Optional) User-supplied free-form text.
* `properties` - (Optional) Extensible Catalog properties.
* `force_destroy` - (Optional) Delete catalog regardless of its contents.

## Import

Expand Down
1 change: 1 addition & 0 deletions docs/resources/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following arguments are required:
* `owner` - (Optional) Username/groupname/sp application_id of the schema owner.
* `comment` - (Optional) User-supplied free-form text.
* `properties` - (Optional) Extensible Schema properties.
* `force_destroy` - (Optional) Delete schema regardless of its contents.

## Import

Expand Down

0 comments on commit 85ffea6

Please sign in to comment.