Skip to content

Commit

Permalink
Merge pull request #4 from Piszmog/develop
Browse files Browse the repository at this point in the history
Add additional function for creating a local client to the config server
  • Loading branch information
Piszmog authored Sep 8, 2018
2 parents f272fd5 + bd04ec7 commit f76f714
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
24 changes: 7 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ of the Config Server do not provide the endpoint necessary to retrieve files for
## Example Usage
Below is an example usage of the library to retrieve a file from the Config Server and to retrieve the application's configurations

* For local config client, ensure `CONFIG_SERVER_URLS` is set
* `CONFIG_SERVER_URLS` is a comma separated list of all the base URLs
* For local config client, there are two ways the create a client
1. Call `client.CreateLocalClientFromEnv()`. Set the environment variable `CONFIG_SERVER_URLS`. It is a comma separated list of all the base URLs
2. Call `CreateLocalClient(baseUrls []string)`. Provide the array of base URLs of Config Servers.
* For running in Cloud Foundry, ensure a Config Server is bounded to the application. `VCAP_SERVICES` will be provided as an environment variables with the credentials to access the Config Server
* For connecting to a Config Server via OAuth2 and not deployed to Cloud Foundry, an OAuth2 Client can be created with `CreateOAuth2Client(credentials []cfservices.Credentials)`

Expand All @@ -45,7 +46,9 @@ type Example struct {

func main() {
// To create a Client for a locally running Spring Config Server
configClient, err := client.CreateLocalClient()
configClient, err := client.CreateLocalClientFromEnv()
// Or
configClient, err := client.CreateLocalClient([]string{"http://localhost:8888"})
// or to create a Client for a Spring Config Server in Cloud Foundry
configClient, err := client.CreateCloudClient()
// or to create a Client for a Spring Config Server with OAuth2
Expand All @@ -55,9 +58,7 @@ func main() {
ClientId: "client id",
AccessTokenUri: "access token uri",
}
creds := make([]cfservices.Credentials, 1)
creds[0] = credentials
configClient, err := client.CreateOAuth2Client(creds)
configClient, err := client.CreateOAuth2Client([]cfservices.Credentials{credentials})

if err != nil {
panic(err)
Expand All @@ -79,17 +80,6 @@ func main() {
}
```

## Config Client Creation
There are two type of clients that can be created. A local client for a locally running Config Server without security (OAuth2)
and a cloud client for a Config Server running in a cloud environment.

### Local
To create a local client, call `client.CreateLocalClient()`. The client is configured with timeouts set and to use a pool of connections.

### Cloud
To create a cloud client, call `client.CreateCloudClient()`. The client is an OAuth2 client (client credentials).
The OAuth2 configurations are determined from the `VCAP_SERVICES` environment variable.

#### VCAP_SERVICES
When an application is deployed to Cloud Foundry, services can be bounded to the application. When a service is bounded to an application,
the application will have the necessary connection information provided in the environment variable `VCAP_SERVICES`.
Expand Down
20 changes: 16 additions & 4 deletions client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ const (
EnvironmentLocalConfigServerUrls = "CONFIG_SERVER_URLS"
)

// CreateLocalClient creates a ConfigClient for a locally running Config Server.
// CreateLocalClientFromEnv creates a ConfigClient for a locally running Config Server. Acquires the base URLs from the
// environment variable 'CONFIG_SERVER_URLS'.
//
// The ConfigClient's underlying http.Client is configured with timeouts and connection pools.
func CreateLocalClient() (*ConfigClient, error) {
func CreateLocalClientFromEnv() (*ConfigClient, error) {
serviceCredentials, err := GetLocalCredentials()
if err != nil {
return nil, errors.Wrap(err, "failed to create a local client")
}
configClients := make([]CloudClient, len(serviceCredentials.Credentials))
baseUrls := make([]string, len(serviceCredentials.Credentials))
for index, cred := range serviceCredentials.Credentials {
configUri := cred.Uri
baseUrls[index] = cred.Uri
}
return CreateLocalClient(baseUrls)
}

// CreateLocalClient creates a ConfigClient for a locally running Config Server.
//
// The ConfigClient's underlying http.Client is configured with timeouts and connection pools.
func CreateLocalClient(baseUrls []string) (*ConfigClient, error) {
configClients := make([]CloudClient, len(baseUrls))
for index, baseUrl := range baseUrls {
configUri := baseUrl
client := httpclient.CreateDefaultHttpClient()
configClients[index] = Client{configUri: configUri, httpClient: client}
}
Expand Down
4 changes: 2 additions & 2 deletions client/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestCreateLocalClient(t *testing.T) {
const localURI = "http://localhost:8080"
os.Setenv(EnvironmentLocalConfigServerUrls, localURI)
defer os.Unsetenv(EnvironmentLocalConfigServerUrls)
configClient, err := CreateLocalClient()
configClient, err := CreateLocalClientFromEnv()
if err != nil {
t.Errorf("failed to create local client with error %v", err)
}
Expand All @@ -19,7 +19,7 @@ func TestCreateLocalClient(t *testing.T) {
}

func TestCreateLocalClientWhenENVNotSet(t *testing.T) {
configClient, err := CreateLocalClient()
configClient, err := CreateLocalClientFromEnv()
if err == nil {
t.Errorf("failed to create local client with error %v", err)
}
Expand Down

0 comments on commit f76f714

Please sign in to comment.