From ffd11deeb9f2b3ca361712a6f4f4d1025cf86a36 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Tue, 16 Apr 2019 16:26:45 +0000 Subject: [PATCH] ui: Normalize/serialize Policies/ServiceID's in the Adapter --- ui-v2/app/adapters/role.js | 39 +++++++++++++++++-- ui-v2/app/adapters/token.js | 2 +- ui-v2/app/serializers/role.js | 25 ------------ .../templates/components/service-identity.hbs | 14 +++++-- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/ui-v2/app/adapters/role.js b/ui-v2/app/adapters/role.js index 9d54d6c72288..efbae90ae74f 100644 --- a/ui-v2/app/adapters/role.js +++ b/ui-v2/app/adapters/role.js @@ -4,12 +4,43 @@ import Adapter, { DATACENTER_QUERY_PARAM as API_DATACENTER_KEY, } from './application'; +import { get } from '@ember/object'; import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/role'; import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc'; import { OK as HTTP_OK } from 'consul-ui/utils/http/status'; import { PUT as HTTP_PUT } from 'consul-ui/utils/http/method'; import minimizeModel from 'consul-ui/utils/minimizeModel'; +const createTemplatedPolicies = function(item) { + item.ServiceIdentities.forEach(function(identity) { + const policy = { + Name: identity.ServiceName, + template: 'service-identity', + Datacenters: identity.Datacenters, + }; + item.Policies.push(policy); + }); + return item; +}; +const createServiceIdentities = function(item) { + item.ServiceIdentities = item.Policies.filter(function(item) { + return get(item, 'template') === 'service-identity'; + }).map(function(item) { + return { + ServiceName: get(item, 'Name'), + Datacenters: get(item, 'Datacenters'), + }; + }); + item.Policies = item.Policies.filter(function(item) { + return get(item, 'template') !== 'service-identity'; + }).map(function(item) { + return { + Name: get(item, 'Name'), + ID: get(item, 'ID'), + }; + }); + return item; +}; export default Adapter.extend({ urlForQuery: function(query, modelName) { return this.appendURL('acl/roles', [], this.cleanQuery(query)); @@ -54,12 +85,12 @@ export default Adapter.extend({ }, handleSingleResponse: function(url, response, primary, slug) { // Sometimes we get `Policies: null`, make null equal an empty array - ['Policies'].forEach(function(prop) { + ['Policies', 'ServiceIdentities'].forEach(function(prop) { if (typeof response[prop] === 'undefined' || response[prop] === null) { response[prop] = []; } }); - return this._super(url, response, primary, slug); + return this._super(url, createTemplatedPolicies(response), primary, slug); }, methodForRequest: function(params) { switch (params.requestType) { @@ -73,8 +104,8 @@ export default Adapter.extend({ switch (params.requestType) { case REQUEST_UPDATE: case REQUEST_CREATE: - data.role.Policies = minimizeModel(data.role.Policies); - data = data.role; + data = createServiceIdentities(data.role); + data.Policies = minimizeModel(data.Policies); break; } return data; diff --git a/ui-v2/app/adapters/token.js b/ui-v2/app/adapters/token.js index 5ece2577ed73..24e2bf12bea7 100644 --- a/ui-v2/app/adapters/token.js +++ b/ui-v2/app/adapters/token.js @@ -110,7 +110,7 @@ export default Adapter.extend({ }, handleSingleResponse: function(url, response, primary, slug) { // Sometimes we get `Policies: null`, make null equal an empty array - ['Policies', 'Roles'].forEach(function(prop) { + ['Policies', 'Roles', 'ServiceIdentities'].forEach(function(prop) { if (typeof response[prop] === 'undefined' || response[prop] === null) { response[prop] = []; } diff --git a/ui-v2/app/serializers/role.js b/ui-v2/app/serializers/role.js index 5871a215b895..4a43dd24f77b 100644 --- a/ui-v2/app/serializers/role.js +++ b/ui-v2/app/serializers/role.js @@ -1,30 +1,5 @@ import Serializer from './application'; import { PRIMARY_KEY } from 'consul-ui/models/role'; -const createTemplatedPolicies = function(item) { - item.ServiceIdentities.forEach(function(item) { - const policy = { - Name: item.ServiceName, - template: 'service-identity', - Datacenters: item.Datacenters, - }; - // if(typeof item.Datancenters !== 'undefined') { - - // } - if (typeof item.Policies === 'undefined') { - item.Policies = []; - } - item.Policies.push(policy); - }); - return item; -}; export default Serializer.extend({ primaryKey: PRIMARY_KEY, - normalizeResponse: function(store, primaryModelClass, payload, id, requestType) { - if (requestType === 'queryRecord') { - payload = createTemplatedPolicies(payload); - } else { - payload = payload.map(createTemplatedPolicies); - } - return this._super(...arguments); - }, }); diff --git a/ui-v2/app/templates/components/service-identity.hbs b/ui-v2/app/templates/components/service-identity.hbs index 5b5cfe0d14c1..281697d11620 100644 --- a/ui-v2/app/templates/components/service-identity.hbs +++ b/ui-v2/app/templates/components/service-identity.hbs @@ -1,6 +1,12 @@ -node_prefix "{{name}}" { - -} service "{{name}}" { - + policy = "write" +} +service "{{name}}-sidecar-proxy" { + policy = "write" +} +service_prefix "" { + policy = "read" +} +node_prefix "" { + policy = "read" } \ No newline at end of file