From 26887b3cfe5d0420563d66946d0406f689573214 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 07:56:36 -0800 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Add=20basic=20type?= =?UTF-8?q?=20support=20for=20host-set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add basic type support for host-set --- addons/api/addon/generated/models/host-set.js | 6 ++ addons/api/addon/models/host-set.js | 52 ++++++++++++ .../mirage/generated/factories/host-set.js | 9 +++ addons/api/tests/unit/models/host-set-test.js | 80 +++++++++++++++++++ 4 files changed, 147 insertions(+) diff --git a/addons/api/addon/generated/models/host-set.js b/addons/api/addon/generated/models/host-set.js index 44c0991d6..048badc73 100644 --- a/addons/api/addon/generated/models/host-set.js +++ b/addons/api/addon/generated/models/host-set.js @@ -50,4 +50,10 @@ export default class GeneratedHostSetModel extends BaseModel { readOnly: true, }) size; + + @attr({ + description: 'Plugin information for this resource', + readOnly: true, + }) + plugin; } diff --git a/addons/api/addon/models/host-set.js b/addons/api/addon/models/host-set.js index 66dd96d52..b453195d1 100644 --- a/addons/api/addon/models/host-set.js +++ b/addons/api/addon/models/host-set.js @@ -4,6 +4,58 @@ import { fragmentArray } from 'ember-data-model-fragments/attributes'; export default class HostSetModel extends GeneratedHostSetModel { // =attributes + /** + * Returns if the host-set is static or not. + * @type {boolean} + */ + get isStatic() { + return this.type === 'static'; + } + + /** + * Returns if the host-set is plugin or not. + * @type {boolean} + */ + get isPlugin() { + return this.type === 'plugin'; + } + + /** + * Returns if a host-set plugin is AWS or not + * @type {boolean} + */ + get isAWS() { + return this.compositeType === 'aws'; + } + + /** + * Return if a host-set plugin is Azure or not. + */ + get isAzure() { + return this.compositeType === 'azure'; + } + + /** + * If a host-set is a plugin return its name, + * otherwise returns the host-set type + * @type {string} + */ + get compositeType() { + return this.plugin ? this.plugin.name : this.type; + } + + /** + * Sets type, if type is different than static, set plugin name to type + */ + set compositeType(type) { + if (type === 'static') { + this.type = 'static'; + } else { + this.type = 'plugin'; + this.plugin = { name: type }; + } + } + /** * Host IDs are read-only under normal circumstances. But these can * be persisted via a dedicated call to `addHosts()`. diff --git a/addons/api/mirage/generated/factories/host-set.js b/addons/api/mirage/generated/factories/host-set.js index 9a8000ef9..057b88926 100644 --- a/addons/api/mirage/generated/factories/host-set.js +++ b/addons/api/mirage/generated/factories/host-set.js @@ -12,4 +12,13 @@ export default Factory.extend({ updated_time: () => date.recent(), version: () => datatype.number(), size: () => datatype.number(), + plugin: function (i) { + if (this.type == 'plugin') { + return { + id: `pl_${datatype.hexaDecimal(5)}`, + name: i % 2 ? 'aws' : 'azure', + description: random.words(), + }; + } + }, }); diff --git a/addons/api/tests/unit/models/host-set-test.js b/addons/api/tests/unit/models/host-set-test.js index f1cf1c110..33189d3d4 100644 --- a/addons/api/tests/unit/models/host-set-test.js +++ b/addons/api/tests/unit/models/host-set-test.js @@ -129,4 +129,84 @@ module('Unit | Model | host set', function (hooks) { const model = store.peekRecord('host-set', '123abc'); await model.removeHost('3'); }); + + 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-set', { type: 'static' }); + const modelB = store.createRecord('host-set', { 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-set', { type: 'plugin' }); + const modelStatic = store.createRecord('host-set', { type: 'static' }); + assert.equal(typeof modelPlugin.isPlugin, 'boolean'); + assert.true(modelPlugin.isPlugin); + assert.false(modelStatic.isPlugin); + }); + + test('it has isAws and return the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelAws = store.createRecord('host-set', { + type: 'plugin', + plugin: { name: 'aws' }, + }); + const modelRandom = store.createRecord('host-set', { + plugin: { name: 'random' }, + }); + assert.equal(typeof modelAws.isAWS, 'boolean'); + assert.true(modelAws.isAWS); + assert.false(modelRandom.isAWS); + }); + + test('it has isAzure and return the expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelAzure = store.createRecord('host-set', { + type: 'plugin', + plugin: { name: 'azure' }, + }); + const modelRandom = store.createRecord('host-set', { + plugin: { name: 'random' }, + }); + assert.equal(typeof modelAzure.isAzure, 'boolean'); + assert.true(modelAzure.isAzure); + assert.false(modelRandom.isAzure); + }); + + test('get compositeType return expected values', async function (assert) { + assert.expect(3); + const store = this.owner.lookup('service:store'); + const modelPlugin = store.createRecord('host-set', { + type: 'plugin', + plugin: { name: 'Test name' }, + }); + const modelStatic = store.createRecord('host-set', { + 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-set', { + compositeType: 'aws', + }); + const modelStatic = store.createRecord('host-set', { + compositeType: 'static', + }); + + assert.equal(modelPlugin.type, 'plugin'); + assert.equal(modelPlugin.plugin.name, 'aws'); + assert.equal(modelStatic.type, 'static'); + }); }); From 995c79804a703a7f5b858f4b9d6366d8228ebddc Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 07:59:27 -0800 Subject: [PATCH 2/7] Add random type values in mirage --- addons/api/mirage/generated/factories/host-set.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/api/mirage/generated/factories/host-set.js b/addons/api/mirage/generated/factories/host-set.js index 057b88926..9d14e3e4f 100644 --- a/addons/api/mirage/generated/factories/host-set.js +++ b/addons/api/mirage/generated/factories/host-set.js @@ -5,7 +5,7 @@ import { random, date, datatype } from 'faker'; * GeneratedHostSetModelFactory */ export default Factory.extend({ - type: 'static', + type: () => random.arrayElement(['static', 'plugin']), name: () => random.words(), description: () => random.words(), created_time: () => date.recent(), From 676c3e0ad04f3a23e65269ba56c39adb13fd87be Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 10:11:25 -0800 Subject: [PATCH 3/7] Type gets inherit from host-catalog --- addons/api/mirage/generated/factories/host-set.js | 1 - 1 file changed, 1 deletion(-) diff --git a/addons/api/mirage/generated/factories/host-set.js b/addons/api/mirage/generated/factories/host-set.js index 9d14e3e4f..84e635d58 100644 --- a/addons/api/mirage/generated/factories/host-set.js +++ b/addons/api/mirage/generated/factories/host-set.js @@ -5,7 +5,6 @@ import { random, date, datatype } from 'faker'; * GeneratedHostSetModelFactory */ export default Factory.extend({ - type: () => random.arrayElement(['static', 'plugin']), name: () => random.words(), description: () => random.words(), created_time: () => date.recent(), From cbc732e9dd60d13dc9971b7dfa9e7d68a32d62d4 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 12:30:21 -0800 Subject: [PATCH 4/7] Move mirage plugin field to a factory. Now plugin inherits from host-catalog --- addons/api/mirage/factories/host-set.js | 5 +++++ addons/api/mirage/generated/factories/host-set.js | 9 --------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/addons/api/mirage/factories/host-set.js b/addons/api/mirage/factories/host-set.js index 3b88650b7..dec391c17 100644 --- a/addons/api/mirage/factories/host-set.js +++ b/addons/api/mirage/factories/host-set.js @@ -12,4 +12,9 @@ export default factory.extend({ 'remove-hosts', ], id: (i) => `host-set-id-${i}`, + plugin() { + if (this.type === 'plugin') { + return this.hostCatalog.plugin; + } + }, }); diff --git a/addons/api/mirage/generated/factories/host-set.js b/addons/api/mirage/generated/factories/host-set.js index 84e635d58..5e0e4836e 100644 --- a/addons/api/mirage/generated/factories/host-set.js +++ b/addons/api/mirage/generated/factories/host-set.js @@ -11,13 +11,4 @@ export default Factory.extend({ updated_time: () => date.recent(), version: () => datatype.number(), size: () => datatype.number(), - plugin: function (i) { - if (this.type == 'plugin') { - return { - id: `pl_${datatype.hexaDecimal(5)}`, - name: i % 2 ? 'aws' : 'azure', - description: random.words(), - }; - } - }, }); From f0b21521303e5561130c003f2362182593b90d40 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 12:34:31 -0800 Subject: [PATCH 5/7] Inherit type from host-catalog --- addons/api/mirage/generated/factories/host-set.js | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/api/mirage/generated/factories/host-set.js b/addons/api/mirage/generated/factories/host-set.js index 5e0e4836e..adb20315c 100644 --- a/addons/api/mirage/generated/factories/host-set.js +++ b/addons/api/mirage/generated/factories/host-set.js @@ -5,6 +5,7 @@ import { random, date, datatype } from 'faker'; * GeneratedHostSetModelFactory */ export default Factory.extend({ + type: () => this.hostCatalog.type, name: () => random.words(), description: () => random.words(), created_time: () => date.recent(), From 92b4dccf78c9e9940f5bd6fbc008538ee5f48c65 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 13:42:58 -0800 Subject: [PATCH 6/7] Default state for type, then if there is host-catalog as parent, inherit its type --- addons/api/mirage/factories/host-set.js | 5 +++++ addons/api/mirage/generated/factories/host-set.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/addons/api/mirage/factories/host-set.js b/addons/api/mirage/factories/host-set.js index dec391c17..d4372bef5 100644 --- a/addons/api/mirage/factories/host-set.js +++ b/addons/api/mirage/factories/host-set.js @@ -12,6 +12,11 @@ export default factory.extend({ 'remove-hosts', ], id: (i) => `host-set-id-${i}`, + type() { + if (this.hostCatalog) { + return this.hostCatalog.type; + } + }, plugin() { if (this.type === 'plugin') { return this.hostCatalog.plugin; diff --git a/addons/api/mirage/generated/factories/host-set.js b/addons/api/mirage/generated/factories/host-set.js index adb20315c..9a8000ef9 100644 --- a/addons/api/mirage/generated/factories/host-set.js +++ b/addons/api/mirage/generated/factories/host-set.js @@ -5,7 +5,7 @@ import { random, date, datatype } from 'faker'; * GeneratedHostSetModelFactory */ export default Factory.extend({ - type: () => this.hostCatalog.type, + type: 'static', name: () => random.words(), description: () => random.words(), created_time: () => date.recent(), From e7cf4c99a577bc9b91b4c4152d97a1f75ecd8855 Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 26 Jan 2022 14:41:39 -0800 Subject: [PATCH 7/7] Refactor type in host-set factory --- addons/api/mirage/factories/host-set.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/addons/api/mirage/factories/host-set.js b/addons/api/mirage/factories/host-set.js index d4372bef5..4bb6ad7bb 100644 --- a/addons/api/mirage/factories/host-set.js +++ b/addons/api/mirage/factories/host-set.js @@ -13,9 +13,7 @@ export default factory.extend({ ], id: (i) => `host-set-id-${i}`, type() { - if (this.hostCatalog) { - return this.hostCatalog.type; - } + return this.hostCatalog?.type || factory.attrs.type; }, plugin() { if (this.type === 'plugin') {