|
| 1 | +/** |
| 2 | + * Copyright IBM Corp. 2016, 2025 |
| 3 | + * SPDX-License-Identifier: BUSL-1.1 |
| 4 | + */ |
| 5 | +import Component from '@glimmer/component'; |
| 6 | +import { service } from '@ember/service'; |
| 7 | +import { tracked } from '@glimmer/tracking'; |
| 8 | +import { task } from 'ember-concurrency'; |
| 9 | +import { waitFor } from '@ember/test-waiters'; |
| 10 | + |
| 11 | +import type SecretMountPath from 'vault/services/secret-mount-path'; |
| 12 | +import type ApiService from 'vault/services/api'; |
| 13 | +import type RouterService from '@ember/routing/router-service'; |
| 14 | +import type FlashMessageService from 'vault/services/flash-messages'; |
| 15 | +import type { ValidationMap } from 'vault/app-types'; |
| 16 | +import type { HTMLElementEvent } from 'vault/forms'; |
| 17 | +import type KmipConfigForm from 'vault/forms/secrets/kmip/config'; |
| 18 | + |
| 19 | +interface Args { |
| 20 | + form: KmipConfigForm; |
| 21 | +} |
| 22 | + |
| 23 | +export default class KmipConfigurePageComponent extends Component<Args> { |
| 24 | + @service declare readonly secretMountPath: SecretMountPath; |
| 25 | + @service declare readonly api: ApiService; |
| 26 | + @service('app-router') declare readonly router: RouterService; |
| 27 | + @service declare readonly flashMessages: FlashMessageService; |
| 28 | + |
| 29 | + @tracked modelValidations: ValidationMap | null = null; |
| 30 | + @tracked invalidFormAlert: string | null = null; |
| 31 | + @tracked errorMessage: string | null = null; |
| 32 | + |
| 33 | + save = task( |
| 34 | + waitFor(async (event: HTMLElementEvent<HTMLFormElement>) => { |
| 35 | + event.preventDefault(); |
| 36 | + this.errorMessage = null; |
| 37 | + |
| 38 | + try { |
| 39 | + const { isValid, state, invalidFormMessage, data } = this.args.form.toJSON(); |
| 40 | + this.modelValidations = isValid ? null : state; |
| 41 | + this.invalidFormAlert = isValid ? '' : invalidFormMessage; |
| 42 | + |
| 43 | + if (isValid) { |
| 44 | + await this.api.secrets.kmipConfigure(this.secretMountPath.currentPath, data); |
| 45 | + this.flashMessages.success('Successfully configured KMIP engine'); |
| 46 | + this.router.transitionTo('vault.cluster.secrets.backend.kmip.configuration'); |
| 47 | + } |
| 48 | + } catch (error) { |
| 49 | + const { message } = await this.api.parseError(error); |
| 50 | + this.errorMessage = message; |
| 51 | + this.invalidFormAlert = 'There was an error submitting this form.'; |
| 52 | + } |
| 53 | + }) |
| 54 | + ); |
| 55 | +} |
0 commit comments