Skip to content

Commit

Permalink
Merge pull request #3196 from weichou1229/issue-3195
Browse files Browse the repository at this point in the history
refactor: Modify config.Clients to use service key
  • Loading branch information
cloudxxx8 authored Feb 26, 2021
2 parents dd4b8fe + afe0876 commit 73dc428
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/core-command/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Port = 8500
Type = 'consul'

[Clients]
[Clients.Metadata]
[Clients.edgex-core-metadata]
Protocol = 'http'
Host = 'localhost'
Port = 48081
Expand Down
2 changes: 1 addition & 1 deletion cmd/core-data/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Port = 8500
Type = 'consul'

[Clients]
[Clients.Metadata]
[Clients.edgex-core-metadata]
Protocol = 'http'
Host = 'localhost'
Port = 48081
Expand Down
4 changes: 2 additions & 2 deletions cmd/core-metadata/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Type = 'consul'


[Clients]
[Clients.Notifications]
[Clients.edgex-support-notifications]
Protocol = 'http'
Host = 'localhost'
Port = 48060
[Clients.CoreData]
[Clients.edgex-core-data]
Protocol = 'http'
Host = 'localhost'
Port = 48080
Expand Down
10 changes: 5 additions & 5 deletions cmd/sys-mgmt-agent/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ Port = 8500
Type = 'consul'

[Clients]
[Clients.Notifications]
[Clients.edgex-support-notifications]
Protocol = 'http'
Host = 'localhost'
Port = 48060

[Clients.Command]
[Clients.edgex-core-command]
Protocol = 'http'
Host = 'localhost'
Port = 48082

[Clients.Metadata]
[Clients.edgex-core-metadata]
Protocol = 'http'
Host = 'localhost'
Port = 48081

[Clients.CoreData]
[Clients.edgex-core-data]
Protocol = 'http'
Host = 'localhost'
Port = 48080

[Clients.Scheduler]
[Clients.edgex-support-scheduler]
Protocol = 'http'
Host = 'localhost'
Port = 48085
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.0.0-dev.13
github.com/edgexfoundry/go-mod-configuration/v2 v2.0.0-dev.3
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.38
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.39
github.com/edgexfoundry/go-mod-messaging/v2 v2.0.0-dev.3
github.com/edgexfoundry/go-mod-registry/v2 v2.0.0-dev.3
github.com/edgexfoundry/go-mod-secrets/v2 v2.0.0-dev.7
Expand Down
8 changes: 4 additions & 4 deletions internal/core/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ func (b *Bootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, _
// initialize clients required by the service
dic.Update(di.ServiceConstructorMap{
container.MetadataDeviceClientName: func(get di.Get) interface{} {
return metadata.NewDeviceClient(local.New(configuration.Clients["Metadata"].Url() + clients.ApiDeviceRoute))
return metadata.NewDeviceClient(local.New(configuration.Clients[clients.CoreMetaDataServiceKey].Url() + clients.ApiDeviceRoute))
},
errorContainer.ErrorHandlerName: func(get di.Get) interface{} {
return errorconcept.NewErrorHandler(lc)
},
V2Container.MetadataDeviceClientName: func(get di.Get) interface{} { // add v2 API MetadataDeviceClient
return V2Clients.NewDeviceClient(configuration.Clients["Metadata"].Url() + V2Routes.ApiDeviceRoute)
return V2Clients.NewDeviceClient(configuration.Clients[clients.CoreMetaDataServiceKey].Url() + V2Routes.ApiDeviceRoute)
},
V2Container.MetadataDeviceProfileClientName: func(get di.Get) interface{} { // add v2 API MetadataDeviceProfileClient
return V2Clients.NewDeviceProfileClient(configuration.Clients["Metadata"].Url() + V2Routes.ApiDeviceProfileRoute)
return V2Clients.NewDeviceProfileClient(configuration.Clients[clients.CoreMetaDataServiceKey].Url() + V2Routes.ApiDeviceProfileRoute)
},
V2Container.MetadataDeviceServiceClientName: func(get di.Get) interface{} { // add v2 API MetadataDeviceServiceClient
return V2Clients.NewDeviceServiceClient(configuration.Clients["Metadata"].Url() + V2Routes.ApiDeviceServiceRoute)
return V2Clients.NewDeviceServiceClient(configuration.Clients[clients.CoreMetaDataServiceKey].Url() + V2Routes.ApiDeviceServiceRoute)
},
V2Container.DeviceServiceCommandClientName: func(get di.Get) interface{} { // add v2 API DeviceServiceCommandClient
return V2Clients.NewDeviceServiceCommandClient()
Expand Down
9 changes: 5 additions & 4 deletions internal/core/data/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/metadata"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/urlclient/local"
contractsV2 "github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-messaging/v2/messaging"
msgTypes "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"

Expand All @@ -57,21 +58,21 @@ func (b *Bootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, st
configuration := dataContainer.ConfigurationFrom(dic.Get)
lc := container.LoggingClientFrom(dic.Get)

mdc := metadata.NewDeviceClient(local.New(configuration.Clients["Metadata"].Url() + clients.ApiDeviceRoute))
msc := metadata.NewDeviceServiceClient(local.New(configuration.Clients["Metadata"].Url() + clients.ApiDeviceRoute))
mdc := metadata.NewDeviceClient(local.New(configuration.Clients[clients.CoreMetaDataServiceKey].Url() + clients.ApiDeviceRoute))
msc := metadata.NewDeviceServiceClient(local.New(configuration.Clients[clients.CoreMetaDataServiceKey].Url() + clients.ApiDeviceRoute))

// For Redis Streams MessageBus, we reuse the Redis instance running for the DB, which may have a password,
// so we need to get and use the DB credentials for the MessageBus connection.
if configuration.MessageQueue.Type == "redisstreams" {
secretProvider := container.SecretProviderFrom(dic.Get)
credentials, err := secretProvider.GetSecrets(configuration.Databases["Primary"].Type)
credentials, err := secretProvider.GetSecrets(configuration.Databases[contractsV2.Primary].Type)
if err != nil {
lc.Error(fmt.Sprintf("Error getting DB creds for RedisStreams: %s", err.Error()))
return false
}

lc.Info("DB Credentials set for using Redis Streams")
configuration.MessageQueue.Optional["Password"] = credentials[secret.PasswordKey]
configuration.MessageQueue.Optional[contractsV2.Password] = credentials[secret.PasswordKey]
}

// Create the messaging client
Expand Down
4 changes: 2 additions & 2 deletions internal/core/metadata/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (b *Bootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, _
},
container.CoreDataValueDescriptorClientName: func(get di.Get) interface{} {
return coredata.NewValueDescriptorClient(
local.New(configuration.Clients["CoreData"].Url() + clients.ApiValueDescriptorRoute))
local.New(configuration.Clients[clients.CoreDataServiceKey].Url() + clients.ApiValueDescriptorRoute))
},
container.NotificationsClientName: func(get di.Get) interface{} {
return notifications.NewNotificationsClient(
local.New(configuration.Clients["Notifications"].Url() + clients.ApiNotificationRoute))
local.New(configuration.Clients[clients.SupportNotificationsServiceKey].Url() + clients.ApiNotificationRoute))

},
})
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/bootstrap/handlers/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
)

