/
server_customattribute.go
85 lines (73 loc) · 2.66 KB
/
server_customattribute.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
/*
(c) Copyright [2015] Hewlett Packard Enterprise Development LP
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 icsp -
package icsp
import "github.com/docker/machine/libmachine/log"
// ValueItem struct
type ValueItem struct {
Scope string `json:"scope,omitempty"` // scope of value
Value string `json:"value,omitempty"` // value of information
}
// CustomAttribute struct
type CustomAttribute struct {
Key string `json:"key,omitempty"` // key for name value pairs
Values []ValueItem `json:"values,omitempty"`
}
// GetValueItem - gets a ValueItem from custom attribute
func (s *Server) GetValueItem(key string, scope string) (int, ValueItem) {
var v ValueItem
i, vitems := s.GetValueItems(key)
if i >= 0 {
for i, vitem := range vitems {
if vitem.Scope == scope {
return i, vitem
}
}
}
return -1, v
}
// GetValueItems - gets a customattribute value item by key
func (s *Server) GetValueItems(key string) (int, []ValueItem) {
var vi []ValueItem
for i, attribute := range s.CustomAttributes {
if attribute.Key == key {
return i, attribute.Values
}
}
return -1, vi
}
// SetValueItems object
func (s *Server) SetValueItems(key string, newv ValueItem) {
_, oldv := s.GetValueItem(key, newv.Scope)
log.Debugf("GetValueItem(%s, %s)=> %+v", key, newv.Scope, oldv)
if i, oldv := s.GetValueItem(key, newv.Scope); i < 0 {
// creat a new ValueItem
log.Debugf("Adding new GetValueItem(%s, %s) => %+v", key, newv.Scope, newv)
vi, _ := s.GetValueItems(key)
if vi < 0 {
// a new key is needed
s.CustomAttributes = append(s.CustomAttributes, CustomAttribute{Key: key, Values: []ValueItem{{Scope: newv.Scope, Value: newv.Value}}})
} else {
s.CustomAttributes[vi].Values = append(s.CustomAttributes[vi].Values, ValueItem{Scope: newv.Scope, Value: newv.Value})
}
} else {
// set an existing one
log.Debugf("Change(%s) %+v to >> %+v", key, oldv, newv)
vi, _ := s.GetValueItems(key)
s.CustomAttributes[vi].Values[i] = ValueItem{Scope: newv.Scope, Value: newv.Value}
}
}
// SetCustomAttribute set a custom attribute for server
func (s *Server) SetCustomAttribute(key string, scope string, value string) {
s.SetValueItems(key, ValueItem{Scope: scope, Value: value})
}