-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtypes.go
526 lines (466 loc) · 17.2 KB
/
types.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deployments
import (
"context"
"fmt"
"path"
"github.com/pkg/errors"
"github.com/ystia/yorc/v4/deployments/store"
"github.com/ystia/yorc/v4/helper/collections"
"github.com/ystia/yorc/v4/helper/consulutil"
"github.com/ystia/yorc/v4/storage"
"github.com/ystia/yorc/v4/storage/types"
"github.com/ystia/yorc/v4/tosca"
)
type typeMissingError struct {
name string
deploymentID string
}
func (e typeMissingError) Error() string {
return fmt.Sprintf("Looking for a type %q that do not exists in deployment %q.", e.name, e.deploymentID)
}
// IsTypeMissingError checks if the given error is a TypeMissing error
func IsTypeMissingError(err error) bool {
cause := errors.Cause(err)
_, ok := cause.(typeMissingError)
return ok
}
// This allows to get the type base data of a tosca type
// If typePath is not provided, it's retrieved
func getTypeBaseInfo(ctx context.Context, deploymentID, typeName, typePath string) (*tosca.Type, error) {
var err error
if typePath == "" {
typePath, err = locateTypeKey(deploymentID, typeName)
if err != nil {
return nil, err
}
}
tType := new(tosca.Type)
exist, err := storage.GetStore(types.StoreTypeDeployment).Get(typePath, tType)
if err != nil {
return nil, err
}
if !exist {
return nil, typeMissingError{deploymentID: deploymentID, name: typeName}
}
return tType, nil
}
// This allows to set the tType structure from its name
// It requires to know the type base
func getExpectedTypeFromName(ctx context.Context, deploymentID, typeName string, tType interface{}) error {
typePath, err := locateTypeKey(deploymentID, typeName)
if err != nil {
return err
}
return getExpectedTypeFromKey(ctx, deploymentID, typeName, typePath, tType)
}
// This allows to set the tType structure from its name and key path
// It requires to know the type base
func getExpectedTypeFromKey(ctx context.Context, deploymentID, typeName, key string, tType interface{}) error {
exist, err := storage.GetStore(types.StoreTypeDeployment).Get(key, tType)
if err != nil {
return err
}
if !exist {
return typeMissingError{deploymentID: deploymentID, name: typeName}
}
return checkTypeIsExpected(typeName, tType)
}
// This allows to define if the type is of the expecting one
func checkTypeIsExpected(typeName string, tType interface{}) error {
var actualType, expectedType tosca.TypeBase
switch t := tType.(type) {
case *tosca.NodeType:
actualType = t.Base
expectedType = tosca.TypeBaseNODE
case *tosca.RelationshipType:
actualType = t.Base
expectedType = tosca.TypeBaseRELATIONSHIP
case *tosca.PolicyType:
actualType = t.Base
expectedType = tosca.TypeBasePOLICY
case *tosca.CapabilityType:
actualType = t.Base
expectedType = tosca.TypeBaseCAPABILITY
case *tosca.DataType:
actualType = t.Base
expectedType = tosca.TypeBaseDATA
case *tosca.ArtifactType:
actualType = t.Base
expectedType = tosca.TypeBaseARTIFACT
}
if actualType != expectedType {
return errors.Errorf("The type %q is not of expecting type %q, but of type %q", typeName, expectedType, actualType)
}
return nil
}
// This allows to return the type structure from its type name without information on its type
// It returns the type path in second position
func getTypeFromName(ctx context.Context, deploymentID, typeName string) (interface{}, string, error) {
typePath, err := locateTypeKey(deploymentID, typeName)
if err != nil {
return nil, "", err
}
// Retrieve type of the type (i.e "node", "relationship", "policy"...) to get the related struct
typeBase, err := getTypeBaseInfo(ctx, deploymentID, typeName, typePath)
if err != nil {
return nil, "", err
}
var tType interface{}
switch typeBase.Base {
case tosca.TypeBaseNODE:
tType = new(tosca.NodeType)
case tosca.TypeBaseCAPABILITY:
tType = new(tosca.CapabilityType)
case tosca.TypeBaseRELATIONSHIP:
tType = new(tosca.RelationshipType)
case tosca.TypeBaseARTIFACT:
tType = new(tosca.ArtifactType)
case tosca.TypeBasePOLICY:
tType = new(tosca.PolicyType)
case tosca.TypeBaseDATA:
tType = new(tosca.DataType)
default:
return nil, "", errors.Errorf("Unknown type:%d", typeBase.Base)
}
err = getExpectedTypeFromKey(ctx, deploymentID, typeName, typePath, tType)
if err != nil {
return nil, "", err
}
return tType, typePath, nil
}
func getTypePropertyDefinitions(ctx context.Context, deploymentID, typeName string) (map[string]tosca.PropertyDefinition, error) {
tType, _, err := getTypeFromName(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
var mapProps map[string]tosca.PropertyDefinition
switch t := tType.(type) {
case *tosca.NodeType:
mapProps = t.Properties
case *tosca.RelationshipType:
mapProps = t.Properties
case *tosca.CapabilityType:
mapProps = t.Properties
case *tosca.DataType:
mapProps = t.Properties
case *tosca.ArtifactType:
mapProps = t.Properties
case *tosca.PolicyType:
mapProps = t.Properties
}
return mapProps, nil
}
func getTypeAttributeDefinitions(ctx context.Context, deploymentID, typeName string) (map[string]tosca.AttributeDefinition, error) {
tType, _, err := getTypeFromName(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
var mapAttrs map[string]tosca.AttributeDefinition
switch t := tType.(type) {
case *tosca.NodeType:
mapAttrs = t.Attributes
case *tosca.RelationshipType:
mapAttrs = t.Attributes
case *tosca.CapabilityType:
mapAttrs = t.Attributes
}
return mapAttrs, nil
}
func getTypeInterfaces(ctx context.Context, deploymentID, typeName string) (map[string]tosca.InterfaceDefinition, error) {
tType, _, err := getTypeFromName(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
var interfaces map[string]tosca.InterfaceDefinition
switch t := tType.(type) {
case *tosca.NodeType:
interfaces = t.Interfaces
case *tosca.RelationshipType:
interfaces = t.Interfaces
}
return interfaces, nil
}
func getTypeArtifacts(ctx context.Context, deploymentID, typeName string) (tosca.ArtifactDefMap, error) {
tType, _, err := getTypeFromName(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
var artifacts tosca.ArtifactDefMap
switch t := tType.(type) {
case *tosca.NodeType:
artifacts = t.Artifacts
case *tosca.RelationshipType:
artifacts = t.Artifacts
}
return artifacts, nil
}
func checkIfTypeExists(typePath string) (bool, error) {
return storage.GetStore(types.StoreTypeDeployment).Exist(typePath)
}
func locateTypeKey(deploymentID, typeName string) (string, error) {
// First check for type in deployment
typeKey := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology/types", typeName)
// Check if node type exist
exist, err := checkIfTypeExists(typeKey)
if err != nil {
return "", err
}
if exist {
return typeKey, nil
}
builtinTypesPaths := store.GetCommonsTypesKeyPaths()
for i := range builtinTypesPaths {
builtinTypesPaths[i] = path.Join(builtinTypesPaths[i], "types", typeName)
exist, err := checkIfTypeExists(builtinTypesPaths[i])
if err != nil {
return "", err
}
if exist {
return builtinTypesPaths[i], nil
}
}
return "", errors.WithStack(typeMissingError{name: typeName, deploymentID: deploymentID})
}
// GetParentType returns the direct parent type of a given type using the 'derived_from' attributes
//
// An empty string denotes a root type
func GetParentType(ctx context.Context, deploymentID, typeName string) (string, error) {
if tosca.IsBuiltinType(typeName) {
return "", nil
}
typ, err := getTypeBaseInfo(ctx, deploymentID, typeName, "")
if err != nil {
return "", err
}
return typ.DerivedFrom, nil
}
// IsTypeDerivedFrom traverses 'derived_from' to check if type derives from another type
func IsTypeDerivedFrom(ctx context.Context, deploymentID, nodeType, derives string) (bool, error) {
if nodeType == derives {
return true, nil
}
parent, err := GetParentType(ctx, deploymentID, nodeType)
if err != nil || parent == "" {
return false, err
}
return IsTypeDerivedFrom(ctx, deploymentID, parent, derives)
}
// GetTypesNames returns the names of the different types for a given deployment.
func GetTypesNames(ctx context.Context, deploymentID string) ([]string, error) {
names := make([]string, 0)
typs, err := storage.GetStore(types.StoreTypeDeployment).Keys(path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology/types"))
if err != nil {
return names, errors.Wrap(err, consulutil.ConsulGenericErrMsg)
}
for _, t := range typs {
names = append(names, path.Base(t))
}
builtinTypesPaths := store.GetCommonsTypesKeyPaths()
for i := range builtinTypesPaths {
builtinTypesPaths[i] = path.Join(builtinTypesPaths[i], "types")
}
for _, builtinTypesPath := range builtinTypesPaths {
typs, err := storage.GetStore(types.StoreTypeDeployment).Keys(builtinTypesPath)
if err != nil {
return names, errors.Wrap(err, consulutil.ConsulGenericErrMsg)
}
for _, t := range typs {
names = append(names, path.Base(t))
}
}
return names, nil
}
// GetTypeProperties returns the list of properties defined for a given type nam of the specified type tType
// tType can be "node", "relationship", "capability", "artifact", "data", policy"
// It lists only properties defined in the given type not in its parent types.
func GetTypeProperties(ctx context.Context, deploymentID, typeName string, exploreParents bool) ([]string, error) {
return getTypeAttributesOrProperties(ctx, deploymentID, typeName, "properties", exploreParents)
}
// GetTypeAttributes returns the list of attributes defined for a given type name of the specified type tType
// tType can be "node", "relationship", "capability"
// It lists only attributes defined in the given type not in its parent types.
func GetTypeAttributes(ctx context.Context, deploymentID, typeName string, exploreParents bool) ([]string, error) {
return getTypeAttributesOrProperties(ctx, deploymentID, typeName, "attributes", exploreParents)
}
func getTypeAttributesOrProperties(ctx context.Context, deploymentID, typeName, paramType string, exploreParents bool) ([]string, error) {
if tosca.IsBuiltinType(typeName) {
return nil, nil
}
results := make([]string, 0)
if paramType == "properties" {
mapProps, err := getTypePropertyDefinitions(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
for k := range mapProps {
results = append(results, k)
}
} else {
mapAttrs, err := getTypeAttributeDefinitions(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
for k := range mapAttrs {
results = append(results, k)
}
}
if exploreParents {
parent, err := GetParentType(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
// Check parent
if parent != "" {
pResults, err := getTypeAttributesOrProperties(ctx, deploymentID, parent, paramType, exploreParents)
if err != nil {
return nil, err
}
results = append(results, pResults...)
}
}
return results, nil
}
// TypeHasProperty returns true if the type has a property named propertyName defined
// exploreParents switch enable property check on parent types
func TypeHasProperty(ctx context.Context, deploymentID, typeName, propertyName string, exploreParents bool) (bool, error) {
props, err := GetTypeProperties(ctx, deploymentID, typeName, exploreParents)
if err != nil {
return false, err
}
return collections.ContainsString(props, propertyName), nil
}
// TypeHasAttribute returns true if the type has a attribute named attributeName defined
// exploreParents switch enable attribute check on parent types
func TypeHasAttribute(ctx context.Context, deploymentID, typeName, attributeName string, exploreParents bool) (bool, error) {
attrs, err := GetTypeAttributes(ctx, deploymentID, typeName, exploreParents)
if err != nil {
return false, err
}
return collections.ContainsString(attrs, attributeName), nil
}
// getTypeDefaultProperty checks if a type has a default value for a given property.
// It returns true if a default value is found false otherwise as first return parameter.
// If no default value is found in a given type then the derived_from hierarchy is explored to find the default value.
// The second boolean result indicates if the result is a TOSCA Function that should be evaluated in the caller context.
func getTypeDefaultProperty(ctx context.Context, deploymentID, typeName, propertyName string, nestedKeys ...string) (*TOSCAValue, bool, error) {
return getTypeDefaultAttributeOrProperty(ctx, deploymentID, typeName, propertyName, true, nestedKeys...)
}
// getTypeDefaultAttribute checks if a node type has a default value for a given attribute.
// It returns true if a default value is found false otherwise as first return parameter.
// If no default value is found in a given type then the derived_from hierarchy is explored to find the default value.
// The second boolean result indicates if the result is a TOSCA Function that should be evaluated in the caller context.
func getTypeDefaultAttribute(ctx context.Context, deploymentID, typeName, attributeName string, nestedKeys ...string) (*TOSCAValue, bool, error) {
return getTypeDefaultAttributeOrProperty(ctx, deploymentID, typeName, attributeName, false, nestedKeys...)
}
// getTypeDefaultProperty checks if a type has a default value for a given property or attribute.
// It returns true if a default value is found false otherwise as first return parameter.
// If no default value is found in a given type then the derived_from hierarchy is explored to find the default value.
// The second boolean result indicates if the result is a TOSCA Function that should be evaluated in the caller context.
func getTypeDefaultAttributeOrProperty(ctx context.Context, deploymentID, typeName, propertyName string, isProperty bool, nestedKeys ...string) (*TOSCAValue, bool, error) {
var vaDef *tosca.ValueAssignment
if isProperty {
def, err := getTypePropertyDefinition(ctx, deploymentID, typeName, propertyName)
if err != nil {
return nil, false, err
}
if def != nil {
vaDef = def.Default
}
} else {
def, err := getTypeAttributeDefinition(ctx, deploymentID, typeName, propertyName)
if err != nil {
return nil, false, err
}
if def != nil {
vaDef = def.Default
}
}
baseDataType, err := getTypePropertyOrAttributeDataType(ctx, deploymentID, typeName, propertyName, isProperty)
if err != nil {
return nil, false, err
}
return getValueAssignmentWithoutResolve(ctx, deploymentID, vaDef, baseDataType, nestedKeys...)
}
// IsTypePropertyRequired checks if a property defined in a given type is required.
//
// As per the TOSCA specification a property is considered as required by default.
// An error is returned if the given type doesn't define the given property.
func IsTypePropertyRequired(ctx context.Context, deploymentID, typeName, propertyName string) (bool, error) {
return isTypePropertyRequired(ctx, deploymentID, typeName, propertyName)
}
func isTypePropertyRequired(ctx context.Context, deploymentID, typeName, propertyName string) (bool, error) {
// Required is true by default
required := true
def, err := getTypePropertyDefinition(ctx, deploymentID, typeName, propertyName)
if err != nil {
return false, err
}
if def != nil && def.Required != nil {
required = *def.Required
}
return required, nil
}
func getTypePropertyDefinition(ctx context.Context, deploymentID, typeName, propertyName string) (*tosca.PropertyDefinition, error) {
mapProps, err := getTypePropertyDefinitions(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
propDef, is := mapProps[propertyName]
if is {
return &propDef, nil
}
// Check parent
parent, err := GetParentType(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
if parent != "" {
return getTypePropertyDefinition(ctx, deploymentID, parent, propertyName)
}
// Not found
return nil, nil
}
func getTypeAttributeDefinition(ctx context.Context, deploymentID, typeName, attributeName string) (*tosca.AttributeDefinition, error) {
mapAttrs, err := getTypeAttributeDefinitions(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
attrDef, is := mapAttrs[attributeName]
if is {
return &attrDef, nil
}
// Check parent
parent, err := GetParentType(ctx, deploymentID, typeName)
if err != nil {
return nil, err
}
if parent != "" {
return getTypeAttributeDefinition(ctx, deploymentID, parent, attributeName)
}
// Not found
return nil, nil
}
// GetTypeImportPath returns the import path relative to the root of a CSAR of a given TOSCA type.
//
// This is particularly useful for resolving artifacts and implementation
func GetTypeImportPath(ctx context.Context, deploymentID, typeName string) (string, error) {
tType, err := getTypeBaseInfo(ctx, deploymentID, typeName, "")
if err != nil {
return "", err
}
// Can be empty if type is defined into the root topology
return tType.ImportPath, nil
}