diff --git a/ui/app/components/alphabet-edit.hbs b/ui/app/components/alphabet-edit.hbs index 0318ee17c5..4476adfbed 100644 --- a/ui/app/components/alphabet-edit.hbs +++ b/ui/app/components/alphabet-edit.hbs @@ -5,44 +5,38 @@ - +

- {{#if (eq this.mode "create")}} + {{#if (eq @mode "create")}} Create Alphabet - {{else if (eq this.mode "edit")}} + {{else if (eq @mode "edit")}} Edit Alphabet {{else}} Alphabet - {{this.model.id}} + {{@model.id}} {{/if}}

-{{#if (eq this.mode "show")}} +{{#if (eq @mode "show")}} - {{#if this.model.updatePath.canDelete}} + {{#if @model.updatePath.canDelete}}
{{/if}} - {{#if this.model.updatePath.canUpdate}} + {{#if @model.updatePath.canUpdate}} {{/if}} -{{#if (or (eq this.mode "edit") (eq this.mode "create"))}} -
+{{#if (or (eq @mode "edit") (eq @mode "create"))}} +
- - - {{#each this.model.attrs as |attr|}} - {{#if (and (eq attr.name "name") (eq this.mode "edit"))}} + + + {{#each @model.attrs as |attr|}} + {{#if (and (eq attr.name "name") (eq @mode "edit"))}} @@ -72,29 +66,29 @@ id={{attr.name}} autocomplete="off" spellcheck="false" - value={{or (get this.model attr.name) this.model.id}} + value={{or (get @model attr.name) @model.id}} readonly class="field input is-readOnly" type={{attr.type}} /> {{else}} - + {{/if}} {{/each}}
- {{#if (eq this.mode "create")}} + {{#if (eq @mode "create")}} {{else}} @@ -102,7 +96,7 @@ @text="Cancel" @color="secondary" @route="vault.cluster.secrets.backend.show" - @models={{array this.model.backend (concat "alphabet/" this.model.id)}} + @models={{array @model.backend (concat "alphabet/" @model.id)}} @query={{hash tab="alphabet"}} /> {{/if}} @@ -110,29 +104,24 @@
{{else}} - {{#if this.model.displayErrors}} -
- -
- {{/if}}
- {{#each this.model.attrs as |attr|}} + {{#each @model.attrs as |attr|}} {{#if (eq attr.type "object")}} {{else if (eq attr.type "array")}} {{else}} {{/if}} {{/each}} diff --git a/ui/app/components/alphabet-edit.js b/ui/app/components/alphabet-edit.js index 646d07208e..99c1f623cd 100644 --- a/ui/app/components/alphabet-edit.js +++ b/ui/app/components/alphabet-edit.js @@ -3,6 +3,82 @@ * SPDX-License-Identifier: BUSL-1.1 */ -import TransformBase from './transform-edit-base'; +import Component from '@glimmer/component'; +import { tracked } from '@glimmer/tracking'; +import { action } from '@ember/object'; +import { service } from '@ember/service'; +import errorMessage from 'vault/utils/error-message'; -export default TransformBase; +/** + * @module AlphabetEdit + * `AlphabetEdit` is a component that allows you to create/edit or view an alphabet. + * + * @example + * ```js + * + * ``` + * @param {object} model - This is the transform alphabet model. + * @param {string} mode - Is either show, create or edit. + */ + +export default class AlphabetEditComponent extends Component { + @service flashMessages; + @service router; + + @tracked errorMessage = ''; + + get breadcrumbs() { + // ideally this is created on the controller in the parent route but this is a generic route and adding breadcrumbs to the controller requires a larger refactor. + const { backend } = this.args.model; + return [ + { + label: backend, + route: 'vault.cluster.secrets.backend.list-root', + model: backend, + query: { tab: 'alphabet' }, + }, + { label: 'Alphabet' }, + ]; + } + + transition(route = 'show') { + this.errorMessage = ''; + const { backend, id } = this.args.model; + if (route === 'list') { + this.router.transitionTo('vault.cluster.secrets.backend.list-root', backend, { + queryParams: { tab: 'alphabet' }, + }); + return; + } else { + this.router.transitionTo('vault.cluster.secrets.backend.show', `alphabet/${id}`); + } + } + + @action async createOrUpdate(event) { + event.preventDefault(); + + if (!this.args.model.hasDirtyAttributes) { + this.flashMessages.info('No changes detected.'); + this.transition(); + return; + } + + try { + await this.args.model.save(); + this.flashMessages.success('Alphabet saved.'); + this.transition(); + } catch (e) { + this.errorMessage = errorMessage(e); + } + } + + @action async onDelete() { + try { + await this.args.model.destroyRecord(); + this.flashMessages.success('Alphabet deleted.'); + this.transition('list'); + } catch (e) { + this.errorMessage = errorMessage(e); + } + } +} diff --git a/ui/app/templates/components/transform-template-edit.hbs b/ui/app/components/transform-template-edit.hbs similarity index 59% rename from ui/app/templates/components/transform-template-edit.hbs rename to ui/app/components/transform-template-edit.hbs index ef747f5809..18ac521f90 100644 --- a/ui/app/templates/components/transform-template-edit.hbs +++ b/ui/app/components/transform-template-edit.hbs @@ -5,62 +5,51 @@ - +

- {{#if (eq this.mode "create")}} + {{#if (eq @mode "create")}} Create Template - {{else if (eq this.mode "edit")}} + {{else if (eq @mode "edit")}} Edit Template {{else}} Template - {{this.model.id}} + {{@model.id}} {{/if}}

-{{#if (eq this.mode "show")}} +{{#if (eq @mode "show")}} - {{#if this.model.updatePath.canDelete}} + {{#if @model.updatePath.canDelete}}
{{/if}} - {{#if this.model.updatePath.canUpdate}} - + {{#if @model.updatePath.canUpdate}} + Edit template - + {{/if}}
{{/if}} -{{#if (or (eq this.mode "edit") (eq this.mode "create"))}} -
+{{#if (or (eq @mode "edit") (eq @mode "create"))}} +
- - - {{#each this.model.writeAttrs as |attr|}} - {{#if (and (eq attr.name "name") (eq this.mode "edit"))}} + + + {{#each @model.writeAttrs as |attr|}} + {{#if (and (eq attr.name "name") (eq @mode "edit"))}} @@ -72,32 +61,32 @@ id={{attr.name}} autocomplete="off" spellcheck="false" - value={{or (get this.model attr.name) attr.options.defaultValue}} + value={{or (get @model attr.name) attr.options.defaultValue}} readonly={{true}} class="field input is-readOnly" type={{attr.type}} /> {{else}} {{#if (eq attr.name "alphabet")}} - + {{/if}} - + {{/if}} {{/each}}
- {{#if (eq this.mode "create")}} + {{#if (eq @mode "create")}} {{else}} @@ -105,7 +94,7 @@ @text="Cancel" @color="secondary" @route="vault.cluster.secrets.backend.show" - @models={{array this.model.backend (concat "template/" this.model.id)}} + @models={{array @model.backend (concat "template/" @model.id)}} @query={{hash tab="template"}} /> {{/if}} @@ -113,19 +102,14 @@
{{else}} - {{#if this.model.displayErrors}} -
- -
- {{/if}}
- {{#each this.model.readAttrs as |attr|}} + {{#each @model.readAttrs as |attr|}} {{#let (capitalize (or attr.options.label (humanize (dasherize attr.name)))) as |label|}} {{#if (eq attr.name "decodeFormats")}} - {{#if (not (is-empty-value this.model.decodeFormats))}} + {{#if (not (is-empty-value @model.decodeFormats))}}
- {{#each-in this.model.decodeFormats as |key value|}} + {{#each-in @model.decodeFormats as |key value|}}

{{key}}

{{value}}

@@ -137,7 +121,7 @@ {{else}} {{/if}} diff --git a/ui/app/components/transform-template-edit.js b/ui/app/components/transform-template-edit.js index 646d07208e..6c835eb4b2 100644 --- a/ui/app/components/transform-template-edit.js +++ b/ui/app/components/transform-template-edit.js @@ -3,6 +3,82 @@ * SPDX-License-Identifier: BUSL-1.1 */ -import TransformBase from './transform-edit-base'; +import Component from '@glimmer/component'; +import { tracked } from '@glimmer/tracking'; +import { action } from '@ember/object'; +import { service } from '@ember/service'; +import errorMessage from 'vault/utils/error-message'; -export default TransformBase; +/** + * @module TransformTemplateEdit + * `TransformTemplateEdit` is a component that allows you to create/edit or view a transform template. + * + * @example + * ```js + * + * ``` + * @param {object} model - This is the transform template model. + * @param {string} mode - Is either show, create or edit. + */ + +export default class TransformTemplateEditComponent extends Component { + @service flashMessages; + @service router; + + @tracked errorMessage = ''; + + get breadcrumbs() { + // ideally this is created on the controller in the parent route but this is a generic route and adding breadcrumbs to the controller requires a larger refactor. + const { backend } = this.args.model; + return [ + { + label: backend, + route: 'vault.cluster.secrets.backend.list-root', + model: backend, + query: { tab: 'template' }, + }, + { label: 'Template' }, + ]; + } + + transition(route = 'show') { + this.errorMessage = ''; + const { backend, id } = this.args.model; + if (route === 'list') { + this.router.transitionTo('vault.cluster.secrets.backend.list-root', backend, { + queryParams: { tab: 'template' }, + }); + return; + } else { + this.router.transitionTo('vault.cluster.secrets.backend.show', `template/${id}`); + } + } + + @action async createOrUpdate(event) { + event.preventDefault(); + + if (!this.args.model.hasDirtyAttributes) { + this.flashMessages.info('No changes detected.'); + this.transition(); + return; + } + + try { + await this.args.model.save(); + this.flashMessages.success('Transform template saved.'); + this.transition(); + } catch (e) { + this.errorMessage = errorMessage(e); + } + } + + @action async onDelete() { + try { + await this.args.model.destroyRecord(); + this.flashMessages.success('Transform template deleted.'); + this.transition('list'); + } catch (e) { + this.errorMessage = errorMessage(e); + } + } +} diff --git a/ui/app/models/transform/alphabet.js b/ui/app/models/transform/alphabet.js index 429fa02754..5bb542467c 100644 --- a/ui/app/models/transform/alphabet.js +++ b/ui/app/models/transform/alphabet.js @@ -10,11 +10,6 @@ import { expandAttributeMeta } from 'vault/utils/field-to-attrs'; export default class Alphabet extends Model { idPrefix = 'alphabet/'; - get idForNav() { - const modelId = this.id || ''; - return `${this.idPrefix}${modelId}`; - } - @attr('string', { readOnly: true, subText: 'The alphabet name. Keep in mind that spaces are not allowed and this cannot be edited later.', diff --git a/ui/app/models/transform/template.js b/ui/app/models/transform/template.js index 606c0323ba..cc88fc90ef 100644 --- a/ui/app/models/transform/template.js +++ b/ui/app/models/transform/template.js @@ -4,28 +4,28 @@ */ import Model, { attr } from '@ember-data/model'; -import { computed } from '@ember/object'; import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities'; import { expandAttributeMeta } from 'vault/utils/field-to-attrs'; -export default Model.extend({ - idPrefix: 'template/', - idForNav: computed('id', 'idPrefix', function () { - const modelId = this.id || ''; - return `${this.idPrefix}${modelId}`; - }), +export default class TransformTemplate extends Model { + idPrefix = 'template/'; - name: attr('string', { + @attr('string', { readOnly: true, subText: 'Templates allow Vault to determine what and how to capture the value to be transformed. This cannot be edited later.', - }), - type: attr('string', { defaultValue: 'regex' }), - pattern: attr('string', { + }) + name; + + @attr('string', { defaultValue: 'regex' }) type; + + @attr('string', { editType: 'regex', subText: 'The template’s pattern defines the data format. Expressed in regex.', - }), - alphabet: attr('array', { + }) + pattern; + + @attr('array', { subText: 'Alphabet defines a set of characters (UTF-8) that is used for FPE to determine the validity of plaintext and ciphertext values. You can choose a built-in one, or create your own.', editType: 'searchSelect', @@ -34,17 +34,22 @@ export default Model.extend({ label: 'Alphabet', models: ['transform/alphabet'], selectLimit: 1, - }), - encodeFormat: attr('string'), - decodeFormats: attr(), - backend: attr('string', { readOnly: true }), + }) + alphabet; - readAttrs: computed(function () { + @attr('string') encodeFormat; + @attr('') decodeFormats; + + @attr('string', { readOnly: true }) backend; + + get readAttrs() { const keys = ['name', 'pattern', 'encodeFormat', 'decodeFormats', 'alphabet']; return expandAttributeMeta(this, keys); - }), - writeAttrs: computed(function () { + } + + get writeAttrs() { return expandAttributeMeta(this, ['name', 'pattern', 'alphabet']); - }), - updatePath: lazyCapabilities(apiPath`${'backend'}/template/${'id'}`, 'backend', 'id'), -}); + } + + @lazyCapabilities(apiPath`${'backend'}/template/${'id'}`, 'backend') updatePath; +}