-
Notifications
You must be signed in to change notification settings - Fork 5
/
configparser.go
106 lines (86 loc) · 2.86 KB
/
configparser.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
package gocbcorex
import (
"strings"
"github.com/couchbase/gocbcorex/contrib/cbconfig"
"golang.org/x/exp/slices"
)
func parseConfigHostname(hostname string, sourceHostname string) string {
if hostname == "" {
// if no hostname is provided, we want to be using the source one
return sourceHostname
}
if strings.Contains(hostname, ":") {
// this appears to be an IPv6 address, wrap it for everyone else
return "[" + hostname + "]"
}
return hostname
}
func parseConfigHostsInto(hostname string, ports *cbconfig.TerseExtNodePortsJson) ParsedConfigAddresses {
var config ParsedConfigAddresses
config.Hostname = hostname
config.NonSSLPorts.Kv = int(ports.Kv)
config.NonSSLPorts.Mgmt = int(ports.Mgmt)
config.NonSSLPorts.Views = int(ports.Capi)
config.NonSSLPorts.Query = int(ports.N1ql)
config.NonSSLPorts.Search = int(ports.Fts)
config.NonSSLPorts.Analytics = int(ports.Cbas)
config.SSLPorts.Kv = int(ports.KvSsl)
config.SSLPorts.Mgmt = int(ports.MgmtSsl)
config.SSLPorts.Views = int(ports.CapiSsl)
config.SSLPorts.Query = int(ports.N1qlSsl)
config.SSLPorts.Search = int(ports.FtsSsl)
config.SSLPorts.Analytics = int(ports.CbasSsl)
return config
}
type ConfigParser struct{}
func (p ConfigParser) ParseTerseConfig(config *cbconfig.TerseConfigJson, sourceHostname string) (*ParsedConfig, error) {
var out ParsedConfig
out.RevID = int64(config.Rev)
out.RevEpoch = int64(config.RevEpoch)
lenNodes := len(config.Nodes)
out.Nodes = make([]ParsedConfigNode, len(config.NodesExt))
for nodeIdx, node := range config.NodesExt {
nodeHostname := parseConfigHostname(node.Hostname, sourceHostname)
var nodeOut ParsedConfigNode
nodeOut.HasData = nodeIdx < lenNodes
nodeOut.Addresses = parseConfigHostsInto(nodeHostname, node.Services)
nodeOut.AltAddresses = make(map[string]ParsedConfigAddresses)
for networkType, altAddrs := range node.AltAddresses {
altHostname := parseConfigHostname(altAddrs.Hostname, nodeHostname)
nodeOut.AltAddresses[networkType] = parseConfigHostsInto(altHostname, altAddrs.Ports)
}
out.Nodes[nodeIdx] = nodeOut
}
if config.Name != "" {
out.BucketUUID = config.UUID
out.BucketName = config.Name
switch config.NodeLocator {
case "ketama":
out.BucketType = bktTypeMemcached
case "vbucket":
if len(config.VBucketServerMap.VBucketMap) == 0 {
out.BucketType = bktTypeCouchbase
out.VbucketMap = nil
break
}
vbMap, err := NewVbucketMap(
config.VBucketServerMap.VBucketMap,
config.VBucketServerMap.NumReplicas)
if err != nil {
return nil, err
}
out.BucketType = bktTypeCouchbase
out.VbucketMap = vbMap
default:
out.BucketType = bktTypeInvalid
}
}
clusterCaps := config.ClusterCapabilities
if clusterCaps != nil {
ftsCaps := config.ClusterCapabilities["fts"]
if ftsCaps != nil {
out.Features.FtsVectorSearch = slices.Contains(ftsCaps, "vectorSearch")
}
}
return &out, nil
}