From cd96313857b09a008b1b7b5031a3d9c67c661149 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Thu, 20 Jan 2022 08:32:13 -0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Add=20fields=20to=20host?= =?UTF-8?q?-catalog=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add new fields to host-catalog model for plugin type host-catalog. Fields are corresponding to Azure and Aws plugins. --- .../addon/generated/models/host-catalog.js | 60 ++++++++++ addons/api/addon/serializers/host-catalog.js | 41 +++++++ addons/api/app/serializers/host-catalog.js | 1 + .../unit/serializers/host-catalog-test.js | 108 ++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 addons/api/addon/serializers/host-catalog.js create mode 100644 addons/api/app/serializers/host-catalog.js create mode 100644 addons/api/tests/unit/serializers/host-catalog-test.js diff --git a/addons/api/addon/generated/models/host-catalog.js b/addons/api/addon/generated/models/host-catalog.js index f1f78db34..ac1b023b6 100644 --- a/addons/api/addon/generated/models/host-catalog.js +++ b/addons/api/addon/generated/models/host-catalog.js @@ -54,4 +54,64 @@ export default class GeneratedHostCatalogModel extends BaseModel { readOnly: true, }) plugin; + + @attr('boolean', { + isNestedAttribute: true, + description: '', + }) + disable_credential_rotation; + + // Azure specific + @attr('string', { + isNestedAttribute: true, + description: '', + }) + region; + + @attr('string', { + isNestedAttribute: true, + readOnly: true, + description: '', + }) + access_key_id; + + @attr('string', { + isNestedAttribute: true, + readOnly: true, + description: '', + }) + secret_access_key; + + // AWS specific + @attr('string', { + isNestedAttribute: true, + description: '', + }) + tenant_id; + + @attr('string', { + isNestedAttribute: true, + description: '', + }) + client_id; + + @attr('string', { + isNestedAttribute: true, + description: '', + }) + subscription_id; + + @attr('string', { + isNestedAttribute: true, + readOnly: true, + description: '', + }) + secret_id; + + @attr('string', { + isNestedAttribute: true, + readOnly: true, + description: '', + }) + secret_value; } diff --git a/addons/api/addon/serializers/host-catalog.js b/addons/api/addon/serializers/host-catalog.js new file mode 100644 index 000000000..9681b6155 --- /dev/null +++ b/addons/api/addon/serializers/host-catalog.js @@ -0,0 +1,41 @@ +import ApplicationSerializer from './application'; + +export default class HostCatalogSerializer extends ApplicationSerializer { + // =methods + serialize(snapshot) { + const serialized = super.serialize(...arguments); + switch (snapshot.record.type) { + case 'static': + return this.serializeStatic(...arguments); + case 'plugin': + return this.serializeDynamic(...arguments); + default: + return serialized; + } + } + + 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/app/serializers/host-catalog.js b/addons/api/app/serializers/host-catalog.js new file mode 100644 index 000000000..e2818e2c7 --- /dev/null +++ b/addons/api/app/serializers/host-catalog.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/host-catalog'; diff --git a/addons/api/tests/unit/serializers/host-catalog-test.js b/addons/api/tests/unit/serializers/host-catalog-test.js new file mode 100644 index 000000000..d889a52b9 --- /dev/null +++ b/addons/api/tests/unit/serializers/host-catalog-test.js @@ -0,0 +1,108 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | host catalog', function (hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function (assert) { + let store = this.owner.lookup('service:store'); + let serializer = store.serializerFor('host-catalog'); + + assert.ok(serializer); + }); + + test('it serializes records', function (assert) { + let store = this.owner.lookup('service:store'); + let record = store.createRecord('host-catalog', {}); + + let serializedRecord = record.serialize(); + + assert.ok(serializedRecord); + }); + + test('it serializes a static host-catalog as expected', 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: 'static', + description: 'this is a static host-catalog', + type: 'static', + attributes: { + disable_credential_rotation: true, + tenant_id: 'a1b2c3', + }, + }); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + const expectedResult = { + name: 'static', + description: 'this is a static host-catalog', + type: 'static', + }; + assert.deepEqual(serializedRecord, expectedResult); + }); + + 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', + description: 'this is a Aws plugin host-catalog', + type: 'plugin', + plugin: { name: 'aws' }, // Read-only field + disable_credential_rotation: true, + tenant_id: 'a1b2c3', + fake_field: 'testing fake field', + client_id: 'a1b2c3', + subscription_id: 'a1b2c3', + region: 'spain', + secret_id: 'a1b2c3', // Read-only field + access_key_id: '1a2b3c', // Azure specific field + }); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + const expectedResult = { + 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', + }, + }; + assert.deepEqual(serializedRecord, expectedResult); + }); + + test('it serializes an Azure plugin as expected, ignoring read-only and Aws 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: 'Azure', + description: 'this is a Azure plugin host-catalog', + type: 'plugin', + plugin: { name: 'azure' }, // Read-only field + disable_credential_rotation: true, + region: 'spain', + access_key_id: 'a1b2c3', // Read-only field + fake_field: 'testing fake field', + }); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + const expectedResult = { + name: 'Azure', + description: 'this is a Azure plugin host-catalog', + type: 'plugin', + attributes: { + disable_credential_rotation: true, + region: 'spain', + }, + }; + assert.deepEqual(serializedRecord, expectedResult); + }); +});