Skip to content

Commit

Permalink
refactor: nacos configstore component.
Browse files Browse the repository at this point in the history
support level1 config functions.
support connect to nacos use acm mode.
fix error in filling in illegal characters when calling nacos set and delete parameters
  • Loading branch information
cyb0225 committed Jun 15, 2023
1 parent df1fb1a commit 29b795f
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 86 deletions.
1 change: 1 addition & 0 deletions components/configstores/nacos/change_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type subscriberHolder struct {
type subscriberKey struct {
group string
key string
label string
}

func newSubscriberHolder() *subscriberHolder {
Expand Down
3 changes: 2 additions & 1 deletion components/configstores/nacos/change_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
package nacos

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_newSubscriberHolder(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/nacos-group/nacos-sdk-go/v2/vo"
)

type NacosConfigClient interface {
type Client interface {
// GetConfig use to get config from nacos server
// dataId require
// group require
Expand Down
59 changes: 46 additions & 13 deletions components/configstores/nacos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,37 @@

package nacos

const (
defaultNamespaceId = "" // if this is not set, then nacos will use the default namespaceId.
defaultGroup = "default"
defaultLabel = "default"
defaultLogDir = "/tmp/layotto/nacos/log"
defaultCacheDir = "/tmp/layotto/nacos/cache"
defaultLogLevel = "debug"
defaultTimeout = 10 // second
)

// map keys
const (
namespaceIdKey = "namespace_id"
appNameKey = "app_name"
userNameKey = "username"
passwordKey = "password"
endPointKey = "end_point"
regionIdKey = "region_id"
accessKey = "access_key"
secretKey = "secret_key"
)

type NacosMetadata struct {
type Metadata struct {
AppName string
NameSpaceId string
Username string
Password string
// ACM & KMS
Endpoint string
RegionId string
AccessKey string
SecretKey string
OpenKMS bool
}

func ParseNacosMetadata(properties map[string]string) (*NacosMetadata, error) {
func ParseNacosMetadata(properties map[string]string) (*Metadata, error) {
if properties == nil {
return nil, errConfigMissingField("metadata")
}

config := &NacosMetadata{}
config := &Metadata{}
config.AppName = properties[appNameKey]
if config.AppName == "" {
return nil, errConfigMissingField(appNameKey)
Expand All @@ -51,5 +55,34 @@ func ParseNacosMetadata(properties map[string]string) (*NacosMetadata, error) {
config.NameSpaceId = defaultNamespaceId
}

if v, ok := properties[userNameKey]; ok && v != "" {
config.Username = v
}

if v, ok := properties[passwordKey]; ok && v != "" {
config.Password = v
}

// ACM & KMS
if v, ok := properties[endPointKey]; ok && v != "" {
config.Endpoint = v
config.OpenKMS = true
}

if v, ok := properties[regionIdKey]; ok && v != "" {
config.RegionId = v
config.OpenKMS = true
}

if v, ok := properties[accessKey]; ok && v != "" {
config.AccessKey = v
config.OpenKMS = true
}

if v, ok := properties[secretKey]; ok && v != "" {
config.SecretKey = v
config.OpenKMS = true
}

return config, nil
}
3 changes: 2 additions & 1 deletion components/configstores/nacos/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
package nacos

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseNacosMetadata(t *testing.T) {
Expand Down
Loading

0 comments on commit 29b795f

Please sign in to comment.