Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

BREAKING CHANGE: private repository moved to public github. #2

Merged
merged 34 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9454574
BREAKING CHANGE: private repository moved to public github.
adamcbrown Jul 18, 2018
59c38de
Updated to have GetCredentials and allows for v2 mappings
adamcbrown Jul 20, 2018
e44e961
fix: added tests and renamed files to Go Convention
Jul 20, 2018
f957b18
Added Deep Search functionality to parse.go
adamcbrown Jul 20, 2018
1f7ac07
fix: added tests for v2/mappings (untested)
Jul 20, 2018
613aba9
Merge branch 'master' of https://github.com/adamcbrown/ibm-cloud-env-…
Jul 20, 2018
076ad93
added stuff
Jul 20, 2018
26d83ca
Properly returns dictionaries in v2
adamcbrown Jul 20, 2018
4315d67
fix: mappingsv2_test.go
Jul 20, 2018
551ce0b
fix: updated Readme with user provided part
Jul 23, 2018
8609060
fix: README.md
Jul 23, 2018
b85b233
fix: Updated GetCredentials to take a json string instead of a gjson …
adamcbrown Jul 23, 2018
6af8834
Merge branch 'master' of https://github.com/adamcbrown/ibm-cloud-env-…
adamcbrown Jul 23, 2018
c1f58f0
fix: renaming functions/files
Jul 23, 2018
f753dfc
Merge branch 'master' of https://github.com/adamcbrown/ibm-cloud-env-…
Jul 23, 2018
06232d0
fix: Update README Supported Search Pattern Types
sydney-ng Jul 24, 2018
2007280
fix: removed comments on cloud_env.go
sydney-ng Jul 24, 2018
09f211f
fix: removed comments mappingsv1_test.go
sydney-ng Jul 24, 2018
4f4e465
fix: remove comments mappings_test.go
sydney-ng Jul 24, 2018
dae7f9d
chore: remove comments mappingsv2_test.go
sydney-ng Jul 24, 2018
05d71cb
fix: updated gopkg to include all libraries
adamcbrown Jul 24, 2018
f91e14e
chore: Update README.md
sydney-ng Jul 24, 2018
1ceb370
chore: fixed indentations to only use spaces
adamcbrown Jul 24, 2018
fc3c3d8
chore: .go files are correctly formatted
adamcbrown Jul 25, 2018
1cc7a04
chore: fix rephrases
sydney-ng Jul 25, 2018
13324a1
chore: Replaced the term module with package
adamcbrown Jul 25, 2018
d143ea1
Updated README.md with new user-provided pattern
adamcbrown Jul 27, 2018
bf3df10
Added copyright to credentials.go
adamcbrown Jul 27, 2018
7c251b9
chore: fixed copyright alignment
sydney-ng Jul 27, 2018
df4ad28
fix: fixed alignment of filtered credentials
Jul 27, 2018
d5a926d
fix: added travis & fixed linting errors
Jul 27, 2018
c11bd0a
fix: removed travis comments
Jul 27, 2018
80b6b75
fix: add user provided for default unknown prefix
sydney-ng Jul 27, 2018
72185c9
Merge branch 'master' of https://github.com/adamcbrown/ibm-cloud-env-…
Jul 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BREAKING CHANGE: private repository moved to public github.
Co-authored-by: sydney-ng <sydney.ng@yahoo.com>
  • Loading branch information
adamcbrown and sydney-ng committed Jul 18, 2018
commit 9454574fdfb48c8d3767aaae68a450762666e643
2 changes: 2 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[commit]
template = .gittemplate
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
vendor/
.DS_Store

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
20 changes: 20 additions & 0 deletions .gittemplate
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Type(<scope>): <subject>
# <body>
# <footer>

# Type should be one of the following:
# * BREAKING CHANGES (breaking change) [major release -- 1.x.x]
# * feat (new feature) [minor release -- x.1.x]
# * fix (bug fix) [patch release -- 0.0.x]
# * docs (changes to documentation)
# * style (formatting, missing semi colons, etc; no code change)
# * refactor (refactoring production code)
# * test (adding missing tests, refactoring tests; no production code change)
# * chore (updating grunt tasks etc; no production code change)
# Scope is just the scope of the change. Something like (admin) or (teacher).
# Subject should use clear words and say what you did.
# The body should go into detail about changes made.
# The footer should contain any Github (or other tool) issue references or actions.

