7903512b26
* Add some tests to check the correct GET API endpoints are called * Refactor adapters 1. Add integration tests for `urlFor...` and majority `handleResponse` methods 2. Refactor out `handleResponse` a little more into single/batch/boolean methods 3. Move setting of the `Datacenter` property into the `handleResponse` method, basically the same place that the uid is being set using the dc parsed form the URL 4. Add some Errors for if you don't pass ids to certain `urlFor` methods
77 lines
2.1 KiB
JavaScript
77 lines
2.1 KiB
JavaScript
import Service, { inject as service } from '@ember/service';
|
|
import { typeOf } from '@ember/utils';
|
|
import { Promise } from 'rsvp';
|
|
import isFolder from 'consul-ui/utils/isFolder';
|
|
import { get, set } from '@ember/object';
|
|
import { PRIMARY_KEY } from 'consul-ui/models/kv';
|
|
|
|
export default Service.extend({
|
|
store: service('store'),
|
|
// this one gives you the full object so key,values and meta
|
|
findBySlug: function(key, dc) {
|
|
if (isFolder(key)) {
|
|
const id = JSON.stringify([dc, key]);
|
|
let item = get(this, 'store').peekRecord('kv', id);
|
|
if (!item) {
|
|
item = this.create();
|
|
set(item, 'Key', key);
|
|
set(item, 'Datacenter', dc);
|
|
}
|
|
return Promise.resolve(item);
|
|
}
|
|
return get(this, 'store').queryRecord('kv', {
|
|
id: key,
|
|
dc: dc,
|
|
});
|
|
},
|
|
// this one only gives you keys
|
|
// https://www.consul.io/api/kv.html
|
|
findAllBySlug: function(key, dc) {
|
|
if (key === '/') {
|
|
key = '';
|
|
}
|
|
return this.get('store')
|
|
.query('kv', {
|
|
id: key,
|
|
dc: dc,
|
|
separator: '/',
|
|
})
|
|
.then(function(items) {
|
|
return items.filter(function(item) {
|
|
return key !== get(item, 'Key');
|
|
});
|
|
})
|
|
.catch(e => {
|
|
if (e.errors && e.errors[0] && e.errors[0].status == '404') {
|
|
const id = JSON.stringify([dc, key]);
|
|
const record = get(this, 'store').peekRecord('kv', id);
|
|
if (record) {
|
|
record.destroyRecord();
|
|
}
|
|
}
|
|
throw e;
|
|
});
|
|
},
|
|
create: function() {
|
|
return get(this, 'store').createRecord('kv');
|
|
},
|
|
persist: function(item) {
|
|
return item.save();
|
|
},
|
|
remove: function(obj) {
|
|
let item = obj;
|
|
if (typeof obj.destroyRecord === 'undefined') {
|
|
item = obj.get('data');
|
|
}
|
|
if (typeOf(item) === 'object') {
|
|
item = get(this, 'store').peekRecord('kv', item[PRIMARY_KEY]);
|
|
}
|
|
return item.destroyRecord().then(item => {
|
|
return get(this, 'store').unloadRecord(item);
|
|
});
|
|
},
|
|
invalidate: function() {
|
|
return get(this, 'store').unloadAll('kv');
|
|
},
|
|
});
|