ItemVpcOpts.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <a-row :gutter="8">
  3. <a-col :span="12">
  4. <a-form-item>
  5. <a-select v-decorator="decorators.vpc" :loading="vpcLoading" :placeholder="$t('db.text_261')" showSearch :filterOption="filterOption" @change="(vpc)=>fetchQueryNetworks(vpc, true)">
  6. <a-select-option :key="item.id" :value="item.id" v-for="item in vpcOptions">
  7. <div class="d-flex">
  8. <span class="text-truncate flex-fill mr-2" :title="item.name">{{ item.name }}</span>
  9. <span style="color: #8492a6; font-size: 13px">{{$t('db.text_268', [ item.manager ])}}</span>
  10. </div>
  11. </a-select-option>
  12. </a-select>
  13. </a-form-item>
  14. </a-col>
  15. <a-col :span="12">
  16. <a-form-item>
  17. <a-select v-decorator="decorators.network" :loading="networkLoading" :placeholder="$t('db.text_269')" showSearch :filterOption="filterOption" @change="handleNetworkChange">
  18. <a-select-option :key="item.id" :value="item.id" v-for="item in networkOptions">
  19. <div class="d-flex">
  20. <span class="text-truncate flex-fill mr-2" :title="item.name">{{ item.name }} ({{item.guest_ip_start}} - {{item.guest_ip_end}})</span>
  21. <span style="color: #8492a6; font-size: 13px">{{$t('db.text_270', [ item.ports - item.ports_used ])}}</span>
  22. </div>
  23. </a-select-option>
  24. </a-select>
  25. </a-form-item>
  26. </a-col>
  27. </a-row>
  28. </template>
  29. <script>
  30. import { Manager } from '@/utils/manager'
  31. export default {
  32. inject: ['form', 'scopeParams'],
  33. props: {
  34. decorators: {
  35. type: Object,
  36. default: () => {
  37. return []
  38. },
  39. },
  40. },
  41. data () {
  42. return {
  43. vpcLoading: false,
  44. networkLoading: false,
  45. vpcOptions: [],
  46. networkOptions: [],
  47. cloudregion: undefined,
  48. }
  49. },
  50. computed: {
  51. FC () {
  52. if (this.form && this.form.fc) {
  53. return this.form.fc
  54. }
  55. return {}
  56. },
  57. getFieldValue () {
  58. if (this.FC && this.FC.getFieldValue) {
  59. return this.FC.getFieldValue
  60. }
  61. return () => null
  62. },
  63. },
  64. created () {
  65. this.vpcManager = new Manager('vpcs', 'v2')
  66. this.netWrokManager = new Manager('networks', 'v2')
  67. },
  68. methods: {
  69. filterOption (input, option) {
  70. return (
  71. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  72. )
  73. },
  74. handleNetworkChange (networkId) {
  75. if (this.networkOptions && this.networkOptions.length > 0) {
  76. for (let i = 0; i < this.networkOptions.length; i++) {
  77. const { _id, manager } = this.networkOptions[i]
  78. if (networkId === _id) {
  79. this.FC.setFieldsValue({
  80. manager: manager,
  81. })
  82. break
  83. }
  84. }
  85. }
  86. },
  87. async fetchQueryVpcs (isUpdate) {
  88. const sku = this.getFieldValue('sku')
  89. const params = {
  90. limit: 0,
  91. usable: true,
  92. cloudregion_id: this.getFieldValue('region'),
  93. ...this.scopeParams,
  94. }
  95. if (sku) {
  96. if ((sku.cloudregion_id === this.cloudregion) && !isUpdate) {
  97. return false
  98. }
  99. params.cloudregion_id = sku.cloudregion_id
  100. params.provider = sku.provider
  101. this.cloudregion = sku.cloudregion_id
  102. }
  103. this.FC.setFieldsValue({
  104. vpc: undefined,
  105. network: undefined,
  106. })
  107. try {
  108. this.vpcLoading = true
  109. const { data = {} } = await this.vpcManager.list({ params })
  110. this.vpcOptions = data.data || []
  111. if (this.vpcOptions && this.vpcOptions.length > 0) {
  112. this.FC.setFieldsValue({
  113. vpc: this.vpcOptions[0].id,
  114. })
  115. }
  116. this.fetchQueryNetworks()
  117. this.vpcLoading = false
  118. } catch (err) {
  119. this.vpcLoading = false
  120. throw err
  121. }
  122. },
  123. async fetchQueryNetworks (vpc, update) {
  124. const params = {
  125. limit: 0,
  126. usable: true,
  127. vpc: vpc || this.getFieldValue('vpc'),
  128. ...this.scopeParams,
  129. }
  130. const sku = this.getFieldValue('sku')
  131. if (sku) {
  132. params.zone = sku.zone_id
  133. }
  134. this.FC.setFieldsValue({
  135. network: undefined,
  136. })
  137. try {
  138. this.networkLoading = true
  139. const { data = {} } = await this.netWrokManager.list({ params })
  140. this.networkOptions = data.data || []
  141. if (this.networkOptions && this.networkOptions.length > 0) {
  142. const { id, manager } = this.networkOptions[0]
  143. this.FC.setFieldsValue({
  144. network: id,
  145. manager: manager,
  146. })
  147. }
  148. this.networkLoading = false
  149. } catch (err) {
  150. this.networkLoading = false
  151. throw err
  152. }
  153. },
  154. },
  155. }
  156. </script>