feat: 🎸 Add worker serializer (#1230)
* feat: 🎸 Add worker serializer Add worker serializer that focus' on `config_tags.type` and `canonical_tags.type`. [API](https://docs.google.com/document/d/1G-lfl8gk9NfoKFImSpHb90E3xC3CSEVDwVy3bJ9eOhA/edit#heading=h.9e06o4b8etpi) ✅ Closes: ICU-5241 * refactor: 💡 refactored `models/worker.js` Because `canonical_tags` and `config_tags` are `readOnly` attributes, we could use `attr` decorator instead of `fragment`. ✅ Closes: ICU-5241 * refactored `model/worker.js and deleted un-needed files * refactor: 💡 Refactored model/worker.js and created test Refactored model/worker.js to include custom method for POST route for adding new worker. BREAKING CHANGE: 🧨 no ✅ Closes: ICU-5241 * refactor: 💡 Refactored custom POST route method for worker Refactored custom method on `model/worker.js` to include serialization for `worker_genrated_auth_token` BREAKING CHANGE: 🧨 no ✅ Closes: ICU-5241 * refactor: 💡 Refactor test Refactor model/worker-test.js and serializers/worker.js BREAKING CHANGE: 🧨 no ✅ Closes: ICU-5241 * Update addons/api/addon/serializers/worker.js Co-authored-by: Dharini Jeeva <dharini@hashicorp.com> * update worker.js documentation block Co-authored-by: Dharini Jeeva <dharini@hashicorp.com>
This commit is contained in:
1 parent
1f0baceb5e
commit
62077d3c25
8 files changed
+117
-39
No files matched your search
@@ -1,11 +0,0 @@
|
||||
import Fragment from 'ember-data-model-fragments/fragment';
|
||||
import { array } from 'ember-data-model-fragments/attributes';
|
||||
|
||||
export default class FragmentTagModel extends Fragment {
|
||||
// =attributes
|
||||
|
||||
@array('string', {
|
||||
readOnly: true,
|
||||
})
|
||||
type;
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
import GeneratedWorkerModel from '../generated/models/worker';
|
||||
import { fragment } from 'ember-data-model-fragments/attributes';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
export default class WorkerModel extends GeneratedWorkerModel {
|
||||
// =attributes
|
||||
|
||||
/**
|
||||
* @type {[FragmentTagModel]}
|
||||
*/
|
||||
@fragment('fragment-tag', {
|
||||
@attr({
|
||||
description:
|
||||
'The deduplicated union of the tags reported by the worker ' +
|
||||
'from its configuration and any tags added through other means.\nOutput only.',
|
||||
@@ -15,13 +12,30 @@ export default class WorkerModel extends GeneratedWorkerModel {
|
||||
})
|
||||
canonical_tags;
|
||||
|
||||
/**
|
||||
* @type {[FragmentTagModel]}
|
||||
*/
|
||||
@fragment('fragment-tag', {
|
||||
@attr({
|
||||
description:
|
||||
"The tags set in the worker's configuration file.\nOutput only.",
|
||||
readOnly: true,
|
||||
})
|
||||
config_tags;
|
||||
|
||||
/**
|
||||
* Method to modify the adpater to handle custom POST route for creating worker.
|
||||
* @param {object} options
|
||||
* @param {object} options.adapterOptions
|
||||
* @return {Promise}
|
||||
*/
|
||||
addWorkerLed(workerGeneratedAuthToken, options = { adapterOptions: {} }) {
|
||||
const defaultAdapterOptions = {
|
||||
method: 'create:worker-led',
|
||||
workerGeneratedAuthToken
|
||||
};
|
||||
return super.save({
|
||||
...options,
|
||||
adapterOptions: {
|
||||
...defaultAdapterOptions,
|
||||
...options.adapterOptions,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import ApplicationSerializer from './application';
|
||||
|
||||
export default class WorkerSerializer extends ApplicationSerializer {
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* If `adapterOptions.workerGeneratedAuthToken` is set,
|
||||
* then the payload is serialized via `serializedWithGeneratedToken`.
|
||||
* @override
|
||||
* @param {Snapshot} snapshot
|
||||
* @return {object}
|
||||
*/
|
||||
serialize(snapshot) {
|
||||
let serialized = super.serialize(...arguments);
|
||||
const workerGeneratedAuthToken = snapshot?.adapterOptions?.workerGeneratedAuthToken;
|
||||
if (workerGeneratedAuthToken)
|
||||
serialized = this.serializeWithGeneratedToken(snapshot, workerGeneratedAuthToken);
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a payload with `worker_generated_auth_token` serialized.
|
||||
* @param {Snapshot} snapshot
|
||||
* @param {[string]} worker_generated_auth_token
|
||||
* @return {object}
|
||||
*/
|
||||
serializeWithGeneratedToken(snapshot, workerGeneratedAuthToken) {
|
||||
return {
|
||||
version: snapshot.attr('version'),
|
||||
worker_generated_auth_token: workerGeneratedAuthToken
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from 'api/models/fragment-tag';
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'api/serializers/worker';
|
||||
@@ -1,13 +0,0 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Model | fragment tag', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
// Replace this with your real tests.
|
||||
test('it exists', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('fragment-tag', {});
|
||||
assert.ok(model);
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,25 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
||||
|
||||
module('Unit | Model | worker', function (hooks) {
|
||||
setupTest(hooks);
|
||||
setupMirage(hooks);
|
||||
|
||||
// Replace this with your real tests.
|
||||
test('it exists', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('worker', {});
|
||||
assert.ok(model);
|
||||
test('it has an `addWorkerLed` method that targets a specific POST API', async function (assert) {
|
||||
assert.expect(2);
|
||||
this.server.post('/v1/workers:create:worker-led', (schema, request) => {
|
||||
const body = JSON.parse(request.requestBody);
|
||||
assert.deepEqual(body, {
|
||||
worker_generated_auth_token: 'token',
|
||||
});
|
||||
return { id: '123abc' };
|
||||
});
|
||||
|
||||
const store = this.owner.lookup('service:store');
|
||||
const model = store.createRecord('worker', { type: 'pki' });
|
||||
await model.addWorkerLed('token');
|
||||
const worker = store.peekRecord('worker', '123abc');
|
||||
assert.ok(worker);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Serializer | worker', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it serializes normally without adapterOptions.workerGeneratedAuthToken', function (assert) {
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('worker');
|
||||
const record = store.createRecord('worker', {
|
||||
name: 'worker',
|
||||
description: 'Description',
|
||||
version: 1,
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
snapshot.adapterOptions = {};
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
version: 1,
|
||||
description: 'Description',
|
||||
name: 'worker',
|
||||
});
|
||||
});
|
||||
|
||||
test('it serializes using `adapterOptions.workerGeneratedAuthToken`', function (assert) {
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('worker');
|
||||
const record = store.createRecord('worker', {
|
||||
name: 'worker',
|
||||
description: 'Description',
|
||||
version: 1,
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
snapshot.adapterOptions = {
|
||||
workerGeneratedAuthToken: '123-abc',
|
||||
};
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
version: 1,
|
||||
worker_generated_auth_token: '123-abc',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user