Skip to content

Commit

Permalink
feat: include the openshift-config-lodaer.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: It is no longer necessary to include the openshift-config-loader separate.  By default, just calling the rest client will do the default config loading.
If a user needs to pass a config into the client, use the settings object.  ex:  settings.config = {...}

fixes #54
  • Loading branch information
lholmquist committed Feb 12, 2018
1 parent ad873ac commit 2e21e0e
Show file tree
Hide file tree
Showing 23 changed files with 1,694 additions and 2,223 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ dist: trusty
language: node_js
node_js:
- "8"
- "7"
- "6"
- "9"
before_install:
- npm install -g npm
- npm install -g greenkeeper-lockfile@1
Expand Down
52 changes: 32 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,47 @@

Node.js based client for the Openshift REST API, not unlike the Fabric8 Maven Plugin, but for node clients/builds.

### Usage
### Basic Usage

`npm install --save openshift-rest-client`

The client needs to be passed a `config` object with the follow properties:
Code:

const openshiftRestClient = require('openshift-rest-client');

{ apiVersion: 'v1',
context:
{ cluster: '192-168-99-100:8443',
namespace: 'for-node-client-testing',
user: 'developer/192-168-99-100:8443' },
user: { token: 'zVBd1ZFeJqEAILJgimm4-gZJauaw3PW4EVqV_peEZ3U' },
cluster: 'https://192.168.99.100:8443' }
openshiftRestClient().then((client) => {
// Use the client object to find a list of projects, for example
client.projects.findAll().then((projects) => {
console.log(projects);
});
});

This can be obtained using the [openshift-config-loader](https://www.npmjs.com/package/openshift-config-loader)

By default, the config loader with look for a config at `~/.kube/config`
### Advanced Usage

By default, the openshift-rest-client will use the [openshift-config-loader](https://www.npmjs.com/package/openshift-config-loader) module to get your openshift configuration.

Code:
You can pass in a custom config object by using the `settings` object

const openshiftConfigLoader = require('openshift-config-loader');
const openshiftRestClient = require('openshift-rest-client');

openshiftConfigLoader(settings).then((config) => {
openshiftRestClient(config).then((client) => {
// Use the client object to find a list of projects, for example
client.projects.findAll().then((projects) => {
console.log(projects);
});
const settings = {};

const customConfig = {
apiVersion: 'v1',
context:
{ cluster: '192-168-99-100:8443',
namespace: 'for-node-client-testing',
user: 'developer/192-168-99-100:8443' },
user: { token: 'zVBd1ZFeJqEAILJgimm4-gZJauaw3PW4EVqV_peEZ3U' },
cluster: 'https://192.168.99.100:8443' }
};

settings.config = customConfig;

openshiftRestClient(settings).then((client) => {
// Use the client object to find a list of projects, for example
client.projects.findAll().then((projects) => {
console.log(projects);
});
});

19 changes: 12 additions & 7 deletions lib/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@
*/

const privates = require('./private-map');
const openshiftConfigLoader = require('openshift-config-loader');

function load (settings) {
if (settings.config) {
return Promise.resolve(settings.config);
}

return openshiftConfigLoader(settings.openshiftConfigLoader);
}

// Takes a config object as JSON
// Returns a promise
function loadConfig (client, config) {
return new Promise((resolve, reject) => {
if (!config) {
return reject(new Error('Config object is require'));
}

function loadConfig (client) {
return load(client.settings).then((config) => {
client.apiUrl = `${config.cluster}/oapi/${config.apiVersion}`;
client.kubeUrl = `${config.cluster}/api/${config.apiVersion}`;

privates.get(client).config = config;

resolve(client);
return client;
});
}

Expand Down
13 changes: 9 additions & 4 deletions lib/openshift-rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ function bindModule (client, input) {
}
}

function openshiftClient (config, settings) {
settings = settings || {};

/**
@param {object} [settings] -
@param {object} [settings.config] - overrides the default openshift configuration
@param {object} [settings.openshiftConfigLoader] - settings to pass to the openshift-config-loader module
@param {object} [settings.request] - settings to pass to the request module
@returns {Promise} - Returns the Rest Client
*/
function openshiftClient (settings = {}) {
const data = {};
const client = {};

Expand Down Expand Up @@ -82,7 +87,7 @@ function openshiftClient (config, settings) {
privates.set(client, data);

// Maybe load the config here?
return loadConfig(client, config);
return loadConfig(client);
}

module.exports = exports = openshiftClient;
23 changes: 9 additions & 14 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"email": "lholmqui@redhat.com"
},
"engines": {
"node": ">= 4.0.0"
"node": ">= 8.0.0"
},
"repository": {
"type": "git",
Expand All @@ -40,6 +40,7 @@
"bugs": "https://github.com/bucharest-gold/openshift-rest-client/issues",
"license": "Apache-2.0",
"dependencies": {
"openshift-config-loader": "^0.4.0",
"request": "^2.83.0"
},
"devDependencies": {
Expand All @@ -56,7 +57,6 @@
"nock": "^9.0.14",
"nsp": "^3.1.0",
"nyc": "^11.1.0",
"openshift-config-loader": "^0.3.0",
"proxyquire": "^1.8.0",
"standard-version": "^4.3.0",
"tap-spec": "^4.1.1",
Expand Down
Loading

0 comments on commit 2e21e0e

Please sign in to comment.