index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <div>
  3. <page-header :title="$t('common_628', [$t('dictionary.vpc_peer_connect')])" />
  4. <page-body needMarginBottom>
  5. <a-form :form="form.fc" v-bind="formItemLayout" hideRequiredMark>
  6. <a-form-item :label="$t('common.attribution_scope')">
  7. <domain-select v-decorator="decorators.project_domain" />
  8. </a-form-item>
  9. <a-form-item :label="$t('common.name')">
  10. <a-input v-decorator="decorators.name" :placeholder="$t('compute.text_210')" />
  11. </a-form-item>
  12. <a-form-item :label="$t('network.text_198')">
  13. <a-select v-decorator="decorators.brand" @change="handleBrandChange">
  14. <a-select-option v-for="item in VPC_PEER_BRANDS" :key="item.key">{{ item.label }}</a-select-option>
  15. </a-select>
  16. </a-form-item>
  17. <a-form-item :label="$t('network.request.vpc')">
  18. <a-row :gutter="6">
  19. <a-col :span="12">
  20. <a-form-item :wrapperCol="{ span: 24 }" class="mb-0 mr-1">
  21. <a-select
  22. v-decorator="decorators.request_cloudregion"
  23. @change="handleRequestRegionChange"
  24. :placeholder="$t('network.text_199')"
  25. :loading="regionLoading"
  26. :disabled="!regionLoaded">
  27. <a-select-option v-for="item in regionArr" :value="item.value" :key="item.value">
  28. <span class="text-color-secondary option-prefix">{{ $t('network.text_199') }}:</span>
  29. {{item.label}}
  30. </a-select-option>
  31. </a-select>
  32. </a-form-item>
  33. </a-col>
  34. <a-col :span="12">
  35. <a-form-item :wrapperCol="{ span: 24 }" class="mb-0 mr-1">
  36. <a-select
  37. v-decorator="decorators.request_vpc"
  38. placeholder="VPC"
  39. :loading="requestVpcLoading"
  40. :disabled="regionLoading || !requestVpcLoaded">
  41. <a-select-option v-for="item in requestVpcArr" :value="item.value" :key="item.value">
  42. <span class="text-color-secondary option-prefix">VPC:</span>
  43. {{item.label}}
  44. </a-select-option>
  45. </a-select>
  46. </a-form-item>
  47. </a-col>
  48. </a-row>
  49. </a-form-item>
  50. <a-form-item :label="$t('network.receive.vpc')">
  51. <a-row :gutter="6">
  52. <a-col :span="12">
  53. <a-form-item :wrapperCol="{ span: 24 }" class="mb-0 mr-1">
  54. <a-select
  55. v-decorator="decorators.receive_cloudregion"
  56. @change="handleReceiveRegionChange"
  57. :placeholder="$t('network.text_199')"
  58. :loading="regionLoading"
  59. :disabled="!regionLoaded">
  60. <a-select-option v-for="item in regionArr" :value="item.value" :key="item.value">
  61. <span class="text-color-secondary option-prefix">{{ $t('network.text_199') }}:</span>
  62. {{item.label}}
  63. </a-select-option>
  64. </a-select>
  65. </a-form-item>
  66. </a-col>
  67. <a-col :span="12">
  68. <a-form-item :wrapperCol="{ span: 24 }" class="mb-0 mr-1">
  69. <a-select
  70. v-decorator="decorators.receive_vpc"
  71. placeholder="VPC"
  72. :loading="receiveVpcLoading"
  73. :disabled="regionLoading || !receiveVpcLoaded">
  74. <a-select-option v-for="item in receiveVpcArr" :value="item.value" :key="item.value">
  75. <span class="text-color-secondary option-prefix">VPC:</span>
  76. {{item.label}}
  77. </a-select-option>
  78. </a-select>
  79. </a-form-item>
  80. </a-col>
  81. </a-row>
  82. </a-form-item>
  83. </a-form>
  84. </page-body>
  85. <page-footer>
  86. <div slot="right">
  87. <a-button class="mr-3" type="primary" @click="handleConfirm" :loading="loading">
  88. {{$t('compute.text_907')}}
  89. </a-button>
  90. <a-button @click="handleCancel">{{$t('compute.text_908')}}</a-button>
  91. </div>
  92. </page-footer>
  93. </div>
  94. </template>
  95. <script>
  96. import { uuid } from '@/utils/utils'
  97. import DomainSelect from '@/sections/DomainSelect'
  98. import { VPC_PEER_BRANDS } from '../constants'
  99. import { Manager } from '@/utils/manager'
  100. export default {
  101. name: 'NetworkVpcPeerConnectCreate',
  102. components: {
  103. DomainSelect,
  104. },
  105. data () {
  106. return {
  107. loading: false,
  108. regionLoading: false,
  109. regionLoaded: false,
  110. requestVpcLoading: false,
  111. requestVpcLoaded: false,
  112. receiveVpcLoading: false,
  113. receiveVpcLoaded: false,
  114. regionArr: [],
  115. requestVpcArr: [],
  116. receiveVpcArr: [],
  117. VPC_PEER_BRANDS,
  118. form: {
  119. fc: this.$form.createForm(this),
  120. },
  121. decorators: {
  122. project_domain: [
  123. 'project_domain',
  124. {
  125. initialValue: this.$store.getters.userInfo.projectDomainId,
  126. },
  127. ],
  128. name: [
  129. 'name',
  130. {
  131. rules: [
  132. { required: true, message: this.$t('compute.text_210') },
  133. ],
  134. },
  135. ],
  136. brand: [
  137. 'brand',
  138. {
  139. initialValue: VPC_PEER_BRANDS[0].key,
  140. },
  141. ],
  142. request_cloudregion: [
  143. 'request_cloudregion',
  144. {
  145. rules: [
  146. { required: true, message: this.$t('network.text_286') },
  147. ],
  148. },
  149. ],
  150. request_vpc: [
  151. 'request_vpc',
  152. {
  153. rules: [
  154. { required: true, message: this.$t('common_226') },
  155. ],
  156. },
  157. ],
  158. receive_cloudregion: [
  159. 'receive_cloudregion',
  160. {
  161. rules: [
  162. { required: true, message: this.$t('network.text_286') },
  163. ],
  164. },
  165. ],
  166. receive_vpc: [
  167. 'receive_vpc',
  168. {
  169. rules: [
  170. { required: true, message: this.$t('common_226') },
  171. ],
  172. },
  173. ],
  174. },
  175. formItemLayout: {
  176. wrapperCol: {
  177. span: 20,
  178. },
  179. labelCol: {
  180. span: 4,
  181. },
  182. },
  183. }
  184. },
  185. created () {
  186. this.fetchRegions()
  187. },
  188. methods: {
  189. async fetchRegions (brand = VPC_PEER_BRANDS[0].key) {
  190. try {
  191. this.regionLoading = true
  192. this.regionLoaded = false
  193. const manager = new this.$Manager('cloudregions')
  194. const res = await manager.list({
  195. params: {
  196. scope: this.$store.getters.scope,
  197. brand,
  198. },
  199. })
  200. this.regionArr = res.data.data.map((item) => {
  201. return {
  202. label: this._$t(item),
  203. value: item.id,
  204. }
  205. })
  206. if (this.regionArr.length) {
  207. const defaultValue = this.regionArr[0].value
  208. this.form.fc.setFieldsValue({
  209. [this.decorators.request_cloudregion[0]]: defaultValue,
  210. [this.decorators.receive_cloudregion[0]]: defaultValue,
  211. })
  212. this.fetchRequestVpcs(defaultValue)
  213. this.fetchReceiveVpcs(defaultValue)
  214. }
  215. this.regionLoading = false
  216. this.regionLoaded = true
  217. } catch (error) {
  218. this.regionLoading = false
  219. throw error
  220. }
  221. },
  222. async fetchRequestVpcs (cloudregion) {
  223. try {
  224. this.requestVpcLoading = true
  225. this.requestVpcLoaded = false
  226. const manager = new this.$Manager('vpcs')
  227. const res = await manager.list({
  228. params: {
  229. $t: uuid(),
  230. scope: this.$store.getters.scope,
  231. cloudregion_id: cloudregion,
  232. },
  233. })
  234. this.requestVpcArr = res.data.data.map((item) => {
  235. return {
  236. label: item.name,
  237. value: item.id,
  238. }
  239. })
  240. if (this.requestVpcArr.length) {
  241. const defaultValue = this.requestVpcArr[0].value
  242. this.form.fc.setFieldsValue({
  243. [this.decorators.request_vpc[0]]: defaultValue,
  244. })
  245. }
  246. this.requestVpcLoading = false
  247. this.requestVpcLoaded = true
  248. } catch (error) {
  249. this.requestVpcLoading = false
  250. }
  251. },
  252. async fetchReceiveVpcs (cloudregion) {
  253. try {
  254. this.receiveVpcLoading = true
  255. this.receiveVpcLoaded = false
  256. const manager = new this.$Manager('vpcs')
  257. const res = await manager.list({
  258. params: {
  259. $t: uuid(),
  260. scope: this.$store.getters.scope,
  261. cloudregion_id: cloudregion,
  262. },
  263. })
  264. this.receiveVpcArr = res.data.data.map((item) => {
  265. return {
  266. label: item.name,
  267. value: item.id,
  268. }
  269. })
  270. if (this.receiveVpcArr.length) {
  271. const defaultValue = this.receiveVpcArr[0].value
  272. this.form.fc.setFieldsValue({
  273. [this.decorators.receive_vpc[0]]: defaultValue,
  274. })
  275. }
  276. this.receiveVpcLoading = false
  277. this.receiveVpcLoaded = true
  278. } catch (error) {
  279. this.receiveVpcLoading = false
  280. }
  281. },
  282. handleRequestRegionChange (v) {
  283. this.fetchRequestVpcs(v)
  284. },
  285. handleReceiveRegionChange (v) {
  286. this.fetchReceiveVpcs(v)
  287. },
  288. handleBrandChange (v) {
  289. this.resetVpc()
  290. this.fetchRegions(v)
  291. },
  292. resetVpc () {
  293. this.form.fc.setFieldsValue({
  294. [this.decorators.request_cloudregion[0]]: undefined,
  295. [this.decorators.receive_cloudregion[0]]: undefined,
  296. [this.decorators.request_vpc[0]]: undefined,
  297. [this.decorators.receive_vpc[0]]: undefined,
  298. })
  299. this.regionArr = []
  300. this.requestVpcArr = []
  301. this.receiveVpcArr = []
  302. },
  303. async doSubmit (data) {
  304. const params = {
  305. generate_name: data.name,
  306. vpc_id: data.request_vpc,
  307. peer_vpc_id: data.receive_vpc,
  308. }
  309. const manager = new Manager('vpc_peering_connections')
  310. return manager.create({
  311. data: params,
  312. })
  313. },
  314. async handleConfirm () {
  315. this.loading = true
  316. try {
  317. const values = await this.form.fc.validateFields()
  318. await this.doSubmit(values)
  319. this.loading = false
  320. this.$message.success(this.$t('common.success'))
  321. this.goBack()
  322. } catch (error) {
  323. this.loading = false
  324. }
  325. },
  326. handleCancel () {
  327. this.goBack()
  328. },
  329. goBack () {
  330. this.$router.push({
  331. name: 'VpcPeerConnect',
  332. })
  333. },
  334. },
  335. }
  336. </script>
  337. <style>
  338. </style>