Form.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div class="w-75">
  3. <a-form :form="form.fc" v-bind="formItemLayout">
  4. <a-form-item :label="$t('k8s.text_41')">
  5. <a-input :placeholder="$t('k8s.text_60')" v-decorator="decorators.name" />
  6. </a-form-item>
  7. <a-form-item :label="$t('k8s.text_305')">
  8. <a-input-number v-decorator="decorators.size" :min="1" :max="500" /> G
  9. </a-form-item>
  10. <a-form-item :label="$t('k8s.text_19')">
  11. <cluster-select v-decorator="decorators.cluster" @input="setCluster" />
  12. </a-form-item>
  13. <a-form-item :label="$t('k8s.text_23')">
  14. <namespace-select v-decorator="decorators.namespace" @input="setNamespace" :cluster="cluster" />
  15. </a-form-item>
  16. <a-form-item :label="$t('k8s.text_22')">
  17. <div slot="extra">{{$t('k8s.text_306')}}<help-link :href="storageClassHref">{{$t('k8s.text_307')}}</help-link>
  18. </div>
  19. <base-select
  20. show-sync
  21. v-decorator="decorators.storageClass"
  22. resource="storageclasses"
  23. version="v1"
  24. id-key="name"
  25. :resList.sync="storageclassOpts"
  26. :need-params="true"
  27. :params="storageclassParams"
  28. :select-props="{ placeholder: $t('k8s.text_308') }" />
  29. </a-form-item>
  30. </a-form>
  31. </div>
  32. </template>
  33. <script>
  34. import * as R from 'ramda'
  35. import ClusterSelect from '@K8S/sections/ClusterSelect'
  36. import NamespaceSelect from '@K8S/sections/NamespaceSelect'
  37. import k8sCreateMixin from '@K8S/mixins/create'
  38. export default {
  39. name: 'K8sPersistentvolumeclaimCreate',
  40. components: {
  41. ClusterSelect,
  42. NamespaceSelect,
  43. },
  44. mixins: [k8sCreateMixin],
  45. data () {
  46. return {
  47. form: {
  48. fc: this.$form.createForm(this, {
  49. onValuesChange: (props, values) => {
  50. R.forEachObjIndexed((value, key) => {
  51. this.form.fd[key] = value
  52. }, values)
  53. },
  54. }),
  55. fd: {
  56. cluster: this.$store.state.common.k8s.cluster,
  57. },
  58. },
  59. formItemLayout: {
  60. labelCol: { span: 4 },
  61. wrapperCol: { span: 20 },
  62. },
  63. decorators: {
  64. name: [
  65. 'name',
  66. {
  67. validateFirst: true,
  68. rules: [
  69. { required: true, message: this.$t('k8s.text_60') },
  70. { min: 2, max: 24, message: this.$t('k8s.text_132'), trigger: 'blur' },
  71. { validator: this.$validate('k8sName') },
  72. ],
  73. },
  74. ],
  75. cluster: [
  76. 'cluster',
  77. {
  78. initialValue: this.$store.state.common.k8s.cluster,
  79. rules: [
  80. { required: true, message: this.$t('k8s.text_30'), trigger: 'blur' },
  81. ],
  82. },
  83. ],
  84. namespace: [
  85. 'namespace',
  86. {
  87. initialValue: this.$store.state.common.k8s.namespace,
  88. rules: [
  89. { required: true, message: this.$t('k8s.text_61'), trigger: 'blur' },
  90. ],
  91. },
  92. ],
  93. size: [
  94. 'size',
  95. {
  96. initialValue: 1,
  97. },
  98. ],
  99. storageClass: [
  100. 'storageClass',
  101. {
  102. rules: [
  103. { required: true, message: this.$t('k8s.text_308'), trigger: 'blur' },
  104. ],
  105. },
  106. ],
  107. },
  108. storageclassOpts: [],
  109. }
  110. },
  111. computed: {
  112. storageClassHref () {
  113. return `/v2/k8s-storageclass/create?cluster=${this.form.fd.cluster}`
  114. },
  115. storageclassParams () {
  116. if (this.form.fd.cluster) {
  117. return {
  118. cluster: this.form.fd.cluster,
  119. limit: 0,
  120. }
  121. }
  122. return {}
  123. },
  124. },
  125. watch: {
  126. storageclassOpts (val, oldV) {
  127. let storageclass
  128. if (!R.equals(val, oldV)) {
  129. const defaultItem = val.find(v => v.isDefault)
  130. if (R.is(Object, defaultItem)) {
  131. storageclass = defaultItem.name
  132. }
  133. }
  134. this.form.fc.setFieldsValue({
  135. [this.decorators.storageClass[0]]: storageclass,
  136. })
  137. },
  138. },
  139. methods: {
  140. validateForm () {
  141. return new Promise((resolve, reject) => {
  142. this.form.fc.validateFieldsAndScroll({ scroll: { alignWithTop: true, offsetTop: 100 } }, (err, values) => {
  143. if (!err) {
  144. resolve(values)
  145. } else {
  146. reject(err)
  147. }
  148. })
  149. })
  150. },
  151. async _doCreate (data) {
  152. await new this.$Manager('persistentvolumeclaims', 'v1').create({ data })
  153. },
  154. async doCreate () {
  155. try {
  156. const values = await this.validateForm()
  157. const params = {
  158. ...values,
  159. size: values.size + 'G',
  160. }
  161. await this._doCreate(params)
  162. this.$message.success(this.$t('k8s.text_46'))
  163. } catch (error) {
  164. throw error
  165. }
  166. },
  167. },
  168. }
  169. </script>