feat: 🎸 add host/host-set association support
This commit is contained in:
9 files changed
+189
-9
No files matched your search
@@ -9,7 +9,8 @@ export default class GeneratedHostSetModel extends BaseModel {
|
||||
// =attributes
|
||||
|
||||
@attr('string', {
|
||||
description: 'The type of the resource, to help differentiate schemas'
|
||||
description: 'The type of the resource, to help differentiate schemas',
|
||||
readOnly: true
|
||||
}) type;
|
||||
|
||||
@attr('string', {
|
||||
@@ -36,7 +37,8 @@ export default class GeneratedHostSetModel extends BaseModel {
|
||||
version;
|
||||
|
||||
@attr('number', {
|
||||
description: 'The total count of hosts in this host set\nOutput only.'
|
||||
description: 'The total count of hosts in this host set\nOutput only.',
|
||||
readOnly: true
|
||||
}) size;
|
||||
|
||||
}
|
||||
@@ -1,3 +1,30 @@
|
||||
import GeneratedHostSetModel from '../generated/models/host-set';
|
||||
import { fragmentArray } from 'ember-data-model-fragments/attributes';
|
||||
|
||||
export default class HostSetModel extends GeneratedHostSetModel {}
|
||||
export default class HostSetModel extends GeneratedHostSetModel {
|
||||
|
||||
// =attributes
|
||||
|
||||
/**
|
||||
* Grants is read-only _most_ under normal circumstances. But grants can
|
||||
* be persisted via a dedicated call to `saveGrants()`.
|
||||
*/
|
||||
@fragmentArray('fragment-string', {readOnly: true}) host_ids;
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Saves the `host_ids` array on the host set via the `set-hosts` method.
|
||||
* See serializer and adapter for more information.
|
||||
* @return {Promise}
|
||||
*/
|
||||
saveHostIDs() {
|
||||
return this.save({
|
||||
adapterOptions: {
|
||||
method: 'set-hosts',
|
||||
serializeHostIDs: true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import ApplicationSerializer from './application';
|
||||
|
||||
export default class HostSetSerializer extends ApplicationSerializer {
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* If `adapterOptions.serializeHostIDs` is true, the serialization should
|
||||
* include **only host_ids** and the version. Normally, these are not
|
||||
* serialized.
|
||||
* @override
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {object}
|
||||
*/
|
||||
serialize(snapshot) {
|
||||
const serializeHostIDs = snapshot?.adapterOptions?.serializeHostIDs;
|
||||
let serialized = super.serialize(...arguments);
|
||||
if (serializeHostIDs) serialized = this.serializeWithHostIDs(snapshot);
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a payload containing only the host_ids array.
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {object}
|
||||
*/
|
||||
serializeWithHostIDs(snapshot) {
|
||||
return {
|
||||
version: snapshot.attr('version'),
|
||||
host_ids: snapshot.attr('host_ids').map(hostID => hostID.attr('value'))
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts string arrays to object arrays for use with `fragment-string`.
|
||||
* E.g. `"foo"` -> `{value: "foo"}`
|
||||
* @override
|
||||
* @param {Model} typeClass
|
||||
* @param {Object} hash
|
||||
* @return {Object}
|
||||
*/
|
||||
normalize(modelClass, resourceHash) {
|
||||
// TODO: can fragment string normalization be handled generically?
|
||||
// TODO: host_ids might actually be attributes.host_ids
|
||||
if (resourceHash.host_ids) {
|
||||
resourceHash.host_ids =
|
||||
resourceHash.host_ids.map(value => ({ value }));
|
||||
}
|
||||
return super.normalize(modelClass, resourceHash);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user