diff --git a/addons/api/addon/generated/models/host-catalog.js b/addons/api/addon/generated/models/host-catalog.js index ac1b023b6..eb8b84342 100644 --- a/addons/api/addon/generated/models/host-catalog.js +++ b/addons/api/addon/generated/models/host-catalog.js @@ -55,13 +55,16 @@ export default class GeneratedHostCatalogModel extends BaseModel { }) plugin; + // AWS & Azure + @attr('boolean', { isNestedAttribute: true, description: '', }) disable_credential_rotation; - // Azure specific + // AWS specific + @attr('string', { isNestedAttribute: true, description: '', @@ -82,7 +85,8 @@ export default class GeneratedHostCatalogModel extends BaseModel { }) secret_access_key; - // AWS specific + // Azure specific + @attr('string', { isNestedAttribute: true, description: '', diff --git a/addons/api/addon/serializers/host-catalog.js b/addons/api/addon/serializers/host-catalog.js index 9681b6155..0bb99d19b 100644 --- a/addons/api/addon/serializers/host-catalog.js +++ b/addons/api/addon/serializers/host-catalog.js @@ -1,41 +1,52 @@ import ApplicationSerializer from './application'; +const fieldsByType = { + aws: [ + 'disable_credential_rotation', + 'region', + 'access_key_id', + 'secret_access_key', + ], + azure: [ + 'disable_credential_rotation', + 'tenant_id', + 'client_id', + 'subscription_id', + 'secret_id', + 'secret_value', + ], +}; + export default class HostCatalogSerializer extends ApplicationSerializer { // =methods serialize(snapshot) { const serialized = super.serialize(...arguments); - switch (snapshot.record.type) { + switch (snapshot.record.compositeType) { case 'static': return this.serializeStatic(...arguments); - case 'plugin': - return this.serializeDynamic(...arguments); default: return serialized; } } + serializeAttribute(snapshot, json, key, attribute) { + const value = super.serializeAttribute(...arguments); + const { isPlugin, compositeType } = snapshot.record; + const { options } = attribute; + // Nested conditionals for readability + // This deletes any fields that don't belong to the record type + if (isPlugin && options.isNestedAttribute && json.attributes) { + // The key must be included in the fieldsByType list above + if (!fieldsByType[compositeType].includes(key)) + delete json.attributes[key]; + } + return value; + } + serializeStatic() { const serialized = super.serialize(...arguments); // Delete unnecessary fields for static host-catalog delete serialized.attributes; return serialized; } - - serializeDynamic(snapshot) { - const { isAWS, isAzure } = snapshot.record; - const serialized = super.serialize(...arguments); - - // Is a Aws plugin, delete all the Azure attributes - if (isAWS) { - delete serialized.attributes.region; - } - - // Is an Azure plugin, delete all the Aws attributes - if (isAzure) { - delete serialized.attributes.tenant_id; - delete serialized.attributes.client_id; - delete serialized.attributes.subscription_id; - } - return serialized; - } } diff --git a/addons/api/tests/unit/serializers/host-catalog-test.js b/addons/api/tests/unit/serializers/host-catalog-test.js index 135c6db2e..5cf3480fc 100644 --- a/addons/api/tests/unit/serializers/host-catalog-test.js +++ b/addons/api/tests/unit/serializers/host-catalog-test.js @@ -40,33 +40,37 @@ module('Unit | Serializer | host catalog', function (hooks) { assert.deepEqual(serializedRecord, expectedResult); }); - test('it serializes an AWS plugin as expected, ignoring read-only and azure fields', async function (assert) { + test('it serializes an AWS plugin as expected, ignoring read-only and Azure fields', async function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-catalog'); const record = store.createRecord('host-catalog', { - name: 'Aws', + compositeType: 'aws', + name: 'AWS', description: 'this is a Aws plugin host-catalog', - type: 'plugin', - plugin: { name: 'aws' }, // Read-only field disable_credential_rotation: true, + // these are AWS fields and should be included + region: 'spain', + access_key_id: 'foobars', + secret_access_key: 'testing', + // these are Azure fields and should be excluded tenant_id: 'a1b2c3', client_id: 'a1b2c3', subscription_id: 'a1b2c3', - region: 'spain', - secret_id: 'a1b2c3', // Read-only field + secret_id: 'a1b2c3', + secret_value: 'a1b2c3', }); const snapshot = record._createSnapshot(); const serializedRecord = serializer.serialize(snapshot); const expectedResult = { - name: 'Aws', + name: 'AWS', description: 'this is a Aws plugin host-catalog', type: 'plugin', attributes: { disable_credential_rotation: true, - tenant_id: 'a1b2c3', - client_id: 'a1b2c3', - subscription_id: 'a1b2c3', + region: 'spain', + access_key_id: 'foobars', + secret_access_key: 'testing', }, }; assert.deepEqual(serializedRecord, expectedResult); @@ -77,13 +81,20 @@ module('Unit | Serializer | host catalog', function (hooks) { const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('host-catalog'); const record = store.createRecord('host-catalog', { + compositeType: 'azure', name: 'Azure', description: 'this is a Azure plugin host-catalog', - type: 'plugin', - plugin: { name: 'azure' }, // Read-only field disable_credential_rotation: true, + // these are AWS fields and should be included region: 'spain', - access_key_id: 'a1b2c3', // Read-only field + access_key_id: 'foobars', + secret_access_key: 'testing', + // these are Azure fields and should be excluded + tenant_id: 'a1b2c3', + client_id: 'a1b2c3', + subscription_id: 'a1b2c3', + secret_id: 'a1b2c3', + secret_value: 'a1b2c3', }); const snapshot = record._createSnapshot(); const serializedRecord = serializer.serialize(snapshot); @@ -93,7 +104,11 @@ module('Unit | Serializer | host catalog', function (hooks) { type: 'plugin', attributes: { disable_credential_rotation: true, - region: 'spain', + tenant_id: 'a1b2c3', + client_id: 'a1b2c3', + subscription_id: 'a1b2c3', + secret_id: 'a1b2c3', + secret_value: 'a1b2c3', }, }; assert.deepEqual(serializedRecord, expectedResult);