forked from open-horizon/anax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_management_policy.go
156 lines (132 loc) · 5.34 KB
/
node_management_policy.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
package exchangecommon
import (
"fmt"
"github.com/open-horizon/anax/externalpolicy"
"github.com/open-horizon/anax/i18n"
"strings"
"time"
)
// Node management policy as represented in the exchange
type ExchangeNodeManagementPolicy struct {
Owner string `json:"owner,omitempty"`
Label string `json:"label"`
Description string `json:"description"`
Constraints externalpolicy.ConstraintExpression `json:"constraints"`
Properties externalpolicy.PropertyList `json:"properties"`
Patterns []string `json:"patterns"`
Enabled bool `json:"enabled"`
PolicyUpgradeTime string `json:"start"`
UpgradeWindowDuration int `json:"startWindow"`
AgentAutoUpgradePolicy *ExchangeAgentUpgradePolicy `json:"agentUpgradePolicy,omitempty"`
AgentImagePolicy *ExchangeAgentImagePolicy `json:"agentImagePolicy,omitempty"`
LastUpdated string `json:"lastUpdated,omitempty"`
Created string `json:"created,omitempty"`
}
func (e ExchangeNodeManagementPolicy) String() string {
return fmt.Sprintf("Owner: %v, Label: %v, Description: %v, Properties: %v, Constraints: %v, Patterns: %v, Enabled: %v, PolicyUpgradeTime: %v, UpgradeWindowDuration: %v AgentAutoUpgradePolicy: %v, LastUpdated: %v, Created: %v",
e.Owner, e.Label, e.Description,
e.Properties, e.Constraints, e.Patterns,
e.Enabled, e.PolicyUpgradeTime, e.UpgradeWindowDuration, e.AgentAutoUpgradePolicy, e.LastUpdated, e.Created)
}
func (e *ExchangeNodeManagementPolicy) Validate() error {
// get message printer
msgPrinter := i18n.GetMessagePrinter()
// Validate the timestamp
if e.PolicyUpgradeTime != "now" {
if _, err := time.Parse(time.RFC3339, e.PolicyUpgradeTime); err != nil {
return fmt.Errorf(msgPrinter.Sprintf("The start time must be in RFC3339 format or set to \"now\"."))
}
}
// Validate the PropertyList.
if e != nil && len(e.Properties) != 0 {
if err := e.Properties.Validate(); err != nil {
return fmt.Errorf(msgPrinter.Sprintf("properties contains an invalid property: %v", err))
}
}
if e.Properties.HasProperty(externalpolicy.PROP_SVC_PRIVILEGED) {
privProp, _ := e.Properties.GetProperty(externalpolicy.PROP_SVC_PRIVILEGED)
if _, ok := privProp.Value.(bool); !ok {
if privProp.Value == "true" {
privProp.Value = true
e.Properties.Add_Property(&privProp, true)
} else if privProp.Value == "false" {
privProp.Value = false
e.Properties.Add_Property(&privProp, true)
} else {
return fmt.Errorf(msgPrinter.Sprintf("The property %s must have a boolean value (true or false).", externalpolicy.PROP_SVC_PRIVILEGED))
}
}
}
// Validate the Constraints expression by invoking the plugins.
if e != nil && len(e.Constraints) != 0 {
_, err := e.Constraints.Validate()
return err
}
// We only get here if the input object is nil OR all of the top level fields are empty.
return nil
}
func (e *ExchangeNodeManagementPolicy) HasNoConstraints() bool {
if e.Constraints == nil || len(e.Constraints) == 0 {
return true
}
// even if the constraints array has non-zero length, the items in it could be empty strings
for _, c := range e.Constraints {
if strings.TrimSpace(c) != "" {
return false
}
}
return true
}
func (e *ExchangeNodeManagementPolicy) HasNoPatterns() bool {
if e.Patterns == nil || len(e.Patterns) == 0 {
return true
}
// even if the pattern array has non-zero length, the items in it could be empty strings
for _, c := range e.Patterns {
if strings.TrimSpace(c) != "" {
return false
}
}
return true
}
// The agent upgrade policy as stored in the exchange
type ExchangeAgentUpgradePolicy struct {
Manifest string `json:"manifest"`
AllowDowngrade bool `json:"allowDowngrade"`
}
func (e ExchangeAgentUpgradePolicy) String() string {
return fmt.Sprintf("Manifest: %v, AllowDowngrade: %v", e.Manifest, e.AllowDowngrade)
}
type UpgradeManifest struct {
Software UpgradeDescription `json:"softwareUpgrade"`
Certificate UpgradeDescription `json:"certificateUpgrade"`
Configuration UpgradeDescription `json:"configurationUpgrade"`
}
type UpgradeDescription struct {
Version string `json:"version"`
FileList []string `json:"files"`
}
type AgentFileVersions struct {
SoftwareVersions []string `json:"agentSoftwareVersions"`
ConfigVersions []string `json:"agentConfigVersions"`
CertVersions []string `json:"agentCertVersions"`
LastUpdated string `json:"lastUpdated,omitempty"`
}
func (a AgentFileVersions) String() string {
return fmt.Sprintf("SoftwareVersions: %v, ConfigVersions: %v, CertVersions: %v", a.SoftwareVersions, a.ConfigVersions, a.CertVersions)
}
type ExchangeAgentImagePolicy struct {
Removal []ImageRemovalPolicy `json:"imageRemovalPolicies,omitempty"`
}
func (e ExchangeAgentImagePolicy) String() string {
str := "Removal: "
for _, pol := range e.Removal {
str = str + fmt.Sprintf(" %v", pol)
}
return str
}
type ImageRemovalPolicy struct {
ImageId string `json:"imageId"`
DeleteAfterMinutes uint64 `json:"deleteAfterMinutes"`
AgentDownloadedOnly bool `json:"agentDownloadedOnly"`
}