Merge branch 'main' into chore-refactor-host-catalog-mirage-data
This commit is contained in:
5 files changed
+125
-36
No files matched your search
@@ -1,9 +0,0 @@
|
||||
import Fragment from 'ember-data-model-fragments/fragment';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
export default class FragmentHostAttributesModel extends Fragment {
|
||||
@attr('string', {
|
||||
description: 'The address (DNS or IP name) used to reach the host',
|
||||
})
|
||||
address;
|
||||
}
|
||||
@@ -1,12 +1,35 @@
|
||||
import GeneratedHostModel from '../generated/models/host';
|
||||
import { fragment } from 'ember-data-model-fragments/attributes';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
export default class HostModel extends GeneratedHostModel {
|
||||
/**
|
||||
* Attributes of this resource, if any, represented as a JSON fragment.
|
||||
* @type {FragmentHostAttributesModel}
|
||||
*/
|
||||
@fragment('fragment-host-attributes', { defaultValue: {} }) attributes;
|
||||
// =attributes
|
||||
|
||||
// Static specific fields
|
||||
@attr('string', {
|
||||
description: 'The address (DNS or IP name) used to reach the host',
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
address;
|
||||
|
||||
// Plugin specific fields
|
||||
@attr({
|
||||
readOnly: true,
|
||||
emptyArrayIfMissing: true,
|
||||
})
|
||||
ip_addresses;
|
||||
|
||||
@attr('string', {
|
||||
description: '',
|
||||
readOnly: true,
|
||||
})
|
||||
external_id;
|
||||
|
||||
// Aws specific
|
||||
@attr({
|
||||
readOnly: true,
|
||||
emptyArrayIfMissing: true,
|
||||
})
|
||||
dns_names;
|
||||
|
||||
/**
|
||||
* True if the host is static.
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from 'api/models/fragment-host-attributes';
|
||||
@@ -4,19 +4,92 @@ import { setupTest } from 'ember-qunit';
|
||||
module('Unit | Serializer | host', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it exists', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let serializer = store.serializerFor('host');
|
||||
|
||||
assert.ok(serializer);
|
||||
test('it serializes host with type static as expected', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const record = store.createRecord('host', {
|
||||
name: 'Host 1',
|
||||
compositeType: 'static',
|
||||
description: 'Description',
|
||||
host_catalog_id: '123',
|
||||
version: 1,
|
||||
address: '188e:68a9:b342:c05c:2595:2f46:499c:759f',
|
||||
});
|
||||
assert.deepEqual(record.serialize(), {
|
||||
name: 'Host 1',
|
||||
description: 'Description',
|
||||
type: 'static',
|
||||
host_catalog_id: '123',
|
||||
version: 1,
|
||||
attributes: {
|
||||
address: '188e:68a9:b342:c05c:2595:2f46:499c:759f',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('it serializes records', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let record = store.createRecord('host', {});
|
||||
test('it normalizes type static record as expected', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('host');
|
||||
const host = store.createRecord('host').constructor;
|
||||
const payload = {
|
||||
id: '1',
|
||||
name: 'host test',
|
||||
type: 'static',
|
||||
authorized_actions: ['no-op', 'read'],
|
||||
address: '10.0.0.1',
|
||||
};
|
||||
const normalized = serializer.normalizeSingleResponse(store, host, payload);
|
||||
assert.deepEqual(normalized, {
|
||||
included: [],
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'host',
|
||||
attributes: {
|
||||
name: 'host test',
|
||||
type: 'static',
|
||||
authorized_actions: ['no-op', 'read'],
|
||||
address: '10.0.0.1',
|
||||
dns_names: [],
|
||||
ip_addresses: [],
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
let serializedRecord = record.serialize();
|
||||
|
||||
assert.ok(serializedRecord);
|
||||
test('it normalizes type plugin (aws) record as expected', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('host');
|
||||
const host = store.createRecord('host').constructor;
|
||||
const payload = {
|
||||
id: '1',
|
||||
name: 'host test',
|
||||
type: 'plugin',
|
||||
plugin: {
|
||||
name: 'aws',
|
||||
},
|
||||
authorized_actions: ['no-op', 'read'],
|
||||
ip_addresses: ['10.0.0.1', '10.0.0.2', '10.0.0.3'],
|
||||
dns_names: ['test.example.internal', 'test.example.external'],
|
||||
};
|
||||
const normalized = serializer.normalizeSingleResponse(store, host, payload);
|
||||
assert.deepEqual(normalized, {
|
||||
included: [],
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'host',
|
||||
attributes: {
|
||||
name: 'host test',
|
||||
type: 'plugin',
|
||||
authorized_actions: ['no-op', 'read'],
|
||||
ip_addresses: ['10.0.0.1', '10.0.0.2', '10.0.0.3'],
|
||||
dns_names: ['test.example.internal', 'test.example.external'],
|
||||
plugin: { name: 'aws' },
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -17,21 +17,24 @@ export default class ScopesScopeHostCatalogsNewRoute extends Route {
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Rollback/destroy any new, unsaved instances from this route before
|
||||
* creating another.
|
||||
*/
|
||||
beforeModel() {
|
||||
if (this.currentModel?.isNew) this.currentModel.rollbackAttributes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new unsaved host catalog belonging to the current scope.
|
||||
* Also rollback/destroy any new, unsaved instances from this route before
|
||||
* creating another, but reuse name/description if available.
|
||||
* @return {HostCatalogModel}
|
||||
*/
|
||||
model() {
|
||||
const scopeModel = this.modelFor('scopes.scope');
|
||||
return this.store.createRecord('host-catalog', { scopeModel });
|
||||
let name, description;
|
||||
if (this.currentModel?.isNew) {
|
||||
({ name, description } = this.currentModel);
|
||||
this.currentModel.rollbackAttributes();
|
||||
}
|
||||
return this.store.createRecord('host-catalog', {
|
||||
scopeModel,
|
||||
name,
|
||||
description,
|
||||
});
|
||||
}
|
||||
|
||||
afterModel(model, transition) {
|
||||
|
||||
Reference in new issue
Block a user