Skip to content

Commit

Permalink
feat(CustomResourceDefinitions): add support for watch paths (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
silasbw committed Mar 16, 2018
1 parent 9323dd3 commit e09b4ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
33 changes: 19 additions & 14 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,36 @@ class Component {
const group = manifest.spec.group;
const version = manifest.spec.version;
const name = manifest.spec.names.plural;
const spec = { paths: {}};

//
// Make just enough of Swagger spec to generate some useful endpoints.
//
const templatePath = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }/{name}`;
const templateOperations = ['delete', 'get', 'patch', 'post', 'put'].reduce((acc, method) => {
acc[method] = {
operationId: `${ method }${ name }`
};
spec.paths[templatePath] = ['delete', 'get', 'patch', 'put'].reduce((acc, method) => {
acc[method] = { operationId: `${ method }Template${ name }` };
return acc;
}, {});

const path = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }`;
const operations = {
get: {
operationId: `list${ name }`
}
};
spec.paths[path] = ['get', 'post'].reduce((acc, method) => {
acc[method] = { operationId: `${ method }${ name }` };
return acc;
}, {});

const spec = {
paths: {
[path]: operations,
[templatePath]: templateOperations
}
const watchPaths = {
watchCluster: `/apis/${ group }/${ version }/watch/${ name }`,
watchNamespace: `/apis/${ group }/${ version }/namespaces/{namespace}/watch/${ name }`,
watchResource: `/apis/${ group }/${ version }/namespaces/{namespace}/watch/${ name }/{name}`
};
Object.keys(watchPaths).forEach(operationId => {
const watchPath = watchPaths[operationId];
spec.paths[watchPath] = {
get: {
operationId: `operationId${ name }`
}
};
});

this._addSpec(spec);
}
Expand Down
5 changes: 4 additions & 1 deletion test/swagger-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,15 @@ describe('lib.swagger-client', () => {
};
client.addCustomResourceDefinition(crd);
assume(client.apis['stable.example.com'].v1.namespaces('default').foos.get).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos.post).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').get).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').delete).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').get).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').patch).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').post).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').put).is.a('function');
assume(client.apis['stable.example.com'].v1.watch.foos.getStream).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').watch.foos.getStream).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').watch.foos('blah').getStream).is.a('function');
});
});
});
Expand Down

0 comments on commit e09b4ee

Please sign in to comment.