Skip to content

Commit

Permalink
refactor: Remove deprecated environment variables and related code
Browse files Browse the repository at this point in the history
closes #337

BREAKING CHANGE: The following environment variables no longer supported:
- `edgex_profile` (replaced by uppercase version)
- `edgex_service`

Signed-off-by: lenny <[email protected]>
  • Loading branch information
lenny committed Feb 25, 2021
1 parent 5ffc6c7 commit e44cd7f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 71 deletions.
49 changes: 0 additions & 49 deletions appsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"errors"
"fmt"
nethttp "net/http"
"net/url"
"os"
"os/signal"
"reflect"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -63,13 +61,8 @@ import (
const (
// ProfileSuffixPlaceholder is used to create unique names for profiles
ProfileSuffixPlaceholder = "<profile>"
envV1Profile = "edgex_profile" // TODO: Remove for release v2.0.0
envProfile = "EDGEX_PROFILE"
envServiceKey = "EDGEX_SERVICE_KEY"
envV1Service = "edgex_service" // deprecated TODO: Remove for release v2.0.0
envServiceProtocol = "Service_Protocol" // Used for envV1Service processing TODO: Remove for release v2.0.0
envServiceHost = "Service_Host" // Used for envV1Service processing TODO: Remove for release v2.0.0
envServicePort = "Service_Port" // Used for envV1Service processing TODO: Remove for release v2.0.0

bindingTypeMessageBus = "MESSAGEBUS"
bindingTypeEdgeXMessageBus = "EDGEX-MESSAGEBUS"
Expand Down Expand Up @@ -377,14 +370,6 @@ func (sdk *AppFunctionsSDK) Initialize() error {

sdk.LoggingClient.Info(fmt.Sprintf("Starting %s %s ", sdk.ServiceKey, internal.ApplicationVersion))

// The use of the edgex_service environment variable (only used for App Services) has been deprecated
// and not included in the common bootstrap. Have to be handle here before calling into the common bootstrap
// so proper overrides are set.
// TODO: Remove for release v2.0.0
if err := sdk.handleEdgexService(); err != nil {
return err
}

sdk.config = &common.ConfigurationStruct{}
dic := di.NewContainer(di.ServiceConstructorMap{
container.ConfigurationName: func(get di.Get) interface{} {
Expand Down Expand Up @@ -516,11 +501,6 @@ func (sdk *AppFunctionsSDK) setServiceKey(profile string) {

// Have to handle environment override here before common bootstrap is used so it is passed the proper service key
profileOverride := os.Getenv(envProfile)
if len(profileOverride) == 0 {
// V2 not set so try V1
profileOverride = os.Getenv(envV1Profile) // TODO: Remove for release v2.0.0:
}

if len(profileOverride) > 0 {
profile = profileOverride
}
Expand All @@ -533,32 +513,3 @@ func (sdk *AppFunctionsSDK) setServiceKey(profile string) {
// No profile specified so remove the placeholder text
sdk.ServiceKey = strings.Replace(sdk.ServiceKey, ProfileSuffixPlaceholder, "", 1)
}

// handleEdgexService checks to see if the "edgex_service" environment variable is set and if so creates appropriate config
// overrides from the URL parts.
// TODO: Remove for release v2.0.0
func (sdk *AppFunctionsSDK) handleEdgexService() error {
if envValue := os.Getenv(envV1Service); envValue != "" {
u, err := url.Parse(envValue)
if err != nil {
return fmt.Errorf(
"failed to parse 'edgex_service' environment value '%s' as a URL: %s",
envValue,
err.Error())
}

_, err = strconv.ParseInt(u.Port(), 10, 0)
if err != nil {
return fmt.Errorf(
"failed to parse port from 'edgex_service' environment value '%s' as an integer: %s",
envValue,
err.Error())
}

os.Setenv(envServiceProtocol, u.Scheme)
os.Setenv(envServiceHost, u.Hostname())
os.Setenv(envServicePort, u.Port())
}

return nil
}
32 changes: 10 additions & 22 deletions appsdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ import (
"reflect"
"testing"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/edgexfoundry/app-functions-sdk-go/v2/appcontext"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/runtime"
triggerHttp "github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/http"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/messagebus"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/webserver"

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

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var lc logger.LoggingClient
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestAddBackgroundPublisher(t *testing.T) {
}

require.NotNil(t, pub.output, "publisher should have an output channel set")
require.NotNil(t, sdk.backgroundChannel, "sdk should have a background channel set for passing to trigger intitialization")
require.NotNil(t, sdk.backgroundChannel, "sdk should have a background channel set for passing to trigger initialization")

// compare addresses since types will not match
assert.Equal(t, fmt.Sprintf("%p", sdk.backgroundChannel), fmt.Sprintf("%p", pub.output),
Expand Down Expand Up @@ -367,14 +368,6 @@ func TestSetServiceKey(t *testing.T) {
originalServiceKey: "MyAppService-" + ProfileSuffixPlaceholder,
expectedServiceKey: "MyAppService-mqtt-export",
},
{
name: "Profile specified with V1 override",
profile: "rules-engine",
profileEnvVar: envV1Profile,
profileEnvValue: "rules-engine-mqtt",
originalServiceKey: "MyAppService-" + ProfileSuffixPlaceholder,
expectedServiceKey: "MyAppService-rules-engine-mqtt",
},
{
name: "Profile specified with V2 override",
profile: "rules-engine",
Expand All @@ -383,13 +376,6 @@ func TestSetServiceKey(t *testing.T) {
originalServiceKey: "MyAppService-" + ProfileSuffixPlaceholder,
expectedServiceKey: "MyAppService-rules-engine-redis",
},
{
name: "No profile specified with V1 override",
profileEnvVar: envV1Profile,
profileEnvValue: "sample",
originalServiceKey: "MyAppService-" + ProfileSuffixPlaceholder,
expectedServiceKey: "MyAppService-sample",
},
{
name: "No profile specified with V2 override",
profileEnvVar: envProfile,
Expand Down Expand Up @@ -444,10 +430,12 @@ func TestSetServiceKey(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if len(test.profileEnvVar) > 0 && len(test.profileEnvValue) > 0 {
os.Setenv(test.profileEnvVar, test.profileEnvValue)
err := os.Setenv(test.profileEnvVar, test.profileEnvValue)
require.NoError(t, err)
}
if len(test.serviceKeyEnvValue) > 0 {
os.Setenv(envServiceKey, test.serviceKeyEnvValue)
err := os.Setenv(envServiceKey, test.serviceKeyEnvValue)
require.NoError(t, err)
}
defer os.Clearenv()

Expand Down

0 comments on commit e44cd7f

Please sign in to comment.