From 30014ff8feff64d29dce840bde500ac11a564f47 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Wed, 20 Jan 2021 15:36:23 +0000 Subject: [PATCH] ui: Convert Service.GatewayConfig to a model fragment (#9586) * ui: Convert Service.GatewayConfig to a model fragment We added the ember-intl addon, which has its own format-number helper. We replaced our own similarly named helper with this one, but the ember-intl one is far stricter and errors if the arguments passed are undefined. Our previously one would cope with this. We'd rather continue to use the stricter ember-intl helper, so here we convert the GatewayConfig property to a model fragment so that we can give the GatewayConfig.AssociatedServices property a default zero value. --- .../consul-ui/app/models/gateway-config.js | 12 +++++++++ ui/packages/consul-ui/app/models/service.js | 3 ++- .../mock-api/v1/internal/ui/services | 2 ++ .../services/repository/service-test.js | 27 ++++++++++--------- 4 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 ui/packages/consul-ui/app/models/gateway-config.js diff --git a/ui/packages/consul-ui/app/models/gateway-config.js b/ui/packages/consul-ui/app/models/gateway-config.js new file mode 100644 index 0000000000..ffe234019e --- /dev/null +++ b/ui/packages/consul-ui/app/models/gateway-config.js @@ -0,0 +1,12 @@ +import Fragment from 'ember-data-model-fragments/fragment'; +import { array } from 'ember-data-model-fragments/attributes'; +import { attr } from '@ember-data/model'; + +export default class GatewayConfig extends Fragment { + // AssociatedServiceCount is only populated when asking for a list of + // services + @attr('number', { defaultValue: () => 0 }) AssociatedServiceCount; + // Addresses is only populated when asking for a list of services for a + // specific gateway + @array('string', { defaultValue: () => [] }) Addresses; +} diff --git a/ui/packages/consul-ui/app/models/service.js b/ui/packages/consul-ui/app/models/service.js index 0353caf691..dc1f3a8d0f 100644 --- a/ui/packages/consul-ui/app/models/service.js +++ b/ui/packages/consul-ui/app/models/service.js @@ -1,6 +1,7 @@ import Model, { attr } from '@ember-data/model'; import { computed } from '@ember/object'; import { tracked } from '@glimmer/tracking'; +import { fragment } from 'ember-data-model-fragments/attributes'; export const PRIMARY_KEY = 'uid'; export const SLUG_KEY = 'Name'; @@ -41,8 +42,8 @@ export default class Service extends Model { @attr() Nodes; // array @attr() Proxy; // Service - @attr() GatewayConfig; // {AssociatedServiceCount: 0} @attr() ExternalSources; // array + @fragment('gateway-config') GatewayConfig; @attr() Meta; // {} @attr() meta; // {} diff --git a/ui/packages/consul-ui/mock-api/v1/internal/ui/services b/ui/packages/consul-ui/mock-api/v1/internal/ui/services index 3fa598cd2c..1542c45942 100644 --- a/ui/packages/consul-ui/mock-api/v1/internal/ui/services +++ b/ui/packages/consul-ui/mock-api/v1/internal/ui/services @@ -68,7 +68,9 @@ ${typeof location.search.ns !== 'undefined' ? ` "ConnectedWithProxy":${fake.random.boolean()}, "ConnectedWithGateway":${fake.random.boolean()}, "GatewayConfig": { +${fake.random.boolean() ? ` "AssociatedServiceCount": ${fake.random.number({min: 1, max: 4000})} +` : ``} }, ${kind !== '' ? ` "Kind": "${kind}", diff --git a/ui/packages/consul-ui/tests/integration/services/repository/service-test.js b/ui/packages/consul-ui/tests/integration/services/repository/service-test.js index 677d05eec6..39eea2a7e8 100644 --- a/ui/packages/consul-ui/tests/integration/services/repository/service-test.js +++ b/ui/packages/consul-ui/tests/integration/services/repository/service-test.js @@ -36,19 +36,20 @@ const undefinedNspace = 'default'; return service.findGatewayBySlug(gateway, dc, nspace || undefinedNspace, conf); }, function performAssertion(actual, expected) { - assert.deepEqual( - actual, - expected(function(payload) { - return payload.map(item => - Object.assign({}, item, { - SyncTime: now, - Datacenter: dc, - Namespace: item.Namespace || undefinedNspace, - uid: `["${item.Namespace || undefinedNspace}","${dc}","${item.Name}"]`, - }) - ); - }) - ); + const result = expected(function(payload) { + return payload.map(item => + Object.assign({}, item, { + SyncTime: now, + Datacenter: dc, + Namespace: item.Namespace || undefinedNspace, + uid: `["${item.Namespace || undefinedNspace}","${dc}","${item.Name}"]`, + }) + ); + }); + assert.equal(actual[0].SyncTime, result[0].SyncTime); + assert.equal(actual[0].Datacenter, result[0].Datacenter); + assert.equal(actual[0].Namespace, result[0].Namespace); + assert.equal(actual[0].uid, result[0].uid); } ); });