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.
|
|
|
|
*/
|
Ember-cli upgrade from ~3.8 to ~3.20 (#9972)
* Update ember-cli to ~3.20
* Remove bad optional-feature
* Remove ember-fetch dep
* re-install ember-fetch
* update model fragments pr
* update ember model fragments correct package name
* update ember composable helpers to solve array helper error
* update ember-concurrency
* add back engine dependencies, automatically removed during ember-cli-upgrade
* make author-form-options component js file otherwise error
* for now comment out withTestWaiter
* add eslint-node and fix if not with unless in templates
* fix linting for tab index of false is now -1 and add type button to all buttons without types
* fix href errors for linting, likely have to come back and fix
* using eslint fix flag to fix all this.gets
* ember modules codemode removed files that had module twice, will fix in next commit
* finish codemode ember-data-codemod needed to rename const model
* more this.get removal codemode did not work
* cont. removal of this.get
* stop mixin rules until figure out how to reconfig them all
* smaller eslint ignores
* get codemode
* testing app small fixes to bring it back after all the changes
* small changes to eslint
* test removal of getProperties
* fix issue with baseKey because value could be unknown needed to add a question mark in nested get
* smaller linting fixes
* get nested fixes
* small linting error fixes
* small linting changes
* working through more small linting changes
* another round of linting modifications
* liniting fixes
* ember module codemod
* quinit dom codemod
* angle bracket codemod
* discovered that components must have js files
* ran all codemods this is all that's left
* small changes to fix get needs two object, should not have been using get.
* fix issue with one input in form field
* fun times with set and onChange from oninput
* fix issue with model not being passed through on secret-edit-display
* fix issue with yarn run test not working, revert without npm run all
* linting and small fix when loading without a selectAuthBackend
* fix failing test with ui-wizard issue
* fix test failure due to model not being asked for correctly with new changes, probably run into this more.
* fix issue with component helper and at props specific to wizard
* rename log to clilog due to conflict with new eslint rule
* small changes for test failures
* component helper at fixes
* Revert to old component style something with new one broke this and can't figure it out for now
* small fishy smelling test fixes will revisit
* small test changes
* more small test changes, appears upgrade treats spaces differently
* comment out code and test that no longer seems relevant but confirm
* clean run on component test though still some potential timing issues on ui-console test
* fixing one auth test issue and timing issue on enable-test
* small mods
* fix this conditional check from upgrade
* linting fixes after master merge
* package updates using yarn upgrade-interactive
* update libraries that did not effect any of the test failures.
* update ember truth helpers library
* settling tests
* Fix ui-panel control group output
* fix features selection test failures
* Fix auth tests (x-vault-token)
* fix shared test
* fix issue with data null on backend
* Revert "Fix auth tests (x-vault-token)"
This reverts commit 89cb174b2f1998efa56d9604d14131415ae65d6f.
* Fix auth tests (x-vault-token) without updating this.set
* Update redirect-to tests
* fix wrapped token test
* skip some flaky test
* fix issue with href and a tags vs buttons
* fix linting
* updates to get tests running (#10409)
* yarn isntall
* increasing resource_class
* whoops
* trying large
* back to xlarge
* Fix param issue on transform item routes
* test fixes
* settle on policies (old) test
* fix browserstack test warning and skips of test confirmed worked
* Fix redirect-to test
* skips
* fix transformation test and skip some kmip
* Skip tests
* Add meep marker to remaining failing tests
* Skip test with failing component
* rever skip on secret-create test
* Skip piece of test that fails due to navigation-input
* fix settings test where can and skip in others after confirming
* fix circle ci test failures
* ssh role settle
* Fix navigate-input and add settled to test
* Remove extra import
* secret cubbyhole and alicloud
* Add settled to gcpkms test
* settles on redirect to test
* Bump browserstack test resource to large
* Update browserstack resource size to xlarge
* update todos
* add back in withTestWaiter
* try and fix credentials conditional action added comment instead
* Update volatile computed properies to get functions
* this step was never reached and we never defined secretType anywhere so I removed
* add settled to policy old test
* Fix navigate-input on policies and leases
* replace ssh test with no var hoping that helps and add settled to other failing tests, unskip console tests
* kmip, transit, role test remove a skip and add in settled
* fix hover copy button, had to remove some testing functionality
* Remove private router service
* remove skip on control ssh and ui panel, fix search select by restructuring how to read the error
* final bit of working through skipped test
* Replace clearNonGlobalModels by linking directly to namespace with href-to
* Remove unused var
* Fix role-ssh id bug by updating form-field-from-model to form-field-group-loop
* Fix transit create id would not update
* Update option toggle selector for ssh-role
* Fix ssh selector
* cleanup pt1
* small clean up
* cleanup part2
* Fix computed on pricing-metrics-form
* small cleanup based on chelseas comments.
Co-authored-by: Chelsea Shaw <chelshaw.dev@gmail.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
2020-12-03 23:00:22 +00:00
|
|
|
import Model from '@ember-data/model';
|
2019-02-14 18:52:34 +00:00
|
|
|
import Service from '@ember/service';
|
2019-06-21 15:18:26 +00:00
|
|
|
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 { 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-10-17 23:19:14 +00:00
|
|
|
import { resolve, reject } from 'rsvp';
|
2019-06-21 15:18:26 +00:00
|
|
|
import { debug } from '@ember/debug';
|
2019-10-17 23:19:14 +00:00
|
|
|
import { dasherize, capitalize } from '@ember/string';
|
|
|
|
import { singularize } from 'ember-inflector';
|
2021-07-13 22:50:27 +00:00
|
|
|
import buildValidations from 'vault/utils/build-api-validators';
|
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) {
|
2019-10-17 23:19:14 +00:00
|
|
|
// remove whitespace + remove trailing and leading slashes
|
2019-02-14 18:52:34 +00:00
|
|
|
return path.trim().replace(/^\/+|\/+$/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
attrs: null,
|
2020-10-19 16:42:01 +00:00
|
|
|
dynamicApiPath: '',
|
2019-02-14 18:52:34 +00:00
|
|
|
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;
|
2019-10-17 23:19:14 +00:00
|
|
|
// if we have a factory, we need to take the existing model into account
|
2019-06-21 15:18:26 +00:00
|
|
|
if (modelFactory) {
|
|
|
|
debug(`Model factory found for ${modelType}`);
|
|
|
|
newModel = modelFactory.class;
|
|
|
|
const modelProto = newModel.proto();
|
|
|
|
if (newModel.merged || modelProto.useOpenAPI !== true) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
helpUrl = modelProto.getHelpUrl(backend);
|
|
|
|
return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName);
|
|
|
|
} else {
|
|
|
|
debug(`Creating new Model for ${modelType}`);
|
Ember-cli upgrade from ~3.8 to ~3.20 (#9972)
* Update ember-cli to ~3.20
* Remove bad optional-feature
* Remove ember-fetch dep
* re-install ember-fetch
* update model fragments pr
* update ember model fragments correct package name
* update ember composable helpers to solve array helper error
* update ember-concurrency
* add back engine dependencies, automatically removed during ember-cli-upgrade
* make author-form-options component js file otherwise error
* for now comment out withTestWaiter
* add eslint-node and fix if not with unless in templates
* fix linting for tab index of false is now -1 and add type button to all buttons without types
* fix href errors for linting, likely have to come back and fix
* using eslint fix flag to fix all this.gets
* ember modules codemode removed files that had module twice, will fix in next commit
* finish codemode ember-data-codemod needed to rename const model
* more this.get removal codemode did not work
* cont. removal of this.get
* stop mixin rules until figure out how to reconfig them all
* smaller eslint ignores
* get codemode
* testing app small fixes to bring it back after all the changes
* small changes to eslint
* test removal of getProperties
* fix issue with baseKey because value could be unknown needed to add a question mark in nested get
* smaller linting fixes
* get nested fixes
* small linting error fixes
* small linting changes
* working through more small linting changes
* another round of linting modifications
* liniting fixes
* ember module codemod
* quinit dom codemod
* angle bracket codemod
* discovered that components must have js files
* ran all codemods this is all that's left
* small changes to fix get needs two object, should not have been using get.
* fix issue with one input in form field
* fun times with set and onChange from oninput
* fix issue with model not being passed through on secret-edit-display
* fix issue with yarn run test not working, revert without npm run all
* linting and small fix when loading without a selectAuthBackend
* fix failing test with ui-wizard issue
* fix test failure due to model not being asked for correctly with new changes, probably run into this more.
* fix issue with component helper and at props specific to wizard
* rename log to clilog due to conflict with new eslint rule
* small changes for test failures
* component helper at fixes
* Revert to old component style something with new one broke this and can't figure it out for now
* small fishy smelling test fixes will revisit
* small test changes
* more small test changes, appears upgrade treats spaces differently
* comment out code and test that no longer seems relevant but confirm
* clean run on component test though still some potential timing issues on ui-console test
* fixing one auth test issue and timing issue on enable-test
* small mods
* fix this conditional check from upgrade
* linting fixes after master merge
* package updates using yarn upgrade-interactive
* update libraries that did not effect any of the test failures.
* update ember truth helpers library
* settling tests
* Fix ui-panel control group output
* fix features selection test failures
* Fix auth tests (x-vault-token)
* fix shared test
* fix issue with data null on backend
* Revert "Fix auth tests (x-vault-token)"
This reverts commit 89cb174b2f1998efa56d9604d14131415ae65d6f.
* Fix auth tests (x-vault-token) without updating this.set
* Update redirect-to tests
* fix wrapped token test
* skip some flaky test
* fix issue with href and a tags vs buttons
* fix linting
* updates to get tests running (#10409)
* yarn isntall
* increasing resource_class
* whoops
* trying large
* back to xlarge
* Fix param issue on transform item routes
* test fixes
* settle on policies (old) test
* fix browserstack test warning and skips of test confirmed worked
* Fix redirect-to test
* skips
* fix transformation test and skip some kmip
* Skip tests
* Add meep marker to remaining failing tests
* Skip test with failing component
* rever skip on secret-create test
* Skip piece of test that fails due to navigation-input
* fix settings test where can and skip in others after confirming
* fix circle ci test failures
* ssh role settle
* Fix navigate-input and add settled to test
* Remove extra import
* secret cubbyhole and alicloud
* Add settled to gcpkms test
* settles on redirect to test
* Bump browserstack test resource to large
* Update browserstack resource size to xlarge
* update todos
* add back in withTestWaiter
* try and fix credentials conditional action added comment instead
* Update volatile computed properies to get functions
* this step was never reached and we never defined secretType anywhere so I removed
* add settled to policy old test
* Fix navigate-input on policies and leases
* replace ssh test with no var hoping that helps and add settled to other failing tests, unskip console tests
* kmip, transit, role test remove a skip and add in settled
* fix hover copy button, had to remove some testing functionality
* Remove private router service
* remove skip on control ssh and ui panel, fix search select by restructuring how to read the error
* final bit of working through skipped test
* Replace clearNonGlobalModels by linking directly to namespace with href-to
* Remove unused var
* Fix role-ssh id bug by updating form-field-from-model to form-field-group-loop
* Fix transit create id would not update
* Update option toggle selector for ssh-role
* Fix ssh selector
* cleanup pt1
* small clean up
* cleanup part2
* Fix computed on pricing-metrics-form
* small cleanup based on chelseas comments.
Co-authored-by: Chelsea Shaw <chelshaw.dev@gmail.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
2020-12-03 23:00:22 +00:00
|
|
|
newModel = Model.extend({});
|
2019-10-17 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we don't have an apiPath for dynamic secrets
|
|
|
|
// and we don't need paths for them yet
|
|
|
|
if (!apiPath) {
|
|
|
|
helpUrl = newModel.proto().getHelpUrl(backend);
|
|
|
|
return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// use paths to dynamically create our openapi help url
|
|
|
|
// if we have a brand new model
|
|
|
|
return this.getPaths(apiPath, backend, itemType)
|
2021-12-17 03:44:29 +00:00
|
|
|
.then((pathInfo) => {
|
2019-06-21 15:18:26 +00:00
|
|
|
const adapterFactory = owner.factoryFor(`adapter:${modelType}`);
|
2019-10-17 23:19:14 +00:00
|
|
|
// if we have an adapter already use that, otherwise create one
|
2019-06-21 15:18:26 +00:00
|
|
|
if (!adapterFactory) {
|
|
|
|
debug(`Creating new adapter for ${modelType}`);
|
2019-10-17 23:19:14 +00:00
|
|
|
const adapter = this.getNewAdapter(pathInfo, itemType);
|
2019-06-21 15:18:26 +00:00
|
|
|
owner.register(`adapter:${modelType}`, adapter);
|
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
let path, paths;
|
|
|
|
// if we have an item we want the create info for that itemType
|
|
|
|
paths = itemType ? this.filterPathsByItemType(pathInfo, itemType) : pathInfo.paths;
|
2021-12-17 03:44:29 +00:00
|
|
|
const createPath = paths.find((path) => path.operations.includes('post') && path.action !== 'Delete');
|
2019-10-17 23:19:14 +00:00
|
|
|
path = createPath.path;
|
|
|
|
path = path.includes('{') ? path.slice(0, path.indexOf('{') - 1) + '/example' : path;
|
|
|
|
if (!path) {
|
|
|
|
// TODO: we don't know if path will ever be falsey
|
|
|
|
// if it is never falsey we can remove this.
|
|
|
|
return reject();
|
2019-06-21 15:18:26 +00:00
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
|
|
|
|
helpUrl = `/v1/${apiPath}${path.slice(1)}?help=true` || newModel.proto().getHelpUrl(backend);
|
|
|
|
pathInfo.paths = paths;
|
|
|
|
newModel = newModel.extend({ paths: pathInfo });
|
2019-06-21 15:18:26 +00:00
|
|
|
return this.registerNewModelWithProps(helpUrl, backend, newModel, modelName);
|
2019-10-17 23:19:14 +00:00
|
|
|
})
|
2021-12-17 03:44:29 +00:00
|
|
|
.catch((err) => {
|
2019-10-17 23:19:14 +00:00
|
|
|
// TODO: we should handle the error better here
|
|
|
|
console.error(err);
|
2019-06-21 15:18:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
reducePathsByPathName(pathInfo, currentPath) {
|
2019-07-01 20:35:18 +00:00
|
|
|
const pathName = currentPath[0];
|
2019-10-17 23:19:14 +00:00
|
|
|
const pathDetails = currentPath[1];
|
|
|
|
const displayAttrs = pathDetails['x-vault-displayAttrs'];
|
2019-07-01 20:35:18 +00:00
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
if (!displayAttrs) {
|
|
|
|
return pathInfo;
|
2019-07-01 20:35:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
let itemType, itemName;
|
|
|
|
if (displayAttrs.itemType) {
|
|
|
|
itemType = displayAttrs.itemType;
|
|
|
|
let items = itemType.split(':');
|
|
|
|
itemName = items[items.length - 1];
|
2021-12-17 03:44:29 +00:00
|
|
|
items = items.map((item) => dasherize(singularize(item.toLowerCase())));
|
2019-10-17 23:19:14 +00:00
|
|
|
itemType = items.join('~*');
|
2019-07-01 20:35:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
if (itemType && !pathInfo.itemTypes.includes(itemType)) {
|
|
|
|
pathInfo.itemTypes.push(itemType);
|
2019-07-01 20:35:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
const operations = [];
|
|
|
|
if (pathDetails.get) {
|
|
|
|
operations.push('get');
|
|
|
|
}
|
|
|
|
if (pathDetails.post) {
|
|
|
|
operations.push('post');
|
|
|
|
}
|
|
|
|
if (pathDetails.delete) {
|
|
|
|
operations.push('delete');
|
2019-07-01 20:35:18 +00:00
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
if (pathDetails.get && pathDetails.get.parameters && pathDetails.get.parameters[0].name === 'list') {
|
|
|
|
operations.push('list');
|
|
|
|
}
|
|
|
|
|
|
|
|
pathInfo.paths.push({
|
|
|
|
path: pathName,
|
|
|
|
itemType: itemType || displayAttrs.itemType,
|
|
|
|
itemName: itemName || pathInfo.itemType || displayAttrs.itemType,
|
|
|
|
operations,
|
|
|
|
action: displayAttrs.action,
|
|
|
|
navigation: displayAttrs.navigation === true,
|
|
|
|
param: pathName.includes('{') ? pathName.split('{')[1].split('}')[0] : false,
|
|
|
|
});
|
2019-07-01 20:35:18 +00:00
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
return pathInfo;
|
|
|
|
},
|
|
|
|
|
|
|
|
filterPathsByItemType(pathInfo, itemType) {
|
|
|
|
if (!itemType) {
|
|
|
|
return pathInfo.paths;
|
|
|
|
}
|
2021-12-17 03:44:29 +00:00
|
|
|
return pathInfo.paths.filter((path) => {
|
2019-10-17 23:19:14 +00:00
|
|
|
return itemType === path.itemType;
|
|
|
|
});
|
2019-07-01 20:35:18 +00:00
|
|
|
},
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
getPaths(apiPath, backend, itemType, itemID) {
|
|
|
|
let debugString =
|
|
|
|
itemID && itemType
|
|
|
|
? `Fetching relevant paths for ${backend} ${itemType} ${itemID} from ${apiPath}`
|
|
|
|
: `Fetching relevant paths for ${backend} ${itemType} from ${apiPath}`;
|
|
|
|
debug(debugString);
|
2021-12-17 03:44:29 +00:00
|
|
|
return this.ajax(`/v1/${apiPath}?help=1`, backend).then((help) => {
|
2019-06-21 15:18:26 +00:00
|
|
|
const pathInfo = help.openapi.paths;
|
2019-07-01 20:35:18 +00:00
|
|
|
let paths = Object.entries(pathInfo);
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
return paths.reduce(this.reducePathsByPathName, {
|
|
|
|
apiPath,
|
|
|
|
itemType,
|
|
|
|
itemTypes: [],
|
|
|
|
paths: [],
|
|
|
|
itemID,
|
2019-07-01 20:35:18 +00:00
|
|
|
});
|
2019-06-21 15:18:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
// Makes a call to grab the OpenAPI document.
|
|
|
|
// Returns relevant information from OpenAPI
|
|
|
|
// as determined by the expandOpenApiProps util
|
2019-02-14 18:52:34 +00:00
|
|
|
getProps(helpUrl, backend) {
|
2020-08-26 16:31:18 +00:00
|
|
|
// add name of thing you want
|
2019-06-21 15:18:26 +00:00
|
|
|
debug(`Fetching schema properties for ${backend} from ${helpUrl}`);
|
2019-07-01 20:35:18 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
return this.ajax(helpUrl, backend).then((help) => {
|
2019-10-17 23:19:14 +00:00
|
|
|
// paths is an array but it will have a single entry
|
2019-06-21 15:18:26 +00:00
|
|
|
// for the scope we're in
|
2020-08-26 16:31:18 +00:00
|
|
|
const path = Object.keys(help.openapi.paths)[0]; // do this or look at name
|
2019-06-21 15:18:26 +00:00
|
|
|
const pathInfo = help.openapi.paths[path];
|
|
|
|
const params = pathInfo.parameters;
|
|
|
|
let paramProp = {};
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
// include url params
|
2019-06-21 15:18:26 +00:00
|
|
|
if (params) {
|
|
|
|
const { name, schema, description } = params[0];
|
2019-10-17 23:19:14 +00:00
|
|
|
let label = capitalize(name.split('_').join(' '));
|
2019-07-23 19:56:22 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
paramProp[name] = {
|
|
|
|
'x-vault-displayAttrs': {
|
2019-07-23 19:56:22 +00:00
|
|
|
name: label,
|
2019-06-21 15:18:26 +00:00
|
|
|
group: 'default',
|
|
|
|
},
|
|
|
|
type: schema.type,
|
|
|
|
description: description,
|
|
|
|
isId: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-16 14:24:07 +00:00
|
|
|
let props = {};
|
|
|
|
const schema = pathInfo?.post?.requestBody?.content['application/json'].schema;
|
|
|
|
if (schema.$ref) {
|
|
|
|
// $ref will be shaped like `#/components/schemas/MyResponseType
|
|
|
|
// which maps to the location of the item within the openApi response
|
|
|
|
let loc = schema.$ref.replace('#/', '').split('/');
|
|
|
|
props = loc.reduce((prev, curr) => {
|
|
|
|
return prev[curr] || {};
|
|
|
|
}, help.openapi).properties;
|
|
|
|
} else if (schema.properties) {
|
|
|
|
props = schema.properties;
|
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
// put url params (e.g. {name}, {role})
|
|
|
|
// at the front of the props list
|
2019-06-21 15:18:26 +00:00
|
|
|
const newProps = assign({}, paramProp, props);
|
|
|
|
return expandOpenApiProps(newProps);
|
2019-02-14 18:52:34 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
getNewAdapter(pathInfo, itemType) {
|
|
|
|
// we need list and create paths to set the correct urls for actions
|
|
|
|
let paths = this.filterPathsByItemType(pathInfo, itemType);
|
|
|
|
let { apiPath } = pathInfo;
|
2021-12-17 03:44:29 +00:00
|
|
|
const getPath = paths.find((path) => path.operations.includes('get'));
|
2019-10-17 23:19:14 +00:00
|
|
|
|
|
|
|
// the action might be "Generate" or something like that so we'll grab the first post endpoint if there
|
|
|
|
// isn't one with "Create"
|
|
|
|
// TODO: look into a more sophisticated way to determine the create endpoint
|
2021-12-17 03:44:29 +00:00
|
|
|
const createPath = paths.find((path) => path.action === 'Create' || path.operations.includes('post'));
|
|
|
|
const deletePath = paths.find((path) => path.operations.includes('delete'));
|
2019-10-17 23:19:14 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
return generatedItemAdapter.extend({
|
2020-10-19 16:42:01 +00:00
|
|
|
urlForItem(id, isList, dynamicApiPath) {
|
2019-10-17 23:19:14 +00:00
|
|
|
const itemType = getPath.path.slice(1);
|
|
|
|
let url;
|
|
|
|
id = encodePath(id);
|
2020-10-19 16:42:01 +00:00
|
|
|
// the apiPath changes when you switch between routes but the apiPath variable does not unless the model is reloaded
|
|
|
|
// overwrite apiPath if dynamicApiPath exist.
|
|
|
|
// dynamicApiPath comes from the model->adapter
|
|
|
|
if (dynamicApiPath) {
|
|
|
|
apiPath = dynamicApiPath;
|
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
// isList indicates whether we are viewing the list page
|
|
|
|
// of a top-level item such as userpass
|
|
|
|
if (isList) {
|
|
|
|
url = `${this.buildURL()}/${apiPath}${itemType}/`;
|
|
|
|
} else {
|
|
|
|
// build the URL for the show page of a nested item
|
|
|
|
// such as a userpass group
|
|
|
|
url = `${this.buildURL()}/${apiPath}${itemType}/${id}`;
|
2019-06-21 15:18:26 +00:00
|
|
|
}
|
2019-10-17 23:19:14 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2019-10-17 23:19:14 +00:00
|
|
|
urlForQueryRecord(id, modelName) {
|
|
|
|
return this.urlForItem(id, modelName);
|
2019-06-21 15:18:26 +00:00
|
|
|
},
|
|
|
|
|
2019-06-21 21:05:45 +00:00
|
|
|
urlForUpdateRecord(id) {
|
2019-10-17 23:19:14 +00:00
|
|
|
const itemType = createPath.path.slice(1, createPath.path.indexOf('{') - 1);
|
|
|
|
return `${this.buildURL()}/${apiPath}${itemType}/${id}`;
|
2019-06-21 15:18:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
urlForCreateRecord(modelType, snapshot) {
|
|
|
|
const { id } = snapshot;
|
2019-10-17 23:19:14 +00:00
|
|
|
const path = createPath.path.slice(1, createPath.path.indexOf('{') - 1);
|
2019-07-23 19:56:22 +00:00
|
|
|
return `${this.buildURL()}/${apiPath}${path}/${id}`;
|
2019-06-21 15:18:26 +00:00
|
|
|
},
|
|
|
|
|
2019-06-21 21:05:45 +00:00
|
|
|
urlForDeleteRecord(id) {
|
2019-10-17 23:19:14 +00:00
|
|
|
const path = deletePath.path.slice(1, deletePath.path.indexOf('{') - 1);
|
2019-07-23 19:56:22 +00:00
|
|
|
return `${this.buildURL()}/${apiPath}${path}/${id}`;
|
2019-06-21 15:18:26 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2019-02-14 18:52:34 +00:00
|
|
|
|
2019-06-21 15:18:26 +00:00
|
|
|
registerNewModelWithProps(helpUrl, backend, newModel, modelName) {
|
2021-12-17 03:44:29 +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 });
|
2019-10-17 23:19:14 +00:00
|
|
|
// if our newModel doesn't have fieldGroups already
|
|
|
|
// we need to create them
|
2019-06-21 15:18:26 +00:00
|
|
|
try {
|
2021-07-13 22:50:27 +00:00
|
|
|
// Initialize prototype to access field groups
|
2019-06-21 15:18:26 +00:00
|
|
|
let fieldGroups = newModel.proto().fieldGroups;
|
|
|
|
if (!fieldGroups) {
|
|
|
|
debug(`Constructing fieldGroups for ${backend}`);
|
|
|
|
fieldGroups = this.getFieldGroups(newModel);
|
|
|
|
newModel = newModel.extend({ fieldGroups });
|
2021-07-13 22:50:27 +00:00
|
|
|
// Build and add validations on model
|
|
|
|
// NOTE: For initial phase, initialize validations only for user pass auth
|
|
|
|
if (backend === 'userpass') {
|
|
|
|
let validations = buildValidations(fieldGroups);
|
|
|
|
newModel = newModel.extend(validations);
|
|
|
|
}
|
2019-06-21 15:18:26 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-10-17 23:19:14 +00:00
|
|
|
// 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 = [];
|
2021-12-17 03:44:29 +00:00
|
|
|
newModel.attributes.forEach((attr) => {
|
2019-10-17 23:19:14 +00:00
|
|
|
// if the attr comes in with a fieldGroup from OpenAPI,
|
|
|
|
// add it to that group
|
2019-06-21 15:18:26 +00:00
|
|
|
if (attr.options.fieldGroup) {
|
|
|
|
if (groups[attr.options.fieldGroup]) {
|
|
|
|
groups[attr.options.fieldGroup].push(attr.name);
|
|
|
|
} else {
|
|
|
|
groups[attr.options.fieldGroup] = [attr.name];
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-17 23:19:14 +00:00
|
|
|
// otherwise just add that attr to the default group
|
2019-06-21 15:18:26 +00:00
|
|
|
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
|
|
|
});
|