2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2019-05-01 18:09:29 +00:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
2018-10-19 15:17:02 +00:00
|
|
|
import builderFactory from 'consul-ui/utils/form/builder';
|
2020-11-10 14:36:27 +00:00
|
|
|
|
|
|
|
import kv from 'consul-ui/forms/kv';
|
|
|
|
import token from 'consul-ui/forms/token';
|
|
|
|
import policy from 'consul-ui/forms/policy';
|
|
|
|
import role from 'consul-ui/forms/role';
|
|
|
|
import intention from 'consul-ui/forms/intention';
|
|
|
|
|
2018-10-19 15:17:02 +00:00
|
|
|
const builder = builderFactory();
|
2020-11-10 14:36:27 +00:00
|
|
|
|
|
|
|
const forms = {
|
|
|
|
kv: kv,
|
|
|
|
token: token,
|
|
|
|
policy: policy,
|
|
|
|
role: role,
|
|
|
|
intention: intention,
|
|
|
|
};
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class FormService extends Service {
|
2018-10-19 15:17:02 +00:00
|
|
|
// a `get` method is added via the form initializer
|
|
|
|
// see initializers/form.js
|
2019-05-01 18:09:29 +00:00
|
|
|
|
|
|
|
// TODO: Temporarily add these here until something else needs
|
|
|
|
// dynamic repos
|
2020-11-10 14:36:27 +00:00
|
|
|
@service('repository/role') role;
|
|
|
|
@service('repository/policy') policy;
|
2019-05-01 18:09:29 +00:00
|
|
|
//
|
2020-11-10 14:36:27 +00:00
|
|
|
forms = [];
|
2020-11-09 09:25:35 +00:00
|
|
|
|
|
|
|
build(obj, name) {
|
2018-10-19 15:17:02 +00:00
|
|
|
return builder(...arguments);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:36:27 +00:00
|
|
|
form(name) {
|
|
|
|
let form = this.forms[name];
|
|
|
|
if (typeof form === 'undefined') {
|
|
|
|
form = this.forms[name] = forms[name](this);
|
|
|
|
// only do special things for our new things for the moment
|
|
|
|
if (name === 'role' || name === 'policy') {
|
|
|
|
const repo = this[name];
|
2022-09-15 08:43:17 +00:00
|
|
|
form.clear(function (obj) {
|
2020-11-10 14:36:27 +00:00
|
|
|
return repo.create(obj);
|
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
form.submit(function (obj) {
|
2020-11-10 14:36:27 +00:00
|
|
|
return repo.persist(obj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return form;
|
|
|
|
}
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|