-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
attributes_dao.go
143 lines (123 loc) · 4.25 KB
/
attributes_dao.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
/*
Copyright 2014 Outbrain Inc.
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 attributes
import (
"fmt"
"strings"
"vitess.io/vitess/go/vt/orchestrator/db"
"vitess.io/vitess/go/vt/orchestrator/external/golib/log"
"vitess.io/vitess/go/vt/orchestrator/external/golib/sqlutils"
)
// SetHostAttributes
func SetHostAttributes(hostname string, attributeName string, attributeValue string) error {
_, err := db.ExecOrchestrator(`
replace
into host_attributes (
hostname, attribute_name, attribute_value, submit_timestamp, expire_timestamp
) VALUES (
?, ?, ?, NOW(), NULL
)
`,
hostname,
attributeName,
attributeValue,
)
if err != nil {
return log.Errore(err)
}
return err
}
func getHostAttributesByClause(whereClause string, args []interface{}) ([]HostAttributes, error) {
res := []HostAttributes{}
query := fmt.Sprintf(`
select
hostname,
attribute_name,
attribute_value,
submit_timestamp ,
ifnull(expire_timestamp, '') as expire_timestamp
from
host_attributes
%s
order by
hostname, attribute_name
`, whereClause)
err := db.QueryOrchestrator(query, args, func(m sqlutils.RowMap) error {
hostAttributes := HostAttributes{}
hostAttributes.Hostname = m.GetString("hostname")
hostAttributes.AttributeName = m.GetString("attribute_name")
hostAttributes.AttributeValue = m.GetString("attribute_value")
hostAttributes.SubmitTimestamp = m.GetString("submit_timestamp")
hostAttributes.ExpireTimestamp = m.GetString("expire_timestamp")
res = append(res, hostAttributes)
return nil
})
if err != nil {
log.Errore(err)
}
return res, err
}
// GetHostAttributesByMatch
func GetHostAttributesByMatch(hostnameMatch string, attributeNameMatch string, attributeValueMatch string) ([]HostAttributes, error) {
terms := []string{}
args := sqlutils.Args()
if hostnameMatch != "" {
terms = append(terms, ` hostname rlike ? `)
args = append(args, hostnameMatch)
}
if attributeNameMatch != "" {
terms = append(terms, ` attribute_name rlike ? `)
args = append(args, attributeNameMatch)
}
if attributeValueMatch != "" {
terms = append(terms, ` attribute_value rlike ? `)
args = append(args, attributeValueMatch)
}
if len(terms) == 0 {
return getHostAttributesByClause("", args)
}
whereCondition := fmt.Sprintf(" where %s ", strings.Join(terms, " and "))
return getHostAttributesByClause(whereCondition, args)
}
// GetHostAttribute expects to return a single attribute for a given hostname/attribute-name combination
// or error on empty result
func GetHostAttribute(hostname string, attributeName string) (string, error) {
whereClause := `where hostname=? and attribute_name=?`
attributes, err := getHostAttributesByClause(whereClause, sqlutils.Args(hostname, attributeName))
if err != nil {
return "", err
}
if len(attributeName) == 0 {
return "", log.Errorf("No attribute found for %+v, %+v", hostname, attributeName)
}
return attributes[0].AttributeValue, nil
}
// SetGeneralAttribute sets an attribute not associated with a host. Its a key-value thing
func SetGeneralAttribute(attributeName string, attributeValue string) error {
if attributeName == "" {
return nil
}
return SetHostAttributes("*", attributeName, attributeValue)
}
// GetGeneralAttribute expects to return a single attribute value (not associated with a specific hostname)
func GetGeneralAttribute(attributeName string) (result string, err error) {
return GetHostAttribute("*", attributeName)
}
// GetHostAttributesByAttribute
func GetHostAttributesByAttribute(attributeName string, valueMatch string) ([]HostAttributes, error) {
if valueMatch == "" {
valueMatch = ".?"
}
whereClause := ` where attribute_name = ? and attribute_value rlike ?`
return getHostAttributesByClause(whereClause, sqlutils.Args(attributeName, valueMatch))
}