feat: 🎸 create storage policy (#2079)

* feat: 🎸 add storage policy
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2024-01-12 11:07:18 -08:00
1 parent 8ad4e93ff7
commit 1cb3b708a2
17 files changed
+741 -16

No files matched your search

@@ -0,0 +1,81 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Rose::Form
@onSubmit={{@submit}}
@cancel={{@cancel}}
@disabled={{@model.isSaving}}
@showEditToggle={{if @model.isNew false true}}
as |form|
>
<Hds::Form::TextInput::Field
@isOptional={{true}}
@value={{@model.name}}
@isInvalid={{@model.errors.name}}
@type='text'
name='name'
disabled={{form.disabled}}
{{on 'input' (set-from-event @model 'name')}}
as |F|
>
<F.Label>{{t 'form.name.label'}}</F.Label>
<F.HelperText>{{t 'form.name.help'}}</F.HelperText>
{{#if @model.errors.name}}
<F.Error as |E|>
{{#each @model.errors.name as |error|}}
<E.Message>{{error.message}}</E.Message>
{{/each}}
</F.Error>
{{/if}}
</Hds::Form::TextInput::Field>
<Hds::Form::Textarea::Field
@isOptional={{true}}
@value={{@model.description}}
@isInvalid={{@model.errors.description}}
name='description'
disabled={{form.disabled}}
as |F|
>
<F.Label>{{t 'form.description.label'}}</F.Label>
<F.HelperText>{{t 'form.description.help'}}</F.HelperText>
{{#if @model.errors.description}}
<F.Error as |E|>
{{#each @model.errors.description as |error|}}
<E.Message>{{error.message}}</E.Message>
{{/each}}
</F.Error>
{{/if}}
</Hds::Form::Textarea::Field>
{{! retention policy }}
<Form::Policy::PolicySelection
@name='retention_policy'
@disabled={{form.disabled}}
@model={{@model}}
@options={{this.listRententionOptions}}
@customInputName='retain_for'
@inputValue={{@model.retain_for.days}}
@selectedOption={{this.selectRetentionPolicyType}}
/>
{{! deletion policy }}
<Form::Policy::PolicySelection
@name='deletion_policy'
@disabled={{form.disabled}}
@model={{@model}}
@options={{this.listDeletionOptions}}
@customInputName='delete_after'
@inputValue={{@model.delete_after.days}}
@selectedOption={{this.selectDeletePolicyType}}
/>
{{#if (can 'save model' @model)}}
<form.actions
@enableEditText={{t 'actions.edit-form'}}
@submitText={{t 'actions.save'}}
@cancelText={{t 'actions.cancel'}}
/>
{{/if}}
</Rose::Form>
@@ -0,0 +1,63 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Component from '@glimmer/component';
const RETENTION_POLICY = {
forever: -1,
custom: 1,
do_not_protect: 0,
};
const DELETION_POLICY = { do_not_delete: 0, custom: 1 };
export default class FormPolicyComponent extends Component {
//attributes
//methods
/**
* Returns retention policy options list
* @type {array}
*/
get listRententionOptions() {
return RETENTION_POLICY;
}
/**
* Returns deletion policy options list, if the retain days are -1 (forever)
* then we should force the delete option to be do not delete
* @type {array}
*/
get listDeletionOptions() {
if (this.args.model.retain_for?.days < 0) {
return { do_not_delete: 0 };
} else {
return DELETION_POLICY;
}
}
/**
* Returns policy type
* @type {string}
*/
get selectRetentionPolicyType() {
if (this.args.model.retain_for?.days < 0) {
return 'forever';
} else if (this.args.model.retain_for?.days >= 1) {
return 'custom';
} else {
return 'do_not_protect';
}
}
/**
* Returns policy type
* @type {string}
*/
get selectDeletePolicyType() {
return this.args.model.delete_after?.days > 0 ? 'custom' : 'do_not_delete';
}
}
@@ -0,0 +1,71 @@
<Hds::Form::Fieldset @isRequired={{true}} class='policy' as |F|>
<F.Legend>{{t (concat 'resources.policy.form.' @name '.label')}}</F.Legend>
<F.HelperText>
{{t (concat 'resources.policy.form.' @name '.help')}}
</F.HelperText>
<F.Control>
<Hds::Form::Select::Field
name={{@name}}
disabled={{or @disabled this.isDeleteDisable}}
aria-label={{t (concat 'resources.policy.form.' @name '.label')}}
@width='80%'
{{on 'change' this.handlePolicyTypeSelection}}
as |F|
>
<F.Options>
<option disabled hidden selected value=''>
{{t 'titles.choose-an-option'}}
</option>
{{#each-in @options as |key value|}}
<option value={{value}} selected={{eq key @selectedOption}}>
{{t
(concat 'resources.policy.form.' @name '_options.' key '.label')
}}
</option>
{{/each-in}}
</F.Options>
</Hds::Form::Select::Field>
</F.Control>
</Hds::Form::Fieldset>
{{#if this.showCustomInput}}
<Hds::Form::Field class='policy-custom-input' @layout='vertical' as |F|>
<F.Label>{{t (concat 'resources.policy.titles.' @customInputName)}}
</F.Label>
<F.Control>
<Hds::SegmentedGroup as |S|>
<S.TextInput
@value={{@inputValue}}
disabled={{@disabled}}
aria-label={{t 'resources.policy.title'}}
{{on 'input' this.handleInputChange}}
name={{@customInputName}}
data-input-type={{@customInputName}}
/>
<S.Generic>
<Hds::Badge
class={{if @disabled 'disabled'}}
@text={{t 'resources.policy.titles.days'}}
@color='neutral'
@type='outlined'
/>
</S.Generic>
</Hds::SegmentedGroup>
{{#if @model.scope.isGlobal}}
<Hds::Form::Toggle::Field
checked={{this.isOverridable}}
disabled={{@disabled}}
name={{@customInputName}}
{{on 'change' this.handleOverridableToggle}}
as |F|
>
<F.Label>{{t 'resources.policy.form.overridable.label'}}</F.Label>
</Hds::Form::Toggle::Field>
{{/if}}
</F.Control>
</Hds::Form::Field>
{{/if}}
@@ -0,0 +1,109 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
export default class FormPolicySelectionComponent extends Component {
//methods
/**
* This is used to show/hide the custom input field
* @type {boolean}
*/
get showCustomInput() {
if (this.args.name === 'retention_policy') {
return this.isCustomRetentionSelected;
} else if (
this.args.name === 'deletion_policy' &&
this.args.model.retain_for?.days >= 0
) {
return this.isCustomDeletionSelected;
} else {
return null;
}
}
/**
* Returns true if rentention days are greater than 0
* @type {boolean}
*/
get isCustomRetentionSelected() {
return this.args.model.retain_for?.days > 0;
}
/**
* Returns true if deletion days are greater than 0
* @type {boolean}
*/
get isCustomDeletionSelected() {
return this.args.model.delete_after?.days > 0;
}
/**
* Returns true if the toggle is on
* @type {boolean}
*/
get isOverridable() {
return this.args.model[this.args.customInputName]?.overridable;
}
get isDeleteDisable() {
if (
this.args.name === 'deletion_policy' &&
this.args.model.retain_for?.days === -1
) {
return true;
} else {
return false;
}
}
//actions
/**
* Handles custom input changes
*/
@action
handleInputChange({ target: { value, name: field } }) {
// Select options return type is string and we want the `days` in integer format
const val = Number(value);
this.args.model[field] = {
...this.args.model[field],
days: val,
};
}
/**
* Toggle retain for & delete after overridables
*/
@action
handleOverridableToggle({ target: { name: field } }) {
this.args.model[field] = {
...this.args.model[field],
overridable: !this.args.model[field].overridable,
};
}
/**
* Show custom text field when custom option is selected
*/
@action
handlePolicyTypeSelection({ target: { value, name: policy } }) {
//Select options return type is string and we want the `days` in integer format
const selectedVal = Number(value);
if (policy === 'retention_policy') {
this.args.model.retain_for = {
...this.args.model.retain_for,
days: selectedVal,
};
if (selectedVal < 0) {
this.args.model.delete_after = {
...this.args.model.delete_after,
days: 0,
};
}
}
if (policy === 'deletion_policy') {
this.args.model.delete_after = {
...this.args.model.delete_after,
days: selectedVal,
};
}
}
}