diff --git a/addons/api/addon/adapters/host-catalog.js b/addons/api/addon/adapters/host-catalog.js new file mode 100644 index 000000000..3f1fab706 --- /dev/null +++ b/addons/api/addon/adapters/host-catalog.js @@ -0,0 +1,23 @@ +import ApplicationAdapter from './application'; + +export default class HostCatalogAdapter extends ApplicationAdapter { + /** + * Overrides the default `buildURL` such that on _create_, URLs for records of + * type `plugin` get an extra query parameter `?plugin_name`. All other + * request types behave as normal. + * + * @override + * @param {string} modelName + * @param {string} id + * @return {string} url + * @return {string} requestType + */ + buildURL(modelName, id, snapshot, requestType) { + let url = super.buildURL(...arguments); + if (requestType === 'createRecord') { + const pluginName = snapshot.attr('plugin')?.name; + if (pluginName) url = `${url}?plugin_name=${pluginName}`; + } + return url; + } +} diff --git a/addons/api/app/adapters/host-catalog.js b/addons/api/app/adapters/host-catalog.js new file mode 100644 index 000000000..00197a624 --- /dev/null +++ b/addons/api/app/adapters/host-catalog.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/host-catalog'; diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index 759eb63c3..bb969a04c 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -270,7 +270,19 @@ export default function () { return hostCatalogs.where({ scopeId }); } ); - this.post('/host-catalogs'); + this.post( + '/host-catalogs', + function ({ hostCatalogs }, { queryParams: { plugin_name } }) { + const attrs = this.normalizedRequestAttrs(); + if (plugin_name) { + attrs.type = 'plugin'; + attrs.plugin = { + name: plugin_name, + }; + } + return hostCatalogs.create(attrs); + } + ); this.get('/host-catalogs/:id'); this.patch('/host-catalogs/:id'); this.del('/host-catalogs/:id'); diff --git a/addons/api/tests/unit/adapters/host-catalog-test.js b/addons/api/tests/unit/adapters/host-catalog-test.js new file mode 100644 index 000000000..2df45a117 --- /dev/null +++ b/addons/api/tests/unit/adapters/host-catalog-test.js @@ -0,0 +1,48 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Adapter | host catalog', function (hooks) { + setupTest(hooks); + + test('it generates correct createRecord URLs for static host catalogs', function (assert) { + assert.expect(1); + const scopeID = 'o_1'; + const mockSnapshot = { + adapterOptions: { + scopeID, + }, + attr() { + return null; + }, + }; + const adapter = this.owner.lookup('adapter:host-catalog'); + const createRecordURL = adapter.buildURL( + 'host-catalog', + null, + mockSnapshot, + 'createRecord' + ); + assert.equal(createRecordURL, '/v1/host-catalogs'); + }); + + test('it generates correct createRecord URLs for dynamic host catalogs with extra query parameter ?plugin_name', function (assert) { + assert.expect(1); + const scopeID = 'o_1'; + const mockSnapshot = { + adapterOptions: { + scopeID, + }, + attr() { + return { name: 'aws' }; + }, + }; + const adapter = this.owner.lookup('adapter:host-catalog'); + const createRecordURL = adapter.buildURL( + 'host-catalog', + null, + mockSnapshot, + 'createRecord' + ); + assert.equal(createRecordURL, '/v1/host-catalogs?plugin_name=aws'); + }); +});