Implement eligibility toggling in the data layer

This commit is contained in:
Michael Lange 2019-10-24 17:52:34 -07:00
parent 94955c8b08
commit aad8536949
3 changed files with 77 additions and 1 deletions

View file

@ -1,3 +1,22 @@
import Watchable from './watchable';
import addToPath from 'nomad-ui/utils/add-to-path';
export default Watchable.extend();
export default Watchable.extend({
setEligible(node) {
return this.setEligibility(node, true);
},
setIneligible(node) {
return this.setEligibility(node, false);
},
setEligibility(node, isEligible) {
const url = addToPath(this.urlForFindRecord(node.id, 'node'), '/eligibility');
return this.ajax(url, 'POST', {
data: {
NodeID: node.id,
Eligibility: isEligible ? 'eligible' : 'ineligible',
},
});
},
});

View file

@ -4,6 +4,7 @@ import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
import RSVP from 'rsvp';
import shortUUIDProperty from '../utils/properties/short-uuid';
import ipParts from '../utils/ip-parts';
@ -70,4 +71,14 @@ export default Model.extend({
return this.status;
}
}),
setEligible() {
if (this.isEligible) return RSVP.resolve();
return this.store.adapterFor('node').setEligible(this);
},
setIneligible() {
if (!this.isEligible) return RSVP.resolve();
return this.store.adapterFor('node').setIneligible(this);
},
});

View file

@ -85,6 +85,52 @@ module('Unit | Adapter | Node', function(hooks) {
'The deleted allocation is removed from the store and the allocations associated with the other node are untouched'
);
});
test('setEligible makes the correct POST request to /:node_id/eligibility', async function(assert) {
const { pretender } = this.server;
const node = await run(() => this.store.findRecord('node', 'node-1'));
await this.subject().setEligible(node);
const request = pretender.handledRequests.lastObject;
assert.equal(
request.url,
`/v1/node/${node.id}/eligibility`,
'Request was made to /:node_id/eligibility'
);
assert.equal(request.method, 'POST', 'Request was made with the POST method');
assert.deepEqual(
JSON.parse(request.requestBody),
{
NodeID: node.id,
Eligibility: 'eligible',
},
'POST request is made with the correct body arguments'
);
});
test('setIneligible makes the correct POST request to /:node_id/eligibility', async function(assert) {
const { pretender } = this.server;
const node = await run(() => this.store.findRecord('node', 'node-1'));
await this.subject().setIneligible(node);
const request = pretender.handledRequests.lastObject;
assert.equal(
request.url,
`/v1/node/${node.id}/eligibility`,
'Request was made to /:node_id/eligibility'
);
assert.equal(request.method, 'POST', 'Request was made with the POST method');
assert.deepEqual(
JSON.parse(request.requestBody),
{
NodeID: node.id,
Eligibility: 'ineligible',
},
'POST request is made with the correct body arguments'
);
});
});
// Using fetchLink on a model's hasMany relationship exercises the adapter's