Skip to content

Commit

Permalink
ui: Normalize/serialize Policies/ServiceID's in the Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cowen committed Apr 16, 2019
1 parent 552e960 commit ffd11de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
39 changes: 35 additions & 4 deletions ui-v2/app/adapters/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ui-v2/app/adapters/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand Down
25 changes: 0 additions & 25 deletions ui-v2/app/serializers/role.js
Original file line number Diff line number Diff line change
@@ -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);
},
});
14 changes: 10 additions & 4 deletions ui-v2/app/templates/components/service-identity.hbs
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit ffd11de

Please sign in to comment.