|
8 | 8 | "regexp" |
9 | 9 | "strings" |
10 | 10 |
|
| 11 | + "github.com/databricks/databricks-sdk-go" |
11 | 12 | "github.com/databricks/databricks-sdk-go/apierr" |
12 | 13 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
13 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
@@ -182,6 +183,7 @@ func makeEmptyBlockSuppressFunc(name string) func(k, old, new string, d *schema. |
182 | 183 | } |
183 | 184 | } |
184 | 185 |
|
| 186 | +// Deprecated: migrate to WorkspaceData |
185 | 187 | func DataResource(sc any, read func(context.Context, any, *DatabricksClient) error) *schema.Resource { |
186 | 188 | // TODO: migrate to go1.18 and get schema from second function argument?.. |
187 | 189 | s := StructToSchema(sc, func(m map[string]*schema.Schema) map[string]*schema.Schema { return m }) |
@@ -213,3 +215,52 @@ func DataResource(sc any, read func(context.Context, any, *DatabricksClient) err |
213 | 215 | }, |
214 | 216 | } |
215 | 217 | } |
| 218 | + |
| 219 | +// WorkspaceData is a generic way to define data resources in Terraform provider. |
| 220 | +// |
| 221 | +// Example usage: |
| 222 | +// |
| 223 | +// type catalogsData struct { |
| 224 | +// Ids []string `json:"ids,omitempty" tf:"computed,slice_set"` |
| 225 | +// } |
| 226 | +// return common.WorkspaceData(func(ctx context.Context, data *catalogsData, w *databricks.WorkspaceClient) error { |
| 227 | +// catalogs, err := w.Catalogs.ListAll(ctx) |
| 228 | +// ... |
| 229 | +// }) |
| 230 | +func WorkspaceData[T any](read func(context.Context, *T, *databricks.WorkspaceClient) error) *schema.Resource { |
| 231 | + var dummy T |
| 232 | + s := StructToSchema(dummy, func(m map[string]*schema.Schema) map[string]*schema.Schema { return m }) |
| 233 | + return &schema.Resource{ |
| 234 | + Schema: s, |
| 235 | + ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) (diags diag.Diagnostics) { |
| 236 | + defer func() { |
| 237 | + // using recoverable() would cause more complex rewrapping of DataToStructPointer & StructToData |
| 238 | + if panic := recover(); panic != nil { |
| 239 | + diags = diag.Errorf("panic: %v", panic) |
| 240 | + } |
| 241 | + }() |
| 242 | + ptr := reflect.New(reflect.ValueOf(dummy).Type()) |
| 243 | + DataToReflectValue(d, &schema.Resource{Schema: s}, ptr.Elem()) |
| 244 | + client := m.(*DatabricksClient) |
| 245 | + w, err := client.WorkspaceClient() |
| 246 | + if err != nil { |
| 247 | + err = nicerError(ctx, err, "read data") |
| 248 | + return diag.FromErr(err) |
| 249 | + } |
| 250 | + err = read(ctx, ptr.Interface().(*T), w) |
| 251 | + if err != nil { |
| 252 | + err = nicerError(ctx, err, "read data") |
| 253 | + diags = diag.FromErr(err) |
| 254 | + } |
| 255 | + StructToData(ptr.Elem().Interface(), s, d) |
| 256 | + // check if the resource schema has the `id` attribute (marked with `json:"id"` in the provided structure). |
| 257 | + // and if yes, then use it as resource ID. If not, then use default value for resource ID (`_`) |
| 258 | + if _, ok := s["id"]; ok { |
| 259 | + d.SetId(d.Get("id").(string)) |
| 260 | + } else { |
| 261 | + d.SetId("_") |
| 262 | + } |
| 263 | + return |
| 264 | + }, |
| 265 | + } |
| 266 | +} |
0 commit comments