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..14acc4d27 100644 --- a/addons/api/addon/models/host.js +++ b/addons/api/addon/models/host.js @@ -7,4 +7,58 @@ export default class HostModel extends GeneratedHostModel { * @type {FragmentHostAttributesModel} */ @fragment('fragment-host-attributes', { defaultValue: {} }) attributes; + + /** + * True if the host is static. + * @type {boolean} + */ + get isStatic() { + return this.type === 'static'; + } + + /** + * True if the host is plugin. + * @type {boolean} + */ + get isPlugin() { + return this.type === 'plugin'; + } + + /** + * True if the host plugin type is AWS. + * @type {boolean} + */ + get isAWS() { + return this.compositeType === 'aws'; + } + + /** + * True if the host plugin type is Azure. + * @type {boolean} + */ + get isAzure() { + return this.compositeType === 'azure'; + } + + /** + * If host is a plugin return `plugin.name`, + * otherwise return the host type. + * @type {string} + */ + get compositeType() { + return this.isPlugin ? this.plugin.name : this.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') { + this.type = 'static'; + } else { + this.type = 'plugin'; + this.plugin = { name: type }; + } + } } diff --git a/addons/api/mirage/factories/host.js b/addons/api/mirage/factories/host.js index 895b44468..980a5b26d 100644 --- a/addons/api/mirage/factories/host.js +++ b/addons/api/mirage/factories/host.js @@ -10,4 +10,12 @@ 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/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'); + }); });