-
Notifications
You must be signed in to change notification settings - Fork 85
/
converter.go
340 lines (293 loc) · 11.5 KB
/
converter.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package apptemplate
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"strings"
"k8s.io/client-go/util/jsonpath"
"github.com/kyma-incubator/compass/components/director/internal/model"
"github.com/kyma-incubator/compass/components/director/internal/repo"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/kyma-incubator/compass/components/director/pkg/graphql"
"github.com/kyma-incubator/compass/components/director/pkg/graphql/graphqlizer"
"github.com/pkg/errors"
)
// AppConverter missing godoc
//
//go:generate mockery --name=AppConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
type AppConverter interface {
CreateJSONInputGQLToJSON(in *graphql.ApplicationJSONInput) (string, error)
}
type converter struct {
appConverter AppConverter
webhookConverter WebhookConverter
}
// NewConverter missing godoc
func NewConverter(appConverter AppConverter, webhookConverter WebhookConverter) *converter {
return &converter{appConverter: appConverter, webhookConverter: webhookConverter}
}
// ToGraphQL missing godoc
func (c *converter) ToGraphQL(in *model.ApplicationTemplate) (*graphql.ApplicationTemplate, error) {
if in == nil {
return nil, nil
}
if in.ApplicationInputJSON == "" {
return nil, apperrors.NewInternalError("application input is empty")
}
gqlAppInput, err := c.graphqliseApplicationCreateInput(in.ApplicationInputJSON)
if err != nil {
return nil, errors.Wrapf(err, "while graphqlising application create input")
}
webhooks, err := c.webhooksToGraphql(in.Webhooks)
if err != nil {
return nil, err
}
return &graphql.ApplicationTemplate{
ID: in.ID,
Name: in.Name,
Description: in.Description,
ApplicationNamespace: in.ApplicationNamespace,
Webhooks: webhooks,
ApplicationInput: gqlAppInput,
Placeholders: c.placeholdersToGraphql(in.Placeholders),
AccessLevel: graphql.ApplicationTemplateAccessLevel(in.AccessLevel),
CreatedAt: graphql.Timestamp(in.CreatedAt),
UpdatedAt: graphql.Timestamp(in.UpdatedAt),
}, nil
}
// MultipleToGraphQL missing godoc
func (c *converter) MultipleToGraphQL(in []*model.ApplicationTemplate) ([]*graphql.ApplicationTemplate, error) {
appTemplates := make([]*graphql.ApplicationTemplate, 0, len(in))
for _, r := range in {
if r == nil {
continue
}
appTemplate, err := c.ToGraphQL(r)
if err != nil {
return nil, errors.Wrapf(err, "while converting application template")
}
appTemplates = append(appTemplates, appTemplate)
}
return appTemplates, nil
}
// InputFromGraphQL missing godoc
func (c *converter) InputFromGraphQL(in graphql.ApplicationTemplateInput) (model.ApplicationTemplateInput, error) {
var appCreateInput string
var err error
if in.ApplicationInput != nil {
appCreateInput, err = c.appConverter.CreateJSONInputGQLToJSON(in.ApplicationInput)
if err != nil {
return model.ApplicationTemplateInput{}, errors.Wrapf(err, "error occurred while converting GraphQL input to Application Template model with name %s", in.Name)
}
}
webhooks, err := c.webhookConverter.MultipleInputFromGraphQL(in.Webhooks)
if err != nil {
return model.ApplicationTemplateInput{}, errors.Wrapf(err, "error occurred while converting webhooks og GraphQL input to Application Template model with name %s", in.Name)
}
var labels map[string]interface{}
if in.Labels != nil {
labels = in.Labels
}
return model.ApplicationTemplateInput{
Name: in.Name,
Description: in.Description,
ApplicationNamespace: in.ApplicationNamespace,
ApplicationInputJSON: appCreateInput,
Placeholders: c.placeholdersFromGraphql(in.Placeholders),
AccessLevel: model.ApplicationTemplateAccessLevel(in.AccessLevel),
Labels: labels,
Webhooks: webhooks,
}, nil
}
// UpdateInputFromGraphQL missing godoc
func (c *converter) UpdateInputFromGraphQL(in graphql.ApplicationTemplateUpdateInput) (model.ApplicationTemplateUpdateInput, error) {
var appCreateInput string
var err error
if in.ApplicationInput != nil {
appCreateInput, err = c.appConverter.CreateJSONInputGQLToJSON(in.ApplicationInput)
if err != nil {
return model.ApplicationTemplateUpdateInput{}, errors.Wrapf(err, "error occurred while converting GraphQL update input to Application Template model with name %s", in.Name)
}
}
webhooks, err := c.webhookConverter.MultipleInputFromGraphQL(in.Webhooks)
if err != nil {
return model.ApplicationTemplateUpdateInput{}, errors.Wrapf(err, "error occurred while converting webhooks og GraphQL input to Application Template model with name %s", in.Name)
}
return model.ApplicationTemplateUpdateInput{
Name: in.Name,
Description: in.Description,
ApplicationNamespace: in.ApplicationNamespace,
ApplicationInputJSON: appCreateInput,
Placeholders: c.placeholdersFromGraphql(in.Placeholders),
AccessLevel: model.ApplicationTemplateAccessLevel(in.AccessLevel),
Labels: in.Labels,
Webhooks: webhooks,
}, nil
}
// ApplicationFromTemplateInputFromGraphQL missing godoc
func (c *converter) ApplicationFromTemplateInputFromGraphQL(appTemplate *model.ApplicationTemplate, in graphql.ApplicationFromTemplateInput) (model.ApplicationFromTemplateInput, error) {
values := make([]*model.ApplicationTemplateValueInput, 0, len(in.Values))
if (in.PlaceholdersPayload != nil) && (len(in.Values) == 0) {
var obj interface{}
if err := json.Unmarshal([]byte(*in.PlaceholdersPayload), &obj); err != nil {
return model.ApplicationFromTemplateInput{}, err
}
parser := jsonpath.New("parser")
for _, placeholder := range appTemplate.Placeholders {
if placeholder.JSONPath != nil && len(*placeholder.JSONPath) > 0 {
if err := parser.Parse(fmt.Sprintf("{%s}", *placeholder.JSONPath)); err != nil {
return model.ApplicationFromTemplateInput{}, errors.Wrapf(err, "while parsing placeholder JSON path: %s", *placeholder.JSONPath)
}
buf := new(bytes.Buffer)
if err := parser.Execute(buf, obj); err != nil {
return model.ApplicationFromTemplateInput{}, errors.Wrapf(err, "while converting GraphQL input to Application From Template model. Value for Placeholder with name %s and path %s, not found in payload %s.", placeholder.Name, *placeholder.JSONPath, *in.PlaceholdersPayload)
}
valueInput := model.ApplicationTemplateValueInput{
Placeholder: placeholder.Name,
Value: buf.String(),
}
values = append(values, &valueInput)
} else {
return model.ApplicationFromTemplateInput{}, errors.Errorf("error occurred while converting GraphQL input to Application From Template model. Placeholder with name %s, do not have the JSONPath.", placeholder.Name)
}
}
} else {
for _, value := range in.Values {
valueInput := model.ApplicationTemplateValueInput{
Placeholder: value.Placeholder,
Value: value.Value,
}
values = append(values, &valueInput)
}
}
var labels map[string]interface{}
if in.Labels != nil {
labels = in.Labels
}
return model.ApplicationFromTemplateInput{
TemplateName: in.TemplateName,
Values: values,
Labels: labels,
}, nil
}
// ToEntity missing godoc
func (c *converter) ToEntity(in *model.ApplicationTemplate) (*Entity, error) {
if in == nil {
return nil, nil
}
placeholders, err := c.placeholdersModelToJSON(in.Placeholders)
if err != nil {
return nil, errors.Wrap(err, "while converting placeholders from model to JSON")
}
return &Entity{
ID: in.ID,
Name: in.Name,
Description: repo.NewNullableString(in.Description),
ApplicationNamespace: repo.NewNullableString(in.ApplicationNamespace),
ApplicationInputJSON: in.ApplicationInputJSON,
PlaceholdersJSON: placeholders,
AccessLevel: string(in.AccessLevel),
CreatedAt: in.CreatedAt,
UpdatedAt: in.UpdatedAt,
}, nil
}
// FromEntity missing godoc
func (c *converter) FromEntity(entity *Entity) (*model.ApplicationTemplate, error) {
if entity == nil {
return nil, nil
}
placeholders, err := c.placeholdersJSONToModel(entity.PlaceholdersJSON)
if err != nil {
return nil, errors.Wrap(err, "while converting placeholders from JSON to model")
}
return &model.ApplicationTemplate{
ID: entity.ID,
Name: entity.Name,
Description: repo.StringPtrFromNullableString(entity.Description),
ApplicationNamespace: repo.StringPtrFromNullableString(entity.ApplicationNamespace),
ApplicationInputJSON: entity.ApplicationInputJSON,
Placeholders: placeholders,
AccessLevel: model.ApplicationTemplateAccessLevel(entity.AccessLevel),
CreatedAt: entity.CreatedAt,
UpdatedAt: entity.UpdatedAt,
}, nil
}
func (c *converter) graphqliseApplicationCreateInput(jsonAppInput string) (string, error) {
var gqlAppCreateInput graphql.ApplicationRegisterInput
err := json.Unmarshal([]byte(jsonAppInput), &gqlAppCreateInput)
if err != nil {
return "", errors.Wrap(err, "while unmarshaling application create input")
}
g := graphqlizer.Graphqlizer{}
gqlAppInput, err := g.ApplicationRegisterInputToGQL(gqlAppCreateInput)
if err != nil {
return "", errors.Wrap(err, "while graphqlising application create input")
}
gqlAppInput = strings.ReplaceAll(gqlAppInput, "\t", "")
gqlAppInput = strings.ReplaceAll(gqlAppInput, "\n", "")
return gqlAppInput, nil
}
func (c *converter) placeholdersJSONToModel(in sql.NullString) ([]model.ApplicationTemplatePlaceholder, error) {
if !in.Valid || in.String == "" {
return nil, nil
}
var placeholders []model.ApplicationTemplatePlaceholder
err := json.Unmarshal([]byte(in.String), &placeholders)
if err != nil {
return nil, err
}
return placeholders, nil
}
func (c *converter) placeholdersModelToJSON(in []model.ApplicationTemplatePlaceholder) (sql.NullString, error) {
result := sql.NullString{}
if in == nil {
return result, nil
}
placeholdersMarshalled, err := json.Marshal(in)
if err != nil {
return result, errors.Wrap(err, "while marshalling placeholders")
}
return repo.NewValidNullableString(string(placeholdersMarshalled)), nil
}
func (c *converter) placeholdersFromGraphql(in []*graphql.PlaceholderDefinitionInput) []model.ApplicationTemplatePlaceholder {
placeholders := make([]model.ApplicationTemplatePlaceholder, 0, len(in))
for _, p := range in {
np := model.ApplicationTemplatePlaceholder{
Name: p.Name,
Description: p.Description,
JSONPath: p.JSONPath,
Optional: p.Optional,
}
placeholders = append(placeholders, np)
}
return placeholders
}
func (c *converter) placeholdersToGraphql(in []model.ApplicationTemplatePlaceholder) []*graphql.PlaceholderDefinition {
placeholders := make([]*graphql.PlaceholderDefinition, 0, len(in))
for _, p := range in {
np := graphql.PlaceholderDefinition{
Name: p.Name,
Description: p.Description,
JSONPath: p.JSONPath,
Optional: p.Optional,
}
placeholders = append(placeholders, &np)
}
return placeholders
}
func (c *converter) webhooksToGraphql(in []model.Webhook) ([]graphql.Webhook, error) {
webhookPtrs := make([]*model.Webhook, len(in))
for i := 0; i < len(in); i++ {
webhookPtrs[i] = &in[i]
}
gqlWebhookPtrs, err := c.webhookConverter.MultipleToGraphQL(webhookPtrs)
if err != nil {
return nil, errors.Wrap(err, "while converting webhooks to graphql")
}
gqlWebhooks := make([]graphql.Webhook, len(gqlWebhookPtrs))
for i := 0; i < len(gqlWebhookPtrs); i++ {
gqlWebhooks[i] = *gqlWebhookPtrs[i]
}
return gqlWebhooks, nil
}