mirror of
https://github.com/status-im/consul.git
synced 2025-02-24 11:28:40 +00:00
Tie up real endpoints
This commit is contained in:
parent
24f6155efc
commit
55bc45832e
@ -1,7 +1,12 @@
|
|||||||
import Adapter, { DATACENTER_KEY as API_DATACENTER_KEY } from './application';
|
import Adapter, {
|
||||||
|
REQUEST_CREATE,
|
||||||
|
REQUEST_UPDATE,
|
||||||
|
DATACENTER_KEY as API_DATACENTER_KEY,
|
||||||
|
} from './application';
|
||||||
import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc';
|
import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc';
|
||||||
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/intention';
|
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/intention';
|
||||||
import { OK as HTTP_OK } from 'consul-ui/utils/http/status';
|
import { OK as HTTP_OK } from 'consul-ui/utils/http/status';
|
||||||
|
import { POST as HTTP_POST } from 'consul-ui/utils/http/method';
|
||||||
import makeAttrable from 'consul-ui/utils/makeAttrable';
|
import makeAttrable from 'consul-ui/utils/makeAttrable';
|
||||||
export default Adapter.extend({
|
export default Adapter.extend({
|
||||||
urlForQuery: function(query, modelName) {
|
urlForQuery: function(query, modelName) {
|
||||||
@ -33,6 +38,14 @@ export default Adapter.extend({
|
|||||||
).pathname
|
).pathname
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
isCreateRecord: function(url, method) {
|
||||||
|
return (
|
||||||
|
method.toUpperCase() === HTTP_POST &&
|
||||||
|
url.pathname ===
|
||||||
|
this.parseURL(this.urlForCreateRecord('intention', makeAttrable({ [DATACENTER_KEY]: '' })))
|
||||||
|
.pathname
|
||||||
|
);
|
||||||
|
},
|
||||||
handleResponse: function(status, headers, payload, requestData) {
|
handleResponse: function(status, headers, payload, requestData) {
|
||||||
let response = payload;
|
let response = payload;
|
||||||
if (status === HTTP_OK) {
|
if (status === HTTP_OK) {
|
||||||
@ -40,7 +53,9 @@ export default Adapter.extend({
|
|||||||
switch (true) {
|
switch (true) {
|
||||||
case this.isQueryRecord(url):
|
case this.isQueryRecord(url):
|
||||||
case this.isUpdateRecord(url):
|
case this.isUpdateRecord(url):
|
||||||
// case this.isCreateRecord(url):
|
// TODO: We just need to upgrade this entire API to
|
||||||
|
// use a full request-like object
|
||||||
|
case this.isCreateRecord(url, requestData.method):
|
||||||
response = {
|
response = {
|
||||||
...response,
|
...response,
|
||||||
...{
|
...{
|
||||||
@ -61,4 +76,13 @@ export default Adapter.extend({
|
|||||||
}
|
}
|
||||||
return this._super(status, headers, response, requestData);
|
return this._super(status, headers, response, requestData);
|
||||||
},
|
},
|
||||||
|
dataForRequest: function(params) {
|
||||||
|
const data = this._super(...arguments);
|
||||||
|
switch (params.requestType) {
|
||||||
|
case REQUEST_UPDATE:
|
||||||
|
case REQUEST_CREATE:
|
||||||
|
return data.intention;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import Controller from '@ember/controller';
|
import Controller from '@ember/controller';
|
||||||
import { get, set } from '@ember/object';
|
import { get, set } from '@ember/object';
|
||||||
// import Changeset from 'ember-changeset';
|
import Changeset from 'ember-changeset';
|
||||||
// import validations from 'consul-ui/validations/acl';
|
import lookupValidator from 'ember-changeset-validations';
|
||||||
// import lookupValidator from 'ember-changeset-validations';
|
|
||||||
|
import validations from 'consul-ui/validations/intention';
|
||||||
|
|
||||||
export default Controller.extend({
|
export default Controller.extend({
|
||||||
setProperties: function(model) {
|
setProperties: function(model) {
|
||||||
this.changeset = model.item; //new Changeset(model.item, lookupValidator(validations), validations);
|
this.changeset = new Changeset(model.item, lookupValidator(validations), validations);
|
||||||
this._super({
|
this._super({
|
||||||
...model,
|
...model,
|
||||||
...{
|
...{
|
||||||
@ -18,15 +19,16 @@ export default Controller.extend({
|
|||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
change: function(e, value, _target) {
|
change: function(e, value, _target) {
|
||||||
|
// normalize back to standard event
|
||||||
const target = e.target || { ..._target, ...{ name: e, value: value } };
|
const target = e.target || { ..._target, ...{ name: e, value: value } };
|
||||||
switch (target.name) {
|
switch (target.name) {
|
||||||
case 'Action':
|
case 'Action':
|
||||||
set(this.changeset, target.name, target.value);
|
set(this.changeset, target.name, target.value);
|
||||||
|
console.log(target.name, target.value, get(this.changeset, target.name));
|
||||||
break;
|
break;
|
||||||
case 'SourceName':
|
case 'SourceName':
|
||||||
case 'DestinationName':
|
case 'DestinationName':
|
||||||
set(this.changeset, target.name, get(target.value, 'Name'));
|
set(this.changeset, target.name, get(target.value, 'Name'));
|
||||||
set(this.item, target.name, get(target.value, 'Name'));
|
|
||||||
set(this, target.name, target.value);
|
set(this, target.name, target.value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import Model from 'ember-data/model';
|
import Model from 'ember-data/model';
|
||||||
import attr from 'ember-data/attr';
|
import attr from 'ember-data/attr';
|
||||||
|
import writable from 'consul-ui/utils/model/writable';
|
||||||
|
|
||||||
export const PRIMARY_KEY = 'uid';
|
export const PRIMARY_KEY = 'uid';
|
||||||
export const SLUG_KEY = 'ID';
|
export const SLUG_KEY = 'ID';
|
||||||
|
const model = Model.extend({
|
||||||
export default Model.extend({
|
|
||||||
[PRIMARY_KEY]: attr('string'),
|
[PRIMARY_KEY]: attr('string'),
|
||||||
[SLUG_KEY]: attr('string'),
|
[SLUG_KEY]: attr('string'),
|
||||||
Description: attr('string'),
|
Description: attr('string'),
|
||||||
@ -12,8 +12,8 @@ export default Model.extend({
|
|||||||
SourceName: attr('string'),
|
SourceName: attr('string'),
|
||||||
DestinationName: attr('string'),
|
DestinationName: attr('string'),
|
||||||
Precedence: attr('number'),
|
Precedence: attr('number'),
|
||||||
SourceType: attr('string'),
|
SourceType: attr('string', { defaultValue: 'consul' }),
|
||||||
Action: attr('string'),
|
Action: attr('string', { defaultValue: 'deny' }),
|
||||||
DefaultAddr: attr('string'),
|
DefaultAddr: attr('string'),
|
||||||
DefaultPort: attr('number'),
|
DefaultPort: attr('number'),
|
||||||
Meta: attr(),
|
Meta: attr(),
|
||||||
@ -23,3 +23,11 @@ export default Model.extend({
|
|||||||
CreateIndex: attr('number'),
|
CreateIndex: attr('number'),
|
||||||
ModifyIndex: attr('number'),
|
ModifyIndex: attr('number'),
|
||||||
});
|
});
|
||||||
|
export const ATTRS = writable(model, [
|
||||||
|
'Action',
|
||||||
|
'SourceName',
|
||||||
|
'DestinationName',
|
||||||
|
'SourceType',
|
||||||
|
'Description',
|
||||||
|
]);
|
||||||
|
export default model;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import Serializer from './application';
|
import Serializer from './application';
|
||||||
import { PRIMARY_KEY } from 'consul-ui/models/intention';
|
import { PRIMARY_KEY, ATTRS } from 'consul-ui/models/intention';
|
||||||
|
|
||||||
export default Serializer.extend({
|
export default Serializer.extend({
|
||||||
primaryKey: PRIMARY_KEY,
|
primaryKey: PRIMARY_KEY,
|
||||||
|
attrs: ATTRS,
|
||||||
});
|
});
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
export const PUT = 'PUT';
|
export const PUT = 'PUT';
|
||||||
export const DELETE = 'DELETE';
|
export const DELETE = 'DELETE';
|
||||||
|
export const POST = 'POST';
|
||||||
|
11
ui-v2/app/utils/model/writable.js
Normal file
11
ui-v2/app/utils/model/writable.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export default function(model, props, attr = {}) {
|
||||||
|
model.eachAttribute(function(item) {
|
||||||
|
attr[item] = {
|
||||||
|
...attr[item],
|
||||||
|
...{
|
||||||
|
serialize: props.indexOf(item) !== -1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return attr;
|
||||||
|
}
|
6
ui-v2/app/validations/intention.js
Normal file
6
ui-v2/app/validations/intention.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { validatePresence, validateLength } from 'ember-changeset-validations/validators';
|
||||||
|
export default {
|
||||||
|
SourceName: [validatePresence(true), validateLength({ min: 1 })],
|
||||||
|
DestinationName: [validatePresence(true), validateLength({ min: 1 })],
|
||||||
|
Action: validatePresence(true),
|
||||||
|
};
|
43
ui-v2/tests/unit/utils/model/writable-test.js
Normal file
43
ui-v2/tests/unit/utils/model/writable-test.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import writable from 'consul-ui/utils/model/writable';
|
||||||
|
import { module, test } from 'qunit';
|
||||||
|
|
||||||
|
module('Unit | Utility | model/writable');
|
||||||
|
|
||||||
|
test('it correctly marks attrs as serialize:true|false', function(assert) {
|
||||||
|
const yes = {
|
||||||
|
Props: true,
|
||||||
|
That: true,
|
||||||
|
Should: true,
|
||||||
|
Be: true,
|
||||||
|
Writable: true,
|
||||||
|
};
|
||||||
|
const no = {
|
||||||
|
Others: true,
|
||||||
|
Read: true,
|
||||||
|
Only: true,
|
||||||
|
};
|
||||||
|
const expectedYes = Object.keys(yes);
|
||||||
|
const expectedNo = Object.keys(no);
|
||||||
|
const model = {
|
||||||
|
eachAttribute: function(cb) {
|
||||||
|
expectedYes.concat(expectedNo).forEach(function(item) {
|
||||||
|
cb(item, {}); // we aren't testing the meta here, just use the same api
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let attrs = writable(model, Object.keys(yes));
|
||||||
|
const actualYes = Object.keys(attrs).filter(item => attrs[item].serialize);
|
||||||
|
const actualNo = Object.keys(attrs).filter(item => !attrs[item].serialize);
|
||||||
|
assert.deepEqual(actualYes, expectedYes, 'writable props are marked as serializable');
|
||||||
|
assert.deepEqual(actualNo, expectedNo, 'writable props are marked as not serializable');
|
||||||
|
attrs = writable(model, Object.keys(yes), {
|
||||||
|
Props: {
|
||||||
|
another: 'property',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
assert.equal(
|
||||||
|
attrs.Props.another,
|
||||||
|
'property',
|
||||||
|
'previous attrs objects can be passed without being overwritten'
|
||||||
|
);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user