feat: 🎸 add host set management support for tagets in API layer

This commit is contained in:
Randall Morey committed 2020-08-24 18:03:45 -04:00
1 parent 6588269274
commit 53a5f91ccf
8 files changed
+230 -3

No files matched your search

@@ -0,0 +1,16 @@
import Fragment from 'ember-data-model-fragments/fragment';
import { attr } from '@ember-data/model';
export default class FragmentHostSetModel extends Fragment {
// =attributes
@attr('string', {
description: 'ID of the related host set'
}) host_set_id;
@attr('string', {
description: 'ID of the catalog to which the host set belongs'
}) host_catalog_id;
}
+37 -1
View File
@@ -1,3 +1,39 @@
import GeneratedTargetModel from '../generated/models/target';
import { fragmentArray } from 'ember-data-model-fragments/attributes';
export default class TargetModel extends GeneratedTargetModel {}
export default class TargetModel extends GeneratedTargetModel {
// =attributes
@fragmentArray('fragment-host-set', {readOnly: true}) host_sets;
// =methods
/**
* Saves the `host_set_ids` array on the via the `set-hosts` method.
* See serializer and adapter for more information.
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
saveHostSets(options={ adapterOptions: {} }) {
const defaultOptions = {
adapterOptions: {
method: 'set-host-sets',
serializeHostSets: true
}
};
// 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,
...options.adapterOptions
}
});
}
}
+35
View File
@@ -0,0 +1,35 @@
import ApplicationSerializer from './application';
export default class TargetSerializer extends ApplicationSerializer {
// =methods
/**
* If `adapterOptions.serializeHostSets` is true, the serialization should
* include **only host sets** and the version. Normally, host sets are not
* serialized.
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
const serializeHostSets = snapshot?.adapterOptions?.serializeHostSets;
let serialized = super.serialize(...arguments);
if (serializeHostSets) serialized = this.serializeWithHostSets(snapshot);
return serialized;
}
/**
* Returns a payload containing only the host_set_ids array.
* @param {Snapshot} snapshot
* @return {object}
*/
serializeWithHostSets(snapshot) {
return {
version: snapshot.attr('version'),
host_set_ids:
snapshot.attr('host_sets').map(hostSet => hostSet.attr('host_set_id'))
};
}
}