openshift-rest-client
Advanced tools
Comparing version 0.0.3 to 0.1.0
'use strict'; | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
const privates = require('./private-map'); | ||
const yamlParser = require('./yaml-parser'); | ||
const DEFAULT_CONFIG_LOCATION = `${os.homedir()}/.kube/config`; | ||
// Takes a config object as JSON | ||
// Returns a promise | ||
function loadConfig (client, options) { | ||
function loadConfig (client, config) { | ||
return new Promise((resolve, reject) => { | ||
options = options || {}; | ||
if (!config) { | ||
return reject(new Error('Config object is require')); | ||
} | ||
const configLocation = options.configLocation ? options.configLocation : DEFAULT_CONFIG_LOCATION; | ||
// First, load the config file from the default location. TODO: make configurable | ||
fs.readFile(configLocation, 'utf8', (err, data) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
client.apiUrl = `${config.cluster}/oapi/${config.apiVersion}`; | ||
client.kubeUrl = `${config.cluster}/api/${config.apiVersion}`; | ||
// Second, parse yaml file to JSON | ||
const jsonConfig = yamlParser.yamlToJson(data); | ||
privates.get(client).config = config; | ||
// Third, find the current context in the config, and then get the user for that context? | ||
const currentContext = jsonConfig.contexts.filter((v) => { | ||
return v.name === jsonConfig['current-context']; | ||
})[0]; | ||
const currentUser = jsonConfig.users.filter((u) => { | ||
return u.name === currentContext.context.user; | ||
})[0]; | ||
const currentCluster = jsonConfig.clusters.filter((c) => { | ||
return c.name === currentContext.context.cluster; | ||
})[0]; | ||
client.apiUrl = `${currentCluster.cluster.server}/oapi/${jsonConfig.apiVersion}`; | ||
client.kubeUrl = `${currentCluster.cluster.server}/api/${jsonConfig.apiVersion}`; | ||
privates.get(client).config = Object.assign({}, | ||
{context: currentContext.context}, | ||
{user: currentUser.user}, | ||
{cluster: currentCluster.cluster.server} | ||
); | ||
return resolve(client); | ||
}); | ||
resolve(client); | ||
}); | ||
@@ -51,0 +20,0 @@ } |
@@ -5,3 +5,2 @@ 'use strict'; | ||
const loadConfig = require('./config-loader'); | ||
// const authenticate = require('./auth'); | ||
@@ -30,3 +29,3 @@ const projects = require('./projects'); | ||
function openshiftClient (settings) { | ||
function openshiftClient (config, settings) { | ||
settings = settings || {}; | ||
@@ -52,8 +51,5 @@ | ||
// Maybe load the config here? | ||
return loadConfig(client, settings); | ||
// return a promise that resolves after auth | ||
// return authenticate(client, settings); | ||
return loadConfig(client, config); | ||
} | ||
module.exports = exports = openshiftClient; |
{ | ||
"name": "openshift-rest-client", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Node.js client for developing with OpenShift", | ||
@@ -41,3 +41,2 @@ "main": "index.js", | ||
"dependencies": { | ||
"js-yaml": "^3.8.4", | ||
"request": "^2.81.0" | ||
@@ -59,2 +58,3 @@ }, | ||
"nyc": "^10.3.2", | ||
"openshift-config-loader": "^0.1.1", | ||
"proxyquire": "^1.8.0", | ||
@@ -61,0 +61,0 @@ "tap-spec": "^4.1.1", |
@@ -5,4 +5,37 @@ [![Build Status](https://travis-ci.org/bucharest-gold/openshift-rest-client.svg?branch=master)](https://travis-ci.org/bucharest-gold/openshift-rest-client) [![Coverage Status](https://coveralls.io/repos/github/bucharest-gold/openshift-rest-client/badge.svg?branch=master)](https://coveralls.io/github/bucharest-gold/openshift-rest-client?branch=master) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/bucharest-gold/openshift-client.svg)](https://greenkeeper.io/) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/bucharest-gold/openshift-rest-client.svg)](https://greenkeeper.io/) | ||
Node.js based client for the Openshift REST API, not unlike the Fabric8 Maven Plugin, but for node clients/builds. | ||
### Usage | ||
`npm install --save openshift-rest-client` | ||
The client needs to be passed a `config` object with the follow properties: | ||
{ 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' } | ||
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` | ||
Code: | ||
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.find().then((projects) => { | ||
console.log(projects); | ||
}); | ||
}); | ||
}); |
1
41
1
26610
17
15
390
- Removedjs-yaml@^3.8.4
- Removedargparse@1.0.10(transitive)
- Removedesprima@4.0.1(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedsprintf-js@1.0.3(transitive)