2019-02-14 18:52:34 +00:00
|
|
|
/*
|
|
|
|
This service is used to pull an OpenAPI document describing the
|
|
|
|
shape of data at a specific path to hydrate a model with attrs it
|
|
|
|
has less (or no) information about.
|
|
|
|
*/
|
|
|
|
import Service from '@ember/service';
|
2019-06-21 15:18:26 +00:00
|
|
|
import DS from 'ember-data';
|
|
|
|
import { encodePath } from 'vault/utils/path-encoding-helpers';
|
2019-02-14 18:52:34 +00:00
|
|
|
import { getOwner } from '@ember/application';
|
2019-06-21 15:18:26 +00:00
|
|
|
import { capitalize } from '@ember/string';
|
|
|
|
import { assign } from '@ember/polyfills';
|
2019-02-14 18:52:34 +00:00
|
|
|
import { expandOpenApiProps, combineAttributes } from 'vault/utils/openapi-to-attrs';
|
2019-06-21 15:18:26 +00:00
|
|
|
import fieldToAttrs from 'vault/utils/field-to-attrs';
|
2019-02-14 18:52:34 +00:00
|
|
|
import { resolve } from 'rsvp';
|
2019-06-21 15:18:26 +00:00
|
|
|
import { debug } from '@ember/debug';
|
2019-02-14 18:52:34 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
import generatedItemAdapter from 'vault/adapters/generated-item-list';
|
2019-02-14 18:52:34 +00:00
|
|
|
export function sanitizePath(path) {
|
|
|
|
//remove whitespace + remove trailing and leading slashes
|
|
|
|
return path.trim().replace(/^\/+|\/+$/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
attrs: null,
|
|
|
|
ajax(url, options = {}) {
|
|
|
|
let appAdapter = getOwner(this).lookup(`adapter:application`);
|
|
|
|
let { data } = options;
|
|
|
|
return appAdapter.ajax(url, 'GET', {
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
getNewModel(modelType, backend, apiPath, itemType) {
|
|
|
|
let owner = getOwner(this);
|
|
|
|
const modelName = `model:${modelType}`;
|
|
|
|
const modelFactory = owner.factoryFor(modelName);
|
|
|
|
let newModel, helpUrl;
|
|
|
|
//if we have a factory, we need to take the existing model into account
|
|
|
|
if (modelFactory) {
|
|
|
|
debug(`Model factory found for ${modelType}`);
|
|
|
|
newModel = modelFactory.class;
|
|
|
|
const modelProto = newModel.proto();
|
|
|
|
if (newModel.merged || modelProto.useOpenAPI !== true) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
helpUrl = modelProto.getHelpUrl(backend);
|
|
|
|
return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName);
|
|
|
|
} else {
|
|
|
|
debug(`Creating new Model for ${modelType}`);
|
|
|
|
newModel = DS.Model.extend({});
|
|
|
|
//use paths to dynamically create our openapi help url
|
|
|
|
//if we have a brand new model
|
|
|
|
return this.getPaths(apiPath, backend, itemType).then(paths => {
|
|
|
|
const adapterFactory = owner.factoryFor(`adapter:${modelType}`);
|
|
|
|
//if we have an adapter already use that, otherwise create one
|
|
|
|
if (!adapterFactory) {
|
|
|
|
debug(`Creating new adapter for ${modelType}`);
|
|
|
|
const adapter = this.getNewAdapter(backend, paths, itemType);
|
|
|
|
owner.register(`adapter:${modelType}`, adapter);
|
|
|
|
}
|
|
|
|
//if we have an item we want the create info for that itemType
|
|
|
|
let tag, path;
|
|
|
|
if (itemType) {
|
|
|
|
tag = paths.create[0].tag;
|
|
|
|
path = paths.create[0].path;
|
|
|
|
path = path.slice(0, path.indexOf('{') - 1) + '/example';
|
|
|
|
} else {
|
|
|
|
//we need the mount config
|
|
|
|
tag = paths.configPath[0].tag;
|
|
|
|
path = paths.configPath[0].path;
|
|
|
|
}
|
|
|
|
helpUrl = `/v1/${tag}/${backend}${path}?help=true`;
|
|
|
|
return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getPaths(apiPath, backend, itemType) {
|
|
|
|
debug(`Fetching relevant paths for ${backend} from ${apiPath}`);
|
|
|
|
return this.ajax(`/v1/${apiPath}?help=1`, backend).then(help => {
|
|
|
|
const pathInfo = help.openapi.paths;
|
|
|
|
let paths = Object.keys(pathInfo);
|
|
|
|
|
|
|
|
//TODO: consolidate this into a single reduce()
|
|
|
|
//config is a get/post endpoint that doesn't take route params
|
|
|
|
//and isn't also a list endpoint
|
|
|
|
const configPath = paths
|
|
|
|
.map(path => {
|
|
|
|
if (
|
|
|
|
pathInfo[path].post &&
|
|
|
|
!path.includes('{') &&
|
|
|
|
pathInfo[path].get &&
|
|
|
|
(!pathInfo[path].get.parameters || pathInfo[path].get.parameters[0].name !== 'list')
|
|
|
|
) {
|
|
|
|
return { path: path, tag: pathInfo[path].get.tags[0] };
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
//list endpoints all have { name: "list" } in their get parameters
|
|
|
|
const listPaths = paths
|
|
|
|
.map(path => {
|
|
|
|
if (
|
|
|
|
pathInfo[path].get &&
|
|
|
|
pathInfo[path].get.parameters &&
|
|
|
|
pathInfo[path].get.parameters[0].name == 'list'
|
|
|
|
) {
|
|
|
|
return { path: path, tag: pathInfo[path].get.tags[0] };
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
//we always want to keep list endpoints for menus
|
|
|
|
//but only use scoped post/delete endpoints
|
|
|
|
if (itemType) {
|
|
|
|
paths = paths.filter(path => path.includes(itemType));
|
|
|
|
}
|
|
|
|
const deletePaths = paths
|
|
|
|
.map(path => {
|
|
|
|
if (pathInfo[path].delete) {
|
|
|
|
return { path: path, tag: pathInfo[path].delete.tags[0] };
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
//create endpoints have path params, signified by "{}"
|
|
|
|
//we have to filter out login endpoints for auth methods
|
|
|
|
const createPaths = paths
|
|
|
|
.map(path => {
|
|
|
|
if (pathInfo[path].post && path.includes('{') && !path.includes('login')) {
|
|
|
|
return { path: path, tag: pathInfo[path].post.tags[0] };
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
const navPaths = paths
|
|
|
|
.map(path => {
|
|
|
|
if (pathInfo[path]['x-vault-displayAttrs'] && pathInfo[path]['x-vault-displayAttrs'].navigation) {
|
|
|
|
return { path: path };
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.compact();
|
|
|
|
//return paths object with all relevant information
|
|
|
|
return {
|
|
|
|
apiPath: apiPath,
|
|
|
|
configPath: configPath,
|
|
|
|
list: listPaths,
|
|
|
|
create: createPaths,
|
|
|
|
delete: deletePaths,
|
|
|
|
navPaths: navPaths,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-02-14 18:52:34 +00:00
|
|
|
//Makes a call to grab the OpenAPI document.
|
|
|
|
//Returns relevant information from OpenAPI
|
|
|
|
//as determined by the expandOpenApiProps util
|
|
|
|
getProps(helpUrl, backend) {
|
2019-06-21 15:18:26 +00:00
|
|
|
debug(`Fetching schema properties for ${backend} from ${helpUrl}`);
|
2019-02-14 18:52:34 +00:00
|
|
|
return this.ajax(helpUrl, backend).then(help => {
|
2019-06-21 15:18:26 +00:00
|
|
|
//paths is an array but it will have a single entry
|
|
|
|
// for the scope we're in
|
|
|
|
const path = Object.keys(help.openapi.paths)[0];
|
|
|
|
const pathInfo = help.openapi.paths[path];
|
|
|
|
const params = pathInfo.parameters;
|
|
|
|
let paramProp = {};
|
|
|
|
|
|
|
|
//include url params
|
|
|
|
if (params) {
|
|
|
|
const { name, schema, description } = params[0];
|
|
|
|
let label = capitalize(name);
|
|
|
|
if (label.toLowerCase() !== 'name') {
|
|
|
|
label += ' name';
|
|
|
|
}
|
|
|
|
paramProp[name] = {
|
|
|
|
'x-vault-displayAttrs': {
|
|
|
|
name: name,
|
|
|
|
group: 'default',
|
|
|
|
},
|
|
|
|
label: label,
|
|
|
|
type: schema.type,
|
|
|
|
description: description,
|
|
|
|
isId: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: handle post endpoints without requestBody
|
|
|
|
const props = pathInfo.post.requestBody.content['application/json'].schema.properties;
|
|
|
|
//put url params (e.g. {name}, {role})
|
|
|
|
//at the front of the props list
|
|
|
|
const newProps = assign({}, paramProp, props);
|
|
|
|
return expandOpenApiProps(newProps);
|
2019-02-14 18:52:34 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
getNewAdapter(backend, paths, itemType) {
|
|
|
|
//we need list and create paths to set the correct urls for actions
|
|
|
|
const { list, create } = paths;
|
|
|
|
return generatedItemAdapter.extend({
|
2019-06-21 21:05:45 +00:00
|
|
|
urlForItem(method, id) {
|
2019-06-21 15:18:26 +00:00
|
|
|
let listPath = list.find(pathInfo => pathInfo.path.includes(itemType));
|
|
|
|
let { tag, path } = listPath;
|
|
|
|
let url = `${this.buildURL()}/${tag}/${backend}${path}/`;
|
|
|
|
if (id) {
|
|
|
|
url = url + encodePath(id);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
urlForFindRecord(id, modelName, snapshot) {
|
|
|
|
return this.urlForItem(modelName, id, snapshot);
|
|
|
|
},
|
|
|
|
|
2019-06-21 21:05:45 +00:00
|
|
|
urlForUpdateRecord(id) {
|
2019-06-21 15:18:26 +00:00
|
|
|
let { tag, path } = create[0];
|
|
|
|
path = path.slice(0, path.indexOf('{') - 1);
|
|
|
|
return `${this.buildURL()}/${tag}/${backend}${path}/${id}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
urlForCreateRecord(modelType, snapshot) {
|
|
|
|
const { id } = snapshot;
|
|
|
|
let { tag, path } = create[0];
|
|
|
|
path = path.slice(0, path.indexOf('{') - 1);
|
|
|
|
return `${this.buildURL()}/${tag}/${backend}${path}/${id}`;
|
|
|
|
},
|
|
|
|
|
2019-06-21 21:05:45 +00:00
|
|
|
urlForDeleteRecord(id) {
|
2019-06-21 15:18:26 +00:00
|
|
|
let { tag, path } = paths.delete[0];
|
|
|
|
path = path.slice(0, path.indexOf('{') - 1);
|
|
|
|
return `${this.buildURL()}/${tag}/${backend}${path}/${id}`;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2019-02-14 18:52:34 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
registerNewModelWithProps(helpUrl, backend, newModel, modelName) {
|
2019-02-14 18:52:34 +00:00
|
|
|
return this.getProps(helpUrl, backend).then(props => {
|
2019-06-21 15:18:26 +00:00
|
|
|
const { attrs, newFields } = combineAttributes(newModel.attributes, props);
|
|
|
|
let owner = getOwner(this);
|
|
|
|
newModel = newModel.extend(attrs, { newFields });
|
|
|
|
//if our newModel doesn't have fieldGroups already
|
|
|
|
//we need to create them
|
|
|
|
try {
|
|
|
|
let fieldGroups = newModel.proto().fieldGroups;
|
|
|
|
if (!fieldGroups) {
|
|
|
|
debug(`Constructing fieldGroups for ${backend}`);
|
|
|
|
fieldGroups = this.getFieldGroups(newModel);
|
|
|
|
newModel = newModel.extend({ fieldGroups });
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
//eat the error, fieldGroups is computed in the model definition
|
2019-02-14 18:52:34 +00:00
|
|
|
}
|
|
|
|
newModel.reopenClass({ merged: true });
|
2019-06-21 15:18:26 +00:00
|
|
|
owner.unregister(modelName);
|
|
|
|
owner.register(modelName, newModel);
|
2019-02-14 18:52:34 +00:00
|
|
|
});
|
|
|
|
},
|
2019-06-21 15:18:26 +00:00
|
|
|
getFieldGroups(newModel) {
|
|
|
|
let groups = {
|
|
|
|
default: [],
|
|
|
|
};
|
|
|
|
let fieldGroups = [];
|
|
|
|
newModel.attributes.forEach(attr => {
|
|
|
|
//if the attr comes in with a fieldGroup from OpenAPI,
|
|
|
|
//add it to that group
|
|
|
|
if (attr.options.fieldGroup) {
|
|
|
|
if (groups[attr.options.fieldGroup]) {
|
|
|
|
groups[attr.options.fieldGroup].push(attr.name);
|
|
|
|
} else {
|
|
|
|
groups[attr.options.fieldGroup] = [attr.name];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//otherwise just add that attr to the default group
|
|
|
|
groups.default.push(attr.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (let group in groups) {
|
|
|
|
fieldGroups.push({ [group]: groups[group] });
|
|
|
|
}
|
|
|
|
return fieldToAttrs(newModel, fieldGroups);
|
|
|
|
},
|
2019-02-14 18:52:34 +00:00
|
|
|
});
|