|
| 1 | +package catalog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/databricks/databricks-sdk-go/service/catalog" |
| 7 | + "github.com/databricks/terraform-provider-databricks/common" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +// This structure contains the fields of catalog.UpdateVolumeRequestContent and catalog.CreateVolumeRequestContent |
| 12 | +// We need to create this because we need Owner, FullNameArg, SchemaName and CatalogName which aren't present in a single of them. |
| 13 | +// We also need to annotate tf:"computed" for the Owner field. |
| 14 | +type VolumeInfo struct { |
| 15 | + // The name of the catalog where the schema and the volume are |
| 16 | + CatalogName string `json:"catalog_name"` |
| 17 | + // The comment attached to the volume |
| 18 | + Comment string `json:"comment,omitempty"` |
| 19 | + // The name of the schema where the volume is |
| 20 | + SchemaName string `json:"schema_name"` |
| 21 | + FullNameArg string `json:"-" url:"-"` |
| 22 | + // The name of the volume |
| 23 | + Name string `json:"name"` |
| 24 | + // The identifier of the user who owns the volume |
| 25 | + Owner string `json:"owner,omitempty" tf:"computed"` |
| 26 | + // The storage location on the cloud |
| 27 | + StorageLocation string `json:"storage_location,omitempty"` |
| 28 | + VolumeType catalog.VolumeType `json:"volume_type"` |
| 29 | +} |
| 30 | + |
| 31 | +func ResourceVolume() *schema.Resource { |
| 32 | + s := common.StructToSchema(VolumeInfo{}, |
| 33 | + func(m map[string]*schema.Schema) map[string]*schema.Schema { |
| 34 | + return m |
| 35 | + }) |
| 36 | + return common.Resource{ |
| 37 | + Schema: s, |
| 38 | + Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 39 | + w, err := c.WorkspaceClient() |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + var createVolumeRequestContent catalog.CreateVolumeRequestContent |
| 44 | + common.DataToStructPointer(d, s, &createVolumeRequestContent) |
| 45 | + v, err := w.Volumes.Create(ctx, createVolumeRequestContent) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + d.SetId(v.FullName) |
| 50 | + |
| 51 | + // Update owner if it is provided |
| 52 | + if d.Get("owner") != "" { |
| 53 | + var updateVolumeRequestContent catalog.UpdateVolumeRequestContent |
| 54 | + common.DataToStructPointer(d, s, &updateVolumeRequestContent) |
| 55 | + updateVolumeRequestContent.FullNameArg = d.Id() |
| 56 | + _, err = w.Volumes.Update(ctx, updateVolumeRequestContent) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | + }, |
| 64 | + Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 65 | + w, err := c.WorkspaceClient() |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + v, err := w.Volumes.ReadByFullNameArg(ctx, d.Id()) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + return common.StructToData(v, s, d) |
| 74 | + }, |
| 75 | + Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 76 | + w, err := c.WorkspaceClient() |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + var updateVolumeRequestContent catalog.UpdateVolumeRequestContent |
| 81 | + common.DataToStructPointer(d, s, &updateVolumeRequestContent) |
| 82 | + updateVolumeRequestContent.FullNameArg = d.Id() |
| 83 | + v, err := w.Volumes.Update(ctx, updateVolumeRequestContent) |
| 84 | + // We need to update the resource data because Name is updatable and FullName consists of Name, |
| 85 | + // So if we don't update the field then the requests would be made to old FullName which doesn't exists. |
| 86 | + d.SetId(v.FullName) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + return nil |
| 91 | + }, |
| 92 | + Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 93 | + w, err := c.WorkspaceClient() |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + return w.Volumes.DeleteByFullNameArg(ctx, d.Id()) |
| 98 | + }, |
| 99 | + }.ToResource() |
| 100 | +} |
0 commit comments