open-vault/ui/app/routes/vault/cluster/settings/auth/configure/section.js

100 lines
3 KiB
JavaScript
Raw Normal View History

import { inject as service } from '@ember/service';
import { set } from '@ember/object';
import Route from '@ember/routing/route';
import RSVP from 'rsvp';
2018-04-03 14:16:57 +00:00
import DS from 'ember-data';
import UnloadModelRoute from 'vault/mixins/unload-model-route';
2019-02-14 18:52:34 +00:00
import { getOwner } from '@ember/application';
2018-04-03 14:16:57 +00:00
export default Route.extend(UnloadModelRoute, {
2018-04-03 14:16:57 +00:00
modelPath: 'model.model',
2019-02-14 18:52:34 +00:00
pathHelp: service('path-help'),
2018-04-03 14:16:57 +00:00
modelType(backendType, section) {
const MODELS = {
'aws-client': 'auth-config/aws/client',
'aws-identity-whitelist': 'auth-config/aws/identity-whitelist',
'aws-roletag-blacklist': 'auth-config/aws/roletag-blacklist',
'azure-configuration': 'auth-config/azure',
2018-04-03 14:16:57 +00:00
'github-configuration': 'auth-config/github',
'gcp-configuration': 'auth-config/gcp',
'jwt-configuration': 'auth-config/jwt',
2018-04-03 14:16:57 +00:00
'kubernetes-configuration': 'auth-config/kubernetes',
'ldap-configuration': 'auth-config/ldap',
'okta-configuration': 'auth-config/okta',
'radius-configuration': 'auth-config/radius',
};
return MODELS[`${backendType}-${section}`];
},
2019-02-14 18:52:34 +00:00
beforeModel() {
const { section_name } = this.paramsFor(this.routeName);
if (section_name === 'options') {
return;
}
const { method } = this.paramsFor('vault.cluster.settings.auth.configure');
const backend = this.modelFor('vault.cluster.settings.auth.configure');
const modelType = this.modelType(backend.type, section_name);
let owner = getOwner(this);
return this.pathHelp.getNewModel(modelType, owner, method);
},
2018-04-03 14:16:57 +00:00
model(params) {
const backend = this.modelFor('vault.cluster.settings.auth.configure');
const { section_name: section } = params;
if (section === 'options') {
return RSVP.hash({
model: backend,
section,
});
}
const modelType = this.modelType(backend.get('type'), section);
if (!modelType) {
const error = new DS.AdapterError();
set(error, 'httpStatus', 404);
2018-04-03 14:16:57 +00:00
throw error;
}
const model = this.store.peekRecord(modelType, backend.id);
if (model) {
return RSVP.hash({
model,
section,
});
}
return this.store
.findRecord(modelType, backend.id)
.then(config => {
config.set('backend', backend);
return RSVP.hash({
model: config,
section,
});
})
.catch(e => {
let config;
// if you haven't saved a config, the API 404s, so create one here to edit and return it
if (e.httpStatus === 404) {
config = this.store.createRecord(modelType, {
id: backend.id,
});
config.set('backend', backend);
return RSVP.hash({
model: config,
section,
});
}
throw e;
});
},
actions: {
willTransition() {
if (this.currentModel.model.constructor.modelName !== 'auth-method') {
this.unloadModel();
return true;
}
},
},
});