diff --git a/addons/api/addon/adapters/host-set.js b/addons/api/addon/adapters/host-set.js index fbbe11495..fe35a1935 100644 --- a/addons/api/addon/adapters/host-set.js +++ b/addons/api/addon/adapters/host-set.js @@ -7,32 +7,12 @@ export default class HostSetAdapter extends ApplicationAdapter { @service store; - // =methods + // =attributes /** - * Append a host catalog URL ahead of the host sets path if `hostCatalogID` is - * passed via adapter options. Otherwise, generate the prefix as normal. - * - * TODO: generalize the interface to resource-prefixed URLs by accepting - * a modelName and an ID, rather than looking for a known key. - * + * Host catalogs use the new, unprefixed pathing style. * @override - * @param {string} path - * @param {string} parentURL - * @param {string} modelName - * @param {string} id - * @param {object} snapshot - * @return {string} urlPrefix */ - urlPrefix(path, parentURL, modelName, id, snapshot) { - const hostCatalogID = snapshot?.adapterOptions?.hostCatalogID; - if (hostCatalogID) { - const hostCatalogAdapter = this.store.adapterFor('host-catalog'); - return hostCatalogAdapter - ._buildPrefixedURL('host-catalog', hostCatalogID, snapshot); - } else { - return super.urlPrefix(...arguments); - } - } + hasScopePrefix = false; } diff --git a/addons/api/addon/serializers/host-set.js b/addons/api/addon/serializers/host-set.js index 3ce7c7161..697e7748d 100644 --- a/addons/api/addon/serializers/host-set.js +++ b/addons/api/addon/serializers/host-set.js @@ -2,6 +2,14 @@ import ApplicationSerializer from './application'; export default class HostSetSerializer extends ApplicationSerializer { + // =attributes + + /** + * Serialized the scope ID into the `scope_id` field. + * @type {boolean} + */ + serializeScopeID = true; + // =methods /** diff --git a/addons/api/tests/unit/adapters/host-set-test.js b/addons/api/tests/unit/adapters/host-set-test.js index 9071a703a..1ab6f7a63 100644 --- a/addons/api/tests/unit/adapters/host-set-test.js +++ b/addons/api/tests/unit/adapters/host-set-test.js @@ -6,23 +6,17 @@ module('Unit | Adapter | host set', function(hooks) { test('it generates correct complete URLs', function (assert) { assert.expect(2); - const scopeID = 'o_1'; const hostCatalogID = 'hc_098'; const method = 'my-custom-method'; - const mockSnapshot = { - adapterOptions: { - scopeID, - method - } - }; + const mockSnapshot = { adapterOptions: { method } }; const adapter = this.owner.lookup('adapter:host-set'); // Without hostCatalogID... let url = adapter.buildURL('host-set', null, mockSnapshot, 'findAll'); - assert.equal(url, '/v1/scopes/o_1/host-sets:my-custom-method'); + assert.equal(url, '/v1/host-sets:my-custom-method'); // With hostCatalogID... mockSnapshot.adapterOptions.hostCatalogID = hostCatalogID; url = adapter.buildURL('host-set', null, mockSnapshot, 'findAll'); - assert.equal(url, '/v1/host-catalogs/hc_098/host-sets:my-custom-method'); + assert.equal(url, '/v1/host-sets:my-custom-method'); }); }); diff --git a/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets.js b/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets.js index 4fbec613f..045a0f632 100644 --- a/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets.js +++ b/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets.js @@ -8,19 +8,6 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo @service intl; @service notify; - // =attributes - - /** - * Configure adapter options using current host catalog and - * the current project scope. - * @return {object} - */ - get adapterOptions() { - const scopeID = this.modelFor('scopes.scope.projects.project').id; - const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id; - return { adapterOptions : { scopeID, hostCatalogID } }; - } - // =methods /** @@ -29,7 +16,9 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo */ async model() { this.store.unloadAll('host-set'); - return this.store.findAll('host-set', this.adapterOptions); + const { id: host_catalog_id } = + this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog'); + return this.store.query('host-set', { host_catalog_id }); } // =actions @@ -54,7 +43,7 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo async save(hostSet) { try { const { isNew } = hostSet; - await hostSet.save(this.adapterOptions); + await hostSet.save(); this.notify.success( this.intl.t(isNew ? 'notify.create-success' : 'notify.save-success') ); @@ -72,7 +61,7 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo @action async delete(hostSet) { try { - await hostSet.destroyRecord(this.adapterOptions); + await hostSet.destroyRecord(); this.notify.success(this.intl.t('notify.delete-success')); this.transitionTo('scopes.scope.projects.project.host-catalogs.host-catalog.host-sets'); } catch (error) { diff --git a/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets/host-set.js b/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets/host-set.js index 4c98b45f2..7b422c1ac 100644 --- a/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets/host-set.js +++ b/ui/core/app/routes/scopes/scope/projects/project/host-catalogs/host-catalog/host-sets/host-set.js @@ -10,12 +10,7 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsHo * @return {HostSetModel} */ async model({ host_set_id }) { - const { id: scopeID } = this.modelFor('scopes.scope.projects.project'); - const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id; - return this.store.findRecord('host-set', host_set_id, { - reload: true, - adapterOptions: { scopeID, hostCatalogID } - }); + return this.store.findRecord('host-set', host_set_id, { reload: true }); } /** diff --git a/ui/core/app/routes/scopes/scope/projects/project/targets/target/add-host-sets.js b/ui/core/app/routes/scopes/scope/projects/project/targets/target/add-host-sets.js index d7723d4f0..96e1cd71a 100644 --- a/ui/core/app/routes/scopes/scope/projects/project/targets/target/add-host-sets.js +++ b/ui/core/app/routes/scopes/scope/projects/project/targets/target/add-host-sets.js @@ -26,12 +26,10 @@ export default class ScopesScopeProjectsProjectTargetsTargetAddHostSetsRoute ext async model() { const target = this.modelFor('scopes.scope.projects.project.targets.target'); - const { scopeID } = target; const { id: scope_id } = this.modelFor('scopes.scope.projects.project'); const hostCatalogs = await this.store.query('host-catalog', { scope_id }); - await all(hostCatalogs.map(({ id: hostCatalogID }) => - this.store - .findAll('host-set', { adapterOptions: { scopeID, hostCatalogID } }) + await all(hostCatalogs.map(({ id: host_catalog_id }) => + this.store.query('host-set', { host_catalog_id }) )); const hostSets = this.store.peekAll('host-set'); return { diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 828c3f971..7c7a1fdf8 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -171,18 +171,14 @@ export default function() { // host-set - this.get('/host-catalogs/:hostCatalogId/host-sets', function ({ hostSets }, { params: { hostCatalogId } }) { + this.get('/host-sets', function ({ hostSets }, { queryParams: { host_catalog_id: hostCatalogId } }) { return hostSets.where({ hostCatalogId }); }); - this.post('/host-catalogs/:hostCatalogId/host-sets', function ({ hostSets }, { params: { hostCatalogId } }) { - const attrs = this.normalizedRequestAttrs(); - attrs.hostCatalogId = hostCatalogId; - return hostSets.create(attrs); - }); - this.get('/host-catalogs/:hostCatalogId/host-sets/:id'); - this.patch('/host-catalogs/:hostCatalogId/host-sets/:id'); - this.del('/host-catalogs/:hostCatalogId/host-sets/:id'); - this.post('/host-catalogs/:hostCatalogId/host-sets/:idMethod', function ({ hostSets }, { params: { idMethod } }) { + this.post('/host-sets'); + this.get('/host-sets/:id'); + this.patch('/host-sets/:id'); + this.del('/host-sets/:id'); + this.post('/host-sets/:idMethod', function ({ hostSets }, { params: { idMethod } }) { const attrs = this.normalizedRequestAttrs(); const id = idMethod.split(':')[0]; const method = idMethod.split(':')[1]; diff --git a/ui/core/mirage/factories/host-catalog.js b/ui/core/mirage/factories/host-catalog.js index 15ae0bb26..f2541fcb7 100644 --- a/ui/core/mirage/factories/host-catalog.js +++ b/ui/core/mirage/factories/host-catalog.js @@ -5,10 +5,10 @@ export default factory.extend({ withChildren: trait({ afterCreate(hostCatalog, server) { - const scope = hostCatalog.scope; + const { scope } = hostCatalog; const scopePojo = { id: scope.id, type: scope.type }; const hosts = server.createList('host', 10, { scope: scopePojo, hostCatalog }); - server.createList('host-set', 3, { scope: scopePojo, hostCatalog, hosts }); + server.createList('host-set', 3, { scope, hostCatalog, hosts }); } }) diff --git a/ui/core/mirage/factories/target.js b/ui/core/mirage/factories/target.js index 7c74806a6..a96e15556 100644 --- a/ui/core/mirage/factories/target.js +++ b/ui/core/mirage/factories/target.js @@ -13,7 +13,12 @@ export default factory.extend({ afterCreate(target, server) { let randomlySelectedHostSets; randomlySelectedHostSets = server.schema.hostSets - .where(item => item.scope.id === target.scope.id).models + // BLERG: fun fact, for no reason at all, the element passed + // into a where function is not a full model instance, as you might + // expect at this level of abstraction, but appears to be a serialized + // representation of the model instance. It's very confusing since + // the result set of `where` _is a collection of model instances_. + .where(hostSet => hostSet.scopeId === target.scope.id).models .filter(() => randomBoolean(hostSetChance)); target.update({hostSets: randomlySelectedHostSets}); } diff --git a/ui/core/mirage/models/host-set.js b/ui/core/mirage/models/host-set.js index 3aa8fbf7e..5a579e119 100644 --- a/ui/core/mirage/models/host-set.js +++ b/ui/core/mirage/models/host-set.js @@ -2,6 +2,7 @@ import { Model, belongsTo, hasMany } from 'ember-cli-mirage'; export default Model.extend({ + scope: belongsTo(), target: belongsTo(), hostCatalog: belongsTo(), hosts: hasMany() diff --git a/ui/core/mirage/serializers/host-set.js b/ui/core/mirage/serializers/host-set.js index bd680326e..16d665375 100644 --- a/ui/core/mirage/serializers/host-set.js +++ b/ui/core/mirage/serializers/host-set.js @@ -10,10 +10,20 @@ export default ApplicationSerializer.extend({ )}_ids`; }, + keyForRelationshipId(relationshipName) { + return `${this._container.inflector.singularize( + underscore(relationshipName) + )}_id`; + }, + _hashForModel(model) { const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); json.host_ids = model.hostIds; json.host_catalog_id = model.hostCatalogId; + json.scope = { + id: model.scope.id, + type: model.scope.type + }; return json; }