# For a full example of how to write a good commit message, check out
# https://conventionalcommits.org/
48 changes: 48 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/tidwall/gjson"
version = "1.1.2"

[prune]
go-tests = true
unused-packages = true
80 changes: 78 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,78 @@
# ibm-cloud-env-golang
IBM Cloud Environment for Go
# IBM Cloud Environment

The `ibm-cloud-env-golang` module allows to abstract environment variables from various Cloud compute providers, such as, but not limited to, CloudFoundry and Kubernetes, so the application could be environment-agnostic.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rephrase: The ibm-cloud-env-golang module allows for the abstraction of environment variables from various Cloud compute providers, such as, but not limited to, CloudFoundry and Kubernetes, so the application can be environment-agnostic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not sure how to rephrase this. This paragraph was taken from the IBM-Cloud-Env repositiory


The module allows to define an array of search patterns that will be executed one by one until required value is found.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rephrase: The module allows for an array of search patterns that will be executed one by one until required value is found.


### Installation

```bash
go get github.com/ibm-developer/ibm-cloud-env-golang
```

### Usage

Create a JSON file containing your mappings and initialize the module

```golang
import "github.com/ibm-developer/ibm-cloud-env-golang"

//in main function
IBMCloudEnv.init("/path/to/the/mappings/file/relative/to/prject/root")
```

#### Supported search patterns types
ibm-cloud-config supports searching for values using three search pattern types - cloudfoundry, env, file.
- Using `cloudfoundry` allows to search for values in VCAP_SERVICES and VCAP_APPLICATIONS environment variables
- Using `env` allows to search for values in environment variables
- Using `file` allows to search for values in text/json files

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add user-provided pattern here too

#### Example search patterns
- cloudfoundry:service-instance-name - searches through parsed VCAP_SERVICES environment variable and returns the `credentials` object of the matching service instance name
- cloudfoundry:$.JSONPath - searches through parsed VCAP_SERVICES and VCAP_APPLICATION environment variables and returns the value that corresponds to JSONPath
- env:env-var-name - returns environment variable named "env-var-name"
- env:env-var-name:$.JSONPath - attempts to parse the environment variable "env-var-name" and return a value that corresponds to JSONPath
- file:/server/config.text - returns content of /server/config.text file
- file:/server/config.json:$.JSONPath - reads the content of /server/config.json file, tries to parse it, returns the value that corresponds to JSONPath

#### mappings.json file example
```javascript
{
"service1-credentials": {
"searchPatterns": [
"cloudfoundry:my-service1-instance-name",
"env:my-service1-credentials",
"file:/localdev/my-service1-credentials.json"
]
},
"service2-username": {
"searchPatterns":[
"cloudfoundry:$.service2[@.name=='my-service2-instance-name'].credentials.username",
"env:my-service2-credentials:$.username",
"file:/localdev/my-service1-credentials.json:$.username"
]
}
}
```

### Using the values in application

In your application retrieve the values using below commands

```golang
service1credentials := IBMCloudEnv.getDictionary("service1-credentials") // this will be a dictionary
service2username := IBMCloudEnv.getString("service2-username") // this will be a string
```

Following the above approach your application can be implemented in an runtime-environment agnostic way, abstracting differences in environment variable management introduced by different cloud compute providers.


## Publishing Changes

In order to publish changes, you will need to fork the repository or ask to join the `ibm-developer` org and branch off the `master` branch.

