/
store_types.go
207 lines (183 loc) · 10.2 KB
/
store_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
// 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 internal
import (
"context"
"net/url"
"path"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/ystia/yorc/v3/helper/consulutil"
"github.com/ystia/yorc/v3/tosca"
)
// TypeExistsFlagName is the name of a Consul key that is used to prove the existence of a TOSCA type
//
// This key doesn't contain any value and allow to detect types even if there is no other values stored for them into consul
const TypeExistsFlagName = ".existFlag"
// StoreAllTypes stores all types of a given topology
func StoreAllTypes(ctx context.Context, consulStore consulutil.ConsulStore, topology tosca.Topology, topologyPrefix, importPath string) error {
storeDataTypes(ctx, consulStore, topology, topologyPrefix, importPath)
if err := storeNodeTypes(ctx, consulStore, topology, topologyPrefix, importPath); err != nil {
return err
}
if err := storeRelationshipTypes(ctx, consulStore, topology, topologyPrefix, importPath); err != nil {
return err
}
storeCapabilityTypes(ctx, consulStore, topology, topologyPrefix, importPath)
storeArtifactTypes(ctx, consulStore, topology, topologyPrefix, importPath)
storePolicyTypes(ctx, consulStore, topology, topologyPrefix, importPath)
return nil
}
func storeCommonType(consulStore consulutil.ConsulStore, commonType tosca.Type, typePrefix, importPath string) {
consulStore.StoreConsulKeyWithFlags(path.Join(typePrefix, TypeExistsFlagName), nil, 0)
// TODO(loicalbertin) Consul mem footprint optimizations: May be empty consider not always storing it, but be careful at least one key is required for now to check if type exist
// may be problematic on normative types
consulStore.StoreConsulKeyAsString(path.Join(typePrefix, "derived_from"), commonType.DerivedFrom)
// TODO(loicalbertin) Consul mem footprint optimizations: May be empty consider not always storing it
consulStore.StoreConsulKeyAsString(path.Join(typePrefix, "importPath"), importPath)
for metaName, metaValue := range commonType.Metadata {
consulStore.StoreConsulKeyAsString(path.Join(typePrefix, "metadata", metaName), metaValue)
}
}
// storeDataTypes store data types
func storeDataTypes(ctx context.Context, consulStore consulutil.ConsulStore, topology tosca.Topology, topologyPrefix, importPath string) error {
dataTypesPrefix := path.Join(topologyPrefix, "types")
for dataTypeName, dataType := range topology.DataTypes {
dtPrefix := path.Join(dataTypesPrefix, dataTypeName)
storeCommonType(consulStore, dataType.Type, dtPrefix, importPath)
for propName, propDefinition := range dataType.Properties {
storePropertyDefinition(ctx, consulStore, path.Join(dtPrefix, "properties", propName), propName, propDefinition)
}
}
return nil
}
// storeNodeTypes stores topology types
func storeNodeTypes(ctx context.Context, consulStore consulutil.ConsulStore, topology tosca.Topology, topologyPrefix, importPath string) error {
typesPrefix := path.Join(topologyPrefix, "types")
for nodeTypeName, nodeType := range topology.NodeTypes {
nodeTypePrefix := typesPrefix + "/" + nodeTypeName
storeCommonType(consulStore, nodeType.Type, nodeTypePrefix, importPath)
propertiesPrefix := nodeTypePrefix + "/properties"
for propName, propDefinition := range nodeType.Properties {
propPrefix := propertiesPrefix + "/" + propName
storePropertyDefinition(ctx, consulStore, propPrefix, propName, propDefinition)
}
storeRequirementDefinitionMap(consulStore, nodeType.Requirements, path.Join(nodeTypePrefix, "requirements"))
capabilitiesPrefix := nodeTypePrefix + "/capabilities"
for capName, capability := range nodeType.Capabilities {
capabilityPrefix := capabilitiesPrefix + "/" + capName
consulStore.StoreConsulKeyAsString(capabilityPrefix+"/type", capability.Type)
consulStore.StoreConsulKeyAsString(capabilityPrefix+"/occurrences/lower_bound", strconv.FormatUint(capability.Occurrences.LowerBound, 10))
consulStore.StoreConsulKeyAsString(capabilityPrefix+"/occurrences/upper_bound", strconv.FormatUint(capability.Occurrences.UpperBound, 10))
consulStore.StoreConsulKeyAsString(capabilityPrefix+"/valid_sources", strings.Join(capability.ValidSourceTypes, ","))
storeMapValueAssignment(consulStore, path.Join(capabilityPrefix, "properties"), capability.Properties)
storeMapValueAssignment(consulStore, path.Join(capabilityPrefix, "attributes"), capability.Attributes)
}
err := storeInterfaces(consulStore, nodeType.Interfaces, nodeTypePrefix, false)
if err != nil {
return err
}
attributesPrefix := nodeTypePrefix + "/attributes"
for attrName, attrDefinition := range nodeType.Attributes {
attrPrefix := attributesPrefix + "/" + attrName
storeAttributeDefinition(ctx, consulStore, attrPrefix, attrName, attrDefinition)
if attrDefinition.Default != nil && attrDefinition.Default.Type == tosca.ValueAssignmentFunction {
err := storeOperationOutput(consulStore, attrDefinition.Default, path.Join(nodeTypePrefix, "interfaces"))
if err != nil {
return err
}
}
}
storeArtifacts(consulStore, nodeType.Artifacts, nodeTypePrefix+"/artifacts")
}
return nil
}
// storeRelationshipTypes stores topology relationships types
func storeRelationshipTypes(ctx context.Context, consulStore consulutil.ConsulStore, topology tosca.Topology, topologyPrefix, importPath string) error {
for relationName, relationType := range topology.RelationshipTypes {
relationTypePrefix := path.Join(topologyPrefix, "types", relationName)
storeCommonType(consulStore, relationType.Type, relationTypePrefix, importPath)
propertiesPrefix := relationTypePrefix + "/properties"
for propName, propDefinition := range relationType.Properties {
propPrefix := propertiesPrefix + "/" + propName
storePropertyDefinition(ctx, consulStore, propPrefix, propName, propDefinition)
}
attributesPrefix := relationTypePrefix + "/attributes"
for attrName, attrDefinition := range relationType.Attributes {
attrPrefix := attributesPrefix + "/" + attrName
storeAttributeDefinition(ctx, consulStore, attrPrefix, attrName, attrDefinition)
if attrDefinition.Default != nil && attrDefinition.Default.Type == tosca.ValueAssignmentFunction {
f := attrDefinition.Default.GetFunction()
opOutputFuncs := f.GetFunctionsByOperator(tosca.GetOperationOutputOperator)
for _, oof := range opOutputFuncs {
if len(oof.Operands) != 4 {
return errors.Errorf("Invalid %q TOSCA function: %v", tosca.GetOperationOutputOperator, oof)
}
entityName := url.QueryEscape(oof.Operands[0].String())
interfaceName := strings.ToLower(url.QueryEscape(oof.Operands[1].String()))
operationName := strings.ToLower(url.QueryEscape(oof.Operands[2].String()))
outputVariableName := url.QueryEscape(oof.Operands[3].String())
consulStore.StoreConsulKeyAsString(relationTypePrefix+"/interfaces/"+interfaceName+"/"+operationName+"/outputs/"+entityName+"/"+outputVariableName+"/expression", oof.String())
}
}
}
err := storeInterfaces(consulStore, relationType.Interfaces, relationTypePrefix, true)
if err != nil {
return err
}
storeArtifacts(consulStore, relationType.Artifacts, relationTypePrefix+"/artifacts")
consulStore.StoreConsulKeyAsString(relationTypePrefix+"/valid_target_type", strings.Join(relationType.ValidTargetTypes, ", "))
}
return nil
}
// storeCapabilityTypes stores topology capabilities types
func storeCapabilityTypes(ctx context.Context, consulStore consulutil.ConsulStore, topology tosca.Topology, topologyPrefix, importPath string) {
for capabilityTypeName, capabilityType := range topology.CapabilityTypes {
capabilityTypePrefix := path.Join(topologyPrefix, "types", capabilityTypeName)
storeCommonType(consulStore, capabilityType.Type, capabilityTypePrefix, importPath)
propertiesPrefix := capabilityTypePrefix + "/properties"
for propName, propDefinition := range capabilityType.Properties {
propPrefix := propertiesPrefix + "/" + propName
storePropertyDefinition(ctx, consulStore, propPrefix, propName, propDefinition)
}
attributesPrefix := capabilityTypePrefix + "/attributes"
for attrName, attrDefinition := range capabilityType.Attributes {
attrPrefix := attributesPrefix + "/" + attrName
storeAttributeDefinition(ctx, consulStore, attrPrefix, attrName, attrDefinition)
}
consulStore.StoreConsulKeyAsString(capabilityTypePrefix+"/valid_source_types", strings.Join(capabilityType.ValidSourceTypes, ","))
}
}
// storeArtifactTypes stores topology artifacts types
func storeArtifactTypes(ctx context.Context, consulStore consulutil.ConsulStore, topology tosca.Topology, topologyPrefix, importPath string) {
typesPrefix := path.Join(topologyPrefix, "types")
for artTypeName, artType := range topology.ArtifactTypes {
// TODO(loicalbertin): remove it when migrating to Alien 2.2. Currently alien-base-types has org.alien4cloud.artifacts.AnsiblePlaybook types that do not derives from tosca.artifacts.Implementation
// as with the change on commons types this types is not overridden by builtin types anymore. This is because we first check on deployments types
// then on commons types.
if !strings.HasPrefix(typesPrefix, consulutil.CommonsTypesKVPrefix) && artTypeName == "org.alien4cloud.artifacts.AnsiblePlaybook" {
continue
}
artTypePrefix := path.Join(typesPrefix, artTypeName)
storeCommonType(consulStore, artType.Type, artTypePrefix, importPath)
consulStore.StoreConsulKeyAsString(artTypePrefix+"/mime_type", artType.MimeType)
consulStore.StoreConsulKeyAsString(artTypePrefix+"/file_ext", strings.Join(artType.FileExt, ","))
propertiesPrefix := artTypePrefix + "/properties"
for propName, propDefinition := range artType.Properties {
propPrefix := propertiesPrefix + "/" + propName
storePropertyDefinition(ctx, consulStore, propPrefix, propName, propDefinition)
}
}
}