diff --git a/configuration/interface.go b/configuration/interface.go index a95e8bb..870073f 100644 --- a/configuration/interface.go +++ b/configuration/interface.go @@ -16,10 +16,6 @@ package configuration -import ( - "github.com/pelletier/go-toml" -) - type Client interface { // HasConfiguration checks to see if the Configuration service contains the service's configuration. HasConfiguration() (bool, error) @@ -27,8 +23,9 @@ type Client interface { // HasSubConfiguration checks to see if the Configuration service contains the service's sub configuration. HasSubConfiguration(name string) (bool, error) - // PutConfigurationToml puts a full toml configuration into the Configuration service - PutConfigurationToml(configuration *toml.Tree, overwrite bool) error + // PutConfigurationMap puts a full map configuration into the Configuration service + // The sub-paths to where the values are to be stored in the Configuration service are generated from the map key. + PutConfigurationMap(configuration map[string]any, overwrite bool) error // PutConfiguration puts a full configuration struct into the Configuration service PutConfiguration(configStruct interface{}, overwrite bool) error diff --git a/configuration/mocks/Client.go b/configuration/mocks/Client.go index 8b0048f..2bd796e 100644 --- a/configuration/mocks/Client.go +++ b/configuration/mocks/Client.go @@ -1,11 +1,8 @@ -// Code generated by mockery v2.20.0. DO NOT EDIT. +// Code generated by mockery v2.20.2. DO NOT EDIT. package mocks -import ( - toml "github.com/pelletier/go-toml" - mock "github.com/stretchr/testify/mock" -) +import mock "github.com/stretchr/testify/mock" // Client is an autogenerated mock type for the Client type type Client struct { @@ -216,12 +213,12 @@ func (_m *Client) PutConfiguration(configStruct interface{}, overwrite bool) err return r0 } -// PutConfigurationToml provides a mock function with given fields: _a0, overwrite -func (_m *Client) PutConfigurationToml(_a0 *toml.Tree, overwrite bool) error { +// PutConfigurationMap provides a mock function with given fields: _a0, overwrite +func (_m *Client) PutConfigurationMap(_a0 map[string]interface{}, overwrite bool) error { ret := _m.Called(_a0, overwrite) var r0 error - if rf, ok := ret.Get(0).(func(*toml.Tree, bool) error); ok { + if rf, ok := ret.Get(0).(func(map[string]interface{}, bool) error); ok { r0 = rf(_a0, overwrite) } else { r0 = ret.Error(0) diff --git a/go.mod b/go.mod index 1811bd6..ca0c14c 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.20 require ( github.com/hashicorp/consul/api v1.20.0 github.com/mitchellh/consulstructure v0.0.0-20190329231841-56fdc4d2da54 - github.com/pelletier/go-toml v1.9.5 github.com/stretchr/testify v1.8.2 ) diff --git a/go.sum b/go.sum index ee8a9e1..cc28e1a 100644 --- a/go.sum +++ b/go.sum @@ -120,8 +120,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= -github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= -github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/internal/pkg/consul/client.go b/internal/pkg/consul/client.go index 91f3287..9f6d7d8 100644 --- a/internal/pkg/consul/client.go +++ b/internal/pkg/consul/client.go @@ -1,5 +1,5 @@ // -// Copyright (c) 2021 Intel Corporation +// Copyright (c) 2023 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package consul import ( "context" + "encoding/json" "errors" "fmt" "net/http" @@ -26,11 +27,9 @@ import ( "sync" "time" + "github.com/edgexfoundry/go-mod-configuration/v3/pkg/types" consulapi "github.com/hashicorp/consul/api" "github.com/mitchellh/consulstructure" - "github.com/pelletier/go-toml" - - "github.com/edgexfoundry/go-mod-configuration/v3/pkg/types" ) const ( @@ -140,11 +139,11 @@ func (client *consulClient) HasSubConfiguration(name string) (bool, error) { return true, nil } -// PutConfigurationToml puts a full toml configuration into Consul -func (client *consulClient) PutConfigurationToml(configuration *toml.Tree, overwrite bool) error { +// PutConfigurationMap puts a full configuration map into Consul. +// The sub-paths to where the values are to be stored in Consul are generated from the map key. +func (client *consulClient) PutConfigurationMap(configuration map[string]any, overwrite bool) error { - configurationMap := configuration.ToMap() - keyValues := convertInterfaceToConsulPairs("", configurationMap) + keyValues := convertInterfaceToConsulPairs("", configuration) // Put config properties into Consul. for _, keyValue := range keyValues { @@ -161,17 +160,18 @@ func (client *consulClient) PutConfigurationToml(configuration *toml.Tree, overw // PutConfiguration puts a full configuration struct into the Configuration provider func (client *consulClient) PutConfiguration(configuration interface{}, overwrite bool) error { - bytes, err := toml.Marshal(configuration) + configMap := make(map[string]any) + bytes, err := json.Marshal(configuration) if err != nil { return err } - tree, err := toml.LoadBytes(bytes) + err = json.Unmarshal(bytes, &configMap) if err != nil { return err } - err = client.PutConfigurationToml(tree, overwrite) + err = client.PutConfigurationMap(configMap, overwrite) if err != nil { return err } diff --git a/internal/pkg/consul/client_test.go b/internal/pkg/consul/client_test.go index fb7d994..f3a677c 100644 --- a/internal/pkg/consul/client_test.go +++ b/internal/pkg/consul/client_test.go @@ -1,5 +1,5 @@ // -// Copyright (c) 2021 Intel Corporation +// Copyright (c) 2023 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package consul import ( "fmt" - "log" "net/http/httptest" "net/url" "os" @@ -29,7 +28,6 @@ import ( "time" "github.com/hashicorp/consul/api" - "github.com/pelletier/go-toml" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -90,10 +88,10 @@ func TestIsAlive(t *testing.T) { func TestHasConfigurationFalse(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) - // Don't push anything in yet so configuration will not exists + // Don't push anything in yet so configuration will not exist actual, err := client.HasConfiguration() if !assert.NoError(t, err) { @@ -106,7 +104,7 @@ func TestHasConfigurationFalse(t *testing.T) { func TestHasConfigurationTrue(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) // Now push a value so the configuration will exist @@ -123,7 +121,7 @@ func TestHasConfigurationTrue(t *testing.T) { func TestHasConfigurationPartialServiceKey(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) base := client.configBasePath @@ -166,7 +164,7 @@ func TestHasConfigurationError(t *testing.T) { func TestHasSubConfigurationFalse(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) // Now push a value so some configuration will exist @@ -183,7 +181,7 @@ func TestHasSubConfigurationFalse(t *testing.T) { func TestHasSubConfigurationTrue(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) // Now push a value so some configuration will exist @@ -221,7 +219,7 @@ func TestConfigurationValueExists(t *testing.T) { client := makeConsulClient(t, uniqueServiceName, "", nil) expected := false - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) actual, err := client.ConfigurationValueExists(key) @@ -310,7 +308,7 @@ func TestPutConfigurationValue(t *testing.T) { client := makeConsulClient(t, uniqueServiceName, "", nil) - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist reset(t, client) _, _ = client.consulClient.KV().Delete(expectedFullKey, nil) @@ -414,7 +412,7 @@ func configValueSet(key string, client *consulClient) bool { return exists } -func TestPutConfigurationTomlNoPreviousValues(t *testing.T) { +func TestPutConfigurationMapNoPreviousValues(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) // Make sure the tree of values doesn't exist. @@ -426,11 +424,7 @@ func TestPutConfigurationTomlNoPreviousValues(t *testing.T) { }() configMap := createKeyValueMap() - configuration, err := toml.TreeFromMap(configMap) - if err != nil { - log.Fatalf("unable to create TOML Tree from map: %v", err) - } - err = client.PutConfigurationToml(configuration, false) + err := client.PutConfigurationMap(configMap, false) if !assert.NoError(t, err) { t.Fatal() } @@ -449,7 +443,7 @@ func TestPutConfigurationTomlNoPreviousValues(t *testing.T) { } } -func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) { +func TestPutConfigurationMapWithoutOverWrite(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) // Make sure the tree of values doesn't exist. @@ -462,8 +456,7 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) { configMap := createKeyValueMap() - configuration, _ := toml.TreeFromMap(configMap) - err := client.PutConfigurationToml(configuration, false) + err := client.PutConfigurationMap(configMap, false) if !assert.NoError(t, err) { t.Fatal() } @@ -474,10 +467,15 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) { configMap["float64"] = 2.4 configMap["string"] = "bye" configMap["bool"] = false + subMap := configMap["sub-map"].(map[string]any) + subMap["int"] = 45 + subMap["int64"] = 789 + subMap["float64"] = 23.45 + subMap["string"] = "another value" + subMap["bool"] = true // Try to put new values with overwrite = false - configuration, _ = toml.TreeFromMap(configMap) - err = client.PutConfigurationToml(configuration, false) + err = client.PutConfigurationMap(configMap, false) if !assert.NoError(t, err) { t.Fatal() } @@ -496,7 +494,7 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) { } } -func TestPutConfigurationTomlOverWrite(t *testing.T) { +func TestPutConfigurationMapOverWrite(t *testing.T) { client := makeConsulClient(t, getUniqueServiceName(), "", nil) // Make sure the tree of values doesn't exist. @@ -508,8 +506,7 @@ func TestPutConfigurationTomlOverWrite(t *testing.T) { configMap := createKeyValueMap() - configuration, _ := toml.TreeFromMap(configMap) - err := client.PutConfigurationToml(configuration, false) + err := client.PutConfigurationMap(configMap, false) if !assert.NoError(t, err) { t.Fatal() } @@ -521,8 +518,7 @@ func TestPutConfigurationTomlOverWrite(t *testing.T) { configMap["bool"] = false // Try to put new values with overwrite = True - configuration, _ = toml.TreeFromMap(configMap) - err = client.PutConfigurationToml(configuration, true) + err = client.PutConfigurationMap(configMap, true) if !assert.NoError(t, err) { t.Fatal() } @@ -585,7 +581,7 @@ func TestWatchForChanges(t *testing.T) { assert.NotNil(t, loggingChanges) logInfo := loggingChanges.(*LoggingInfo) - // first pass is for Consul Decoder always sending data once watch has been setup. It hasn't actually changed + // first pass is for Consul Decoder always sending data once watch has been set up. It hasn't actually changed if loggingPass == 1 { if !assert.Equal(t, logInfo.File, expectedConfig.Logging.File) { t.Fatal() @@ -613,7 +609,7 @@ func TestAccessToken(t *testing.T) { client := makeConsulClient(t, uniqueServiceName, "", nil) expectedErrMsg := "Unexpected response code: 403" valueName := "testAccess" - // Test if have access to endpoint w/o access token set + // Test if there is access to endpoint w/o access token set _, err := client.GetConfigurationValue(valueName) require.NoError(t, err) @@ -668,20 +664,27 @@ func makeConsulClient(t *testing.T, serviceName string, accessToken string, toke return client } -func createKeyValueMap() map[string]interface{} { - configMap := make(map[string]interface{}) +func createKeyValueMap() map[string]any { + configMap := make(map[string]any) configMap["int"] = 1 configMap["int64"] = int64(64) - configMap["float64"] = float64(1.4) + configMap["float64"] = 1.4 configMap["string"] = "hello" configMap["bool"] = true + configMap["sub-map"] = map[string]any{ + "string": "some value", + "bool": false, + "int": 6, + "int64": int64(34), + "float64": 6.3, + } return configMap } func reset(t *testing.T, client *consulClient) { - // Make sure the configuration doesn't already exists + // Make sure the configuration doesn't already exist if mockConsul != nil { mockConsul.Reset() } else {