Skip to content

Commit

Permalink
feat: Add StatefulSet resource (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
tremes authored and lholmquist committed Nov 13, 2018
1 parent 6f9b197 commit e154ef6
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/openshift-rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const routes = require('./routes');
const secrets = require('./secrets');
const services = require('./services');
const serviceinstances = require('./serviceinstances');
const statefulsets = require('./statefulsets');

function bindModule (client, input) {
if (typeof input === 'object') {
Expand Down Expand Up @@ -89,7 +90,8 @@ function openshiftClient (settings = {}) {
routes,
secrets,
services,
serviceinstances
serviceinstances,
statefulsets
}));

client.settings = settings;
Expand Down
147 changes: 147 additions & 0 deletions lib/statefulsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
'use strict';

/*
*
* Copyright 2016-2017 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}/statefulsets`;

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

return request(client, req);
};
}

function find (client) {
return function find (statefulSetName, options = {}) {
const clientConfig = privates.get(client).config;

if (!statefulSetName) {
return Promise.reject(new Error('Stateful Set Name is required'));
}
const url = `${clientConfig.cluster}${client.apis.v1beta1.endpoints().apps}/namespaces/${clientConfig
.context.namespace}/statefulsets/${statefulSetName}`;

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

return request(client, req);
};
}

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

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

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

function update (client) {
return function create (statefulSetName, statefulSet, options = {}) {
if (!statefulSetName) {
return Promise.reject(new Error('Stateful Set Name is required'));
}

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

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

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

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

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}/statefulsets`;

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
};
179 changes: 179 additions & 0 deletions test/statefulset-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
'use strict';

const test = require('tape');
const nock = require('nock');

const openshiftRestClient = require('../');
const privates = require('../lib/private-map');

const settings = {
config: {
apiVersion: 'v1beta1',
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' }
};

test('find - statefulsets - basic findAll', (t) => {
openshiftRestClient(settings).then((client) => {
t.equal(typeof client.statefulsets.findAll, 'function', 'There is a findAll method on the statefulsets object');

const clientConfig = privates.get(client).config;

nock(clientConfig.cluster)
.matchHeader('authorization', `Bearer ${clientConfig.user.token}`) // taken from the config
.get(`/apis/apps/v1beta1/namespaces/${clientConfig.context.namespace}/statefulsets`)
.reply(200, {kind: 'StatefulSetList'});

const findResult = client.statefulsets.findAll().then((statefulSetList) => {
t.equal(statefulSetList.kind, 'StatefulSetList', 'returns an object with StatefulSetList');
t.end();
});

t.equal(findResult instanceof Promise, true, 'should return a Promise');
});
});

test('find - statefulsets - basic find', (t) => {
openshiftRestClient(settings).then((client) => {
t.equal(typeof client.statefulsets.find, 'function', 'There is a find method on the statefulsets object');

const clientConfig = privates.get(client).config;
const statefulSetName = 'cool-statefulset-name-1';

nock(clientConfig.cluster)
.matchHeader('authorization', `Bearer ${clientConfig.user.token}`) // taken from the config
.get(`/apis/apps/v1beta1/namespaces/${clientConfig.context.namespace}/statefulsets/${statefulSetName}`)
.reply(200, {kind: 'StatefulSet'});

const findResult = client.statefulsets.find(statefulSetName).then((statefulset) => {
t.equal(statefulset.kind, 'StatefulSet', 'returns an object with StatefulSet');
t.end();
}).catch(e => {
console.log(e);
});

t.equal(findResult instanceof Promise, true, 'should return a Promise');
});
});

test('find - statefulsets - find - no statefulset name', (t) => {
openshiftRestClient(settings).then((client) => {
client.statefulsets.find().catch((err) => {
t.equal(err.message, 'Stateful Set Name is required', 'error message should return');
t.end();
});
});
});

test('create - statefulset', (t) => {
openshiftRestClient(settings).then((client) => {
t.equal(typeof client.statefulsets.create, 'function', 'There is a create method on the statefulsets object');

const clientConfig = privates.get(client).config;
const statefulset = {
kind: 'StatefulSet'
};

nock(clientConfig.cluster)
.matchHeader('authorization', `Bearer ${clientConfig.user.token}`) // taken from the config
.post(`/apis/apps/v1beta1/namespaces/${clientConfig.context.namespace}/statefulsets`)
.reply(200, {kind: 'StatefulSet'});

const createResult = client.statefulsets.create(statefulset).then((statefulset) => {
t.equal(statefulset.kind, 'StatefulSet', 'returns an object with StatefulSet');
t.end();
});

t.equal(createResult instanceof Promise, true, 'should return a Promise');
});
});

test('update - statefulset', (t) => {
openshiftRestClient(settings).then(client => {
t.equal(typeof client.statefulsets.create, 'function', 'There is a create method on the statefulsets object');

const clientConfig = privates.get(client).config;
const statefulSet = {
kind: 'StatefulSet'
};
const statefulSetName = 'cool-statefulset-name-1';
const url = `${client.apis.v1beta1.endpoints().apps}/namespaces/${clientConfig.context.namespace}/statefulsets/${statefulSetName}`;

nock(clientConfig.cluster)
.matchHeader('authorization', `Bearer ${clientConfig.user.token}`) // taken from the config
.put(url)
.reply(200, {kind: 'StatefulSet'});

const createResult = client.statefulsets.update(statefulSetName, statefulSet).then(updated => {
t.equal(updated.kind, 'StatefulSet', 'returns an object with StatefulSet');
t.end();
});

t.equal(createResult instanceof Promise, true, 'should return a Promise');
});
});

test('update - statefulset with no statefulset name', (t) => {
openshiftRestClient(settings).then((client) => {
client.statefulsets.update().catch((err) => {
t.equal(err.message, 'Stateful Set Name is required', 'error message should return');
t.end();
});
});
});

test('remove - statefulsets - basic removeAll', (t) => {
openshiftRestClient(settings).then((client) => {
t.equal(typeof client.statefulsets.removeAll, 'function', 'There is a removeAll method on the statefulsets object');

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

nock(clientConfig.cluster)
.matchHeader('authorization', `Bearer ${clientConfig.user.token}`) // taken from the config
.delete(url)
.reply(200, {kind: 'Status'});

const removeResult = client.statefulsets.removeAll().then(statefulSetList => {
t.equal(statefulSetList.kind, 'Status', 'returns an object with Status');
t.end();
});

t.equal(removeResult instanceof Promise, true, 'should return a Promise');
});
});

test('remove - statefulsets - basic remove', (t) => {
openshiftRestClient(settings).then((client) => {
t.equal(typeof client.statefulsets.remove, 'function', 'There is a remove method on the statefulsets object');

const clientConfig = privates.get(client).config;
const statefulSetName = 'cool-statefulset-name-1';
const url = `${client.apis.v1beta1.endpoints().apps}/namespaces/${clientConfig.context.namespace}/statefulsets/${statefulSetName}`;

nock(clientConfig.cluster)
.matchHeader('authorization', `Bearer ${clientConfig.user.token}`) // taken from the config
.delete(url)
.reply(200, {kind: 'Status'});

const removeResult = client.statefulsets.remove(statefulSetName).then(status => {
t.equal(status.kind, 'Status', 'returns an object with Status');
t.end();
});

t.equal(removeResult instanceof Promise, true, 'should return a Promise');
});
});

test('remove - statefulset - no statefulset name', (t) => {
openshiftRestClient(settings).then((client) => {
client.statefulsets.remove().catch((err) => {
t.equal(err.message, 'Stateful Set Name is required', 'error message should return');
t.end();
});
});
});

0 comments on commit e154ef6

Please sign in to comment.