-
Notifications
You must be signed in to change notification settings - Fork 353
/
resource_workspace_file.go
110 lines (106 loc) · 3.01 KB
/
resource_workspace_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package workspace
import (
"context"
"encoding/base64"
"log"
"path/filepath"
"github.com/databricks/databricks-sdk-go/service/workspace"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// ResourceWorkspaceFile manages files in workspace
func ResourceWorkspaceFile() common.Resource {
s := FileContentSchema(map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Computed: true,
},
"object_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"workspace_path": {
Type: schema.TypeString,
Computed: true,
},
})
return common.Resource{
Schema: s,
SchemaVersion: 1,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
content, err := ReadContent(d)
if err != nil {
return err
}
client, err := c.WorkspaceClient()
if err != nil {
return err
}
path := d.Get("path").(string)
importReq := workspace.Import{
Content: base64.StdEncoding.EncodeToString(content),
Format: workspace.ImportFormatAuto,
Path: path,
Overwrite: true,
ForceSendFields: []string{"Content"},
}
err = client.Workspace.Import(ctx, importReq)
if err != nil {
if isParentDoesntExistError(err) {
parent := filepath.ToSlash(filepath.Dir(path))
log.Printf("[DEBUG] Parent folder '%s' doesn't exist, creating...", parent)
err = client.Workspace.MkdirsByPath(ctx, parent)
if err != nil {
return err
}
err = client.Workspace.Import(ctx, importReq)
}
if err != nil {
return err
}
}
d.SetId(path)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
client, err := c.WorkspaceClient()
if err != nil {
return err
}
objectStatus, err := common.RetryOnTimeout(ctx, func(ctx context.Context) (*workspace.ObjectInfo, error) {
return client.Workspace.GetStatusByPath(ctx, d.Id())
})
if err != nil {
return err
}
d.Set("url", c.FormatURL("#workspace", d.Id()))
d.Set("workspace_path", "/Workspace"+objectStatus.Path)
return common.StructToData(objectStatus, s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
client, err := c.WorkspaceClient()
if err != nil {
return err
}
content, err := ReadContent(d)
if err != nil {
return err
}
return client.Workspace.Import(ctx, workspace.Import{
Content: base64.StdEncoding.EncodeToString(content),
Format: workspace.ImportFormatAuto,
Overwrite: true,
Path: d.Id(),
ForceSendFields: []string{"Content"},
})
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
client, err := c.WorkspaceClient()
if err != nil {
return err
}
return client.Workspace.Delete(ctx, workspace.Delete{Path: d.Id(), Recursive: false})
},
}
}