-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_dropbox_file.go
139 lines (119 loc) · 3.46 KB
/
resource_dropbox_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package dropbox
import (
"fmt"
"strings"
db "github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDropboxFile() *schema.Resource {
return &schema.Resource{
Create: resourceDropboxFileCreate,
Read: resourceDropboxFileRead,
Update: resourceDropboxFileUpdate,
Delete: resourceDropboxFileDelete,
Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: convertContentToB64(),
},
"path": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateWithRegExp(uploadPathPattern),
},
"mode": {
Type: schema.TypeString,
Optional: true,
Default: "add",
Description: "Valid formats modes: add, overwrite, and update",
ValidateFunc: validateFileWriteMode(),
},
"auto_rename": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"mute": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"hash": {
Type: schema.TypeString,
Computed: true,
Description: "A generated hash of the uploaded content",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the uploaded data in bytes",
},
},
}
}
func resourceDropboxFileCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*ProviderConfig).DropboxConfig
client := files.New(*config)
content := d.Get("content").(string)
reader := strings.NewReader(content)
opts := &files.CommitInfo{
Path: d.Get("path").(string),
Mode: &files.WriteMode{Tagged: db.Tagged{Tag: d.Get("mode").(string)}},
Autorename: d.Get("auto_rename").(bool),
Mute: d.Get("mute").(bool),
}
metadata, err := client.Upload(opts, reader)
if err != nil {
return fmt.Errorf("File Creation Failure: %s", err)
}
d.SetId(metadata.Id)
return resourceDropboxFileRead(d, meta)
}
func resourceDropboxFileRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*ProviderConfig).DropboxConfig
client := files.New(*config)
opts := files.NewDownloadArg(d.Get("path").(string))
res, _, err := client.Download(opts)
if err != nil {
return fmt.Errorf("File Read Failure: %s", err)
}
d.Set("hash", res.ContentHash)
d.Set("size", res.Size)
return nil
}
func resourceDropboxFileUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*ProviderConfig).DropboxConfig
client := files.New(*config)
optsRead := files.NewGetMetadataArg(d.Id())
res, err := client.GetMetadata(optsRead)
if err != nil {
return fmt.Errorf("File Update Failure: %s", err)
}
oldPath := res.(*files.FileMetadata).PathDisplay
d.Partial(true)
if d.HasChange("path") {
newPath := files.NewRelocationPath(oldPath, d.Get("path").(string))
optsMove := &files.RelocationArg{
RelocationPath: *newPath,
}
_, err := client.MoveV2(optsMove)
if err != nil {
return fmt.Errorf("File Update Failure: %s", err)
}
}
d.Partial(false)
return nil
}
func resourceDropboxFileDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*ProviderConfig).DropboxConfig
client := files.New(*config)
opts := &files.DeleteArg{Path: d.Get("path").(string)}
_, err := client.DeleteV2(opts)
if err != nil {
return fmt.Errorf("File Deletion Failure: %s", err)
}
return nil
}