CreateSnapshotPolicy.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{action}}</div>
  4. <div slot="body">
  5. <a-alert class="mb-2" type="warning">
  6. <div slot="message">{{$t('compute.text_1087')}}<br />{{$t('compute.text_1088')}}<br />{{$t('compute.text_1089')}}</div>
  7. </a-alert>
  8. <a-form :form="form.fc" hideRequiredMark>
  9. <a-form-item :label="$t('compute.text_297', [$t('dictionary.project')])" v-bind="formItemLayout">
  10. <domain-project :fc="form.fc" :form-layout="formItemLayout" isDefaultSelect :decorators="{ project: decorators.project, domain: decorators.domain }" />
  11. </a-form-item>
  12. <a-form-item :label="$t('compute.text_428')" v-bind="formItemLayout">
  13. <a-input
  14. v-decorator="decorators.generate_name"
  15. :placeholder="$t('compute.text_1090')"
  16. @change="nameChangeHandle" />
  17. <div slot="extra" v-if="showRepeatTips">
  18. <div>{{$t('compute.text_1091')}}</div>
  19. </div>
  20. </a-form-item>
  21. <a-form-item :label="$t('common.description')" v-bind="formItemLayout">
  22. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  23. </a-form-item>
  24. <a-form-item :label="$t('common.resource_type')" v-bind="formItemLayout">
  25. <a-select v-decorator="decorators.type" :disabled="params.type === 'update'">
  26. <a-select-option value="server" v-if="types.includes('server')">{{$t('dictionary.server')}}</a-select-option>
  27. <a-select-option value="disk" v-if="types.includes('disk')">{{$t('dictionary.disk')}}</a-select-option>
  28. </a-select>
  29. </a-form-item>
  30. <a-form-item :label="$t('compute.text_431')" v-bind="formItemLayout">
  31. <a-checkbox-group v-decorator="decorators.repeat_weekdays">
  32. <a-checkbox
  33. v-for="(week, idx) of weekOptions"
  34. :key="idx + 1"
  35. :value="idx + 1">{{ week }}</a-checkbox>
  36. </a-checkbox-group>
  37. </a-form-item>
  38. <a-form-item :label="$t('compute.text_432')" v-bind="formItemLayout">
  39. <!-- <a-button-group v-decorator="decorators.time_points">
  40. <a-button
  41. class="select-btn"
  42. v-for="(time, idx) of timeOptions"
  43. size="small"
  44. :key="idx"
  45. :type="form.fd.time_points.includes(idx) ? 'primary' : ''"
  46. @click="timeSelectHandle(idx)">{{ time }}</a-button>
  47. </a-button-group> -->
  48. <a-select v-decorator="decorators.time_points" mode="multiple">
  49. <a-select-option v-for="(time, idx) in timeOptions" :key="idx" :value="idx">{{time}}</a-select-option>
  50. </a-select>
  51. </a-form-item>
  52. <a-form-item :label="$t('compute.text_433')" v-bind="formItemLayout">
  53. <a-radio-group v-decorator="decorators.alwaysReserved">
  54. <div class="mb-2">
  55. <a-radio value="day" :disabled="params.type === 'update' && form.fd.alwaysReserved !== 'day'">
  56. <span class="mr-2">{{$t('compute.text_1092')}}</span>
  57. <a-input-number
  58. size="small"
  59. v-decorator="decorators.retention_days"
  60. :step="1"
  61. :max="49"
  62. :min="1"
  63. :disabled="params.type === 'update' && form.fd.alwaysReserved !== 'day'"
  64. step-strictly />
  65. <span style="color: #606266;" class="ml-2">{{$t('compute.text_1093')}}</span>
  66. </a-radio>
  67. </div>
  68. <div class="mb-2">
  69. <a-radio value="count" :disabled="params.type === 'update' && form.fd.alwaysReserved !== 'count'">
  70. <span class="mr-2">{{$t('compute.retention_count_prefix')}}</span>
  71. <a-input-number
  72. size="small"
  73. v-decorator="decorators.retention_count"
  74. :step="1"
  75. :min="1"
  76. :disabled="params.type === 'update' && form.fd.alwaysReserved !== 'count'"
  77. step-strictly />
  78. <span style="color: #606266;" class="ml-2">{{$t('compute.retention_count_suffix')}}</span>
  79. </a-radio>
  80. </div>
  81. <div>
  82. <a-radio value="always" :disabled="params.type === 'update' && form.fd.alwaysReserved !== 'always'">
  83. {{$t('compute.text_1094')}}
  84. </a-radio>
  85. </div>
  86. </a-radio-group>
  87. </a-form-item>
  88. </a-form>
  89. </div>
  90. <div slot="footer">
  91. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  92. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  93. </div>
  94. </base-dialog>
  95. </template>
  96. <script>
  97. import debounce from 'lodash/debounce'
  98. import DialogMixin from '@/mixins/dialog'
  99. import WindowsMixin from '@/mixins/windows'
  100. import DomainProject from '@/sections/DomainProject'
  101. import validateForm, { isRequired } from '@/utils/validate'
  102. import { weekOptions, timeOptions } from '../constants'
  103. export default {
  104. name: 'CreateSnapshotPolicyDialog',
  105. components: {
  106. DomainProject,
  107. },
  108. mixins: [DialogMixin, WindowsMixin],
  109. data () {
  110. const tenant = this.params.extParams && this.params.extParams.tenant
  111. const domain = this.params.extParams && this.params.extParams.domain
  112. const types = (this.params.extParams && this.params.extParams.types) || ['server', 'disk']
  113. const initData = this.params.type === 'update' ? this.params.data[0] : {}
  114. let alwaysReserved = 'day'
  115. if (initData.retention_count) {
  116. alwaysReserved = 'count'
  117. } else if (initData.retention_days > 0) {
  118. alwaysReserved = 'day'
  119. } else {
  120. alwaysReserved = 'always'
  121. }
  122. return {
  123. loading: false,
  124. action: this.$t('compute.text_1095'),
  125. form: {
  126. fc: this.$form.createForm(this, { onValuesChange: this.onValuesChange }),
  127. fd: {
  128. generate_name: initData.name || '',
  129. description: initData.description || '',
  130. repeat_weekdays: initData.repeat_weekdays || [],
  131. time_points: initData.time_points || [],
  132. retention_days: initData.retention_days || 7,
  133. retention_count: initData.retention_count || 7,
  134. type: initData.type || types[0],
  135. alwaysReserved,
  136. },
  137. },
  138. types,
  139. decorators: {
  140. generate_name: [
  141. 'generate_name',
  142. {
  143. validateFirst: true,
  144. initialValue: initData.name || '',
  145. rules: [
  146. { required: true, message: this.$t('compute.text_1090') },
  147. { validator: validateForm('serverName') },
  148. ],
  149. },
  150. ],
  151. description: ['description', { initialValue: initData.description || '' }],
  152. type: [
  153. 'type',
  154. {
  155. initialValue: initData.type || types[0],
  156. },
  157. ],
  158. repeat_weekdays: [
  159. 'repeat_weekdays',
  160. {
  161. initialValue: initData.repeat_weekdays || [],
  162. rules: [
  163. { required: true, message: this.$t('compute.text_1096') },
  164. ],
  165. },
  166. ],
  167. time_points: [
  168. 'time_points',
  169. {
  170. initialValue: initData.time_points || [],
  171. rules: [
  172. { required: true, message: this.$t('compute.text_1097') },
  173. ],
  174. },
  175. ],
  176. retention_days: [
  177. 'retention_days',
  178. {
  179. initialValue: initData.retention_days || 7,
  180. },
  181. ],
  182. alwaysReserved: [
  183. 'alwaysReserved',
  184. {
  185. initialValue: alwaysReserved,
  186. },
  187. ],
  188. retention_count: [
  189. 'retention_count',
  190. {
  191. initialValue: initData.retention_count || 7,
  192. rules: [
  193. { required: true, message: this.$t('common.tips.input', [this.$t('compute.retention_count')]) },
  194. ],
  195. },
  196. ],
  197. domain: [
  198. 'domain',
  199. {
  200. initialValue: initData.domain_id || domain || this.$store.getters.userInfo.projectDomainId,
  201. rules: [
  202. { validator: isRequired(), message: this.$t('rules.domain'), trigger: 'change' },
  203. ],
  204. },
  205. ],
  206. project: [
  207. 'project',
  208. {
  209. initialValue: initData.tenant_id || tenant || this.$store.getters.userInfo.projectId,
  210. rules: [
  211. { validator: isRequired(), message: this.$t('rules.project'), trigger: 'change' },
  212. ],
  213. },
  214. ],
  215. },
  216. snapshotpolicies: [],
  217. weekOptions: [...weekOptions],
  218. timeOptions: [...timeOptions],
  219. formItemLayout: {
  220. wrapperCol: {
  221. span: 21,
  222. },
  223. labelCol: {
  224. span: 3,
  225. },
  226. },
  227. }
  228. },
  229. computed: {
  230. showRepeatTips () {
  231. const name = this.form.fd.generate_name || ''
  232. return !!this.snapshotpolicies.find(item => item.name === name.toLowerCase())
  233. },
  234. },
  235. created () {
  236. this.manager = new this.$Manager('snapshotpolicies')
  237. this.debounceFetchSnapshotpolicies = debounce(this.fetchSnapshotpolicies, 1000)
  238. },
  239. methods: {
  240. fetchSnapshotpolicies () {
  241. const params = {
  242. scope: this.$store.getters.scope,
  243. filter: `name.contains("${this.form.fd.generate_name}")`,
  244. }
  245. this.manager.list({ params }).then(res => {
  246. const data = res.data.data || []
  247. this.snapshotpolicies = data
  248. })
  249. },
  250. genParams () {
  251. const { alwaysReserved, retention_count, retention_days, domain, project, time_points, ...rest } = this.form.fd
  252. const params = {
  253. ...rest,
  254. }
  255. if (alwaysReserved === 'count') {
  256. params.retention_count = retention_count
  257. params.retention_days = -1
  258. } else if (alwaysReserved === 'always') {
  259. params.retention_days = -1
  260. } else if (alwaysReserved === 'day') {
  261. params.retention_days = retention_days
  262. }
  263. if (domain) {
  264. params.domain = domain.key
  265. }
  266. if (project) {
  267. params.tenant = project.key
  268. }
  269. params.time_points = time_points
  270. return params
  271. },
  272. async doCreateSnapshotPolicySubmit () {
  273. const params = this.genParams()
  274. return this.manager.create({ data: params })
  275. },
  276. async doUpdateSnapshotPolicySubmit () {
  277. const params = this.genParams()
  278. return this.params.onManager('update', {
  279. id: this.params.data[0].id,
  280. managerArgs: {
  281. data: { ...params, name: this.form.fd.generate_name },
  282. },
  283. })
  284. },
  285. async handleConfirm () {
  286. this.loading = true
  287. try {
  288. await this.form.fc.validateFields()
  289. if (this.params.type === 'update') {
  290. await this.doUpdateSnapshotPolicySubmit()
  291. } else {
  292. await this.doCreateSnapshotPolicySubmit()
  293. this.params.refresh && this.params.refresh()
  294. this.params.success && this.params.success()
  295. }
  296. this.loading = false
  297. this.cancelDialog()
  298. } catch (error) {
  299. this.loading = false
  300. throw error
  301. }
  302. },
  303. nameChangeHandle (val) {
  304. this.form.fd.generate_name = val
  305. this.debounceFetchSnapshotpolicies()
  306. },
  307. // timeSelectHandle (val) {
  308. // this.form.fd.time_points = [val]
  309. // this.$nextTick(() => {
  310. // this.form.fc.setFieldsValue({ time_points: [val] })
  311. // })
  312. // },
  313. onValuesChange (props, values) {
  314. Object.keys(values).forEach((key) => {
  315. this.form.fd[key] = values[key]
  316. })
  317. },
  318. },
  319. }
  320. </script>
  321. <style lang="less" scoped>
  322. .select-btn {
  323. margin-left: 0 !important;
  324. margin-right: 8px;
  325. margin-bottom: 5px;
  326. width: 80px;
  327. }
  328. .select-btn.ant-btn-primary + .ant-btn:not(.ant-btn-primary):not([disabled]) {
  329. border-left-color: #d9d9d9 !important;
  330. }
  331. </style>