index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="port-mapping">
  3. <a-form-item class="mb-0" :extra="(isImportCluster && form.fc.getFieldValue(decorators.serviceType[0]) === 'external') ? $t('k8s.text_91') : ''">
  4. <a-radio-group v-decorator="decorators.serviceType">
  5. <a-radio-button v-if="!ignoreNone" value="none">{{$t('k8s.text_75')}}</a-radio-button>
  6. <a-radio-button value="internal">{{$t('k8s.text_92')}}</a-radio-button>
  7. <a-radio-button value="external">{{$t('k8s.text_93')}}</a-radio-button>
  8. <a-radio-button value="nodePort">{{$t('k8s.node_port')}}</a-radio-button>
  9. </a-radio-group>
  10. </a-form-item>
  11. <lb-cluster
  12. v-if="!isImportCluster && form.fc.getFieldValue(decorators.serviceType[0]) === 'external' && isAdminMode"
  13. :decorator="decorators.loadBalancerCluster" />
  14. <lb-network
  15. v-if="!isImportCluster && form.fc.getFieldValue(decorators.serviceType[0]) === 'external'"
  16. :decorator="decorators.loadBalancerNetwork"
  17. :addr-decorator="decorators.loadBalancerAddress"
  18. :vpc-id="vpcId" />
  19. <div class="mt-3" v-if="form.fc.getFieldValue(decorators.serviceType[0]) !== 'none'">
  20. <div class="d-flex" v-for="(item, i) in portList" :key="item.key">
  21. <port :decorators="getDecorators(item)" :protocolDisabled="getProtocolDisabled(i)" :serviceType="getServiceType()" @protocolChange="protocolChange" />
  22. <a-button v-if="portList.length > 1" type="danger" shape="circle" icon="delete" @click="del(item)" class="mt-1 ml-2" />
  23. </div>
  24. <a-button type="primary" icon="plus" @click="add">{{$t('k8s.text_94')}}</a-button>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import * as R from 'ramda'
  30. import { mapGetters } from 'vuex'
  31. import Port from './Port'
  32. import LbCluster from './LbCluster'
  33. import LbNetwork from './LbNetwork'
  34. import { uuid } from '@/utils/utils'
  35. export default {
  36. name: 'K8SPortMapping',
  37. components: {
  38. Port,
  39. LbCluster,
  40. LbNetwork,
  41. },
  42. props: {
  43. decorators: {
  44. type: Object,
  45. validator: val => R.is(Array, val.serviceType) && R.is(Object, val.ports) && R.is(Array, val.loadBalancerNetwork) && R.is(Array, val.loadBalancerCluster),
  46. },
  47. form: {
  48. type: Object,
  49. required: true,
  50. validator: val => val.fc,
  51. },
  52. clusterObj: {
  53. type: Object,
  54. required: true,
  55. },
  56. ignoreNone: {
  57. type: Boolean,
  58. default: false,
  59. },
  60. },
  61. data () {
  62. return {
  63. portList: [
  64. { key: uuid() },
  65. ],
  66. currentProtocol: 'TCP',
  67. }
  68. },
  69. computed: {
  70. ...mapGetters(['isAdminMode']),
  71. isImportCluster () {
  72. if (this.clusterObj.mode === 'import') { // 导入的集群新建外部服务时不能选择网络
  73. return true
  74. }
  75. return false
  76. },
  77. vpcId () {
  78. return this.clusterObj.vpc_id
  79. },
  80. },
  81. methods: {
  82. add () {
  83. const key = uuid()
  84. this.portList.push({ key })
  85. this.$nextTick(() => {
  86. this.form.fc.setFieldsValue({
  87. [`protocols[${key}]`]: this.currentProtocol,
  88. })
  89. })
  90. },
  91. del (item) {
  92. const index = this.portList.findIndex(val => val.key === item.key)
  93. this.portList.splice(index, 1)
  94. },
  95. getDecorators (val) {
  96. const ret = {}
  97. R.forEachObjIndexed((item, key) => {
  98. ret[key] = item(val.key)
  99. }, this.decorators.ports)
  100. return ret
  101. },
  102. getProtocolDisabled (i) {
  103. if (i > 0 && this.form.fc.getFieldValue(this.decorators.serviceType[0]) === 'external') {
  104. return true
  105. }
  106. return false
  107. },
  108. getServiceType () {
  109. return this.form.fc.getFieldValue(this.decorators.serviceType[0])
  110. },
  111. protocolChange (val) {
  112. this.currentProtocol = val
  113. const value = {}
  114. this.portList.forEach(v => {
  115. value[`protocols[${v.key}]`] = val
  116. })
  117. this.form.fc.setFieldsValue(value)
  118. },
  119. },
  120. }
  121. </script>