Skip to content

Commit a694cb3

Browse files
authored
Merge pull request #9 from srikanthkm/master
Go SDK update
2 parents 7714b5f + 61682d1 commit a694cb3

File tree

15 files changed

+214
-146
lines changed

15 files changed

+214
-146
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
# You are encouraged to use static refs such as tags, instead of branch name
1212
#
1313
# Running "pre-commit autoupdate" would automatically updates rev to latest tag
14-
rev: 0.13.1+ibm.35.dss
14+
rev: 0.13.1+ibm.36.dss
1515
hooks:
1616
- id: detect-secrets # pragma: whitelist secret
1717
# Add options for detect-secrets-hook binary. You can run `detect-secrets-hook --help` to list out all possible options.

.secrets.baseline

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "^.secrets.baseline$|go.sum|examples/SampleApp/go.sum|vendor",
44
"lines": null
55
},
6-
"generated_at": "2021-05-26T13:33:49Z",
6+
"generated_at": "2021-05-27T19:00:52Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -25,6 +25,7 @@
2525
"name": "CloudantDetector"
2626
},
2727
{
28+
"ghe_instance": "github.ibm.com",
2829
"name": "GheDetector"
2930
},
3031
{
@@ -69,15 +70,15 @@
6970
"hashed_secret": "d4c3d66fd0c38547a3c7a4c6bdc29c36911bc030",
7071
"is_secret": false,
7172
"is_verified": false,
72-
"line_number": 62,
73+
"line_number": 63,
7374
"type": "Secret Keyword",
7475
"verified_result": null
7576
},
7677
{
7778
"hashed_secret": "a94906aadfc6ddd1659ef976cf1b5ecae197b9a3",
7879
"is_secret": false,
7980
"is_verified": false,
80-
"line_number": 129,
81+
"line_number": 131,
8182
"type": "Secret Keyword",
8283
"verified_result": null
8384
}
@@ -93,7 +94,7 @@
9394
}
9495
]
9596
},
96-
"version": "0.13.1+ibm.35.dss",
97+
"version": "0.13.1+ibm.36.dss",
9798
"word_list": {
9899
"file": null,
99100
"hash": null

README.md

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ properties for distributed applications centrally.
2525

2626
## Installation
2727

28+
The current version of this SDK: 0.1.0
29+
2830
There are a few different ways to download and install the IBM App Configuration Go SDK project for use by your Go
2931
application:
3032

@@ -55,7 +57,10 @@ Initialize the sdk to connect with your App Configuration service instance.
5557
```go
5658
appConfiguration := AppConfiguration.GetInstance()
5759
appConfiguration.Init("region", "guid", "apikey")
58-
appConfiguration.SetContext("collectionId", "environmentId")
60+
61+
collectionId := "airlines-webapp"
62+
environmentId := "dev"
63+
appConfiguration.SetContext(collectionId, environmentId)
5964
```
6065

6166
- region : Region name where the App Configuration service instance is created. Use
@@ -78,37 +83,42 @@ You can also work offline with local configuration file and perform feature and
7883
After [`appConfiguration.Init("region", "guid", "apikey")`](#using-the-sdk), follow the below steps
7984

8085
```go
81-
appConfiguration.SetContext("collectionId", "environmentId", AppConfiguration.ContextOptions{
82-
ConfigurationFile: "path/to/configuration/file.json",
83-
LiveConfigUpdateEnabled: false,
86+
appConfiguration.SetContext("airlines-webapp", "dev", AppConfiguration.ContextOptions{
87+
ConfigurationFile: "saflights/flights.json",
88+
LiveConfigUpdateEnabled: false
8489
})
8590
```
8691

8792
- ConfigurationFile: Path to the JSON file, which contains configuration details.
8893
- LiveConfigUpdateEnabled: Set this value to `false` if the new configuration values shouldn't be fetched from the
8994
server. Make sure to provide a proper JSON file in the path. By default, this value is enabled.
9095

96+
### Permissions required by SDK
97+
Add write permission for `non-root` users to `appconfiguration.json` file which is used as cache in AppConfiguration SDK.
98+
AppConfiguration cache location will be the application root folder.
99+
91100
## Get single feature
92101

93102
```go
94-
feature := appConfiguration.GetFeature("featureId")
103+
feature, err := appConfiguration.GetFeature("online-check-in")
104+
if err == nil {
105+
fmt.Println("Feature Name", feature.GetFeatureName())
106+
fmt.Println("Feature Id", feature.GetFeatureId())
107+
fmt.Println("Feature Type", feature.GetFeatureDataType())
95108

96-
if (feature.IsEnabled()) {
109+
if (feature.IsEnabled()) {
97110
// feature flag is enabled
98-
} else {
111+
} else {
99112
// feature flag is disabled
113+
}
100114
}
101-
fmt.Println("Feature Name", feature.GetFeatureName())
102-
fmt.Println("Feature Id", feature.GetFeatureId())
103-
fmt.Println("Feature Type", feature.GetFeatureDataType())
104-
fmt.Println("Feature is enabled", feature.IsEnabled())
105115
```
106116

107117
## Get all features
108118

109119
```go
110120
features := appConfiguration.GetFeatures()
111-
feature := features["featureId"]
121+
feature := features["online-check-in"]
112122

113123
fmt.Println("Feature Name", feature.GetFeatureName())
114124
fmt.Println("Feature Id", feature.GetFeatureId())
@@ -118,34 +128,35 @@ fmt.Println("Feature is enabled", feature.IsEnabled())
118128

119129
## Evaluate a feature
120130

121-
You can use the ` feature.GetCurrentValue(identityId, identityAttributes)` method to evaluate the value of the feature
122-
flag. You should pass an unique identityId as the parameter to perform the feature flag evaluation. If the feature flag
131+
You can use the ` feature.GetCurrentValue(entityId, entityAttributes)` method to evaluate the value of the feature
132+
flag. You should pass an unique entityId as the parameter to perform the feature flag evaluation. If the feature flag
123133
is configured with segments in the App Configuration service, you can set the attributes values as a map.
124134

125135
```go
126-
identityId := "identityId"
127-
identityAttributes := make(map[string]interface{})
128-
identityAttributes["email"] = "ibm.com"
129-
identityAttributes["city"] = "Bangalore"
136+
entityId := "john_doe"
137+
entityAttributes := make(map[string]interface{})
138+
entityAttributes["city"] = "Bangalore"
139+
entityAttributes["country"] = "India"
130140

131-
featureVal := feature.GetCurrentValue(identityId, identityAttributes)
141+
featureVal := feature.GetCurrentValue(entityId, entityAttributes)
132142
```
133143

134144
## Get single property
135145

136146
```go
137-
property := appConfiguration.GetProperty("propertyId")
138-
139-
fmt.Println("Property Name", property.GetPropertyName())
140-
fmt.Println("Property Id", property.GetPropertyId())
141-
fmt.Println("Property Type", property.GetPropertyDataType())
147+
property, err := appConfiguration.GetProperty("check-in-charges")
148+
if err == nil {
149+
fmt.Println("Property Name", property.GetPropertyName())
150+
fmt.Println("Property Id", property.GetPropertyId())
151+
fmt.Println("Property Type", property.GetPropertyDataType())
152+
}
142153
```
143154

144155
## Get all properties
145156

146157
```go
147158
properties := appConfiguration.GetProperties()
148-
property := properties["propertyId"]
159+
property := properties["check-in-charges"]
149160

150161
fmt.Println("Property Name", property.GetPropertyName())
151162
fmt.Println("Property Id", property.GetPropertyId())
@@ -154,22 +165,22 @@ fmt.Println("Property Type", property.GetPropertyDataType())
154165

155166
## Evaluate a property
156167

157-
You can use the ` property.GetCurrentValue(identityId, identityAttributes)` method to evaluate the value of the
158-
property. You should pass an unique identityId as the parameter to perform the property evaluation. If the property is
168+
You can use the ` property.GetCurrentValue(entityId, entityAttributes)` method to evaluate the value of the
169+
property. You should pass an unique entityId as the parameter to perform the property evaluation. If the property is
159170
configured with segments in the App Configuration service, you can set the attributes values as a map.
160171

161172
```go
162-
identityId := "identityId"
163-
identityAttributes := make(map[string]interface{})
164-
identityAttributes["email"] = "ibm.com"
165-
identityAttributes["city"] = "Bengaluru"
173+
entityId := "john_doe"
174+
entityAttributes := make(map[string]interface{})
175+
entityAttributes["city"] = "Bangalore"
176+
entityAttributes["country"] = "India"
166177

167-
propertyVal := property.GetCurrentValue(identityId, identityAttributes)
178+
propertyVal := property.GetCurrentValue(entityId, entityAttributes)
168179
```
169180

170181
## Set listener for feature or property data changes
171182

172-
To listen to the data changes add the following code in your application
183+
To listen to the configurations changes in your App Configuration service instance, implement the `RegisterConfigurationUpdateListener` event listener as mentioned below
173184

174185
```go
175186
appConfiguration.RegisterConfigurationUpdateListener(func () {

examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Sample Application
1+
# Example Application
22

3-
> **DISCLAIMER**: This is a guideline sample application and is used for demonstrative and illustrative purposes only. This is not a production ready code.
3+
> **DISCLAIMER**: This is a guideline example application and is used for demonstrative and illustrative purposes only. This is not a production ready code.
44
55
## Step 1: Create an instance of App Configuration service
66

@@ -89,7 +89,7 @@ Click Save rule & Add the targeting.
8989
```
9090
- Use the `id` of default environment created on instance creation.
9191

92-
- In the sample app, provide the collectionId, environmentId, featureId & propertyId in file [`main.go`](main.go#L23)
92+
- In the example app, provide the collectionId, environmentId, featureId & propertyId in file [`main.go`](main.go#L23)
9393

9494
## Step 4: Run the app
9595

examples/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ module examples
22

33
go 1.16
44

5-
65
require (
7-
github.com/IBM/appconfiguration-go-sdk v0.0.1
6+
github.com/IBM/appconfiguration-go-sdk v0.1.0
87
github.com/gorilla/mux v1.7.2
98
)

examples/main.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,33 @@ func main() {
1919
appConfiguration := AppConfiguration.GetInstance()
2020
appConfiguration.Init(AppConfiguration.REGION_US_SOUTH, "<guid>", "<apikey>")
2121
appConfiguration.SetContext("<collectionId>", "<environmentId>")
22-
identityId := "user123"
23-
identityAttributes := make(map[string]interface{})
24-
identityAttributes["city"] = "Bangalore"
25-
identityAttributes["radius"] = 60
26-
27-
appConfiguration.RegisterConfigurationUpdateListener(func() {
28-
29-
fmt.Println("\n\nFEATURE FLAG OPERATIONS\n")
30-
feature := appConfiguration.GetFeature("<featureId>")
22+
entityId := "user123"
23+
entityAttributes := make(map[string]interface{})
24+
entityAttributes["city"] = "Bangalore"
25+
entityAttributes["radius"] = 60
26+
27+
fmt.Println("\n\nFEATURE FLAG OPERATIONS\n")
28+
feature, err := appConfiguration.GetFeature("<featureId>")
29+
if err == nil {
3130
fmt.Println("Feature Name:", feature.GetFeatureName())
3231
fmt.Println("Feature Id:", feature.GetFeatureId())
3332
fmt.Println("Feature Data type:", feature.GetFeatureDataType())
3433
fmt.Println("Is Feature enabled?", feature.IsEnabled())
35-
fmt.Println("Feature evaluated value is:", feature.GetCurrentValue(identityId, identityAttributes))
34+
fmt.Println("Feature evaluated value is:", feature.GetCurrentValue(entityId, entityAttributes))
35+
}
3636

37-
fmt.Println("\n\nPROPERTY OPERATIONS\n")
38-
property := appConfiguration.GetProperty("<propertyId>")
37+
fmt.Println("\n\nPROPERTY OPERATIONS\n")
38+
property, err := appConfiguration.GetProperty("<propertyId>")
39+
if err == nil {
3940
fmt.Println("Property Name:", property.GetPropertyName())
4041
fmt.Println("Property Id:", property.GetPropertyId())
4142
fmt.Println("Property Data type:", property.GetPropertyDataType())
42-
fmt.Println("Property evaluated value is:", property.GetCurrentValue(identityId, identityAttributes))
43-
43+
fmt.Println("Property evaluated value is:", property.GetCurrentValue(entityId, entityAttributes))
44+
}
45+
//whenever the configurations get changed/updated on the app configuration service instance the function inside this listener is triggered.
46+
//So, to keep track of live changes to configurations use this listener.
47+
appConfiguration.RegisterConfigurationUpdateListener(func() {
48+
fmt.Println("configurations updated")
4449
})
4550

4651
myRouter := mux.NewRouter().StrictSlash(true)

lib/AppConfiguration.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package lib
1818

1919
import (
20-
"github.com/IBM/appconfiguration-go-sdk/lib/internal/constants"
20+
"errors"
2121
"os"
2222

23+
"github.com/IBM/appconfiguration-go-sdk/lib/internal/constants"
24+
2325
"github.com/IBM/appconfiguration-go-sdk/lib/internal/messages"
2426
"github.com/IBM/appconfiguration-go-sdk/lib/internal/models"
2527

@@ -60,10 +62,10 @@ func (ac *AppConfiguration) Init(region string, guid string, apikey string) {
6062
log.Error(messages.REGION_ERROR)
6163
}
6264
if len(guid) == 0 {
63-
log.Error(messages.APIKEY_ERROR)
65+
log.Error(messages.GUID_ERROR)
6466
}
6567
if len(apikey) == 0 {
66-
log.Error(messages.GUID_ERRROR)
68+
log.Error(messages.APIKEY_ERROR)
6769
}
6870
return
6971
}
@@ -100,11 +102,17 @@ func (ac *AppConfiguration) SetContext(collectionId string, environmentId string
100102
return
101103
}
102104
ac.isInitializedConfig = true
103-
if _, err := os.Stat(constants.FEATURE_FILE); os.IsNotExist(err) {
105+
file, err := os.Stat(constants.FEATURE_FILE)
106+
if os.IsNotExist(err) {
104107
ac.configurationHandlerInstance.loadData()
105108
} else {
106-
ac.configurationHandlerInstance.loadConfigurations()
107-
go ac.configurationHandlerInstance.loadData()
109+
if file.Size() == 0 {
110+
log.Error(constants.FEATURE_FILE + messages.ConfigurationFileEmpty)
111+
ac.configurationHandlerInstance.loadData()
112+
} else {
113+
ac.configurationHandlerInstance.loadConfigurations()
114+
go ac.configurationHandlerInstance.loadData()
115+
}
108116
}
109117
}
110118

@@ -124,12 +132,12 @@ func (ac *AppConfiguration) RegisterConfigurationUpdateListener(fhl configuratio
124132
}
125133
}
126134

127-
func (ac *AppConfiguration) GetFeature(featureId string) models.Feature {
135+
func (ac *AppConfiguration) GetFeature(featureId string) (models.Feature, error) {
128136
if ac.isInitializedConfig == true && ac.configurationHandlerInstance != nil {
129137
return ac.configurationHandlerInstance.getFeature(featureId)
130138
} else {
131139
log.Error(messages.COLLECTION_INIT_ERROR)
132-
return models.Feature{}
140+
return models.Feature{}, errors.New(messages.ERROR_INVALID_FEATURE_ACTION)
133141
}
134142
}
135143
func (ac *AppConfiguration) GetFeatures() map[string]models.Feature {
@@ -140,12 +148,12 @@ func (ac *AppConfiguration) GetFeatures() map[string]models.Feature {
140148
return nil
141149
}
142150
}
143-
func (ac *AppConfiguration) GetProperty(propertyId string) models.Property {
151+
func (ac *AppConfiguration) GetProperty(propertyId string) (models.Property, error) {
144152
if ac.isInitializedConfig == true && ac.configurationHandlerInstance != nil {
145153
return ac.configurationHandlerInstance.getProperty(propertyId)
146154
} else {
147155
log.Error(messages.COLLECTION_INIT_ERROR)
148-
return models.Property{}
156+
return models.Property{}, errors.New(messages.ERROR_INVALID_PROPERTY_ACTION)
149157
}
150158
}
151159
func (ac *AppConfiguration) GetProperties() map[string]models.Property {

0 commit comments

Comments
 (0)