Skip to content

Commit

Permalink
feat: add v1beta1 api endpoints (#70)
Browse files Browse the repository at this point in the history
This commit adds the set of v1beta1 endpoint URLs documented here:
https://docs.openshift.com/container-platform/3.7/rest_api/index.html

The only functional endpoint at the moment is for deployments. All of
the others simply expose the endpoint URL but do expose any functional
behavior.

I've added a `client.apis` object which maps to the the 3 semi-supported
APIs at the moment.

```js
client.apis.oapi // { version: config.apiVersion, baseUrl: client.apiUrl }
client.apis.kube // { version: config.apiVersion, baseUrl: client.kubeUrl }
client.apis.v1beta1 // function endpoints() returning array of endpoint URLs
```

Fixes: #69
  • Loading branch information
lance committed May 24, 2018
1 parent 5ba0109 commit cb8da7a
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 18 deletions.
21 changes: 21 additions & 0 deletions lib/config-loader.js
Expand Up @@ -36,6 +36,27 @@ function loadConfig (client) {
client.apiUrl = `${config.cluster}/oapi/${config.apiVersion}`;
client.kubeUrl = `${config.cluster}/api/${config.apiVersion}`;

client.apis = {
oapi: {
version: config.apiVersion,
baseUrl: client.apiUrl
},
kube: {
version: config.apiVersion,
baseUrl: client.kubeUrl
},
v1beta1: {
endpoints: function endpoints () {
return ['apps', 'extensions', 'policy',
'authentication.k8s.io', 'authentication.k8s.io', 'rbac.authorization.k8s.io',
'certificates.k8s.io', 'storage.k8s.io']
.map(name => {
return { name, url: `/apis/${name}/v1beta1` };
}).reduce((map, obj) => { map[obj.name] = obj.url; return map; }, {});
}
}
};

privates.get(client).config = config;

return client;
Expand Down
151 changes: 151 additions & 0 deletions lib/deployments.js
@@ -0,0 +1,151 @@
'use strict';

/*
*
* Copyright Red Hat, Inc. and individual contributors.
*
* 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.
*
*/

const request = require('./common-request');
const privates = require('./private-map');

function findAll (client) {
return function findAll (options = {}) {
const clientConfig = privates.get(client).config;
const url =
`${clientConfig.cluster}${client.apis.v1beta1.endpoints()
.apps}/namespaces/${clientConfig
.context.namespace}/deployments`;

const req = {
method: 'GET',
url,
qs: options.qs
};

return request(client, req);
};
}

function find (client) {
return function find (deploymentName, options = {}) {
if (!deploymentName) {
return Promise.reject(new Error('Deployment Name is required'));
}

const clientConfig = privates.get(client).config;
const url =
`${clientConfig.cluster}${client.apis.v1beta1.endpoints()
.apps}/namespaces/${clientConfig.context.namespace}/deployments/${deploymentName}`;

const req = {
method: 'GET',
url
};

return request(client, req);
};
}

function create (client) {
return function create (deployment, options = {}) {
const clientConfig = privates.get(client).config;
const url =
`${clientConfig.cluster}${client.apis.v1beta1.endpoints()
.apps}/namespaces/${clientConfig.context.namespace}/deployments`;

const req = {
method: 'POST',
url,
json: false,
body: JSON.stringify(deployment)
};

return request(client, req).then(body => {
return JSON.parse(body);
});
};
}

function update (client) {
return function create (name, deployment, options = {}) {
if (!name) {
return Promise.reject(new Error('Deployment Name is required'));
}

const clientConfig = privates.get(client).config;
const url =
`${clientConfig.cluster}${client.apis.v1beta1.endpoints()
.apps}/namespaces/${clientConfig.context.namespace}/deployments/${name}`;

const req = {
method: 'PUT',
json: false,
url,
body: JSON.stringify(deployment)
};

return request(client, req).then(body => {
return JSON.parse(body);
});
};
}

function remove (client) {
return function remove (name, options = {}) {
if (!name) {
return Promise.reject(new Error('Deployment Name is required'));
}
const clientConfig = privates.get(client).config;
const url =
`${clientConfig.cluster}${client.apis.v1beta1.endpoints()
.apps}/namespaces/${clientConfig.context.namespace}/deployments/${name}`;

const req = {
method: 'DELETE',
url,
body: options.body,
qs: options.qs
};

return request(client, req);
};
}

function removeAll (client) {
return function removeAll (options = {}) {
const clientConfig = privates.get(client).config;
const url =
`${clientConfig.cluster}${client.apis.v1beta1.endpoints()
.apps}/namespaces/${clientConfig.context.namespace}/deployments`;

const req = {
method: 'DELETE',
url,
qs: options.qs
};

return request(client, req);
};
}

module.exports = {
findAll: findAll,
find: find,
create: create,
update: update,
remove: remove,
removeAll: removeAll
};
38 changes: 20 additions & 18 deletions lib/openshift-rest-client.js
Expand Up @@ -24,8 +24,9 @@ const loadConfig = require('./config-loader');
const buildconfigs = require('./build-configs');
const builds = require('./builds');
const clusterrolebindings = require('./cluster-role-bindings');
const configMaps = require('./configmaps');
const configmaps = require('./configmaps');
const deploymentconfigs = require('./deployment-config');
const deployments = require('./deployments');
const events = require('./events');
const groups = require('./groups');
const imagestreams = require('./imagestreams');
Expand Down Expand Up @@ -65,23 +66,24 @@ function openshiftClient (settings = {}) {
const client = {};

Object.assign(client, bindModule(client, {
buildconfigs: buildconfigs,
builds: builds,
clusterrolebindings: clusterrolebindings,
configmaps: configMaps,
deploymentconfigs: deploymentconfigs,
events: events,
groups: groups,
imagestreams: imagestreams,
persistentvolumeclaims: persistentvolumeclaims,
pods: pods,
projectrequests: projectrequests,
projects: projects,
replicationcontrollers: replicationcontrollers,
rolebindings: rolebindings,
routes: routes,
secrets: secrets,
services: services
buildconfigs,
builds,
clusterrolebindings,
configmaps,
deployments,
deploymentconfigs,
events,
groups,
imagestreams,
persistentvolumeclaims,
pods,
projectrequests,
projects,
replicationcontrollers,
rolebindings,
routes,
secrets,
services
}));

client.settings = settings;
Expand Down

0 comments on commit cb8da7a

Please sign in to comment.