mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
* Gallery allows picking stuff * Small fixes * added sentinel templates * Can set enforcement level on policies * Working on the interactive sentinel dev mode * Very rough development flow on FE * Changed position in gutter menu * More sentinel stuff * PR cleanup: removed testmode, removed unneeded mixins and deps * Heliosification * Index-level sentinel policy deletion and page title fixes * Makes the Canaries sentinel policy real and then comments out the unfinished ones * rename Access Control to Administration in prep for moving Sentinel Policies and Node Pool admin there * Sentinel policies moved within the Administration section * Mirage fixture for sentinel policy endpoints * Description length check and 500 prevention * Sync review PR feedback addressed, implied butons on radio cards * Cull un-used sentinel policies --------- Co-authored-by: Mike Nomitch <mail@mikenomitch.com>
92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
// @ts-check
|
|
|
|
import { action } from '@ember/object';
|
|
import { inject as service } from '@ember/service';
|
|
import { alias } from '@ember/object/computed';
|
|
import Component from '@glimmer/component';
|
|
import { tracked } from '@glimmer/tracking';
|
|
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
|
|
|
|
export default class RoleEditorComponent extends Component {
|
|
@service notifications;
|
|
@service router;
|
|
@service store;
|
|
|
|
@alias('args.role') role;
|
|
|
|
@tracked rolePolicies = [];
|
|
|
|
// when this renders, set up rolePOlicies
|
|
constructor() {
|
|
super(...arguments);
|
|
this.rolePolicies = this.role.policies.toArray() || [];
|
|
}
|
|
|
|
@action updateRoleName({ target: { value } }) {
|
|
this.role.set('name', value);
|
|
}
|
|
|
|
@action updateRolePolicies(policy, event) {
|
|
let { checked } = event.target;
|
|
if (checked) {
|
|
this.rolePolicies.push(policy);
|
|
} else {
|
|
this.rolePolicies = this.rolePolicies.filter((p) => p !== policy);
|
|
}
|
|
}
|
|
|
|
@action async save(e) {
|
|
if (e instanceof Event) {
|
|
e.preventDefault(); // code-mirror "command+enter" submits the form, but doesnt have a preventDefault()
|
|
}
|
|
try {
|
|
const nameRegex = '^[a-zA-Z0-9-]{1,128}$';
|
|
if (!this.role.name?.match(nameRegex)) {
|
|
throw new Error(
|
|
`Role name must be 1-128 characters long and can only contain letters, numbers, and dashes.`
|
|
);
|
|
}
|
|
|
|
const shouldRedirectAfterSave = this.role.isNew;
|
|
|
|
if (this.role.isNew && this.store.peekRecord('role', this.role.name)) {
|
|
throw new Error(`A role with name ${this.role.name} already exists.`);
|
|
}
|
|
|
|
// If no policies are selected, throw an error
|
|
if (this.rolePolicies.length === 0) {
|
|
throw new Error(`You must select at least one policy.`);
|
|
}
|
|
|
|
this.role.policies = this.rolePolicies;
|
|
|
|
await this.role.save();
|
|
|
|
this.notifications.add({
|
|
title: 'Role Saved',
|
|
color: 'success',
|
|
});
|
|
|
|
if (shouldRedirectAfterSave) {
|
|
this.router.transitionTo('administration.roles.role', this.role.id);
|
|
}
|
|
} catch (err) {
|
|
let message = err.errors?.length
|
|
? messageFromAdapterError(err)
|
|
: err.message || 'Unknown Error';
|
|
|
|
this.notifications.add({
|
|
title: `Error creating Role ${this.role.name}`,
|
|
message,
|
|
color: 'critical',
|
|
sticky: true,
|
|
});
|
|
}
|
|
}
|
|
}
|