forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agreement.go
219 lines (183 loc) · 4.95 KB
/
agreement.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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 agreement
import (
"net"
log "github.com/Sirupsen/logrus"
"github.com/hashicorp/memberlist"
"github.com/intelsdi-x/snap/core"
)
const (
RestPort = "rest_api_port"
RestProtocol = "rest_proto"
RestInsecureSkipVerify = "rest_insecure"
)
var logger = log.WithFields(log.Fields{
"_module": "tribe-agreement",
})
type Agreement struct {
Name string `json:"name"`
PluginAgreement *pluginAgreement `json:"plugin_agreement,omitempty"`
TaskAgreement *taskAgreement `json:"task_agreement,omitempty"`
Members map[string]*Member `json:"members,omitempty"`
}
type plugins []Plugin
type pluginAgreement struct {
Name string `json:"-"`
Plugins plugins `json:"plugins,omitempty"`
}
type tasks []Task
type taskAgreement struct {
Name string `json:"-"`
Tasks tasks `json:"tasks,omitempty"`
}
type Task struct {
ID string `json:"id"`
StartOnCreate bool `json:"start_on_create"`
}
func New(name string) *Agreement {
return &Agreement{
Name: name,
PluginAgreement: &pluginAgreement{
Name: name,
Plugins: plugins{},
},
TaskAgreement: &taskAgreement{
Name: name,
Tasks: tasks{},
},
Members: map[string]*Member{},
}
}
type Member struct {
Tags map[string]string `json:"tags,omitempty"`
Name string `json:"name"`
Node *memberlist.Node `json:"-"`
PluginAgreement *pluginAgreement `json:"-"`
TaskAgreements map[string]*taskAgreement `json:"-"`
}
func NewMember(node *memberlist.Node) *Member {
return &Member{
Name: node.Name,
Node: node,
TaskAgreements: map[string]*taskAgreement{},
}
}
func (m *Member) GetRestPort() string {
return m.Tags[RestPort]
}
func (m *Member) GetRestProto() string {
return m.Tags[RestProtocol]
}
func (m *Member) GetRestInsecureSkipVerify() bool {
if m.Tags[RestInsecureSkipVerify] == "true" {
return true
}
return false
}
func (m *Member) GetName() string {
return m.Name
}
func (m *Member) GetAddr() net.IP {
return m.Node.Addr
}
type Plugin struct {
Name_ string `json:"name"`
Version_ int `json:"version"`
Type_ core.PluginType `json:"type"`
}
func (p Plugin) Name() string {
return p.Name_
}
func (p Plugin) Version() int {
return p.Version_
}
func (p Plugin) TypeName() string {
return p.Type_.String()
}
func newPlugin(n string, v int, t core.PluginType) *Plugin {
return &Plugin{
Name_: n,
Version_: v,
Type_: t,
}
}
// contains - Returns boolean indicating whether the plugin was found.
// If the plugin is found the index returned as the second return value.
func (p plugins) Contains(item Plugin) (bool, int) {
for idx, i := range p {
if i.Name() == item.Name() && i.Version() == item.Version() && i.TypeName() == item.TypeName() {
return true, idx
}
}
return false, -1
}
// contains - Returns boolean indicating whether the plugin was found.
// If the plugin is found the index returned as the second return value.
func (t tasks) Contains(item Task) (bool, int) {
for idx, i := range t {
if i.ID == item.ID {
return true, idx
}
}
return false, -1
}
func (a *pluginAgreement) Remove(plugin Plugin) bool {
logger.WithFields(log.Fields{
"agreement": a.Name,
"plugin": plugin.Name(),
"_block": "remove",
}).Debugln("Removing plugin")
if ok, idx := a.Plugins.Contains(plugin); ok {
a.Plugins = append(a.Plugins[idx+1:], a.Plugins[:idx]...)
return true
}
return false
}
func (a *pluginAgreement) Add(plugin Plugin) bool {
logger.WithFields(log.Fields{
"agreement": a.Name,
"plugin": plugin.Name(),
"_block": "add",
}).Debugln("Adding plugin")
if ok, _ := a.Plugins.Contains(plugin); ok {
return false
}
a.Plugins = append(a.Plugins, plugin)
return true
}
func (a *taskAgreement) Add(task Task) bool {
logger.WithFields(log.Fields{
"agreement": a.Name,
"task_id": task.ID,
"_block": "add",
}).Debugln("Adding task")
if ok, _ := a.Tasks.Contains(task); ok {
return false
}
a.Tasks = append(a.Tasks, task)
return true
}
func (a *taskAgreement) Remove(task Task) bool {
logger.WithFields(log.Fields{
"agreement": a.Name,
"task_id": task.ID,
"_block": "remove",
}).Debugln("Removing task")
if ok, idx := a.Tasks.Contains(task); ok {
a.Tasks = append(a.Tasks[idx+1:], a.Tasks[:idx]...)
return true
}
return false
}