Merge pull request #170 from hashicorp/refactor-host-set-pathing
Refactor host set pathing
This commit is contained in:
11 files changed
+47
-71
No files matched your search
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
+5
-16
@@ -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) {
|
||||
|
||||
+1
-6
@@ -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 });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Model, belongsTo, hasMany } from 'ember-cli-mirage';
|
||||
|
||||
export default Model.extend({
|
||||
|
||||
scope: belongsTo(),
|
||||
target: belongsTo(),
|
||||
hostCatalog: belongsTo(),
|
||||
hosts: hasMany()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user