-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_contentful_space.go
118 lines (94 loc) · 2.25 KB
/
resource_contentful_space.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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
contentful "github.com/tolgaakyuz/contentful-go"
)
func resourceContentfulSpace() *schema.Resource {
return &schema.Resource{
Create: resourceSpaceCreate,
Read: resourceSpaceRead,
Update: resourceSpaceUpdate,
Delete: resourceSpaceDelete,
Schema: map[string]*schema.Schema{
"version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
// Space specific props
"default_locale": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "en",
},
},
}
}
func resourceSpaceCreate(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Contentful)
space := &contentful.Space{
Name: d.Get("name").(string),
DefaultLocale: d.Get("default_locale").(string),
}
err = client.Spaces.Upsert(space)
if err != nil {
return err
}
err = updateSpaceProperties(d, space)
if err != nil {
return err
}
d.SetId(space.Sys.ID)
return nil
}
func resourceSpaceRead(d *schema.ResourceData, m interface{}) error {
client := m.(*contentful.Contentful)
spaceID := d.Id()
_, err := client.Spaces.Get(spaceID)
if _, ok := err.(contentful.NotFoundError); ok {
d.SetId("")
return nil
}
return err
}
func resourceSpaceUpdate(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Contentful)
spaceID := d.Id()
space, err := client.Spaces.Get(spaceID)
if err != nil {
return err
}
space.Name = d.Get("name").(string)
err = client.Spaces.Upsert(space)
if err != nil {
return err
}
return updateSpaceProperties(d, space)
}
func resourceSpaceDelete(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Contentful)
spaceID := d.Id()
space, err := client.Spaces.Get(spaceID)
if err != nil {
return err
}
err = client.Spaces.Delete(space)
if _, ok := err.(contentful.NotFoundError); ok {
return nil
}
return err
}
func updateSpaceProperties(d *schema.ResourceData, space *contentful.Space) error {
err := d.Set("version", space.Sys.Version)
if err != nil {
return err
}
err = d.Set("name", space.Name)
if err != nil {
return err
}
return nil
}