Skip to content

Commit

Permalink
feat(aliases): add the nice-to-have aliases to the dynamic client code (
Browse files Browse the repository at this point in the history
  • Loading branch information
silasbw authored Mar 8, 2018
1 parent bea29a8 commit b1b355f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 45 deletions.
19 changes: 19 additions & 0 deletions examples/convenience-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Use the shorter resource name aliases.
//
const Client = require('kubernetes-client').SyncClient
const config = require('kubernetes-client').config;

const deploymentManifest = require('./nginx-deployment.json');

async function main() {
try {
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
const create = await client.apis.apps.v1.ns('default').deploy.post({ body: deploymentManifest });
console.log('Result: ', create);
} catch (err) {
console.error('Error: ', err);
}
}

main();
100 changes: 56 additions & 44 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
'use strict';

module.exports.aliasResources = function (resourceObject) {
// We support the full names and all the abbbreviated aliases:
// http://kubernetes.io/docs/user-guide/kubectl-overview/
// and anything else we think is useful.
const resourceAliases = {
clusterroles: [],
clusterrolebindings: [],
componentstatuses: ['cs'],
configmaps: ['cm'],
cronjobs: [],
customresourcedefinitions: ['crd'],
daemonsets: ['ds'],
deployments: ['deploy'],
events: ['ev'],
endpoints: ['ep'],
horizontalpodautoscalers: ['hpa'],
ingresses: ['ing'],
jobs: [],
limitranges: ['limits'],
namespaces: ['ns'],
nodes: ['no'],
persistentvolumes: ['pv'],
persistentvolumeclaims: ['pvc'],
// Deprecated name of statefulsets in kubernetes 1.4
petsets: [],
pods: ['po'],
replicationcontrollers: ['rc'],
replicasets: ['rs'],
resourcequotas: ['quota'],
roles: [],
rolebindings: [],
// Deprecated name of cronjobs in kubernetes 1.4
scheduledjobs: [],
secrets: [],
serviceaccounts: [],
services: ['svc'],
statefulsets: [],
// Deprecated name of customresourcedefinition in kubernetes 1.7
thirdpartyresources: []
};
// We support the full names and all the abbbreviated aliases:
// http://kubernetes.io/docs/user-guide/kubectl-overview/
// and anything else we think is useful.
const resourceAliases = {
clusterroles: [],
clusterrolebindings: [],
componentstatuses: ['cs'],
configmaps: ['cm'],
cronjobs: [],
customresourcedefinitions: ['crd'],
daemonsets: ['ds'],
deployments: ['deploy'],
events: ['ev'],
endpoints: ['ep'],
horizontalpodautoscalers: ['hpa'],
ingresses: ['ing'],
jobs: [],
limitranges: ['limits'],
namespaces: ['ns'],
nodes: ['no'],
persistentvolumes: ['pv'],
persistentvolumeclaims: ['pvc'],
// Deprecated name of statefulsets in kubernetes 1.4
petsets: [],
pods: ['po'],
replicationcontrollers: ['rc'],
replicasets: ['rs'],
resourcequotas: ['quota'],
roles: [],
rolebindings: [],
// Deprecated name of cronjobs in kubernetes 1.4
scheduledjobs: [],
secrets: [],
serviceaccounts: [],
services: ['svc'],
statefulsets: [],
// Deprecated name of customresourcedefinition in kubernetes 1.7
thirdpartyresources: []
};

const esPlurals = {
componentstatuses: true,
ingresses: true
};
const esPlurals = {
componentstatuses: true,
ingresses: true
};

module.exports.aliasResources = function (resourceObject) {
for (const resourceType of Object.keys(resourceAliases)) {
if (resourceObject[resourceType]) {
for (const alias of resourceAliases[resourceType]) {
Expand All @@ -58,3 +58,15 @@ module.exports.aliasResources = function (resourceObject) {
}
}
};

module.exports.getAliases = function (resourceType) {
let aliases = [resourceType];
if (resourceAliases[resourceType]) {
aliases = aliases.concat(resourceAliases[resourceType]);
}

const trimLength = esPlurals[resourceType] ? 2 : 1;
const single = resourceType.substr(0, resourceType.length - trimLength);
aliases.push(single);
return aliases;
};
5 changes: 4 additions & 1 deletion lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

const merge = require('lodash.merge');

const getAliases = require('./common').getAliases;

class Endpoint {
/**
* Internal representation of a Swagger Path Item Object.
Expand Down Expand Up @@ -106,11 +108,12 @@ class Component {
}

if (!(split in parent)) {
parent[split] = new Component({
const component = new Component({
parameter: parameter,
splits: splits,
http: this.http
});
for (const type of getAliases(split)) { parent[type] = component; }
}
parent = parent[split];

Expand Down
16 changes: 16 additions & 0 deletions test/swagger-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,21 @@ describe('lib.swagger', () => {
assume(client.foo).is.a('function');
assume(client.foo('zoo').bar).is.truthy();
});

it('aliases resources', () => {
const spec = {
paths: {
'/foo/deployments': {
get: {
operationId: 'fooDeploymentsGet'
}
}
}
};
const client = new SwaggerClient({ spec });
assume(client.foo.deployments).is.truthy();
assume(client.foo.deployment).is.truthy();
assume(client.foo.deploy).is.truthy();
});
});
});

0 comments on commit b1b355f

Please sign in to comment.