-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpolicies.go
222 lines (202 loc) · 6.8 KB
/
policies.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
// 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"
"github.com/pkg/errors"
"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"
"path"
)
func getPolicyType(ctx context.Context, deploymentID, policyTypeName string) (*tosca.PolicyType, error) {
policyTyp := new(tosca.PolicyType)
err := getExpectedTypeFromName(ctx, deploymentID, policyTypeName, policyTyp)
if err != nil {
return nil, err
}
return policyTyp, nil
}
func getPolicy(ctx context.Context, deploymentID, policyName string) (*tosca.Policy, error) {
key := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "policies", policyName)
policy := new(tosca.Policy)
exist, err := storage.GetStore(types.StoreTypeDeployment).Get(key, policy)
if err != nil {
return nil, err
}
if !exist {
return nil, errors.Errorf("No policy with name %q found", policyName)
}
return policy, nil
}
// GetPoliciesForType retrieves all policies with or derived from policyTypeName
func GetPoliciesForType(ctx context.Context, deploymentID, policyTypeName string) ([]string, error) {
p := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "policies")
keys, err := storage.GetStore(types.StoreTypeDeployment).Keys(p)
if err != nil {
return nil, errors.Wrap(err, consulutil.ConsulGenericErrMsg)
}
policies := make([]string, 0)
for _, key := range keys {
policyName := path.Base(key)
policy, err := getPolicy(ctx, deploymentID, policyName)
if err != nil {
return nil, err
}
if policy.Type == "" {
return nil, errors.Errorf("Missing mandatory attribute \"type\" for policy %q", path.Base(key))
}
// Check policy type
isType, err := IsTypeDerivedFrom(ctx, deploymentID, policy.Type, policyTypeName)
if err != nil {
return nil, err
}
// Check policy targets
if isType {
policies = append(policies, policyName)
}
}
return policies, nil
}
// GetPoliciesForTypeAndNode retrieves all policies with or derived from policyTypeName and with nodeName as target
func GetPoliciesForTypeAndNode(ctx context.Context, deploymentID, policyTypeName, nodeName string) ([]string, error) {
policiesForType, err := GetPoliciesForType(ctx, deploymentID, policyTypeName)
if err != nil {
return nil, err
}
policies := make([]string, 0)
for _, policy := range policiesForType {
is, err := IsTargetForPolicy(ctx, deploymentID, policy, nodeName, false)
if err != nil {
return nil, err
}
if is {
policies = append(policies, policy)
}
}
return policies, nil
}
// GetPolicyPropertyValue retrieves the value for a given property in a given policy
//
// It returns true if a value is found false otherwise as first return parameter.
// If the property is not found in the policy then the type hierarchy is explored to find a default value.
func GetPolicyPropertyValue(ctx context.Context, deploymentID, policyName, propertyName string, nestedKeys ...string) (*TOSCAValue, error) {
policy, err := getPolicy(ctx, deploymentID, policyName)
if err != nil {
return nil, err
}
var propDataType string
hasProp, err := TypeHasProperty(ctx, deploymentID, policy.Type, propertyName, true)
if err != nil {
return nil, err
}
if hasProp {
propDataType, err = GetTypePropertyDataType(ctx, deploymentID, policy.Type, propertyName)
if err != nil {
return nil, err
}
}
// Check at policy template level
var va *tosca.ValueAssignment
if policy.Properties != nil {
va = policy.Properties[propertyName]
}
value, err := getValueAssignment(ctx, deploymentID, policyName, "", "", propDataType, va, nestedKeys...)
if err != nil || value != nil {
return value, err
}
// Retrieve related propertyDefinition with default property
propDef, err := getTypePropertyDefinition(ctx, deploymentID, policy.Type, propertyName)
if err != nil {
return nil, err
}
if propDef != nil {
return getValueAssignment(ctx, deploymentID, policyName, "", "", propDataType, propDef.Default, nestedKeys...)
}
// Not found
return nil, nil
}
// GetPolicyType returns the type of the policy
func GetPolicyType(ctx context.Context, deploymentID, policyName string) (string, error) {
policy, err := getPolicy(ctx, deploymentID, policyName)
if err != nil {
return "", err
}
if policy.Type == "" {
return "", errors.Errorf("Missing mandatory attribute \"type\" for policy %q", policyName)
}
return policy.Type, nil
}
// IsTargetForPolicy returns true if the node name is a policy target
func IsTargetForPolicy(ctx context.Context, deploymentID, policyName, nodeName string, recursive bool) (bool, error) {
targets, err := GetPolicyTargets(ctx, deploymentID, policyName)
if err != nil {
return false, err
}
if !recursive {
return collections.ContainsString(targets, nodeName), nil
}
nodeType, err := GetNodeType(ctx, deploymentID, nodeName)
if err != nil {
return false, err
}
policyType, err := GetPolicyType(ctx, deploymentID, policyName)
if err != nil {
return false, err
}
targets, err = GetPolicyTargetsForType(ctx, deploymentID, policyType)
if err != nil {
return false, err
}
for _, target := range targets {
is, err := IsTypeDerivedFrom(ctx, deploymentID, nodeType, target)
if err != nil {
return false, err
}
if is {
return true, nil
}
}
return false, nil
}
// GetPolicyTargets retrieves the policy template targets
// these targets are node names
func GetPolicyTargets(ctx context.Context, deploymentID, policyName string) ([]string, error) {
policy, err := getPolicy(ctx, deploymentID, policyName)
if err != nil {
return nil, err
}
return policy.Targets, nil
}
// GetPolicyTargetsForType retrieves the policy type targets
// this targets are node types
func GetPolicyTargetsForType(ctx context.Context, deploymentID, policyTypeName string) ([]string, error) {
policyType, err := getPolicyType(ctx, deploymentID, policyTypeName)
if err != nil {
return nil, err
}
if policyType.Targets != nil {
return policyType.Targets, nil
}
parentType, err := GetParentType(ctx, deploymentID, policyTypeName)
if err != nil {
return nil, err
}
if parentType == "" {
return nil, nil
}
return GetPolicyTargetsForType(ctx, deploymentID, parentType)
}