Merge "feat: support quota when create trove instance"

This commit is contained in:
Zuul 2022-08-10 03:10:22 +00:00 committed by Gerrit Code Review
commit 0db91b1e55
3 changed files with 104 additions and 2 deletions

View File

@ -122,7 +122,7 @@ export class StepDetails extends Base {
},
{
name: 'size',
label: t('Size (GiB)'),
label: t('Database Disk (GiB)'),
type: 'input-int',
min: 1,
placeholder: t('Size'),

View File

@ -15,6 +15,8 @@
import { StepAction } from 'containers/Action';
import { inject, observer } from 'mobx-react';
import globalInstancesStore from 'stores/trove/instances';
import globalProjectStore from 'stores/keystone/project';
import { message as $message } from 'antd';
import StepDetails from './StepDetails';
import StepNetworking from './StepNetworking';
import StepInitializeDatabases from './StepInitializeDatabases';
@ -23,6 +25,10 @@ import StepAdvanced from './StepAdvanced';
export class StepCreate extends StepAction {
init() {
this.store = globalInstancesStore;
this.projectStore = globalProjectStore;
this.getQuota();
this.state.isLoading = true;
this.errorMsg = '';
}
static id = 'create-database-instance';
@ -70,6 +76,102 @@ export class StepCreate extends StepAction {
];
}
get showQuota() {
return this.props.rootStore.hasAdminOnlyRole;
}
async getQuota() {
if (this.showQuota) {
await this.projectStore.fetchProjectTroveQuota(this.currentProjectId);
this.setState({
isLoading: false,
});
}
}
get quotaInfo() {
if (this.state.isLoading) {
return [];
}
const { instances = {}, volumes = {} } = this.projectStore.troveQuota || {};
const { left = 0 } = instances || {};
const { data: { size = 0 } = {} } = this.state;
const instanceQuotaInfo = {
...instances,
add: left ? 1 : 0,
name: 'instance',
title: t('Database Instance'),
};
const { left: volumeLeft = 0 } = volumes;
const volumeSizeQuotaInfo = {
...volumes,
add: volumeLeft === -1 || size <= volumeLeft ? size : 0,
name: 'volumeSize',
title: t('Database Disk (GiB)'),
type: 'line',
};
this.checkQuota(this.state.data, this.projectStore.troveQuota);
return [instanceQuotaInfo, volumeSizeQuotaInfo];
}
getQuotaMessage(input, left, name) {
if (left === -1) {
return '';
}
if (left === 0) {
return t('Quota: Insufficient { name } quota to create resources.', {
name,
});
}
if (input > left) {
return t(
'Insufficient {name} quota to create resources(left { quota }, input { input }).',
{ name, quota: left, input }
);
}
return '';
}
checkQuota(data, quota) {
const {
instances: { left: instanceLeft = 0 } = {},
volumes: { left: volumeLeft = 0 } = {},
} = quota || {};
const { size = 0 } = data || {};
const instanceMsg = this.getQuotaMessage(
1,
instanceLeft,
t('Database Instance')
);
const sizeMsg = this.getQuotaMessage(
size,
volumeLeft,
t('Database Disk (GiB)')
);
if (!instanceMsg && !sizeMsg) {
this.errorMsg = '';
} else {
const msg = instanceMsg || sizeMsg;
if (this.errorMsg !== msg) {
$message.error(msg);
}
this.errorMsg = msg;
}
}
get disableNext() {
return !!this.errorMsg;
}
get disableSubmit() {
return !!this.errorMsg;
}
onSubmit = (values) => {
let network = [];

View File

@ -101,7 +101,7 @@ export class Instances extends Base {
isHideable: true,
},
{
title: t('Volume Capacity (GiB)'),
title: t('Database Disk (GiB)'),
dataIndex: 'size',
isHideable: true,
render: (value) => (value ? `${value}GiB` : '-'),