Form.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div class="w-75">
  3. <a-form :form="form.fc" v-bind="formItemLayout">
  4. <a-form-item :label="$t('k8s.text_34')">
  5. <a-radio-group v-model="form.fd.type">
  6. <a-radio-button value="keypair">{{$t('k8s.text_332')}}</a-radio-button>
  7. <a-radio-button value="cephCSI">Ceph CSI</a-radio-button>
  8. </a-radio-group>
  9. </a-form-item>
  10. <a-form-item :label="$t('k8s.text_41')">
  11. <a-input :placeholder="$t('k8s.text_60')" v-decorator="decorators.name" />
  12. </a-form-item>
  13. <a-form-item :label="$t('k8s.text_19')">
  14. <cluster-select v-decorator="decorators.cluster" @input="setCluster" />
  15. </a-form-item>
  16. <a-form-item :label="$t('k8s.text_23')">
  17. <namespace-select v-decorator="decorators.namespace" @input="setNamespace" :cluster="cluster" :namespaceObj.sync="namespaceObj" />
  18. </a-form-item>
  19. <template v-if="form.fd.type === 'keypair'">
  20. <a-form-item :label="$t('k8s.text_52')">
  21. <a-input v-decorator="decorators.server" :placeholder="$t('k8s.text_53')" />
  22. </a-form-item>
  23. <a-form-item :label="$t('k8s.text_54')">
  24. <a-input v-decorator="decorators.user" :placeholder="$t('k8s.text_55')" />
  25. </a-form-item>
  26. <a-form-item :label="$t('k8s.text_56')">
  27. <a-input v-decorator="decorators.password" :placeholder="$t('k8s.text_57')" show-password />
  28. </a-form-item>
  29. <a-form-item :label="$t('k8s.text_58')">
  30. <a-input v-decorator="decorators.email" :placeholder="$t('k8s.text_59')" />
  31. </a-form-item>
  32. </template>
  33. <template v-else>
  34. <a-form-item label="UserId">
  35. <a-input v-decorator="decorators.userId" :placeholder="$t('k8s.text_333')" />
  36. </a-form-item>
  37. <a-form-item label="UserKey">
  38. <a-input v-decorator="decorators.userKey" :placeholder="$t('k8s.text_334')" />
  39. </a-form-item>
  40. </template>
  41. </a-form>
  42. </div>
  43. </template>
  44. <script>
  45. import _ from 'lodash'
  46. import ClusterSelect from '@K8S/sections/ClusterSelect'
  47. import NamespaceSelect from '@K8S/sections/NamespaceSelect'
  48. import { SECRET_DEFAULT_TYPE } from '@K8S/constants'
  49. import k8sCreateMixin from '@K8S/mixins/create'
  50. import { REGEXP } from '@/utils/validate'
  51. const validateEmail = (rule, value, cb) => {
  52. if (!value) {
  53. cb()
  54. } else {
  55. const { regexp, message } = REGEXP.email
  56. if (regexp.test(value)) {
  57. cb()
  58. } else {
  59. cb(Error(message))
  60. }
  61. }
  62. }
  63. export default {
  64. name: 'K8sConfigmapCreate',
  65. components: {
  66. ClusterSelect,
  67. NamespaceSelect,
  68. },
  69. mixins: [k8sCreateMixin],
  70. data () {
  71. const type = _.get(this.$route, 'query.storageType') || 'keypair'
  72. return {
  73. form: {
  74. fc: this.$form.createForm(this),
  75. fd: {
  76. type,
  77. },
  78. },
  79. formItemLayout: {
  80. wrapperCol: {
  81. md: { span: 16 },
  82. xl: { span: 18 },
  83. xxl: { span: 20 },
  84. },
  85. labelCol: {
  86. md: { span: 8 },
  87. xl: { span: 6 },
  88. xxl: { span: 4 },
  89. },
  90. },
  91. decorators: {
  92. name: [
  93. 'name',
  94. {
  95. validateFirst: true,
  96. rules: [
  97. { required: true, message: this.$t('k8s.text_60') },
  98. { min: 2, max: 24, message: this.$t('k8s.text_132'), trigger: 'blur' },
  99. { validator: this.$validate('k8sName') },
  100. ],
  101. },
  102. ],
  103. cluster: [
  104. 'cluster',
  105. {
  106. initialValue: this.$store.state.common.k8s.cluster,
  107. rules: [
  108. { required: true, message: this.$t('k8s.text_30') },
  109. ],
  110. },
  111. ],
  112. namespace: [
  113. 'namespace',
  114. {
  115. initialValue: this.$store.state.common.k8s.namespace,
  116. rules: [
  117. { required: true, message: this.$t('k8s.text_61') },
  118. ],
  119. },
  120. ],
  121. server: [
  122. 'server',
  123. {
  124. rules: [
  125. { required: true, message: this.$t('k8s.text_53'), trigger: 'blur' },
  126. ],
  127. },
  128. ],
  129. user: [
  130. 'user',
  131. {
  132. rules: [
  133. { required: true, message: this.$t('k8s.text_55'), trigger: 'blur' },
  134. ],
  135. },
  136. ],
  137. password: [
  138. 'password',
  139. {
  140. rules: [
  141. { required: true, message: this.$t('k8s.text_57'), trigger: 'blur' },
  142. ],
  143. },
  144. ],
  145. userId: [
  146. 'userId',
  147. {
  148. rules: [
  149. { required: true, message: this.$t('k8s.text_335'), trigger: 'blur' },
  150. ],
  151. },
  152. ],
  153. userKey: [
  154. 'userKey',
  155. {
  156. rules: [
  157. { required: true, message: this.$t('k8s.text_336'), trigger: 'blur' },
  158. ],
  159. },
  160. ],
  161. email: [
  162. 'email',
  163. {
  164. rules: [
  165. { validator: validateEmail, trigger: 'blur' },
  166. ],
  167. },
  168. ],
  169. },
  170. namespaceObj: {},
  171. }
  172. },
  173. methods: {
  174. async _doCreate (data, resouce) {
  175. await new this.$Manager('secrets', 'v1').create({ data })
  176. },
  177. async doCreate () {
  178. try {
  179. const values = await this.form.fc.validateFields({ scroll: { alignWithTop: true, offsetTop: 100 } })
  180. const params = {
  181. name: values.name,
  182. cluster: values.cluster,
  183. namespace: values.namespace,
  184. type: SECRET_DEFAULT_TYPE,
  185. }
  186. if (this.form.fd.type === 'keypair') {
  187. params.dockerConfigJson = {
  188. email: values.email,
  189. password: values.password,
  190. user: values.user,
  191. server: values.server,
  192. }
  193. } else {
  194. params.type = 'yunion.io/ceph-csi'
  195. params.cephCSI = {}
  196. params.cephCSI.userId = values.userId
  197. params.cephCSI.userKey = values.userKey
  198. }
  199. await this._doCreate(params)
  200. this.$message.success(this.$t('k8s.text_46'))
  201. } catch (error) {
  202. throw error
  203. }
  204. },
  205. },
  206. }
  207. </script>