Skip to content

Commit

Permalink
[Exporter] Add support for databricks_online_table
Browse files Browse the repository at this point in the history
  • Loading branch information
alexott committed Jul 24, 2024
1 parent b3cea48 commit 629cd3d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/guides/experimental-exporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Services are just logical groups of resources used for filtering and organizatio
* `uc-grants` - [databricks_grants](../resources/grants.md). *Please note that during export the list of grants is expanded to include the identity that does the export! This is done to allow to creation of objects in case when catalogs/schemas have different owners than the current identity.*.
* `uc-metastores` - **listing** [databricks_metastore](../resources/metastore.md) and [databricks_metastore_assignment](../resource/metastore_assignment.md) (only on account-level). *Please note that when using workspace-level configuration, only the metastores from the workspace's region are listed!*
* `uc-models` - **listing** (*we can't list directly, only via dependencies to top-level object*) [databricks_registered_model](../resources/registered_model.md)
* `uc-online-tables` - **listing** (*we can't list directly, only via dependencies to top-level object*) [databricks_online_table](../resources/online_table.md)
* `uc-schemas` - **listing** (*we can't list directly, only via dependencies to top-level object*) [databricks_schema](../resources/schema.md)
* `uc-shares` - **listing** [databricks_share](../resources/share.md) and [databricks_recipient](../resources/recipient.md)
* `uc-storage-credentials` - **listing** exports [databricks_storage_credential](../resources/storage_credential.md) resources on workspace or account level.
Expand Down Expand Up @@ -196,6 +197,7 @@ Exporter aims to generate HCL code for most of the resources within the Databric
| [databricks_mws_permission_assignment](../resources/mws_permission_assignment.md) | Yes | No | No | Yes |
| [databricks_notebook](../resources/notebook.md) | Yes | Yes | Yes | No |
| [databricks_obo_token](../resources/obo_token.md) | Not Applicable | No | No | No |
| [databricks_online_table](../resources/online_table.md) | Yes | Yes | Yes | No |
| [databricks_permissions](../resources/permissions.md) | Yes | No | Yes | No |
| [databricks_pipeline](../resources/pipeline.md) | Yes | Yes | Yes | No |
| [databricks_recipient](../resources/recipient.md) | Yes | Yes | Yes | No |
Expand Down
42 changes: 38 additions & 4 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2534,15 +2534,24 @@ var resourcesMap map[string]importable = map[string]importable{
ID: table.FullName,
DependsOn: dependsOn,
}, table.UpdatedAt, fmt.Sprintf("table '%s'", table.FullName))
case "FOREIGN":
// TODO: it's better to use SecurableKind if it will be added to the Go SDK
switch table.DataSourceFormat {
case "VECTOR_INDEX_FORMAT":
case "MYSQL_FORMAT":
ic.EmitIfUpdatedAfterMillis(&resource{
Resource: "databricks_online_table",
ID: table.FullName,
DependsOn: dependsOn,
}, table.UpdatedAt, fmt.Sprintf("table '%s'", table.FullName))
default:
log.Printf("[DEBUG] Skipping foreign table %s of format %s", table.FullName, table.DataSourceFormat)
}
default:
log.Printf("[DEBUG] Skipping table %s of type %s", table.FullName, table.TableType)
}
}
}
// TODO: list VectorSearch indexes

// TODO: list online tables

return nil
},
ShouldOmitField: shouldOmitForUnityCatalog,
Expand Down Expand Up @@ -2603,6 +2612,9 @@ var resourcesMap map[string]importable = map[string]importable{
switch pathString {
case "storage_location":
return d.Get("table_type").(string) == "MANAGED"
case "enable_predictive_optimization":
epo := d.Get(pathString).(string)
return epo == "" || epo == "INHERIT"
}
return shouldOmitForUnityCatalog(ic, pathString, as, d)
},
Expand Down Expand Up @@ -3185,4 +3197,26 @@ var resourcesMap map[string]importable = map[string]importable{
{Path: "parent_path", Resource: "databricks_service_principal"},
},
},
"databricks_online_table": {
WorkspaceLevel: true,
Service: "uc-online-tables",
Import: func(ic *importContext, r *resource) error {
tableFullName := r.ID
ic.emitUCGrantsWithOwner("table/"+tableFullName, r)
ic.Emit(&resource{
Resource: "databricks_sql_table",
ID: r.Data.Get("spec.0.source_table_full_name").(string),
})
// TODO: emit owner? See comment in catalog resource
return nil
},
Ignore: generateIgnoreObjectWithoutName("databricks_online_table"),
ShouldOmitField: shouldOmitForUnityCatalog,
Depends: []reference{
{Path: "catalog_name", Resource: "databricks_catalog"},
{Path: "schema_name", Resource: "databricks_schema", Match: "name",
IsValidApproximation: isMatchingCatalogAndSchema, SkipDirectLookup: true},
{Path: "spec.source_table_full_name", Resource: "databricks_sql_table"},
},
},
}
36 changes: 36 additions & 0 deletions exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2236,3 +2236,39 @@ func TestImportGrants(t *testing.T) {
err = resourcesMap["databricks_grants"].Import(ic, r)
assert.NoError(t, err)
}

func TestImportUcOnlineTable(t *testing.T) {
qa.HTTPFixturesApply(t, []qa.HTTPFixture{}, func(ctx context.Context, client *common.DatabricksClient) {
ic := importContextForTestWithClient(ctx, client)
tmpDir := fmt.Sprintf("/tmp/tf-%s", qa.RandomName())
defer os.RemoveAll(tmpDir)
os.Mkdir(tmpDir, 0700)
ic.Directory = tmpDir
ic.enableServices("uc-tables,uc-grants")
ic.currentMetastore = currentMetastoreResponse

otTableName := "main.tmp.tbl_ot"
d := tfcatalog.ResourceOnlineTable().ToResource().TestResourceData()
ot := catalog.OnlineTable{
Name: otTableName,
Spec: &catalog.OnlineTableSpec{
SourceTableFullName: "main.tmp.tbl",
PrimaryKeyColumns: []string{"id"},
},
}
d.SetId(otTableName)
d.MarkNewResource()
scm := tfcatalog.ResourceOnlineTable().Schema
err := common.StructToData(ot, scm, d)
require.NoError(t, err)

err = resourcesMap["databricks_online_table"].Import(ic, &resource{
ID: otTableName,
Data: d,
})
assert.NoError(t, err)
require.Equal(t, 2, len(ic.testEmits))
assert.True(t, ic.testEmits["databricks_sql_table[<unknown>] (id: main.tmp.tbl)"])
assert.True(t, ic.testEmits["databricks_grants[<unknown>] (id: table/main.tmp.tbl_ot)"])
})
}

0 comments on commit 629cd3d

Please sign in to comment.