Skip to content

Commit

Permalink
modified GetASConfig to return all configs if input path is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmayja committed Mar 20, 2024
1 parent 073d934 commit 1847923
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 22 deletions.
57 changes: 43 additions & 14 deletions asconfig/as_getconfig.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package asconfig

import (
"fmt"
"strings"

aero "github.com/aerospike/aerospike-client-go/v6"
"github.com/go-logr/logr"

aero "github.com/aerospike/aerospike-client-go/v7"
lib "github.com/aerospike/aerospike-management-lib"
"github.com/aerospike/aerospike-management-lib/deployment"
"github.com/aerospike/aerospike-management-lib/info"
)

// GetASConfig returns the value of the given path from the aerospike config from given host.
func GetASConfig(path string, conn *deployment.ASConn, aerospikePolicy *aero.ClientPolicy) (
func GetASConfig(path *string, conn *deployment.ASConn, aerospikePolicy *aero.ClientPolicy) (
confToReturn interface{}, err error) {
h := aero.Host{
Name: conn.AerospikeHostName,
Expand All @@ -19,10 +22,13 @@ func GetASConfig(path string, conn *deployment.ASConn, aerospikePolicy *aero.Cli
}
asinfo := info.NewAsInfo(conn.Log, &h, aerospikePolicy)

// Get the corresponding sets info also if context is namespace.
ctxs := []string{ContextKey(path)}
if ctxs[0] == info.ConfigNamespaceContext {
ctxs = append(ctxs, info.ConfigSetContext)
var ctxs []string
if path != nil {
// Get the corresponding sets info also if context is namespace.
ctxs = []string{ContextKey(*path)}
if ctxs[0] == info.ConfigNamespaceContext {
ctxs = append(ctxs, info.ConfigSetContext)
}
}

conf, err := asinfo.GetAsConfig(ctxs...)
Expand All @@ -31,31 +37,54 @@ func GetASConfig(path string, conn *deployment.ASConn, aerospikePolicy *aero.Cli
return nil, err
}

if path == nil {
return conf, nil
}

confToReturn = conf[ctxs[0]]
if confToReturn == nil {
conn.Log.Info("Config is nil", "context", ctxs[0])
return nil, nil
}

confToReturn, err = traverseConfig(conn.Log, confToReturn, *path, ctxs[0])
if err != nil {
conn.Log.Error(err, "failed to traverse config")
return nil, err
}

return confToReturn, nil
}

// traverseConfig recursively traverses the configuration based on the given path.
func traverseConfig(logger logr.Logger, conf interface{}, path, context string) (interface{}, error) {
tokens := strings.Split(path, sep)
for idx, token := range tokens[1:] {
if ReCurlyBraces.MatchString(token) {
name := strings.Trim(token, "{}")

if ctxs[0] == info.ConfigLoggingContext && name == constLoggingConsole {
if context == info.ConfigLoggingContext && name == constLoggingConsole {
name = constLoggingStderr
}

confToReturn = confToReturn.(lib.Stats)[name]
stats, ok := conf.(lib.Stats)
if !ok {
return nil, fmt.Errorf("invalid configuration type")
}

conf = stats[name]
} else {
confToReturn = confToReturn.(lib.Stats)[token]
stats, ok := conf.(lib.Stats)
if !ok {
return nil, fmt.Errorf("invalid configuration type")
}
conf = stats[token]
}

if confToReturn == nil {
conn.Log.Info("Config is nil", strings.Join(tokens[:idx+2], sep))
break
if conf == nil {
logger.Info("Config is nil", "path", strings.Join(tokens[:idx+2], sep))
return nil, nil
}
}

return confToReturn, nil
return conf, nil
}
11 changes: 8 additions & 3 deletions asconfig/as_setconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-logr/logr"

aero "github.com/aerospike/aerospike-client-go/v6"
aero "github.com/aerospike/aerospike-client-go/v7"
"github.com/aerospike/aerospike-management-lib/deployment"
"github.com/aerospike/aerospike-management-lib/info"
)
Expand Down Expand Up @@ -387,20 +387,23 @@ func rearrangeConfigMap(log logr.Logger, configMap DynamicConfigMap) []string {
for k, v := range configMap {
baseKey := BaseKey(k)
context := ContextKey(k)
_, removeOP := v[Remove]
tokens := SplitKey(log, k, sep)

if context == info.ConfigXDRContext && baseKey == KeyName {
switch tokens[len(tokens)-3] {
// Handle DCs added/removed
case info.ConfigDCContext:
dc := rearrangedConfigMap.PushFront(k)
if lastDC == nil {
lastDC = dc
}
// Handle Namespaces added/removed
case info.ConfigNamespaceContext:
if removeOP {
if _, ok := v[Remove]; ok {
// If namespace is removed, directly add it to the final list
finalList = append(finalList, k)
} else {
// If namespace is added, add it after all DCs and their direct fields
if lastNAP == nil {
if lastDC != nil {
rearrangedConfigMap.InsertAfter(k, lastDC)
Expand All @@ -418,11 +421,13 @@ func rearrangeConfigMap(log logr.Logger, configMap DynamicConfigMap) []string {
continue
}

// Handle DC direct fields
if tokens[len(tokens)-3] == info.ConfigDCContext {
var nap *list.Element
if lastDC == nil {
nap = rearrangedConfigMap.PushFront(k)
} else {
// Add modified DC direct fields after the DC names and before the namespaces
nap = rearrangedConfigMap.InsertAfter(k, lastDC)
}
if lastNAP == nil {
Expand Down
2 changes: 1 addition & 1 deletion asconfig/as_setconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"

aero "github.com/aerospike/aerospike-client-go/v6"
aero "github.com/aerospike/aerospike-client-go/v7"
"github.com/aerospike/aerospike-management-lib/deployment"
)

Expand Down
2 changes: 2 additions & 0 deletions asconfig/asconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ func (s *AsConfigTestSuite) TestAsConfigGetDiff() {
"security": map[string]interface{}{
"log": map[string]interface{}{
"report-authentication": true,
"report-data-op": []string{"ns1 set1", "ns3 set2"},
},
},
},
DynamicConfigMap{
"security.log.report-authentication": {Update: false},
"security.log.report-data-op": {Remove: []string{"ns1 set1", "ns3 set2"}},
},
},

Expand Down
2 changes: 1 addition & 1 deletion asconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func detailedDiff(log logr.Logger, desired, current Conf, isFlat,
if diffUpdated = handleMissingSection(log, key, desired, current, d, desiredToActual); !diffUpdated {
var err error
// Add default values to config parameter if available in schema.
// If key is not present in current, then check if any key which starts with key is present in current
// If key is not present in current, then check if any key in desired which starts with key is present in current
// eg. desired has security: {} current has security.log.report-sys-admin: true
// final diff should be map[security.log.report-sys-admin] = <default value>
diffUpdated, err = handlePartialMissingSection(key, ver, current, d)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/aerospike/aerospike-management-lib
go 1.21

require (
github.com/aerospike/aerospike-client-go/v6 v6.15.0
github.com/aerospike/aerospike-client-go/v7 v7.1.0
github.com/deckarep/golang-set/v2 v2.3.1
github.com/docker/go-connections v0.4.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/aerospike/aerospike-client-go/v6 v6.15.0 h1:UF9GaKRjgwLsfRPfzgO14ivr9+DWIV8SkHhnb4AjbD4=
github.com/aerospike/aerospike-client-go/v6 v6.15.0/go.mod h1:76do+aMM6LwPAaWuMQKsOKM65NN/2P7InSmQqsuwJv4=
github.com/aerospike/aerospike-client-go/v7 v7.1.0 h1:yvCTKdbpqZxHvv7sWsFHV1j49jZcC8yXRooWsDFqKtA=
github.com/aerospike/aerospike-client-go/v7 v7.1.0/go.mod h1:AkHiKvCbqa1c16gCNGju3c5X/yzwLVvblNczqjxNwNk=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down

0 comments on commit 1847923

Please sign in to comment.