/
resource_content_type.go
216 lines (184 loc) · 5.83 KB
/
resource_content_type.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
package provider
import (
"context"
"encoding/json"
"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 resourceContentTypeType struct{}
type ContentTypeData struct {
UID types.String `tfsdk:"uid"`
Title types.String `tfsdk:"title"`
Description types.String `tfsdk:"description"`
Schema types.String `tfsdk:"schema"`
}
// Global Field Resource schema
func (r resourceContentTypeType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: `
Content type defines the structure or schema of a page or a section of
your web or mobile property. To create content for your application, you
are required to first create a content type, and then create entries
using the content type.
Note: Removing a field or modifying its properties may result in data
loss or invalidate field visibility rules.
`,
Attributes: map[string]tfsdk.Attribute{
"uid": {
Type: types.StringType,
Required: true,
},
"title": {
Type: types.StringType,
Required: true,
},
"description": {
Type: types.StringType,
Optional: true,
},
"schema": {
Type: types.StringType,
Optional: true,
Description: "The schema as JSON. Use jsonencode(jsonecode(<schema>)) to work around wrong changes.",
},
},
}, nil
}
// New resource instance
func (r resourceContentTypeType) NewResource(_ context.Context, p tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) {
return resourceContentType{
p: *(p.(*provider)),
}, nil
}
type resourceContentType struct {
p provider
}
func (r resourceContentType) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) {
var plan ContentTypeData
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
input := NewContentTypeInput(&plan)
resource, err := r.p.stack.ContentTypeCreate(ctx, *input)
if err != nil {
diags := processRemoteError(err)
resp.Diagnostics.Append(diags...)
return
}
diags = processResponse(resource, input)
resp.Diagnostics.Append(diags...)
// Write to state.
state := NewContentTypeData(resource)
MergeContentType(state, &plan)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
func (r resourceContentType) Read(ctx context.Context, req tfsdk.ReadResourceRequest, resp *tfsdk.ReadResourceResponse) {
var state ContentTypeData
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resource, err := r.p.stack.ContentTypeFetch(ctx, state.UID.Value)
if err != nil {
if IsNotFoundError(err) {
d := diag.NewErrorDiagnostic(
"Error retrieving global field",
fmt.Sprintf("The global field with UID %s was not found.", state.UID.Value))
resp.Diagnostics.Append(d)
} else {
diags := processRemoteError(err)
resp.Diagnostics.Append(diags...)
}
return
}
curr := NewContentTypeInput(&state)
diags = processResponse(resource, curr)
resp.Diagnostics.Append(diags...)
// Set state
newState := NewContentTypeData(resource)
diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
}
func (r resourceContentType) Delete(ctx context.Context, req tfsdk.DeleteResourceRequest, resp *tfsdk.DeleteResourceResponse) {
var state ContentTypeData
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Delete order by calling API
err := r.p.stack.ContentTypeDelete(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 resourceContentType) Update(ctx context.Context, req tfsdk.UpdateResourceRequest, resp *tfsdk.UpdateResourceResponse) {
// Get plan values
var plan ContentTypeData
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get current state
var state ContentTypeData
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
input := NewContentTypeInput(&plan)
resource, err := r.p.stack.ContentTypeUpdate(ctx, state.UID.Value, *input)
if err != nil {
diags = processRemoteError(err)
resp.Diagnostics.Append(diags...)
return
}
diags = processResponse(resource, input)
resp.Diagnostics.Append(diags...)
// Set state
result := NewContentTypeData(resource)
MergeContentType(result, &plan)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
}
func (r resourceContentType) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
tfsdk.ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("id"), req, resp)
}
func NewContentTypeData(field *management.ContentType) *ContentTypeData {
schemaContent, err := field.Schema.MarshalJSON()
if err != nil {
panic(err)
}
state := &ContentTypeData{
UID: types.String{Value: field.UID},
Title: types.String{Value: field.Title},
Description: types.String{Value: field.Description},
Schema: types.String{Value: string(schemaContent)},
}
return state
}
func NewContentTypeInput(field *ContentTypeData) *management.ContentTypeInput {
input := &management.ContentTypeInput{
UID: &field.UID.Value,
Title: &field.Title.Value,
Description: &field.Description.Value,
Schema: json.RawMessage(field.Schema.Value),
}
return input
}
func MergeContentType(out *ContentTypeData, in *ContentTypeData) {
out.Schema = in.Schema
}