From f64f6bfd05b39e241a3baed5e6b7521f384137ed Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Tue, 1 Feb 2022 11:14:43 -0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Add=20new=20fields?= =?UTF-8?q?=20to=20host-set=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add new fields to host-set model --- addons/api/addon/models/host-set.js | 6 +++ .../tests/unit/serializers/host-set-test.js | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/addons/api/addon/models/host-set.js b/addons/api/addon/models/host-set.js index b453195d1..2d85fd113 100644 --- a/addons/api/addon/models/host-set.js +++ b/addons/api/addon/models/host-set.js @@ -66,6 +66,12 @@ export default class HostSetModel extends GeneratedHostSetModel { }) host_ids; + // Not sure if re-use this fragment. + @fragmentArray('fragment-string', { + emptyArrayIfMissing: true, // Not sure if should be true. + }) + preferred_endpoints; + // =methods /** diff --git a/addons/api/tests/unit/serializers/host-set-test.js b/addons/api/tests/unit/serializers/host-set-test.js index 9c5c8136e..96335a9f8 100644 --- a/addons/api/tests/unit/serializers/host-set-test.js +++ b/addons/api/tests/unit/serializers/host-set-test.js @@ -13,6 +13,7 @@ module('Unit | Serializer | host set', function (hooks) { description: 'Description', host_catalog_id: '123', version: 1, + preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], }); const snapshot = record._createSnapshot(); snapshot.adapterOptions = {}; @@ -22,6 +23,7 @@ module('Unit | Serializer | host set', function (hooks) { description: 'Description', host_catalog_id: '123', version: 1, + preferred_endpoints: ['option 1', 'option 2'], }); }); @@ -55,6 +57,7 @@ module('Unit | Serializer | host set', function (hooks) { id: '1', name: 'Host Set 1', host_ids: ['1', '2', '3'], + preferred_endpoints: ['option 1', 'option 2'], }; const normalized = serializer.normalizeSingleResponse( store, @@ -70,6 +73,7 @@ module('Unit | Serializer | host set', function (hooks) { authorized_actions: [], name: 'Host Set 1', host_ids: [{ value: '1' }, { value: '2' }, { value: '3' }], + preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], }, relationships: {}, }, @@ -101,6 +105,39 @@ module('Unit | Serializer | host set', function (hooks) { name: 'Host Set 1', scope: { scope_id: 'o_123' }, host_ids: [], + preferred_endpoints: [], + }, + relationships: {}, + }, + }); + }); + + test('it normalizes missing preferred_endpoints to empty array', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('host-set'); + const hostSet = store.createRecord('host-set').constructor; + const payload = { + id: '1', + name: 'Host Set 1', + scope: { id: 'o_123' }, + }; + const normalized = serializer.normalizeSingleResponse( + store, + hostSet, + payload + ); + assert.deepEqual(normalized, { + included: [], + data: { + id: '1', + type: 'host-set', + attributes: { + authorized_actions: [], + name: 'Host Set 1', + scope: { scope_id: 'o_123' }, + host_ids: [], + preferred_endpoints: [], }, relationships: {}, }, From 5e918cdddcbce4b4ca822036294ae6732df25370 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 2 Feb 2022 15:50:38 -0800 Subject: [PATCH 2/5] Add all the new fields. Refactor serializer to fit new changes. Work in progress with normalize --- addons/api/addon/generated/models/host-set.js | 7 ++ addons/api/addon/models/host-set.js | 10 ++- addons/api/addon/serializers/host-set.js | 38 +++++++++- .../tests/unit/serializers/host-set-test.js | 75 +++++++++++++++++-- 4 files changed, 117 insertions(+), 13 deletions(-) diff --git a/addons/api/addon/generated/models/host-set.js b/addons/api/addon/generated/models/host-set.js index 048badc73..9d8c13d6e 100644 --- a/addons/api/addon/generated/models/host-set.js +++ b/addons/api/addon/generated/models/host-set.js @@ -56,4 +56,11 @@ export default class GeneratedHostSetModel extends BaseModel { readOnly: true, }) plugin; + + // Azure specific + @attr('string', { + description: '', + isNestedAttribute: true, + }) + filter; } diff --git a/addons/api/addon/models/host-set.js b/addons/api/addon/models/host-set.js index 2d85fd113..98f7cd5e2 100644 --- a/addons/api/addon/models/host-set.js +++ b/addons/api/addon/models/host-set.js @@ -66,12 +66,18 @@ export default class HostSetModel extends GeneratedHostSetModel { }) host_ids; - // Not sure if re-use this fragment. @fragmentArray('fragment-string', { - emptyArrayIfMissing: true, // Not sure if should be true. + emptyArrayIfMissing: true, }) preferred_endpoints; + // AWS specific + @fragmentArray('fragment-string', { + emptyArrayIfMissing: true, + isNestedAttribute: true, + }) + filters; + // =methods /** diff --git a/addons/api/addon/serializers/host-set.js b/addons/api/addon/serializers/host-set.js index f0e0c25ad..2372d717e 100644 --- a/addons/api/addon/serializers/host-set.js +++ b/addons/api/addon/serializers/host-set.js @@ -1,5 +1,9 @@ import ApplicationSerializer from './application'; +const fieldByType = { + aws: ['filters'], + azure: ['filter'], +}; export default class HostSetSerializer extends ApplicationSerializer { // =properties @@ -19,10 +23,18 @@ export default class HostSetSerializer extends ApplicationSerializer { * @return {object} */ serialize(snapshot) { - let serialized = super.serialize(...arguments); const hostIDs = snapshot?.adapterOptions?.hostIDs; - if (hostIDs) serialized = this.serializeWithHostIDs(snapshot, hostIDs); - return serialized; + if (hostIDs) { + return this.serializeWithHostIDs(snapshot, hostIDs); + } else { + console.log('Carlos: ', snapshot.record.compositeType); + switch (snapshot.record.compositeType) { + case 'static': + return this.serializeStatic(...arguments); + default: + return super.serialize(...arguments); + } + } } /** @@ -37,4 +49,24 @@ export default class HostSetSerializer extends ApplicationSerializer { host_ids: hostIDs, }; } + + serializeStatic() { + let serialized = super.serialize(...arguments); + // Delete unnecessary fields for static type + delete serialized.attributes; + return serialized; + } + + serializeAttribute(snapshot, json, key, attribute) { + const value = super.serializeAttribute(...arguments); + const { isPlugin, compositeType } = snapshot.record; + const { options } = attribute; + + if (isPlugin && options.isNestedAttribute) { + if (!fieldByType[compositeType].includes(key)) { + delete json.attributes[key]; + } + } + return value; + } } diff --git a/addons/api/tests/unit/serializers/host-set-test.js b/addons/api/tests/unit/serializers/host-set-test.js index 96335a9f8..17b64151b 100644 --- a/addons/api/tests/unit/serializers/host-set-test.js +++ b/addons/api/tests/unit/serializers/host-set-test.js @@ -4,21 +4,29 @@ import { setupTest } from 'ember-qunit'; module('Unit | Serializer | host set', function (hooks) { setupTest(hooks); + // DONE // Serializes host sets normally, no host_ids, no plugin. + // DONE // Serializes host sets only host_ids. + // DONE // Serializes host sets as expected for a Aws plugin. + // DONE // Serializes host sets as expected for Azure plugin. + // WIP // Normalizes records with array fields. + // ? // Normalizes records missing host_ids to empty array. + // ? // Normalizes missing preferred_endpoints to empty array. + // ? // Normalizes records with array fields, static. + // ? // Normalizes records with array fields, aws. + // ? // Normalizes records with array fields, azure. + test('it serializes host sets normally, without host_ids', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); - const serializer = store.serializerFor('host-set'); const record = store.createRecord('host-set', { name: 'Host Set 1', + compositeType: 'static', description: 'Description', host_catalog_id: '123', version: 1, preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], }); - const snapshot = record._createSnapshot(); - snapshot.adapterOptions = {}; - const serializedRecord = serializer.serialize(snapshot); - assert.deepEqual(serializedRecord, { + assert.deepEqual(record.serialize(), { name: 'Host Set 1', description: 'Description', host_catalog_id: '123', @@ -48,7 +56,55 @@ module('Unit | Serializer | host set', function (hooks) { }); }); - test('it normalizes records with array fields', function (assert) { + test('it serializes host sets as expected for Aws plugin', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const record = store.createRecord('host-set', { + name: 'Host Set 1', + compositeType: 'aws', + description: 'Description', + host_catalog_id: '123', + version: 1, + preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], + filters: [{ value: 'filter 1' }, { value: 'filter 2' }], + }); + assert.deepEqual(record.serialize(), { + name: 'Host Set 1', + description: 'Description', + host_catalog_id: '123', + version: 1, + preferred_endpoints: ['option 1', 'option 2'], + attributes: { + filters: ['filter 1', 'filter 2'], + }, + }); + }); + + test('it serializes host sets as expected for Azure plugin', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const record = store.createRecord('host-set', { + name: 'Host Set 1', + compositeType: 'azure', + description: 'Description', + host_catalog_id: '123', + version: 1, + preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], + filter: 'filter 1 && filter 2', + }); + assert.deepEqual(record.serialize(), { + name: 'Host Set 1', + description: 'Description', + host_catalog_id: '123', + version: 1, + preferred_endpoints: ['option 1', 'option 2'], + attributes: { + filter: 'filter 1 && filter 2', + }, + }); + }); + + test.skip('it normalizes records with array fields', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-set'); @@ -56,6 +112,7 @@ module('Unit | Serializer | host set', function (hooks) { const payload = { id: '1', name: 'Host Set 1', + type: 'static', host_ids: ['1', '2', '3'], preferred_endpoints: ['option 1', 'option 2'], }; @@ -64,12 +121,14 @@ module('Unit | Serializer | host set', function (hooks) { hostSetModelClass, payload ); + assert.deepEqual(normalized, { included: [], data: { id: '1', type: 'host-set', attributes: { + type: 'static', authorized_actions: [], name: 'Host Set 1', host_ids: [{ value: '1' }, { value: '2' }, { value: '3' }], @@ -80,7 +139,7 @@ module('Unit | Serializer | host set', function (hooks) { }); }); - test('it normalizes missing host_ids to empty array', function (assert) { + test.skip('it normalizes missing host_ids to empty array', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-set'); @@ -112,7 +171,7 @@ module('Unit | Serializer | host set', function (hooks) { }); }); - test('it normalizes missing preferred_endpoints to empty array', function (assert) { + test.skip('it normalizes missing preferred_endpoints to empty array', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-set'); From 5e12566a6596af3d373837bff2315aebc50ae45c Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Thu, 3 Feb 2022 11:26:34 -0800 Subject: [PATCH 3/5] Refactor tests --- addons/api/addon/serializers/host-set.js | 1 - .../tests/unit/serializers/host-set-test.js | 28 ++++++++----------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/addons/api/addon/serializers/host-set.js b/addons/api/addon/serializers/host-set.js index 2372d717e..928e80d0c 100644 --- a/addons/api/addon/serializers/host-set.js +++ b/addons/api/addon/serializers/host-set.js @@ -27,7 +27,6 @@ export default class HostSetSerializer extends ApplicationSerializer { if (hostIDs) { return this.serializeWithHostIDs(snapshot, hostIDs); } else { - console.log('Carlos: ', snapshot.record.compositeType); switch (snapshot.record.compositeType) { case 'static': return this.serializeStatic(...arguments); diff --git a/addons/api/tests/unit/serializers/host-set-test.js b/addons/api/tests/unit/serializers/host-set-test.js index 17b64151b..14714ef2b 100644 --- a/addons/api/tests/unit/serializers/host-set-test.js +++ b/addons/api/tests/unit/serializers/host-set-test.js @@ -4,17 +4,6 @@ import { setupTest } from 'ember-qunit'; module('Unit | Serializer | host set', function (hooks) { setupTest(hooks); - // DONE // Serializes host sets normally, no host_ids, no plugin. - // DONE // Serializes host sets only host_ids. - // DONE // Serializes host sets as expected for a Aws plugin. - // DONE // Serializes host sets as expected for Azure plugin. - // WIP // Normalizes records with array fields. - // ? // Normalizes records missing host_ids to empty array. - // ? // Normalizes missing preferred_endpoints to empty array. - // ? // Normalizes records with array fields, static. - // ? // Normalizes records with array fields, aws. - // ? // Normalizes records with array fields, azure. - test('it serializes host sets normally, without host_ids', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); @@ -104,7 +93,7 @@ module('Unit | Serializer | host set', function (hooks) { }); }); - test.skip('it normalizes records with array fields', function (assert) { + test('it normalizes records with array fields', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-set'); @@ -114,7 +103,8 @@ module('Unit | Serializer | host set', function (hooks) { name: 'Host Set 1', type: 'static', host_ids: ['1', '2', '3'], - preferred_endpoints: ['option 1', 'option 2'], + preferred_endpoints: ['endpoint 1', 'endpoint 2'], + filters: ['filter 1', 'filter 2'], }; const normalized = serializer.normalizeSingleResponse( store, @@ -132,14 +122,18 @@ module('Unit | Serializer | host set', function (hooks) { authorized_actions: [], name: 'Host Set 1', host_ids: [{ value: '1' }, { value: '2' }, { value: '3' }], - preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], + preferred_endpoints: [ + { value: 'endpoint 1' }, + { value: 'endpoint 2' }, + ], + filters: [{ value: 'filter 1' }, { value: 'filter 2' }], }, relationships: {}, }, }); }); - test.skip('it normalizes missing host_ids to empty array', function (assert) { + test('it normalizes missing host_ids to empty array', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-set'); @@ -165,13 +159,14 @@ module('Unit | Serializer | host set', function (hooks) { scope: { scope_id: 'o_123' }, host_ids: [], preferred_endpoints: [], + filters: [], }, relationships: {}, }, }); }); - test.skip('it normalizes missing preferred_endpoints to empty array', function (assert) { + test('it normalizes missing array fields to empty array', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-set'); @@ -197,6 +192,7 @@ module('Unit | Serializer | host set', function (hooks) { scope: { scope_id: 'o_123' }, host_ids: [], preferred_endpoints: [], + filters: [], }, relationships: {}, }, From 4f14891c841d34dedb50a5a75396812400caf24a Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Thu, 3 Feb 2022 14:31:21 -0800 Subject: [PATCH 4/5] Add new fields to mirage --- addons/api/mirage/factories/host-set.js | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/addons/api/mirage/factories/host-set.js b/addons/api/mirage/factories/host-set.js index 35b26e560..cfda11aed 100644 --- a/addons/api/mirage/factories/host-set.js +++ b/addons/api/mirage/factories/host-set.js @@ -1,5 +1,6 @@ import factory from '../generated/factories/host-set'; import permissions from '../helpers/permissions'; +import { datatype, internet, database } from 'faker'; export default factory.extend({ id: (i) => `host-set-id-${i}`, @@ -18,4 +19,31 @@ export default factory.extend({ return this.hostCatalog.plugin; } }, + preferred_endpoints() { + if (this.type === 'plugin') { + const enpointsAmount = datatype.number({ min: 1, max: 5 }); + let result = []; + for (let i = 0; i < enpointsAmount; ++i) { + result.push(internet.ip()); + } + return result; + } + }, + // AWS specific + filters() { + if (this.type === 'plugin' && this.plugin?.name === 'aws') { + const filtersAmount = datatype.number({ min: 1, max: 5 }); + let result = []; + for (let i = 0; i < filtersAmount; ++i) { + result.push(`${datatype.string(3)}=${datatype.string(8)}`); + } + return result; + } + }, + // Azure specific + filter() { + if (this.type === 'plugin' && this.plugin?.name === 'azure') { + return `${database.column}=${database.collation}`; + } + }, }); From e52093c6ed87ba13e727c77a4141ce44dcbbce9d Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Fri, 4 Feb 2022 09:26:26 -0800 Subject: [PATCH 5/5] PR suggestions applied --- addons/api/addon/serializers/host-set.js | 5 +++-- addons/api/mirage/factories/host-set.js | 4 ++-- addons/api/tests/unit/serializers/host-set-test.js | 2 -- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/addons/api/addon/serializers/host-set.js b/addons/api/addon/serializers/host-set.js index 928e80d0c..613caf1ab 100644 --- a/addons/api/addon/serializers/host-set.js +++ b/addons/api/addon/serializers/host-set.js @@ -1,8 +1,8 @@ import ApplicationSerializer from './application'; const fieldByType = { - aws: ['filters'], - azure: ['filter'], + aws: ['preferred_endpoints', 'filters'], + azure: ['preferred_endpoints', 'filter'], }; export default class HostSetSerializer extends ApplicationSerializer { // =properties @@ -53,6 +53,7 @@ export default class HostSetSerializer extends ApplicationSerializer { let serialized = super.serialize(...arguments); // Delete unnecessary fields for static type delete serialized.attributes; + delete serialized.preferred_endpoints; return serialized; } diff --git a/addons/api/mirage/factories/host-set.js b/addons/api/mirage/factories/host-set.js index cfda11aed..038bf6a7c 100644 --- a/addons/api/mirage/factories/host-set.js +++ b/addons/api/mirage/factories/host-set.js @@ -31,7 +31,7 @@ export default factory.extend({ }, // AWS specific filters() { - if (this.type === 'plugin' && this.plugin?.name === 'aws') { + if (this.plugin?.name === 'aws') { const filtersAmount = datatype.number({ min: 1, max: 5 }); let result = []; for (let i = 0; i < filtersAmount; ++i) { @@ -42,7 +42,7 @@ export default factory.extend({ }, // Azure specific filter() { - if (this.type === 'plugin' && this.plugin?.name === 'azure') { + if (this.plugin?.name === 'azure') { return `${database.column}=${database.collation}`; } }, diff --git a/addons/api/tests/unit/serializers/host-set-test.js b/addons/api/tests/unit/serializers/host-set-test.js index 14714ef2b..edb98d519 100644 --- a/addons/api/tests/unit/serializers/host-set-test.js +++ b/addons/api/tests/unit/serializers/host-set-test.js @@ -13,14 +13,12 @@ module('Unit | Serializer | host set', function (hooks) { description: 'Description', host_catalog_id: '123', version: 1, - preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }], }); assert.deepEqual(record.serialize(), { name: 'Host Set 1', description: 'Description', host_catalog_id: '123', version: 1, - preferred_endpoints: ['option 1', 'option 2'], }); });