Skip to content

Commit

Permalink
ui: Add blocking queries to gateways (#7967)
Browse files Browse the repository at this point in the history
* Remove gateway endpoint adapter, model, and serializer and tests

* Update service tests to handle gateway-services-nodes

* Upgrade consul-api-double to 2.15.2

* Add a fairly temporary shouldReconcile method

Co-authored-by: John Cowen <[email protected]>
  • Loading branch information
kaxcode and John Cowen committed Jun 3, 2020
1 parent 12b1bc2 commit d459bfd
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 251 deletions.
17 changes: 0 additions & 17 deletions ui-v2/app/adapters/gateway.js

This file was deleted.

25 changes: 18 additions & 7 deletions ui-v2/app/adapters/service.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import Adapter from './application';
// TODO: Update to use this.formatDatacenter()
export default Adapter.extend({
requestForQuery: function(request, { dc, ns, index }) {
return request`
GET /v1/internal/ui/services?${{ dc }}
requestForQuery: function(request, { dc, ns, index, gateway }) {
if (typeof gateway !== 'undefined') {
return request`
GET /v1/internal/ui/gateway-services-nodes/${gateway}?${{ dc }}
${{
...this.formatNspace(ns),
index,
}}
${{
...this.formatNspace(ns),
index,
}}
`;
} else {
return request`
GET /v1/internal/ui/services?${{ dc }}
${{
...this.formatNspace(ns),
index,
}}
`;
}
},
requestForQueryRecord: function(request, { dc, ns, index, id }) {
if (typeof id === 'undefined') {
Expand Down
12 changes: 0 additions & 12 deletions ui-v2/app/models/gateway.js

This file was deleted.

1 change: 1 addition & 0 deletions ui-v2/app/models/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default Model.extend({
ProxyFor: attr(),
Kind: attr('string'),
ExternalSources: attr(),
GatewayConfig: attr(),
Meta: attr(),
Address: attr('string'),
TaggedAddresses: attr(),
Expand Down
3 changes: 1 addition & 2 deletions ui-v2/app/routes/dc/services/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default Route.extend({
intentionRepo: service('repository/intention'),
chainRepo: service('repository/discovery-chain'),
proxyRepo: service('repository/proxy'),
gatewayRepo: service('repository/gateway'),
settings: service('settings'),
model: function(params, transition = {}) {
const dc = this.modelFor('dc').dc.Name;
Expand Down Expand Up @@ -55,7 +54,7 @@ export default Route.extend({
.then(model => {
return ['ingress-gateway', 'terminating-gateway'].includes(get(model, 'item.Service.Kind'))
? hash({
gateway: this.gatewayRepo.findBySlug(params.name, dc, nspace),
gatewayServices: this.repo.findGatewayBySlug(params.name, dc, nspace),
...model,
})
: model;
Expand Down
17 changes: 0 additions & 17 deletions ui-v2/app/serializers/gateway.js

This file was deleted.

5 changes: 5 additions & 0 deletions ui-v2/app/services/client/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export default Service.extend({
body: function(strs, ...values) {
let body = {};
const doubleBreak = strs.reduce(function(prev, item, i) {
// Ensure each line has no whitespace either end, including empty lines
item = item
.split('\n')
.map(item => item.trim())
.join('\n');
if (item.indexOf('\n\n') !== -1) {
return i;
}
Expand Down
2 changes: 1 addition & 1 deletion ui-v2/app/services/data-source/protocols/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default Service.extend({
// always be complete, they should never have things like '///model'
let find;
const repo = this[model];
if (typeof repo.reconcile === 'function') {
if (repo.shouldReconcile(src)) {
configuration.createEvent = function(result = {}, configuration) {
const event = {
type: 'message',
Expand Down
3 changes: 3 additions & 0 deletions ui-v2/app/services/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default Service.extend({
},
//
store: service('store'),
shouldReconcile: function(method) {
return true;
},
reconcile: function(meta = {}) {
// unload anything older than our current sync date/time
if (typeof meta.date !== 'undefined') {
Expand Down
8 changes: 0 additions & 8 deletions ui-v2/app/services/repository/gateway.js

This file was deleted.

18 changes: 18 additions & 0 deletions ui-v2/app/services/repository/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export default RepositoryService.extend({
getModelName: function() {
return modelName;
},
shouldReconcile: function(method) {
switch (method) {
case 'findGatewayBySlug':
return false;
}
return this._super(...arguments);
},
findBySlug: function(slug, dc) {
return this._super(...arguments).then(function(item) {
// TODO: Move this to the Serializer
Expand Down Expand Up @@ -69,4 +76,15 @@ export default RepositoryService.extend({
throw e;
});
},
findGatewayBySlug: function(slug, dc, nspace, configuration) {
const query = {
dc: dc,
ns: nspace,
gateway: slug,
};
if (typeof configuration.cursor !== 'undefined') {
query.index = configuration.cursor;
}
return this.store.query(this.getModelName(), query);
},
});
6 changes: 2 additions & 4 deletions ui-v2/app/services/repository/type/event-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ const createProxy = function(repo, find, settings, cache, serialize = JSON.strin
const client = this.client;
// custom createEvent, here used to reconcile the ember-data store for each tick
let createEvent;
if (typeof repo.reconcile === 'function') {
if (repo.shouldReconcile(find)) {
createEvent = function(result = {}, configuration) {
const event = {
type: 'message',
data: result,
};
if (repo.reconcile === 'function') {
repo.reconcile(get(event, 'data.meta'));
}
repo.reconcile(get(event, 'data.meta'));
return event;
};
}
Expand Down
4 changes: 2 additions & 2 deletions ui-v2/app/templates/dc/services/show/services.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div id="services" class="tab-section">
<div role="tabpanel">
{{#if (gt gateway.Services.length 0)}}
{{#if (gt gatewayServices.length 0)}}
<section>
<p>
The following services may receive traffic from external services through this gateway. Learn more about configuring gateways in our
<a href="{{env 'CONSUL_DOCS_URL'}}/connect/terminating_gateway.html" target="_blank" rel="noopener noreferrer">step-by-step guide</a>.
</p>
<ConsulServiceList @routeName="dc.services.show" @items={{gateway.Services}} @nspace={{nspace}} />
<ConsulServiceList @routeName="dc.services.show" @items={{gatewayServices}} @nspace={{nspace}} />
</section>
{{else}}
<p>
Expand Down
4 changes: 2 additions & 2 deletions ui-v2/app/templates/dc/services/show/upstreams.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div id="upstreams" class="tab-section">
<div role="tabpanel">
{{#if (gt gateway.Services.length 0)}}
{{#if (gt gatewayServices.length 0)}}
<section>
<p>
Upstreams are services that may receive traffic from this gateway. Learn more about configuring gateways in our <a href="{{env 'CONSUL_DOCS_URL'}}/connect/ingress_gateway.html" target="_blank" rel="noopener noreferrer">documentation</a>.
</p>
{{#let item.Service.Namespace as |nspace|}}
<ListCollection @items={{gateway.Services}} class="consul-upstream-list" as |item index|>
<ListCollection @items={{gatewayServices}} class="consul-upstream-list" as |item index|>
{{#if (service/exists item)}}
<a data-test-service-name href={{href-to 'dc.services.show' item.Name}}>
{{item.Name}}
Expand Down
27 changes: 0 additions & 27 deletions ui-v2/tests/integration/adapters/gateway-test.js

This file was deleted.

15 changes: 15 additions & 0 deletions ui-v2/tests/integration/adapters/service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ module('Integration | Adapter | service', function(hooks) {
actual = actual.join('\n').trim();
assert.equal(actual, `${shouldHaveNspace(nspace) ? `ns=${nspace}` : ``}`);
});
test(`requestForQuery returns the correct url/method when called with gateway when nspace is ${nspace}`, function(assert) {
const adapter = this.owner.lookup('adapter:service');
const client = this.owner.lookup('service:client/http');
const gateway = 'gateway';
const expected = `GET /v1/internal/ui/gateway-services-nodes/${gateway}?dc=${dc}`;
let actual = adapter.requestForQuery(client.url, {
dc: dc,
ns: nspace,
gateway: gateway,
});
actual = actual.split('\n');
assert.equal(actual.shift().trim(), expected);
actual = actual.join('\n').trim();
assert.equal(actual, `${shouldHaveNspace(nspace) ? `ns=${nspace}` : ``}`);
});
test(`requestForQueryRecord returns the correct url/method when nspace is ${nspace}`, function(assert) {
const adapter = this.owner.lookup('adapter:service');
const client = this.owner.lookup('service:client/http');
Expand Down
47 changes: 0 additions & 47 deletions ui-v2/tests/integration/serializers/gateway-test.js

This file was deleted.

31 changes: 31 additions & 0 deletions ui-v2/tests/integration/serializers/service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ module('Integration | Serializer | service', function(hooks) {
assert.deepEqual(actual, expected);
});
});
test(`respondForQuery returns the correct data for list endpoint when gateway is set when nspace is ${nspace}`, function(assert) {
const serializer = this.owner.lookup('serializer:service');
const gateway = 'gateway';
const request = {
url: `/v1/internal/ui/gateway-services-nodes/${gateway}?dc=${dc}${
typeof nspace !== 'undefined' ? `&ns=${nspace}` : ``
}`,
};
return get(request.url).then(function(payload) {
const expected = payload.map(item =>
Object.assign({}, item, {
Namespace: item.Namespace || undefinedNspace,
Datacenter: dc,
uid: `["${item.Namespace || undefinedNspace}","${dc}","${item.Name}"]`,
})
);
const actual = serializer.respondForQuery(
function(cb) {
const headers = {};
const body = payload;
return cb(headers, body);
},
{
dc: dc,
ns: nspace,
gateway: gateway,
}
);
assert.deepEqual(actual, expected);
});
});
test(`respondForQueryRecord returns the correct data for item endpoint when nspace is ${nspace}`, function(assert) {
const serializer = this.owner.lookup('serializer:service');
const id = 'service-name';
Expand Down
Loading

0 comments on commit d459bfd

Please sign in to comment.