From 0167257135df93a6f754655c2c9fbecc17ab40ed Mon Sep 17 00:00:00 2001 From: xusongfu Date: Tue, 9 Aug 2022 11:21:00 +0800 Subject: [PATCH] feat: support quota when create trove instance support quota when create trove instance Change-Id: I2124271308d1544b56f19584921dc0430bde0720 --- .../actions/StepCreate/StepDetails/index.jsx | 2 +- .../Instances/actions/StepCreate/index.jsx | 102 ++++++++++++++++++ .../database/containers/Instances/index.jsx | 2 +- 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/src/pages/database/containers/Instances/actions/StepCreate/StepDetails/index.jsx b/src/pages/database/containers/Instances/actions/StepCreate/StepDetails/index.jsx index 417fe9f9..fa54b004 100644 --- a/src/pages/database/containers/Instances/actions/StepCreate/StepDetails/index.jsx +++ b/src/pages/database/containers/Instances/actions/StepCreate/StepDetails/index.jsx @@ -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'), diff --git a/src/pages/database/containers/Instances/actions/StepCreate/index.jsx b/src/pages/database/containers/Instances/actions/StepCreate/index.jsx index 5e96c74c..758cc1af 100644 --- a/src/pages/database/containers/Instances/actions/StepCreate/index.jsx +++ b/src/pages/database/containers/Instances/actions/StepCreate/index.jsx @@ -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 = []; diff --git a/src/pages/database/containers/Instances/index.jsx b/src/pages/database/containers/Instances/index.jsx index 15842a6e..1a1431d7 100644 --- a/src/pages/database/containers/Instances/index.jsx +++ b/src/pages/database/containers/Instances/index.jsx @@ -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` : '-'),