|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <div class="form-layout" v-ctrl-enter="handleSubmit"> |
| 20 | + <a-spin :spinning="loading"> |
| 21 | + <a-form |
| 22 | + :ref="formRef" |
| 23 | + :model="form" |
| 24 | + :rules="rules" |
| 25 | + @finish="handleSubmit" |
| 26 | + layout="vertical"> |
| 27 | + <a-form-item name="name" ref="name"> |
| 28 | + <template #label> |
| 29 | + <tooltip-label :title="$t('label.name')" :tooltip="apiParams.name.description"/> |
| 30 | + </template> |
| 31 | + <a-input |
| 32 | + v-model:value="form.name" |
| 33 | + :placeholder="apiParams.name.description" |
| 34 | + v-focus="true" /> |
| 35 | + </a-form-item> |
| 36 | + <a-form-item name="description" ref="description"> |
| 37 | + <template #label> |
| 38 | + <tooltip-label :title="$t('label.description')" :tooltip="apiParams.description.description"/> |
| 39 | + </template> |
| 40 | + <a-input |
| 41 | + v-model:value="form.description" |
| 42 | + :placeholder="apiParams.description.description"/> |
| 43 | + </a-form-item> |
| 44 | + <a-form-item name="type" ref="type"> |
| 45 | + <template #label> |
| 46 | + <tooltip-label :title="$t('label.type')" :tooltip="apiParams.type.description"/> |
| 47 | + </template> |
| 48 | + <a-select |
| 49 | + v-model:value="form.type" |
| 50 | + showSearch |
| 51 | + optionFilterProp="label" |
| 52 | + :placeholder="apiParams.type.description"> |
| 53 | + <a-select-option v-for="opt in affinityGroupTypes" :key="opt" :label="opt"> |
| 54 | + {{ opt }} |
| 55 | + </a-select-option> |
| 56 | + </a-select> |
| 57 | + </a-form-item> |
| 58 | + <ownership-selection v-if="isAdminOrDomainAdmin()" @fetch-owner="fetchOwnerOptions"/> |
| 59 | + <div :span="24" class="action-button"> |
| 60 | + <a-button @click="closeAction">{{ $t('label.cancel') }}</a-button> |
| 61 | + <a-button :loading="loading" ref="submit" type="primary" @click="handleSubmit">{{ $t('label.ok') }}</a-button> |
| 62 | + </div> |
| 63 | + </a-form> |
| 64 | + </a-spin> |
| 65 | + </div> |
| 66 | +</template> |
| 67 | + |
| 68 | +<script> |
| 69 | +import { ref, reactive, toRaw } from 'vue' |
| 70 | +import { postAPI } from '@/api' |
| 71 | +import { mixinForm } from '@/utils/mixin' |
| 72 | +import TooltipLabel from '@/components/widgets/TooltipLabel' |
| 73 | +import OwnershipSelection from '@/views/compute/wizard/OwnershipSelection' |
| 74 | +
|
| 75 | +export default { |
| 76 | + name: 'CreateAffinityGroup', |
| 77 | + mixins: [mixinForm], |
| 78 | + components: { |
| 79 | + TooltipLabel, |
| 80 | + OwnershipSelection |
| 81 | + }, |
| 82 | + data () { |
| 83 | + return { |
| 84 | + loading: false, |
| 85 | + owner: {}, |
| 86 | + affinityGroupTypes: [ |
| 87 | + 'host affinity', |
| 88 | + 'host anti-affinity', |
| 89 | + 'non-strict host affinity', |
| 90 | + 'non-strict host anti-affinity' |
| 91 | + ] |
| 92 | + } |
| 93 | + }, |
| 94 | + beforeCreate () { |
| 95 | + this.apiParams = this.$getApiParams('createAffinityGroup') |
| 96 | + }, |
| 97 | + created () { |
| 98 | + this.initForm() |
| 99 | + }, |
| 100 | + methods: { |
| 101 | + initForm () { |
| 102 | + this.formRef = ref() |
| 103 | + this.form = reactive({}) |
| 104 | + this.rules = reactive({ |
| 105 | + name: [{ required: true, message: this.$t('message.error.name') }], |
| 106 | + type: [{ required: true, message: this.$t('label.required') }] |
| 107 | + }) |
| 108 | + }, |
| 109 | + isAdminOrDomainAdmin () { |
| 110 | + return ['Admin', 'DomainAdmin'].includes(this.$store.getters.userInfo.roletype) |
| 111 | + }, |
| 112 | + fetchOwnerOptions (ownerOptions) { |
| 113 | + this.owner = {} |
| 114 | + if (ownerOptions.selectedAccountType === 'Account') { |
| 115 | + if (!ownerOptions.selectedAccount) { |
| 116 | + return |
| 117 | + } |
| 118 | + this.owner.account = ownerOptions.selectedAccount |
| 119 | + this.owner.domainid = ownerOptions.selectedDomain |
| 120 | + } else if (ownerOptions.selectedAccountType === 'Project') { |
| 121 | + if (!ownerOptions.selectedProject) { |
| 122 | + return |
| 123 | + } |
| 124 | + this.owner.projectid = ownerOptions.selectedProject |
| 125 | + } |
| 126 | + }, |
| 127 | + handleSubmit (e) { |
| 128 | + e.preventDefault() |
| 129 | + if (this.loading) return |
| 130 | + this.formRef.value.validate().then(() => { |
| 131 | + const formRaw = toRaw(this.form) |
| 132 | + const values = this.handleRemoveFields(formRaw) |
| 133 | + this.loading = true |
| 134 | + const params = { |
| 135 | + name: values.name, |
| 136 | + description: values.description, |
| 137 | + type: values.type |
| 138 | + } |
| 139 | + if (this.owner.account) { |
| 140 | + params.account = this.owner.account |
| 141 | + params.domainid = this.owner.domainid |
| 142 | + } else if (this.owner.projectid) { |
| 143 | + params.projectid = this.owner.projectid |
| 144 | + } |
| 145 | + postAPI('createAffinityGroup', params).then(json => { |
| 146 | + this.$message.success(this.$t('label.add.affinity.group') + ' - ' + values.name) |
| 147 | + }).catch(error => { |
| 148 | + this.$notifyError(error) |
| 149 | + }).finally(() => { |
| 150 | + this.$emit('refresh-data') |
| 151 | + this.loading = false |
| 152 | + this.closeAction() |
| 153 | + }) |
| 154 | + }).catch(error => { |
| 155 | + this.formRef.value.scrollToField(error.errorFields[0].name) |
| 156 | + }) |
| 157 | + }, |
| 158 | + closeAction () { |
| 159 | + this.$emit('close-action') |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | +</script> |
| 164 | + |
| 165 | +<style scoped lang="less"> |
| 166 | + .form-layout { |
| 167 | + width: 80vw; |
| 168 | +
|
| 169 | + @media (min-width: 600px) { |
| 170 | + width: 450px; |
| 171 | + } |
| 172 | + } |
| 173 | +</style> |
0 commit comments