index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div class="k8s-deployment-create 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_373')">
  8. <base-select
  9. resource="federatedclusterroles"
  10. version="v1"
  11. :params="params"
  12. need-params
  13. idKey="name"
  14. :select-props="{ placeholder: $t('common.select') }"
  15. v-decorator="decorators.role" />
  16. </a-form-item>
  17. <a-form-item label="Subject">
  18. <a-radio-group v-decorator="decorators.subjectType" @change="subjectTypeChange">
  19. <a-radio-button v-for="item in subjectTypeOpts" :value="item.key" :key="item.key">{{ item.label }}</a-radio-button>
  20. </a-radio-group>
  21. </a-form-item>
  22. <a-form-item label="Subject Name">
  23. <base-select
  24. v-if="subjectOpts.length"
  25. v-decorator="decorators.subject"
  26. :options="subjectOpts"
  27. version="v1"
  28. idKey="name"
  29. :select-props="{ placeholder: $t('common.select') }" />
  30. <a-input v-else v-decorator="decorators.subject" :placeholder="$t('common.placeholder')" />
  31. </a-form-item>
  32. </a-form>
  33. </div>
  34. </template>
  35. <script>
  36. import k8sCreateMixin from '@K8S/mixins/create'
  37. const API_GROUP = 'rbac.authorization.k8s.io'
  38. export default {
  39. name: 'K8sFederatedClusterRolebindingFormCreate',
  40. components: {
  41. },
  42. mixins: [k8sCreateMixin],
  43. data () {
  44. return {
  45. form: {
  46. fc: this.$form.createForm(this),
  47. },
  48. formItemLayout: {
  49. labelCol: { span: 4 },
  50. wrapperCol: { span: 20 },
  51. },
  52. decorators: {
  53. name: [
  54. 'name',
  55. {
  56. validateFirst: true,
  57. rules: [
  58. { required: true, message: this.$t('k8s.text_60') },
  59. { min: 2, max: 24, message: this.$t('k8s.text_132'), trigger: 'blur' },
  60. { validator: this.$validate('k8sName') },
  61. ],
  62. },
  63. ],
  64. cluster: [
  65. 'cluster',
  66. {
  67. initialValue: this.$store.state.common.k8s.cluster,
  68. rules: [
  69. { required: true, message: this.$t('k8s.text_30'), trigger: 'blur' },
  70. ],
  71. },
  72. ],
  73. role: [
  74. 'role',
  75. {
  76. rules: [
  77. { required: true, message: this.$t('common.select'), trigger: 'blur' },
  78. ],
  79. },
  80. ],
  81. subjectType: [
  82. 'subjectType',
  83. {
  84. initialValue: 'User',
  85. },
  86. ],
  87. subject: [
  88. 'subject',
  89. {
  90. rules: [
  91. { required: true, message: this.$t('common.select'), trigger: 'blur' },
  92. ],
  93. },
  94. ],
  95. },
  96. roleRefOpts: [
  97. { key: 'Role', label: this.$t('k8s.text_370') },
  98. { key: 'ClusterRole', label: this.$t('k8s.text_373') },
  99. ],
  100. subjectTypeOpts: [
  101. { key: 'User', label: 'User' },
  102. { key: 'Group', label: 'Group' },
  103. ],
  104. subjectOpts: [],
  105. subjectType: 'User',
  106. params: {
  107. limit: 0,
  108. scope: this.$store.getters.scope,
  109. },
  110. }
  111. },
  112. created () {
  113. this.getSubjectOpts()
  114. },
  115. methods: {
  116. subjectTypeChange (e) {
  117. this.subjectType = e.target.value
  118. this.getSubjectOpts()
  119. this.form.fc.setFieldsValue({
  120. [this.decorators.subject[0]]: undefined,
  121. })
  122. },
  123. async getSubjectOpts () {
  124. try {
  125. let spec = ''
  126. if (this.subjectType === 'User') spec = 'cluster-users'
  127. if (this.subjectType === 'Group') spec = 'cluster-user-groups'
  128. const { data } = await new this.$Manager('federatedclusterroles', 'v1').get({ id: spec, params: { scope: this.$store.getters.scope } })
  129. this.subjectOpts = data
  130. } catch (error) {
  131. throw error
  132. }
  133. },
  134. async validateForm () {
  135. try {
  136. const values = await this.form.fc.validateFields()
  137. const subjects = [{
  138. kind: values.subjectType,
  139. name: values.subject,
  140. apiGroup: API_GROUP,
  141. }]
  142. const data = {
  143. name: values.name,
  144. spec: {
  145. template: {
  146. roleRef: {
  147. kind: 'ClusterRole',
  148. name: values.role,
  149. apiGroup: API_GROUP,
  150. },
  151. subjects,
  152. },
  153. },
  154. }
  155. return data
  156. } catch (error) {
  157. throw error
  158. }
  159. },
  160. async doCreate () {
  161. try {
  162. const values = await this.validateForm()
  163. this.$emit('submit', values)
  164. } catch (error) {
  165. throw error
  166. }
  167. },
  168. },
  169. }
  170. </script>