-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(BaseObject): add a
ContainerBaseObject
to facilitate named…
… resources Some named resources (e.g., `pod('foo'))` can contain resources (e.g., `pod('foo').log` or `deploy('bar').status`). The ContainerBaseObject makes implementing these resources easier. This is groundwork for #47.
- Loading branch information
Silas Boyd-Wickizer
committed
May 14, 2017
1 parent
a4708cc
commit d2987aa
Showing
3 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const BaseObject = require('./base'); | ||
|
||
class ContainerBaseObject extends BaseObject { | ||
/** | ||
* Create generic Kubernetes API object that might contain other resources. | ||
* For example, a named Pod contains .log resources (core.ns.pods('foo').log). | ||
* | ||
* @param {object} options - Options object | ||
* @param {string} options.resources - Array of resources to add | ||
*/ | ||
constructor(options) { | ||
super(options); | ||
if (options.resources) { | ||
options.resources.forEach(resource => this.addResource(resource)); | ||
} | ||
} | ||
|
||
/** | ||
* Add a resource to the container object. | ||
* @param {string|object} options - resource name or options object | ||
* @param {string} options.name - resource name | ||
* @param {fn} options.Constructor - constructor for new resource | ||
* @returns {object} returns this to facilitate chaining | ||
*/ | ||
addResource(options) { | ||
if (typeof options === 'string') { | ||
options = { name: options, Constructor: BaseObject }; | ||
} else if (!options.name || !options.Constructor) { | ||
throw new RangeError( | ||
'NamedBaseObject.addResource: options requires .name and .Constructor'); | ||
} | ||
|
||
if (this[options.name]) { | ||
throw new RangeError( | ||
`NamedBaseObject.addResource: .${ options.name } already exists`); | ||
} | ||
this[options.name] = new options.Constructor({ | ||
api: this.api, | ||
name: options.name, | ||
parentPath: this.path | ||
}); | ||
|
||
return this; | ||
} | ||
} | ||
|
||
module.exports = ContainerBaseObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const assume = require('assume'); | ||
|
||
const ContainerBaseObject = require('../lib/container-base-object'); | ||
|
||
describe('lib.container-base-object', () => { | ||
describe('.ContainerBaseObject', () => { | ||
it('adds resources specified in the constructor', () => { | ||
const fake = new ContainerBaseObject({ resources: ['foo'] }); | ||
assume(fake.foo).is.a('function'); | ||
}); | ||
|
||
it('throws an error if missing resource name', () => { | ||
const fn = () => new ContainerBaseObject({ resources: [{ Constructor: 'fake' }] }); | ||
assume(fn).throws(); | ||
}); | ||
|
||
it('throws an error if missing resource Constructor', () => { | ||
const fn = () => new ContainerBaseObject({ resources: [{ name: 'fake' }] }); | ||
assume(fn).throws(); | ||
}); | ||
|
||
it('throws an error for adding the resource', () => { | ||
const fake = new ContainerBaseObject({ resources: ['foo'] }); | ||
const fn = () => fake.addResource('foo'); | ||
assume(fn).throws(); | ||
}); | ||
}); | ||
}); |