Merge pull request #166 from hashicorp/host-set-custom-methods
Add add-hosts and remove-hosts custom methods to a hostset
This commit is contained in:
3 files changed
+48
-23
No files matched your search
@@ -27,31 +27,56 @@ export default class HostSetModel extends GeneratedHostSetModel {
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Saves the `host_ids` array on the host set via the `set-hosts` method.
|
||||
* Adds hosts via the `add-hosts` method.
|
||||
* See serializer and adapter for more information.
|
||||
* @param {[string]} hostIDs
|
||||
* @param {object} options
|
||||
* @param {object} options.adapterOptions
|
||||
* @param {string} options.adapterOptions.hostCatalogID
|
||||
* @return {Promise}
|
||||
*/
|
||||
saveHostIDs(options={ adapterOptions: {} }) {
|
||||
const defaultOptions = {
|
||||
adapterOptions: {
|
||||
method: 'set-hosts',
|
||||
serializeHostIDs: true
|
||||
}
|
||||
addHosts(hostIDs, options={ adapterOptions: {} }) {
|
||||
const defaultAdapterOptions = {
|
||||
method: 'add-hosts',
|
||||
hostIDs
|
||||
};
|
||||
// There is no "deep merge" in ES.
|
||||
// All of this nonsense is here to ensure we get
|
||||
// a decent merge of `adapterOptions`.
|
||||
return this.save({
|
||||
...defaultOptions,
|
||||
...options,
|
||||
adapterOptions: {
|
||||
...defaultOptions.adapterOptions,
|
||||
...defaultAdapterOptions,
|
||||
...options.adapterOptions
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete hosts via the `remove-hosts` method.
|
||||
* See serializer and adapter for more information.
|
||||
* @param {[string]} hostIDs
|
||||
* @param {object} options
|
||||
* @param {object} options.adapterOptions
|
||||
* @return {Promise}
|
||||
*/
|
||||
removeHosts(hostIDs, options={ adapterOptions: {} }) {
|
||||
const defaultAdapterOptions = {
|
||||
method: 'remove-hosts',
|
||||
hostIDs
|
||||
};
|
||||
return this.save({
|
||||
...options,
|
||||
adapterOptions: {
|
||||
...defaultAdapterOptions,
|
||||
...options.adapterOptions
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single host via the `remove-hosts` method.
|
||||
* @param {string} hostID
|
||||
* @param {object} options
|
||||
* @return {Promise}
|
||||
*/
|
||||
removeHost(hostID, options) {
|
||||
return this.removeHosts([hostID], options);
|
||||
}
|
||||
}
|
||||
@@ -13,21 +13,22 @@ export default class HostSetSerializer extends ApplicationSerializer {
|
||||
* @return {object}
|
||||
*/
|
||||
serialize(snapshot) {
|
||||
const serializeHostIDs = snapshot?.adapterOptions?.serializeHostIDs;
|
||||
let serialized = super.serialize(...arguments);
|
||||
if (serializeHostIDs) serialized = this.serializeWithHostIDs(snapshot);
|
||||
const hostIDs = snapshot?.adapterOptions?.hostIDs;
|
||||
if (hostIDs) serialized = this.serializeWithHostIDs(snapshot, hostIDs);
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a payload containing only the host_ids array.
|
||||
* @param {Snapshot} snapshot
|
||||
* @param {[string]} hostIDs
|
||||
* @return {object}
|
||||
*/
|
||||
serializeWithHostIDs(snapshot) {
|
||||
serializeWithHostIDs(snapshot, hostIDs) {
|
||||
return {
|
||||
version: snapshot.attr('version'),
|
||||
host_ids: snapshot.attr('host_ids').map(hostID => hostID.attr('value'))
|
||||
host_ids: hostIDs,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -35,8 +36,8 @@ export default class HostSetSerializer extends ApplicationSerializer {
|
||||
* Converts string arrays to object arrays for use with `fragment-string`.
|
||||
* E.g. `"foo"` -> `{value: "foo"}`
|
||||
* @override
|
||||
* @param {Model} typeClass
|
||||
* @param {Object} hash
|
||||
* @param {Model} modelClass
|
||||
* @param {Object} resourceHash
|
||||
* @return {Object}
|
||||
*/
|
||||
normalize(modelClass, resourceHash) {
|
||||
|
||||
@@ -11,7 +11,6 @@ module('Unit | Serializer | host set', function(hooks) {
|
||||
const record = store.createRecord('host-set', {
|
||||
name: 'Host Set 1',
|
||||
description: 'Description',
|
||||
host_ids: [{value: '1'}, {value: '2'}, {value: '3'}],
|
||||
version: 1
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
@@ -24,7 +23,7 @@ module('Unit | Serializer | host set', function(hooks) {
|
||||
});
|
||||
});
|
||||
|
||||
test('it serializes only host_ids when `adapterOptions.serializeHostIDs` is true', function (assert) {
|
||||
test('it serializes only host_ids when `adapterOptions.hostIDs` is true', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('host-set');
|
||||
@@ -36,11 +35,11 @@ module('Unit | Serializer | host set', function(hooks) {
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
snapshot.adapterOptions = {
|
||||
serializeHostIDs: true
|
||||
hostIDs: ['4', '5']
|
||||
};
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
host_ids: ['1', '2', '3'],
|
||||
host_ids: ['4', '5'],
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user