diff --git a/addons/api/addon/models/fragment-host-attributes.js b/addons/api/addon/models/fragment-host-attributes.js deleted file mode 100644 index 3f0ac9073..000000000 --- a/addons/api/addon/models/fragment-host-attributes.js +++ /dev/null @@ -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; -} diff --git a/addons/api/addon/models/host.js b/addons/api/addon/models/host.js index 14acc4d27..035db1cd9 100644 --- a/addons/api/addon/models/host.js +++ b/addons/api/addon/models/host.js @@ -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. diff --git a/addons/api/app/models/fragment-host-attributes.js b/addons/api/app/models/fragment-host-attributes.js deleted file mode 100644 index 201efcb7a..000000000 --- a/addons/api/app/models/fragment-host-attributes.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'api/models/fragment-host-attributes'; diff --git a/addons/api/tests/unit/serializers/host-test.js b/addons/api/tests/unit/serializers/host-test.js index 4c531ca82..42d3aede0 100644 --- a/addons/api/tests/unit/serializers/host-test.js +++ b/addons/api/tests/unit/serializers/host-test.js @@ -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: {}, + }, + }); }); });