-
Notifications
You must be signed in to change notification settings - Fork 11
/
cibadmin.go
96 lines (76 loc) · 2.68 KB
/
cibadmin.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
package gatherers
import (
log "github.com/sirupsen/logrus"
"github.com/trento-project/agent/internal/factsengine/factscache"
"github.com/trento-project/agent/pkg/factsengine/entities"
"github.com/trento-project/agent/pkg/utils"
)
const (
CibAdminGathererName = "cibadmin"
CibAdminGathererCache = "cibadmin"
)
// nolint:gochecknoglobals
var (
CibAdminCommandError = entities.FactGatheringError{
Type: "cibadmin-command-error",
Message: "error running cibadmin command",
}
CibAdminDecodingError = entities.FactGatheringError{
Type: "cibadmin-decoding-error",
Message: "error decoding cibadmin output",
}
)
type CibAdminGatherer struct {
executor utils.CommandExecutor
cache *factscache.FactsCache
}
func NewDefaultCibAdminGatherer() *CibAdminGatherer {
return NewCibAdminGatherer(utils.Executor{}, nil)
}
func NewCibAdminGatherer(executor utils.CommandExecutor, cache *factscache.FactsCache) *CibAdminGatherer {
return &CibAdminGatherer{
executor: executor,
cache: cache,
}
}
func (g *CibAdminGatherer) SetCache(cache *factscache.FactsCache) {
g.cache = cache
}
func memoizeCibAdmin(args ...interface{}) (interface{}, error) {
executor, ok := args[0].(utils.CommandExecutor)
if !ok {
return nil, ImplementationError.Wrap("error using memoizeCibAdmin. executor must be 1st argument")
}
return executor.Exec("cibadmin", "--query", "--local")
}
func (g *CibAdminGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) {
log.Infof("Starting %s facts gathering process", CibAdminGathererName)
content, err := factscache.GetOrUpdate(g.cache, CibAdminGathererCache, memoizeCibAdmin, g.executor)
if err != nil {
return nil, CibAdminCommandError.Wrap(err.Error())
}
cibadmin, ok := content.([]byte)
if !ok {
return nil, CibAdminDecodingError.Wrap("error casting the command output")
}
elementsToList := map[string]bool{"primitive": true, "clone": true, "master": true, "group": true,
"nvpair": true, "op": true, "rsc_location": true, "rsc_order": true,
"rsc_colocation": true, "cluster_property_set": true, "meta_attributes": true}
factValueMap, err := parseXMLToFactValueMap(cibadmin, elementsToList, entities.WithStringConversion())
if err != nil {
return nil, CibAdminDecodingError.Wrap(err.Error())
}
facts := []entities.Fact{}
for _, factReq := range factsRequests {
var fact entities.Fact
if value, err := factValueMap.GetValue(factReq.Argument); err == nil {
fact = entities.NewFactGatheredWithRequest(factReq, value)
} else {
log.Error(err)
fact = entities.NewFactGatheredWithError(factReq, err)
}
facts = append(facts, fact)
}
log.Infof("Requested %s facts gathered", CibAdminGathererName)
return facts, err
}