-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_dropbox_paper_folder.go
66 lines (57 loc) · 1.63 KB
/
data_source_dropbox_paper_folder.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
package dropbox
import (
"fmt"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/paper"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceDropboxPaperFolder() *schema.Resource {
return &schema.Resource{
Read: dataSourceDropboxPaperFolderRead,
Schema: map[string]*schema.Schema{
"doc_id": {
Type: schema.TypeString,
Required: true,
},
"folders": {
Type: schema.TypeList,
Computed: true,
Description: "List of folders that contain the document reference",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceDropboxPaperFolderRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*ProviderConfig).DropboxConfig
client := paper.New(*config)
opts := paper.NewRefPaperDoc(d.Get("doc_id").(string))
info, err := client.DocsGetFolderInfo(opts)
if err != nil {
return fmt.Errorf("Paper Folder Data Failure: %s", err)
}
outputFolders := flattenPaperFolders(info.Folders)
d.SetId(fmt.Sprintf("folder_ds:%s", d.Get("doc_id").(string)))
d.Set("folders", outputFolders)
return nil
}
func flattenPaperFolders(folders []*paper.Folder) []map[string]interface{} {
outputFolders := make([]map[string]interface{}, 0, len(folders))
for _, f := range folders {
newFolder := make(map[string]interface{})
newFolder["id"] = f.Id
newFolder["name"] = f.Name
outputFolders = append(outputFolders, newFolder)
}
return outputFolders
}