Skip to content

Commit efaf3c8

Browse files
author
Frank Martinez
authored
Property cleanup (#202)
* cleanup property doc; use non-deprecated method for registration * tweak example * cleanup doc * cleanup doc * cleanup doc * automatically load built-in external property resolvers * fix cyclic dependency
1 parent e4e0e44 commit efaf3c8

File tree

5 files changed

+68
-35
lines changed

5 files changed

+68
-35
lines changed

app/propertyresolver/env.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"strings"
77

88
"github.com/project-flogo/core/data/property"
9-
"github.com/project-flogo/core/engine"
109
"github.com/project-flogo/core/support/log"
1110
)
1211

1312
const EnvAppPropertyEnvConfigKey = "FLOGO_APP_PROPS_ENV"
13+
const ResolverNameEnv = "env"
1414

1515
type PropertyMappings struct {
1616
Mappings map[string]string `json:"mappings"`
@@ -36,7 +36,7 @@ func init() {
3636
panic("")
3737
}
3838
}
39-
_ = property.RegisterPropertyResolver(resolver)
39+
_ = property.RegisterExternalResolver(resolver)
4040
}
4141
}
4242

@@ -55,7 +55,7 @@ type EnvVariableValueResolver struct {
5555
}
5656

5757
func (resolver *EnvVariableValueResolver) Name() string {
58-
return engine.PropertyResolverEnv
58+
return ResolverNameEnv
5959
}
6060

