|
| 1 | +package catalog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/databrickslabs/terraform-provider-databricks/common" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +type TablesAPI struct { |
| 12 | + client *common.DatabricksClient |
| 13 | + context context.Context |
| 14 | +} |
| 15 | + |
| 16 | +func NewTablesAPI(ctx context.Context, m interface{}) TablesAPI { |
| 17 | + return TablesAPI{m.(*common.DatabricksClient), ctx} |
| 18 | +} |
| 19 | + |
| 20 | +type ColumnInfo struct { |
| 21 | + Name string `json:"name"` |
| 22 | + TypeText string `json:"type_text"` |
| 23 | + TypeJson string `json:"type_json,omitempty"` |
| 24 | + TypeName string `json:"type_name"` |
| 25 | + TypePrecision int32 `json:"type_precision,omitempty"` |
| 26 | + TypeScale int32 `json:"type_scale,omitempty"` |
| 27 | + TypeIntervalType string `json:"type_interval_type,omitempty"` |
| 28 | + Position int32 `json:"position"` |
| 29 | + Comment string `json:"comment,omitempty"` |
| 30 | + Nullable bool `json:"nullable,omitempty" tf:"default:true"` |
| 31 | + PartitionIndex int32 `json:"partition_index,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +type TableInfo struct { |
| 35 | + Name string `json:"name" tf:"force_new"` |
| 36 | + CatalogName string `json:"catalog_name"` |
| 37 | + SchemaName string `json:"schema_name"` |
| 38 | + TableType string `json:"table_type"` |
| 39 | + DataSourceFormat string `json:"data_source_format"` |
| 40 | + ColumnInfos []ColumnInfo `json:"columns" tf:"alias:column"` |
| 41 | + StorageLocation string `json:"storage_location"` |
| 42 | + StorageCredentialName string `json:"storage_credential_name,omitempty"` |
| 43 | + ViewDefinition string `json:"view_definition,omitempty"` |
| 44 | + Owner string `json:"owner,omitempty" tf:"computed"` |
| 45 | + Comment string `json:"comment,omitempty"` |
| 46 | + Properties map[string]string `json:"properties,omitempty"` |
| 47 | +} |
| 48 | + |
| 49 | +func (ti TableInfo) FullName() string { |
| 50 | + return fmt.Sprintf("%s.%s.%s", ti.CatalogName, ti.SchemaName, ti.Name) |
| 51 | +} |
| 52 | + |
| 53 | +func (a TablesAPI) createTable(ti *TableInfo) error { |
| 54 | + return a.client.Post(a.context, "/unity-catalog/tables", ti, ti) |
| 55 | +} |
| 56 | + |
| 57 | +func (a TablesAPI) getTable(name string) (ti TableInfo, err error) { |
| 58 | + err = a.client.Get(a.context, "/unity-catalog/tables/"+name, nil, &ti) |
| 59 | + return |
| 60 | +} |
| 61 | + |
| 62 | +func (a TablesAPI) updateTable(ti TableInfo) error { |
| 63 | + return a.client.Patch(a.context, "/unity-catalog/tables/"+ti.Name, ti) |
| 64 | +} |
| 65 | + |
| 66 | +func (a TablesAPI) deleteTable(name string) error { |
| 67 | + return a.client.Delete(a.context, "/unity-catalog/tables/"+name, nil) |
| 68 | +} |
| 69 | + |
| 70 | +func ResourceTable() *schema.Resource { |
| 71 | + tableSchema := common.StructToSchema(TableInfo{}, |
| 72 | + func(m map[string]*schema.Schema) map[string]*schema.Schema { |
| 73 | + return m |
| 74 | + }) |
| 75 | + return common.Resource{ |
| 76 | + Schema: tableSchema, |
| 77 | + Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 78 | + var ti TableInfo |
| 79 | + common.DataToStructPointer(d, tableSchema, &ti) |
| 80 | + if err := NewTablesAPI(ctx, c).createTable(&ti); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + d.SetId(ti.FullName()) |
| 84 | + return nil |
| 85 | + }, |
| 86 | + Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 87 | + ti, err := NewTablesAPI(ctx, c).getTable(d.Id()) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + return common.StructToData(ti, tableSchema, d) |
| 92 | + }, |
| 93 | + Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 94 | + var ti TableInfo |
| 95 | + common.DataToStructPointer(d, tableSchema, &ti) |
| 96 | + return NewTablesAPI(ctx, c).updateTable(ti) |
| 97 | + }, |
| 98 | + Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 99 | + return NewTablesAPI(ctx, c).deleteTable(d.Id()) |
| 100 | + }, |
| 101 | + }.ToResource() |
| 102 | +} |
0 commit comments