-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_environment.go
231 lines (198 loc) · 5.94 KB
/
resource_environment.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/labd/contentstack-go-sdk/management"
)
type resourceEnvironmentType struct{}
type EnvironmentData struct {
UID types.String `tfsdk:"uid"`
Name types.String `tfsfk:"name"`
URLs []EnvironmentUrlData `tfsdk:"url"`
}
type EnvironmentUrlData struct {
Locale types.String `tfsdk:"locale"`
URL types.String `tfsdk:"url"`
}
// Environment Resource schema
func (r resourceEnvironmentType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: `
Contentstack environment are designated destinations to which you can publish
your content. Environments are global, meaning they are available across all
branches of your stack. An environment can also have a list of URLs to be used
as a prefix for published content.
`,
Attributes: map[string]tfsdk.Attribute{
"uid": {
Type: types.StringType,
Computed: true,
},
"name": {
Type: types.StringType,
Required: true,
},
},
Blocks: map[string]tfsdk.Block{
"url": {
NestingMode: tfsdk.BlockNestingModeList,
Blocks: map[string]tfsdk.Block{},
MinItems: 0,
Attributes: map[string]tfsdk.Attribute{
"locale": {
Type: types.StringType,
Required: true,
},
"url": {
Type: types.StringType,
Required: true,
},
},
},
},
}, nil
}
// New resource instance
func (r resourceEnvironmentType) NewResource(_ context.Context, p tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) {
return resourceEnvironment{
p: *(p.(*provider)),
}, nil
}
type resourceEnvironment struct {
p provider
}
func (r resourceEnvironment) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) {
var plan EnvironmentData
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
input := NewEnvironmentInput(&plan)
environment, err := r.p.stack.EnvironmentCreate(ctx, *input)
if err != nil {
diags := processRemoteError(err)
resp.Diagnostics.Append(diags...)
return
}
diags = processResponse(environment, input)
resp.Diagnostics.Append(diags...)
// Write to state
state := NewEnvironmentData(environment)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
func (r resourceEnvironment) Read(ctx context.Context, req tfsdk.ReadResourceRequest, resp *tfsdk.ReadResourceResponse) {
var state EnvironmentData
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
environment, err := r.p.stack.EnvironmentFetch(ctx, state.UID.Value)
if err != nil {
if IsNotFoundError(err) {
d := diag.NewErrorDiagnostic(
"Error retrieving environment",
fmt.Sprintf("The environment with UID %s was not found.", state.UID.Value))
resp.Diagnostics.Append(d)
} else {
diags := processRemoteError(err)
resp.Diagnostics.Append(diags...)
}
return
}
curr := NewEnvironmentInput(&state)
diags = processResponse(environment, curr)
resp.Diagnostics.Append(diags...)
// Set state
newState := NewEnvironmentData(environment)
diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
}
func (r resourceEnvironment) Delete(ctx context.Context, req tfsdk.DeleteResourceRequest, resp *tfsdk.DeleteResourceResponse) {
var state EnvironmentData
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Delete environment by calling API
err := r.p.stack.EnvironmentDelete(ctx, state.UID.Value)
if err != nil {
diags = processRemoteError(err)
resp.Diagnostics.Append(diags...)
return
}
// Remove resource from state
resp.State.RemoveResource(ctx)
}
func (r resourceEnvironment) Update(ctx context.Context, req tfsdk.UpdateResourceRequest, resp *tfsdk.UpdateResourceResponse) {
// Get plan values
var plan EnvironmentData
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get current state
var state EnvironmentData
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
input := NewEnvironmentInput(&plan)
environment, err := r.p.stack.EnvironmentUpdate(ctx, state.UID.Value, *input)
if err != nil {
diags = processRemoteError(err)
resp.Diagnostics.Append(diags...)
return
}
diags = processResponse(environment, input)
resp.Diagnostics.Append(diags...)
// Set state
result := NewEnvironmentData(environment)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
}
func (r resourceEnvironment) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
tfsdk.ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("id"), req, resp)
}
func NewEnvironmentData(environment *management.Environment) *EnvironmentData {
urls := []EnvironmentUrlData{}
for i := range environment.URLs {
s := environment.URLs[i]
url := EnvironmentUrlData{
Locale: types.String{Value: s.Locale},
URL: types.String{Value: s.URL},
}
urls = append(urls, url)
}
state := &EnvironmentData{
UID: types.String{Value: environment.UID},
Name: types.String{Value: environment.Name},
URLs: urls,
}
return state
}
func NewEnvironmentInput(environment *EnvironmentData) *management.EnvironmentInput {
urls := []management.EnvironmentUrl{}
for i := range environment.URLs {
s := environment.URLs[i]
url := management.EnvironmentUrl{
Locale: s.Locale.Value,
URL: s.URL.Value,
}
urls = append(urls, url)
}
input := &management.EnvironmentInput{
Name: environment.Name.Value,
URLs: urls,
}
return input
}