Skip to content

Commit

Permalink
refactor: Restructure Trigger configuration (#724)
Browse files Browse the repository at this point in the history
closes #719 & #655

BREAKING CHANGE:
- Renamed `Binding` to `Trigger`
- Removed deprecated `MessageBus` trigger type, replaced by`edgex-messagebus`
- Renamed `MessageBus` to `EdgexMessageBus`
- Move `EdgexMessageBus` and `ExternalMqtt` under `Trigger` configuration

Signed-off-by: lenny <leonard.goodell@intel.com>
  • Loading branch information
lenny-goodell authored Feb 26, 2021
1 parent 866257f commit 8767d03
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 287 deletions.
2 changes: 1 addition & 1 deletion app-service-template/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ APPVERSION=$(shell cat ./VERSION 2>/dev/null || echo 0.0.0)

# This pulls the version of the SDK from the go.mod file. If the SDK is the only required module,
# it must first remove the word 'required' so the offset of $2 is the same if there are multiple required modules
SDKVERSION=$(shell cat ./go.mod | grep 'github.com/edgexfoundry/app-functions-sdk-go v' | sed 's/require//g' | awk '{print $$2}')
SDKVERSION=$(shell cat ./go.mod | grep 'github.com/edgexfoundry/app-functions-sdk-go/v2 v' | sed 's/require//g' | awk '{print $$2}')

MICROSERVICE=new-app-service
GOFLAGS=-ldflags "-X github.com/edgexfoundry/app-functions-sdk-go/v2/internal.SDKVersion=$(SDKVERSION) -X github.com/edgexfoundry/app-functions-sdk-go/v2/internal.ApplicationVersion=$(APPVERSION)"
Expand Down
68 changes: 38 additions & 30 deletions app-service-template/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
# https://docs.edgexfoundry.org/1.3/microservices/application/GeneralAppServiceConfig/
[Writable]
LogLevel = 'INFO'
[Writable.StoreAndForward]
Enabled = false
RetryInterval = '5m'
MaxRetryCount = 10

# TODO: if not running in secure mode, but do have secrets then add them here.
[Writable.InsecureSecrets]
[Writable.InsecureSecrets.DB]
path = "redisdb"
[Writable.InsecureSecrets.DB.Secrets]
username = ""
password = ""
[Writable.StoreAndForward]
Enabled = false
RetryInterval = '5m'
MaxRetryCount = 10

# TODO: if not running in secure mode, but do have secrets then add them here.
[Writable.InsecureSecrets]
[Writable.InsecureSecrets.DB]
path = "redisdb"
[Writable.InsecureSecrets.DB.Secrets]
username = ""
password = ""

[Service]
BootTimeout = '30s'
Expand Down Expand Up @@ -52,48 +53,55 @@ ServerName = ''
TokenFile = '/vault/config/assets/resp-init.json'
AdditionalRetryAttempts = 10
RetryWaitPeriod = "1s"
[SecretStore.Authentication]
AuthType = 'X-Vault-Token'
[SecretStore.Authentication]
AuthType = 'X-Vault-Token'

[Clients]
[Clients.CoreData]
Protocol = 'http'
Host = 'localhost'
Port = 48080

[Binding]
[Trigger]
Type="edgex-messagebus"
SubscribeTopics="events, edgex/events"
PublishTopic="event-xml" #TODO: remove if service is NOT publishing back to the message bus

[MessageBus]
Type = 'zero'
[MessageBus.SubscribeHost]
[Trigger.EdgexMessageBus]
Type = 'zero'
[Trigger.EdgexMessageBus.SubscribeHost]
Host = 'localhost'
Port = 5563
Protocol = 'tcp'
[MessageBus.PublishHost] # TODO: Remove if service is NOT publishing back to the message bus
[Trigger.EdgexMessageBus.PublishHost] # TODO: Remove if service is NOT publishing back to the message bus
Host = '*'
Port = 5564
Protocol = 'tcp'

# TODO: If using mqtt messagebus, Uncomment this section and remove above [Binding] & [MessageBus],
# Otherwise remove this commentedout block
#[Binding]
# TODO: If using mqtt messagebus, Uncomment this section and remove above [Trigger] section,
# Otherwise remove this commented out block
#[Trigger]
#Type="edgex-messagebus"
#SubscribeTopics="events, edgex/events/#"
#PublishTopic="event-xml" # TODO: Remove if service is NOT publishing back to the message bus
#
#[MessageBus]
# Type = 'mqtt'
# [MessageBus.SubscribeHost]
# [Trigger.EdgexMessageBus]
# Type = 'mqtt'
# [Trigger.EdgexMessageBus.SubscribeHost]
# Host = 'localhost'
# Port = 1883
# Protocol = 'tcp'
# [Trigger.EdgexMessageBus.PublishHost] # TODO: Remove if service is NOT publishing back to the message bus
# Host = 'localhost'
# Port = 1883
# Protocol = 'tcp'
# [MessageBus.PublishHost] # TODO: Remove if service is NOT publishing back to the message bus
# Host = 'localhost'
# Port = 1883
# Protocol = 'tcp'
# [Trigger.EdgexMessageBus.Optional]
# ClientId ="new-app-service"
# Qos = "0" # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once)
# KeepAlive = "10" # Seconds (must be 2 or greater)
# Retained = "false"
# AutoReconnect = "true"
# ConnectTimeout = "30" # Seconds
# SkipCertVerify = "false"


[ApplicationSettings]
# TODO: Add custom settings needed by your app service
Expand Down
15 changes: 7 additions & 8 deletions appsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ const (
envProfile = "EDGEX_PROFILE"
envServiceKey = "EDGEX_SERVICE_KEY"

bindingTypeMessageBus = "MESSAGEBUS"
bindingTypeEdgeXMessageBus = "EDGEX-MESSAGEBUS"
bindingTypeMQTT = "EXTERNAL-MQTT"
bindingTypeHTTP = "HTTP"
TriggerTypeMessageBus = "EDGEX-MESSAGEBUS"
TriggerTypeMQTT = "EXTERNAL-MQTT"
TriggerTypeHTTP = "HTTP"

OptionalPasswordKey = "Password"
)
Expand Down Expand Up @@ -423,15 +422,15 @@ func (sdk *AppFunctionsSDK) Initialize() error {

// If using the RedisStreams MessageBus implementation then need to make sure the
// password for the Redis DB is set in the MessageBus Optional properties.
bindingType := strings.ToUpper(sdk.config.Binding.Type)
if (bindingType == bindingTypeMessageBus || bindingType == bindingTypeEdgeXMessageBus) &&
sdk.config.MessageBus.Type == messaging.RedisStreams {
triggerType := strings.ToUpper(sdk.config.Trigger.Type)
if triggerType == TriggerTypeMessageBus &&
sdk.config.Trigger.EdgexMessageBus.Type == messaging.RedisStreams {

credentials, err := sdk.secretProvider.GetSecrets(sdk.config.Database.Type)
if err != nil {
return fmt.Errorf("unable to set RedisStreams password from DB credentials")
}
sdk.config.MessageBus.Optional[OptionalPasswordKey] = credentials[secret.PasswordKey]
sdk.config.Trigger.EdgexMessageBus.Optional[OptionalPasswordKey] = credentials[secret.PasswordKey]
}

// We do special processing when the writeable section of the configuration changes, so have
Expand Down
16 changes: 8 additions & 8 deletions appsdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var lc logger.LoggingClient

func TestMain(m *testing.M) {
// No remote and no file results in STDOUT logging only
lc = logger.NewMockClient()
lc = logger.NewClient("cc", "DEBUG")
m.Run()
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestSetupHTTPTrigger(t *testing.T) {
sdk := AppFunctionsSDK{
LoggingClient: lc,
config: &common.ConfigurationStruct{
Binding: common.BindingInfo{
Trigger: common.TriggerInfo{
Type: "htTp",
},
},
Expand All @@ -105,8 +105,8 @@ func TestSetupMessageBusTrigger(t *testing.T) {
sdk := AppFunctionsSDK{
LoggingClient: lc,
config: &common.ConfigurationStruct{
Binding: common.BindingInfo{
Type: "meSsaGebus",
Trigger: common.TriggerInfo{
Type: TriggerTypeMessageBus,
},
},
}
Expand All @@ -122,8 +122,8 @@ func TestSetFunctionsPipelineNoTransforms(t *testing.T) {
sdk := AppFunctionsSDK{
LoggingClient: lc,
config: &common.ConfigurationStruct{
Binding: common.BindingInfo{
Type: "meSsaGebus",
Trigger: common.TriggerInfo{
Type: TriggerTypeMessageBus,
},
},
}
Expand All @@ -137,8 +137,8 @@ func TestSetFunctionsPipelineOneTransform(t *testing.T) {
LoggingClient: lc,
runtime: &runtime.GolangRuntime{},
config: &common.ConfigurationStruct{
Binding: common.BindingInfo{
Type: "meSsaGebus",
Trigger: common.TriggerInfo{
Type: TriggerTypeMessageBus,
},
},
}
Expand Down
34 changes: 16 additions & 18 deletions appsdk/triggerfactory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2020 Technotects
// Copyright (c) 2020 Technocrats
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,25 +56,24 @@ func (sdk *AppFunctionsSDK) defaultTriggerContextBuilder(env types.MessageEnvelo
}

// RegisterCustomTriggerFactory allows users to register builders for custom trigger types
func (s *AppFunctionsSDK) RegisterCustomTriggerFactory(name string,
func (sdk *AppFunctionsSDK) RegisterCustomTriggerFactory(name string,
factory func(TriggerConfig) (Trigger, error)) error {
nu := strings.ToUpper(name)

if nu == bindingTypeEdgeXMessageBus ||
nu == bindingTypeMessageBus ||
nu == bindingTypeHTTP ||
nu == bindingTypeMQTT {
if nu == TriggerTypeMessageBus ||
nu == TriggerTypeHTTP ||
nu == TriggerTypeMQTT {
return errors.New(fmt.Sprintf("cannot register custom trigger for builtin type (%s)", name))
}

if s.customTriggerFactories == nil {
s.customTriggerFactories = make(map[string]func(sdk *AppFunctionsSDK) (Trigger, error), 1)
if sdk.customTriggerFactories == nil {
sdk.customTriggerFactories = make(map[string]func(sdk *AppFunctionsSDK) (Trigger, error), 1)
}

s.customTriggerFactories[nu] = func(sdk *AppFunctionsSDK) (Trigger, error) {
sdk.customTriggerFactories[nu] = func(sdk *AppFunctionsSDK) (Trigger, error) {
return factory(TriggerConfig{
Config: s.config,
Logger: s.LoggingClient,
Config: sdk.config,
Logger: sdk.LoggingClient,
ContextBuilder: sdk.defaultTriggerContextBuilder,
MessageProcessor: sdk.defaultTriggerMessageProcessor,
})
Expand All @@ -86,19 +85,18 @@ func (s *AppFunctionsSDK) RegisterCustomTriggerFactory(name string,
// setupTrigger configures the appropriate trigger as specified by configuration.
func (sdk *AppFunctionsSDK) setupTrigger(configuration *common.ConfigurationStruct, runtime *runtime.GolangRuntime) Trigger {
var t Trigger
// Need to make dynamic, search for the binding that is input
// Need to make dynamic, search for the trigger that is input

switch triggerType := strings.ToUpper(configuration.Binding.Type); triggerType {
case bindingTypeHTTP:
switch triggerType := strings.ToUpper(configuration.Trigger.Type); triggerType {
case TriggerTypeHTTP:
sdk.LoggingClient.Info("HTTP trigger selected")
t = &http.Trigger{Configuration: configuration, Runtime: runtime, Webserver: sdk.webserver, EdgeXClients: sdk.EdgexClients}

case bindingTypeMessageBus,
bindingTypeEdgeXMessageBus: // Allows for more explicit name now that we have plain MQTT option also
case TriggerTypeMessageBus:
sdk.LoggingClient.Info("EdgeX MessageBus trigger selected")
t = &messagebus.Trigger{Configuration: configuration, Runtime: runtime, EdgeXClients: sdk.EdgexClients}

case bindingTypeMQTT:
case TriggerTypeMQTT:
sdk.LoggingClient.Info("External MQTT trigger selected")
t = mqtt.NewTrigger(configuration, runtime, sdk.EdgexClients, sdk.secretProvider)

Expand All @@ -111,7 +109,7 @@ func (sdk *AppFunctionsSDK) setupTrigger(configuration *common.ConfigurationStru
return nil
}
} else {
sdk.LoggingClient.Error(fmt.Sprintf("Invalid Trigger type of '%s' specified", configuration.Binding.Type))
sdk.LoggingClient.Error(fmt.Sprintf("Invalid Trigger type of '%s' specified", configuration.Trigger.Type))
}
}

Expand Down
Loading

0 comments on commit 8767d03

Please sign in to comment.