Make sure to follow the [conventional commit specification](https://conventionalcommits.org/) before contributing. To help you with commit a commit template is provide. Run `config.sh` to initialize the commit template to your `.git/config`

Once you are finished with your changes, run `go test` to make sure all tests pass.

Do a pull request against `master`, make sure the build passes. A team member will review and merge your pull request.
8 changes: 8 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

CMD=$(cat .git/config | grep "commit")
if [ -z $CMD ]; then
echo "$(cat .git/config)\n$(cat .gitconfig)" > ./.git/config
else
echo "commit file template already saved"
fi
162 changes: 162 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* © Copyright IBM Corp. 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package IBMCloudEnv
import (
"testing"
"os"
"github.com/tidwall/gjson"
)

const jsonObject =
`{
"service1": [
{
"name": "service1-name1",
"credentials": {
"username": "service1-username1"
}
},
{
"name": "service1-name2",
"credentials": {
"username": "service1-username2"
}
}
],
"user-provided": [
{
"name": "service2-name1",
"credentials":{
"username": "service2-username1"
}
}
]
}`

const vcap_application = `{"application_name": "test-application"}`
const var_string = `test-12345`
const credentials = `{"credentials": {
"username": "env-var-json-username"
}}`

func setEnvVariable() {
os.Setenv("VCAP_APPLICATION", vcap_application)
os.Setenv("VCAP_SERVICES", jsonObject)
os.Setenv("ENV_VAR_STRING", var_string)
os.Setenv("ENV_VAR_JSON", credentials)

Initialize("/invalid-file-name")
Initialize("mappings.json")
}


func TestPlainTextFile(t *testing.T) {
setEnvVariable()
testString, _ := GetString("file_var1")
if testString != "plain-text-string" {
t.Errorf("testSTring is: " + testString)
t.Errorf("can't read " + testString + " from GetString()")
}
result := GetDictionary("file_var1").Get("value")
if result.String() != "plain-text-string" {
t.Errorf("can't read " + result.String() + " text from GetDictionary()")
}
}

func TestJsonFileAndPath(t *testing.T){
setEnvVariable()
testString, _ := GetString("file_var2")
if testString != gjson.Parse("{\"level2\":12345}").String() {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "{\"level2\":12345}")
}

testString = GetDictionary("file_var2").Get("level2").String()
if testString != "12345" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "12345")
}
}

func TestReadVcapsWithJsonPath(t *testing.T){
setEnvVariable()
testString, _ := GetString("cf_var2")

if testString != "service1-username1" {
t.Errorf("can't read " + testString + " from GetString()")
}

result := GetDictionary("cf_var2").Get("value")
if result.String() != "service1-username1" {
t.Errorf("can't read " + result.String() + " text from GetDictionary()")
}

testString2, _ := GetString("cf_var3")
if testString2 != "test-application" {
t.Errorf("can't read " + testString2 + " from GetString()")
}
result2 := GetDictionary("cf_var3").Get("value")
if result2.String() != "test-application" {
t.Errorf("can't read " + result2.String() + " text from GetDictionary()")
}

//removed this test because there wasn't a cf_var4 in mappings.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to delete this block before merging

/*testString3, _ := GetString("cf_var4")
if testString3 != "service1-username1" {
t.Errorf("can't read " + testString3 + " from GetString()")
}
result3 := GetDictionary("cf_var4").Get("value")
if result3.String() != "service1-username1" {
t.Errorf("can't read " + result3.String() + " text from GetDictionary()")
}*/
}

func TestSimpleStringFromEnvVar(t *testing.T){
setEnvVariable()
testString, _ := GetString("env_var1")
if testString != "test-12345" {
t.Errorf("can't read " + testString + " from GetString()")
}
result := GetDictionary("env_var1").Get("value")
if result.String() != "test-12345" {
t.Errorf("can't read " + result.String() + " text from GetDictionary()")
}
}

func TestJsonFromEnvVar(t *testing.T){
setEnvVariable()
testString, _ := GetString("env_var2")
if testString != credentials {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, credentials)
}

testString = GetDictionary("env_var2").Get("credentials").Get("username").String()
if testString != "env-var-json-username" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "env-var-json-username")
}
}

func TestJsonPathFromEnvVar(t *testing.T){
setEnvVariable()
testString, _ := GetString("env_var3")
if testString != "env-var-json-username" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "env-var-json-username")
}

testString = GetDictionary("env_var3").Get("value").String()
if testString != "env-var-json-username" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "env-var-json-username")
}
}
Loading