6161
func (resolver *EnvVariableValueResolver) LookupValue(key string) (interface{}, bool) {

app/propertyresolver/json.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package propertyresolver
22

33
import (
44
"encoding/json"
5-
"github.com/project-flogo/core/data/property"
6-
"github.com/project-flogo/core/engine"
75
"io/ioutil"
86
"os"
97
"strings"
108

9+
"github.com/project-flogo/core/data/property"
1110
"github.com/project-flogo/core/support/log"
1211
)
1312

@@ -18,6 +17,7 @@ var preload = make(map[string]interface{})
1817
// Comma separated list of json files overriding default application property values
1918
// e.g. FLOGO_APP_PROPS_JSON=app1.json,common.json
2019
const EnvAppPropertyFileConfigKey = "FLOGO_APP_PROPS_JSON"
20+
const ResolverNameJson = "json"
2121

2222
func init() {
2323

@@ -26,7 +26,7 @@ func init() {
2626
filePaths := getExternalFiles()
2727
if filePaths != "" {
2828
// Register value resolver
29-
_ = property.RegisterPropertyResolver(&JSONFileValueResolver{})
29+
_ = property.RegisterExternalResolver(&JSONFileValueResolver{})
3030

3131
// preload props from files
3232
files := strings.Split(filePaths, ",")
@@ -66,7 +66,7 @@ type JSONFileValueResolver struct {
6666
}
6767

6868
func (resolver *JSONFileValueResolver) Name() string {
69-
return engine.PropertyResolverJson
69+
return ResolverNameJson
7070
}
7171

7272
func (resolver *JSONFileValueResolver) LookupValue(toResolve string) (interface{}, bool) {

data/property/external.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717

1818
// Resolver used to resolve property value from external configuration like env, file etc
1919
type ExternalResolver interface {
20+
// Name of the resolver (e.g., consul)
2021
Name() string
2122
// Should return value and true if the given key exists in the external configuration otherwise should return nil and false.
2223
LookupValue(key string) (interface{}, bool)

docs/properties.md

+57-23
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,33 @@ Even though the engine itself doesn't support property grouping, this can be acc
7272

7373
These properties can be accessed via `$property[PURCHASE.SERVICE.DB.URL]` or `$property[INVENTORY.SERVICE.DB.URL]`
7474

75-
### Overriding poperties at runtime
75+
### Overriding properties at runtime
76+
77+
In order to override properties at runtime, you have to enable external property resolvers.
78+
79+
This can be done by setting the `FLOGO_APP_PROP_RESOLVERS` environment variable. Currently, there are two built-in external
80+
property resolvers: json(JSON) and env(Environment Variable).
81+
82+
83+
```terminal
84+
FLOGO_APP_PROP_RESOLVERS=env,json ./<app_binary>
85+
```
7686

7787
You can override app properties at runtime in two ways:
7888

79-
#### Using JSON
89+
#### Resolver: json
90+
91+
When using the `json` property resolver, you can provide a comma separated list of json files that
92+
will override the application's existing property values.
93+
```env
94+
FLOGO_APP_PROPS_JSON=app1.json,common.json
95+
```
8096

81-
Define your new value for a given app prop in a json file as shown below:
97+
**Example**
8298

83-
props.json:
99+
Let's say you want to override some of your properties. You will need to define your new value for a given property in your json file.
100+
101+
_props.json_
84102

85103
```json
86104
{
@@ -89,60 +107,76 @@ props.json:
89107
}
90108
```
91109

92-
Run the application with the environment variable `FLOGO_APP_PROPS_OVERRIDE` set to `props.json`. For example:
110+
Now run your application:
93111

94112
```terminal
95-
FLOGO_APP_PROPS_OVERRIDE=props.json ./MyApp
113+
114+
export FLOGO_APP_PROPS_JSON=props.json
115+
FLOGO_APP_PROP_RESOLVERS=json ./MyApp
96116
```
97-
or
117+
118+
#### Resolver: env
119+
120+
In order to override properties using environment variables, you just need enable the `env` property resolver
98121

99122
```terminal
100-
export FLOGO_APP_PROPS_OVERRIDE=props.json
101-
./MyApp
123+
FLOGO_APP_PROP_RESOLVERS=env ./<app_binary>
102124
```
103125

126+
**Example**
104127

105-
#### Using Key/Value pair
106-
107-
Run the application with the environment variable `FLOGO_APP_PROPS_OVERRIDE` set to the key/value pairs. For example:
128+
Let's say you want to override `myprop` property in your app. You would do the following:
108129

109130
```terminal
110-
FLOGO_APP_PROPS_OVERRIDE="MyProp1=This is newvalue,MyProp2=30" ./MyApp
131+
132+
export myprop=bar
133+
FLOGO_APP_PROP_RESOLVERS=env ./MyApp
111134
```
112135

113-
### Working with external configuration management services
114136

115-
You can plug-in your proeprty value resolver to resolve application property values from external configuration management services, such as, Consul, Spring Cloud Config etc. Just implement the following interface and register implementation with the runtime:
137+
### Custom External Resolver
138+
139+
You can plug-in your own property resolver to resolve application property values from external configuration management services, such as, Consul, Spring Cloud Config etc. Just implement the following interface and register implementation with the runtime:
116140

117141
```go
118-
// PropertyValueResolver used to resolve value from external configuration like env, file etc
119-
type PropertyValueResolver interface {
120-
// Should return value and true if the given application property exists in the external configuration otherwise should return nil and false.
121-
LookupValue(propertyName string) (interface{}, bool)
142+
// Resolver used to resolve property value from external configuration like env, file etc
143+
type ExternalResolver interface {
144+
// Name of the resolver (e.g., consul)
145+
Name() string
146+
// Should return value and true if the given key exists in the external configuration otherwise should return nil and false.
147+
LookupValue(key string) (interface{}, bool)
122148
}
149+
123150
```
124151

125152
#### Sample Resolver
126153

127154
```go
128155
package sampleresolver
129156

157+
import "github.com/project-flogo/core/data/property"
158+
159+
130160
type SamplePropertyResolver struct {
131161
}
132162

133163
func init() {
134-
app.RegisterPropertyValueResolver("sampleresolver", &SamplePropertyResolver{})
164+
_ = property.RegisterExternalResolver(&SamplePropertyResolver{})
165+
}
166+
167+
func (resolver *SamplePropertyResolver) Name() string {
168+
return "sampleresolver"
135169
}
136170

137171
func (resolver *SamplePropertyResolver) LookupValue(propertyName string) (interface{}, bool) {
138172
// Resolve property value
139-
return some_value, true
173+
return "some_value"", true
140174
}
141175
```
142-
*Note: In order for your resolver to be loaded in the go code, you need to add an entry to your resolver in the imports section of the the engine.json*
176+
*Note: In order for your resolver to be loaded in the go code, you need to add an entry to your resolver in the imports section of the engine.json*
143177
144178
145-
Set the `FLOGO_APP_PROPS_RESOLVERS` env var to `sampleresolver` while running application. For example:
179+
Set the `FLOGO_APP_PROP_RESOLVERS` environment variable to `sampleresolver` while running your application. For example:
146180
147181
```terminal
148182
FLOGO_APP_PROPS_RESOLVERS=sampleresolver ./<app_binary>

engine/envconfig.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"strings"
88

9+
"github.com/project-flogo/core/app/propertyresolver"
910
"github.com/project-flogo/core/data/property"
1011
"github.com/project-flogo/core/engine/runner"
1112
"github.com/project-flogo/core/support/log"
@@ -32,9 +33,6 @@ const (
3233

3334
ValueRunnerTypePooled = "POOLED"
3435
ValueRunnerTypeDirect = "DIRECT"
35-
36-
PropertyResolverEnv = "env"
37-
PropertyResolverJson = "json"
3836
)
3937

4038
func IsSchemaSupportEnabled() bool {
@@ -149,7 +147,7 @@ func GetAppPropertyValueResolvers(logger log.Logger) string {
149147
var resolvers, builtinResolvers []string
150148

151149
for resolver := range property.RegisteredResolvers {
152-
if resolver != PropertyResolverEnv && resolver != PropertyResolverJson {
150+
if resolver != propertyresolver.ResolverNameEnv && resolver != propertyresolver.ResolverNameJson {
153151
resolvers = append(resolvers, resolver)
154152
} else {
155153
builtinResolvers = append(builtinResolvers, resolver)
@@ -163,7 +161,7 @@ func GetAppPropertyValueResolvers(logger log.Logger) string {
163161
}
164162

165163
if len(builtinResolvers) == 2 { // force priority between the two builtin resolvers
166-
builtinResolvers = []string{PropertyResolverEnv, PropertyResolverJson}
164+
builtinResolvers = []string{propertyresolver.ResolverNameEnv, propertyresolver.ResolverNameJson}
167165
}
168166

169167
resolvers = append(resolvers, builtinResolvers...)

0 commit comments

Comments
 (0)