From 929b020cf7aeb390cb032e4024745dc72b556876 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 08:52:14 -0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Add=20host=20type=20?= =?UTF-8?q?basic=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/generated/models/host.js | 6 ++ addons/api/addon/models/host.js | 53 +++++++++++++++ addons/api/tests/unit/models/host-test.js | 80 +++++++++++++++++++++++ 3 files changed, 139 insertions(+) diff --git a/addons/api/addon/generated/models/host.js b/addons/api/addon/generated/models/host.js index 10486fe1d..08efa6397 100644 --- a/addons/api/addon/generated/models/host.js +++ b/addons/api/addon/generated/models/host.js @@ -43,4 +43,10 @@ export default class GeneratedHostModel extends BaseModel { description: 'Current version number of this resource.', }) version; + + @attr({ + description: 'Plugin information for this resource.', + readOnly: true, + }) + plugin; } diff --git a/addons/api/addon/models/host.js b/addons/api/addon/models/host.js index 7748e023b..7bd65be52 100644 --- a/addons/api/addon/models/host.js +++ b/addons/api/addon/models/host.js @@ -7,4 +7,57 @@ export default class HostModel extends GeneratedHostModel { * @type {FragmentHostAttributesModel} */ @fragment('fragment-host-attributes', { defaultValue: {} }) attributes; + + /** + * Returns if the host is static type or not. + * @type {boolean} + */ + get isStatic() { + return this.type === 'static'; + } + + /** + * Returns if the host is plugin or not. + * @type {boolean} + */ + get isPlugin() { + return this.type === 'plugin'; + } + + /** + * Returns if host plugin is AWS or not. + * @type {boolean} + */ + get isAWS() { + return this.compositeType === 'aws'; + } + + /** + * Returns if hist is Azure or not. + * @type {boolean} + */ + get isAzure() { + return this.compositeType === 'azure'; + } + + /** + * If host is a plugin return its name, + * otherwise returns the host type + * @type {string} + */ + get compositeType() { + return this.isPlugin ? this.plugin.name : this.type; + } + + /** + * Sets type, if type is different than static, sets plugin name to type + */ + set compositeType(type) { + if (type === 'static') { + this.type = 'static'; + } else { + this.type = 'plugin'; + this.plugin = { name: type }; + } + } } diff --git a/addons/api/tests/unit/models/host-test.js b/addons/api/tests/unit/models/host-test.js index 625774e71..e6a4141f0 100644 --- a/addons/api/tests/unit/models/host-test.js +++ b/addons/api/tests/unit/models/host-test.js @@ -22,4 +22,84 @@ module('Unit | Model | host', function (hooks) { }); assert.equal(model.attributes.address, '127.0.0.1'); }); + + test('it has isStatic and returns the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelA = store.createRecord('host', { type: 'static' }); + const modelB = store.createRecord('host', { type: 'plugin' }); + assert.equal(typeof modelA.isStatic, 'boolean'); + assert.true(modelA.isStatic); + assert.false(modelB.isStatic); + }); + + test('it has isPlugin and returns the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelPlugin = store.createRecord('host', { type: 'plugin' }); + const modelStatic = store.createRecord('host', { type: 'static' }); + assert.equal(typeof modelPlugin.isPlugin, 'boolean'); + assert.true(modelPlugin.isPlugin); + assert.false(modelStatic.isPlugin); + }); + + test('it has isAWS and returns the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelAws = store.createRecord('host', { + type: 'plugin', + plugin: { name: 'aws' }, + }); + const modelRandom = store.createRecord('host', { + plugin: { name: 'random' }, + }); + assert.equal(typeof modelAws.isAWS, 'boolean'); + assert.true(modelAws.isAWS); + assert.false(modelRandom.isAWS); + }); + + test('it has isAzure and returns the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelAzure = store.createRecord('host', { + type: 'plugin', + plugin: { name: 'azure' }, + }); + const modelRandom = store.createRecord('host', { + plugin: { name: 'random' }, + }); + assert.equal(typeof modelAzure.isAzure, 'boolean'); + assert.true(modelAzure.isAzure); + assert.false(modelRandom.isAzure); + }); + + test('get compositeType returns expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelPlugin = store.createRecord('host', { + type: 'plugin', + plugin: { name: 'Test name' }, + }); + const modelStatic = store.createRecord('host', { + type: 'static', + }); + assert.equal(typeof modelPlugin.compositeType, 'string'); + assert.equal(modelPlugin.compositeType, 'Test name'); + assert.equal(modelStatic.compositeType, 'static'); + }); + + test('set compositeType sets expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelPlugin = store.createRecord('host', { + compositeType: 'aws', + }); + const modelStatic = store.createRecord('host', { + compositeType: 'static', + }); + + assert.equal(modelPlugin.type, 'plugin'); + assert.equal(modelPlugin.plugin.name, 'aws'); + assert.equal(modelStatic.type, 'static'); + }); }); From bb9d99eefbb34ebdda95d1dfa98bc86b9aace53b Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 11:19:20 -0800 Subject: [PATCH 2/5] Add mirage support --- addons/api/mirage/generated/factories/host.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/addons/api/mirage/generated/factories/host.js b/addons/api/mirage/generated/factories/host.js index db9de3b56..58e679e7b 100644 --- a/addons/api/mirage/generated/factories/host.js +++ b/addons/api/mirage/generated/factories/host.js @@ -5,7 +5,6 @@ import { random, date, internet, datatype } from 'faker'; * GeneratedHostModelFactory */ export default Factory.extend({ - type: 'static', name: () => random.words(), description: () => random.words(), created_time: () => date.recent(), @@ -14,4 +13,9 @@ export default Factory.extend({ attributes: () => { return { address: internet.ipv6() }; }, + plugin: function () { + if (this.type === 'plugin') { + return this.hostCatalog.plugin; + } + }, }); From 6ad42a5eaa803bc1d277149f898743e8375aeb4e Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 11:39:01 -0800 Subject: [PATCH 3/5] Move plugin field generation from generated to a factory --- addons/api/mirage/factories/host.js | 5 +++++ addons/api/mirage/generated/factories/host.js | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/addons/api/mirage/factories/host.js b/addons/api/mirage/factories/host.js index 895b44468..60300cba4 100644 --- a/addons/api/mirage/factories/host.js +++ b/addons/api/mirage/factories/host.js @@ -10,4 +10,9 @@ export default factory.extend({ 'delete', ], id: (i) => `host-id-${i}`, + plugin() { + if (this.type === 'plugin') { + return this.hostCatalog.plugin; + } + }, }); diff --git a/addons/api/mirage/generated/factories/host.js b/addons/api/mirage/generated/factories/host.js index 58e679e7b..7c6e5a24d 100644 --- a/addons/api/mirage/generated/factories/host.js +++ b/addons/api/mirage/generated/factories/host.js @@ -13,9 +13,4 @@ export default Factory.extend({ attributes: () => { return { address: internet.ipv6() }; }, - plugin: function () { - if (this.type === 'plugin') { - return this.hostCatalog.plugin; - } - }, }); From da1056d22222dd7702bccedf46665566903a4ca6 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 14:06:22 -0800 Subject: [PATCH 4/5] Refactor host model comments --- addons/api/addon/models/host.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/addons/api/addon/models/host.js b/addons/api/addon/models/host.js index 7bd65be52..14acc4d27 100644 --- a/addons/api/addon/models/host.js +++ b/addons/api/addon/models/host.js @@ -9,7 +9,7 @@ export default class HostModel extends GeneratedHostModel { @fragment('fragment-host-attributes', { defaultValue: {} }) attributes; /** - * Returns if the host is static type or not. + * True if the host is static. * @type {boolean} */ get isStatic() { @@ -17,7 +17,7 @@ export default class HostModel extends GeneratedHostModel { } /** - * Returns if the host is plugin or not. + * True if the host is plugin. * @type {boolean} */ get isPlugin() { @@ -25,7 +25,7 @@ export default class HostModel extends GeneratedHostModel { } /** - * Returns if host plugin is AWS or not. + * True if the host plugin type is AWS. * @type {boolean} */ get isAWS() { @@ -33,7 +33,7 @@ export default class HostModel extends GeneratedHostModel { } /** - * Returns if hist is Azure or not. + * True if the host plugin type is Azure. * @type {boolean} */ get isAzure() { @@ -41,8 +41,8 @@ export default class HostModel extends GeneratedHostModel { } /** - * If host is a plugin return its name, - * otherwise returns the host type + * If host is a plugin return `plugin.name`, + * otherwise return the host type. * @type {string} */ get compositeType() { @@ -50,7 +50,8 @@ export default class HostModel extends GeneratedHostModel { } /** - * Sets type, if type is different than static, sets plugin name to type + * Sets `type`. If type is different than `static`, sets `type` to `plugin` + * and `plugin.name` to the specified type. */ set compositeType(type) { if (type === 'static') { From e150b15f0035cfed1a40884501fbeede2c18f983 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Thu, 27 Jan 2022 11:36:29 -0800 Subject: [PATCH 5/5] Add default value for type in mirage --- addons/api/mirage/factories/host.js | 3 +++ addons/api/mirage/generated/factories/host.js | 1 + 2 files changed, 4 insertions(+) diff --git a/addons/api/mirage/factories/host.js b/addons/api/mirage/factories/host.js index 60300cba4..980a5b26d 100644 --- a/addons/api/mirage/factories/host.js +++ b/addons/api/mirage/factories/host.js @@ -10,6 +10,9 @@ export default factory.extend({ 'delete', ], id: (i) => `host-id-${i}`, + type() { + return this.hostCatalog?.type || factory.attrs.type; + }, plugin() { if (this.type === 'plugin') { return this.hostCatalog.plugin; diff --git a/addons/api/mirage/generated/factories/host.js b/addons/api/mirage/generated/factories/host.js index 7c6e5a24d..db9de3b56 100644 --- a/addons/api/mirage/generated/factories/host.js +++ b/addons/api/mirage/generated/factories/host.js @@ -5,6 +5,7 @@ import { random, date, internet, datatype } from 'faker'; * GeneratedHostModelFactory */ export default Factory.extend({ + type: 'static', name: () => random.words(), description: () => random.words(), created_time: () => date.recent(),