/
resource_project.go
159 lines (141 loc) · 3.9 KB
/
resource_project.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package googleappscript
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/pkg/errors"
"google.golang.org/api/script/v1"
)
var (
scriptValidFileTypes = []string{"JSON", "HTML", "SERVER_JS", "ENUM_TYPE_UNSPECIFIED"}
)
func resourceProject() *schema.Resource {
return &schema.Resource{
Create: resourceProjectCreate,
Read: resourceProjectRead,
Update: resourceProjectUpdate,
Delete: resourceProjectDelete,
Schema: map[string]*schema.Schema{
"title": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"parent_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"update_time": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"script": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(scriptValidFileTypes, false),
},
"source": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func resourceProjectCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
request := &script.CreateProjectRequest{
Title: d.Get("title").(string),
ParentId: d.Get("parent_id").(string)}
project, err := config.script.Projects.Create(request).Do()
if err != nil {
return errors.Wrap(err, "failed to create project")
}
scriptId := project.ScriptId
scripts := parseScripts(d)
content := &script.Content{
ScriptId: scriptId,
Files: scripts,
}
_, err = config.script.Projects.UpdateContent(scriptId, content).Do()
if err != nil {
return errors.Wrap(err, "couldn't update project content")
}
d.SetId(scriptId)
return resourceProjectRead(d, meta)
}
func resourceProjectRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id := d.Id()
project, err := config.script.Projects.Get(id).Do()
if err != nil {
return errors.Wrap(err, "failed to read project")
}
d.Set("title", project.Title)
d.Set("parent_id", project.ParentId)
d.Set("script_id", project.ScriptId)
d.Set("update_time", project.UpdateTime)
content, err := config.script.Projects.GetContent(id).Do()
if err != nil {
return errors.Wrap(err, "failed to read project content")
}
d.Set("script", flattenProjectFiles(content.Files))
return nil
}
func resourceProjectUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id := d.Id()
scripts := parseScripts(d)
content := &script.Content{
ScriptId: id,
Files: scripts,
}
_, err := config.script.Projects.UpdateContent(id, content).Do()
if err != nil {
return errors.Wrap(err, "couldn't update project content")
}
return resourceProjectRead(d, meta)
}
func resourceProjectDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id := d.Id()
err := config.drive.Files.Delete(id).Do()
if err != nil {
return errors.Wrap(err, "couldn't delete project ")
}
return nil
}
func parseScripts(d *schema.ResourceData) []*script.File {
scriptsRaw := d.Get("script").(*schema.Set)
scripts := make([]*script.File, scriptsRaw.Len())
for i, v := range scriptsRaw.List() {
m := v.(map[string]interface{})
scripts[i] = &script.File{
Name: m["name"].(string),
Type: m["type"].(string),
Source: m["source"].(string),
}
}
return scripts
}
func flattenProjectFiles(list []*script.File) []map[string]interface{} {
result := make([]map[string]interface{}, len(list))
for i, v := range list {
result[i] = map[string]interface{}{
"name": v.Name,
"type": v.Type,
"source": v.Source,
}
}
return result
}