// httpServer defines the contract used to determine whether or not the http httpServer is running.
Expand Down Expand Up @@ -70,7 +71,7 @@ func (d Database) newDBClient(
lc logger.LoggingClient,
credentials bootstrapConfig.Credentials) (dbInterfaces.DBClient, error) {

databaseInfo := d.database.GetDatabaseInfo()["Primary"]
databaseInfo := d.database.GetDatabaseInfo()[v2.Primary]
switch databaseInfo.Type {
case db.RedisDB:
conf := db.Configuration{
Expand Down Expand Up @@ -103,7 +104,7 @@ func (d Database) BootstrapHandler(
for startupTimer.HasNotElapsed() {
var err error

secrets, err := secretProvider.GetSecrets(d.database.GetDatabaseInfo()["Primary"].Type)
secrets, err := secretProvider.GetSecrets(d.database.GetDatabaseInfo()[v2.Primary].Type)
if err == nil {
credentials = bootstrapConfig.Credentials{
Username: secrets[secret.UsernameKey],
Expand Down
7 changes: 4 additions & 3 deletions internal/pkg/v2/bootstrap/handlers/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (
"github.com/edgexfoundry/edgex-go/internal/pkg/db"
"github.com/edgexfoundry/edgex-go/internal/pkg/v2/infrastructure/redis"
v2Interface "github.com/edgexfoundry/edgex-go/internal/pkg/v2/interfaces"

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/secret"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
)

// httpServer defines the contract used to determine whether or not the http httpServer is running.
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewDatabase(httpServer httpServer, database interfaces.Database, dBClientIn
func (d Database) newDBClient(
lc logger.LoggingClient,
credentials bootstrapConfig.Credentials) (v2Interface.DBClient, error) {
databaseInfo := d.database.GetDatabaseInfo()["Primary"]
databaseInfo := d.database.GetDatabaseInfo()[v2.Primary]
switch databaseInfo.Type {
case "redisdb":
return redis.NewClient(
Expand Down Expand Up @@ -78,7 +79,7 @@ func (d Database) BootstrapHandler(
for startupTimer.HasNotElapsed() {
var err error

secrets, err := secretProvider.GetSecrets(d.database.GetDatabaseInfo()["Primary"].Type)
secrets, err := secretProvider.GetSecrets(d.database.GetDatabaseInfo()[v2.Primary].Type)
if err == nil {
credentials = bootstrapConfig.Credentials{
Username: secrets[secret.UsernameKey],
Expand Down
3 changes: 2 additions & 1 deletion internal/security/bootstrapper/redis/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
)

const (
Expand Down Expand Up @@ -67,7 +68,7 @@ func (handler *Handler) GetCredentials(ctx context.Context, _ *sync.WaitGroup, s

for startupTimer.HasNotElapsed() {
// retrieve database credentials from secretstore
secrets, err := secretProvider.GetSecrets(config.Databases["Primary"].Type)
secrets, err := secretProvider.GetSecrets(config.Databases[v2.Primary].Type)
if err == nil {
credentials.Username = secrets[secret.UsernameKey]
credentials.Password = secrets[secret.PasswordKey]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ var (
testSubscriptionResendLimit = int64(5)
testSubscriptionResendInterval = "10s"
unsupportedChannelType = "unsupportedChannelType"
unsupportedCategory = "unsupportedCategory"
)

func mockDic() *di.Container {
Expand Down

0 comments on commit 73dc428

Please sign in to comment.