mixin.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import { mapGetters } from 'vuex'
  2. import * as R from 'ramda'
  3. import _ from 'lodash'
  4. import DomainProject from '@/sections/DomainProject'
  5. import { isRequired } from '@/utils/validate'
  6. import CloudproviderRegionZone from '@/sections/CloudproviderRegionZone'
  7. import i18n from '@/locales'
  8. import Tag from '@/sections/Tag'
  9. export default {
  10. components: {
  11. DomainProject,
  12. CloudproviderRegionZone,
  13. Tag,
  14. },
  15. props: {
  16. type: String,
  17. },
  18. data () {
  19. return {
  20. domain: {},
  21. project: {},
  22. vpcObj: {},
  23. networkObj: {},
  24. form: {
  25. fc: this.$form.createForm(this, { onValuesChange: this.onValuesChange }),
  26. fd: {
  27. address_type: 'internet',
  28. charge_type: 'traffic',
  29. bandwidth: 1,
  30. ip: 'ipv4',
  31. },
  32. },
  33. formItemLayout: {
  34. wrapperCol: {
  35. span: 20,
  36. },
  37. labelCol: {
  38. span: 4,
  39. },
  40. },
  41. decorators: {
  42. domain: [
  43. 'domain',
  44. {
  45. initialValue: this.$store.getters.userInfo.projectDomainId,
  46. rules: [
  47. { required: true, message: this.$t('rules.domain'), trigger: 'change' },
  48. ],
  49. },
  50. ],
  51. project: [
  52. 'project',
  53. {
  54. initialValue: this.$store.getters.userInfo.projectId,
  55. rules: [
  56. { required: true, message: this.$t('rules.project'), trigger: 'change' },
  57. ],
  58. },
  59. ],
  60. name: [
  61. 'name',
  62. {
  63. validateFirst: true,
  64. rules: [
  65. { required: true, message: i18n.t('network.text_116') },
  66. { validator: this.$validate('resourceName') },
  67. ],
  68. },
  69. ],
  70. description: ['description'],
  71. cluster_id: [
  72. 'cluster_id',
  73. {
  74. rules: [
  75. { required: true, message: i18n.t('network.text_79'), trigger: 'change' },
  76. ],
  77. },
  78. ],
  79. vpc: [
  80. 'vpc',
  81. {
  82. rules: [
  83. { required: true, message: i18n.t('network.text_274'), trigger: 'change' },
  84. ],
  85. },
  86. ],
  87. network: [
  88. 'network',
  89. {
  90. rules: [
  91. { required: true, message: i18n.t('network.text_284'), trigger: 'change' },
  92. ],
  93. },
  94. ],
  95. manager: [
  96. 'manager',
  97. {
  98. // initialValue: { key: '', label: '' },
  99. rules: [
  100. { required: true, message: i18n.t('network.text_285') },
  101. ],
  102. },
  103. ],
  104. cloudregion: [
  105. 'cloudregion',
  106. {
  107. initialValue: { key: '', label: '' },
  108. rules: [
  109. { validator: isRequired(), message: i18n.t('network.text_286') },
  110. ],
  111. },
  112. ],
  113. zone: [
  114. 'zone',
  115. {
  116. initialValue: { key: '', label: '' },
  117. rules: [
  118. { validator: isRequired(), message: i18n.t('scope.text_65') },
  119. ],
  120. },
  121. ],
  122. loadbalancer_spec: [
  123. 'loadbalancer_spec',
  124. {
  125. rules: [
  126. { required: true, message: i18n.t('network.text_269') },
  127. ],
  128. },
  129. ],
  130. instance_type: [
  131. 'instance_type',
  132. {
  133. initialValue: 'application',
  134. rules: [
  135. { required: true, message: i18n.t('network.text_269') },
  136. ],
  137. },
  138. ],
  139. address_type: [
  140. 'address_type',
  141. {
  142. initialValue: 'internet',
  143. },
  144. ],
  145. chargeType: [
  146. 'charge_type',
  147. {
  148. initialValue: 'traffic',
  149. },
  150. ],
  151. bandwidth: [
  152. 'bandwidth',
  153. {
  154. initialValue: 1,
  155. rules: [
  156. { required: true, message: i18n.t('network.text_288'), trigger: 'change', type: 'number' },
  157. ],
  158. },
  159. ],
  160. ip: [
  161. 'ip',
  162. {
  163. initialValue: 'ipv4',
  164. },
  165. ],
  166. eip: {
  167. type: [
  168. 'eip_type',
  169. {
  170. initialValue: 'none',
  171. },
  172. ],
  173. charge_type: [
  174. 'eip_charge_type',
  175. ],
  176. bgp_type: [
  177. 'eip_bgp_type',
  178. {
  179. initialValue: '',
  180. },
  181. ],
  182. bandwidth: [
  183. 'eip_bw',
  184. {
  185. initialValue: 30,
  186. },
  187. ],
  188. eip: [
  189. 'eip',
  190. {
  191. rules: [
  192. { required: true, message: i18n.t('network.text_278') },
  193. ],
  194. },
  195. ],
  196. },
  197. __meta__: [
  198. '__meta__',
  199. {
  200. rules: [
  201. { validator: this.$validate('tagName') },
  202. ],
  203. },
  204. ],
  205. },
  206. }
  207. },
  208. provide () {
  209. return {
  210. form: this.form,
  211. }
  212. },
  213. computed: {
  214. ...mapGetters(['isAdminMode', 'scope', 'isDomainMode', 'userInfo', 'l3PermissionEnable']),
  215. project () {
  216. return this.form.fd.project ? this.form.fd.project : this.$store.getters.userInfo.projectId
  217. },
  218. provider () {
  219. if (this.form.fd.provider) {
  220. return this.form.fd.provider.toLocaleLowerCase()
  221. }
  222. return null
  223. },
  224. isAliyun () {
  225. return this.provider === 'aliyun'
  226. },
  227. isAws () {
  228. return this.provider === 'aws'
  229. },
  230. isHuawei () {
  231. return this.provider === 'huawei'
  232. },
  233. isQcloud () {
  234. return this.provider === 'qcloud'
  235. },
  236. isShowCloudprovider () {
  237. const { address_type } = this.form.fd
  238. if (this.isAliyun) {
  239. return address_type === 'internet'
  240. }
  241. return false
  242. },
  243. scopeParams () {
  244. const params = {}
  245. if (this.isAdminMode) {
  246. if (!this.l3PermissionEnable) {
  247. params.project_domain = 'default'
  248. }
  249. if (R.is(String, this.domain)) {
  250. params.project_domain = this.domain
  251. }
  252. } else {
  253. params.scope = this.scope
  254. }
  255. return params
  256. },
  257. vpcParams () {
  258. const { cloudregion } = this.form.fd
  259. if (cloudregion && !R.isEmpty(this.scopeParams)) {
  260. const params = {
  261. ...this.scopeParams,
  262. cloudregion: _.isObject(cloudregion) ? cloudregion.key : cloudregion,
  263. }
  264. return params
  265. }
  266. return {}
  267. },
  268. networkParams () {
  269. let params = {}
  270. if (this.form.fd.vpc && !R.isEmpty(this.scopeParams)) {
  271. params = { ...this.scopeParams, usable: true }
  272. params.limit = 0
  273. params.vpc = this.form.fd.vpc
  274. if (this.form.fd.zone) params.zone = this.form.fd.zone
  275. }
  276. return params
  277. },
  278. cloudregionParams () {
  279. const params = {
  280. limit: 0,
  281. capability: undefined,
  282. cloud_env: this.type,
  283. ...this.scopeParams,
  284. }
  285. if (this.type === 'private') {
  286. params['filter.0'] = 'provider.in(OpenStack,HCSO,HCS)'
  287. } else {
  288. delete params.provider
  289. }
  290. return params
  291. },
  292. cloudproviderParams () {
  293. if (R.isEmpty(this.scopeParams)) return {}
  294. const { provider } = this.form.fd
  295. const params = {
  296. limit: 0,
  297. enabled: true,
  298. provider,
  299. status: 'connected',
  300. cloudregion: this.form.fd.cloudregion,
  301. ...this.scopeParams,
  302. }
  303. return params
  304. },
  305. zoneParams () {
  306. const params = {
  307. usable: true,
  308. show_emulated: true,
  309. order_by: 'created_at',
  310. order: 'asc',
  311. limit: 0,
  312. ...this.scopeParams,
  313. }
  314. return params
  315. },
  316. cityParams () {
  317. const params = {
  318. ...this.scopeParams,
  319. }
  320. if (this.type === 'public') params.public_cloud = true
  321. return params
  322. },
  323. areaParams () {
  324. return {
  325. cityParams: this.cityParams,
  326. providerParams: this.scopeParams,
  327. cloudregionParams: this.cloudregionParams,
  328. zoneParams: this.zoneParams,
  329. isRequired: true,
  330. ...this.formItemLayout,
  331. event: {
  332. providerFetchSuccess: (list) => {
  333. return list
  334. },
  335. },
  336. }
  337. },
  338. },
  339. methods: {
  340. vpcLabelFormat (item) {
  341. if (!item.cidr_block) return item.name
  342. return `${item.name}(${item.account ? item.account + ', ' : ''}${item.cidr_block})`
  343. },
  344. vpcFormatter (v) {
  345. return {
  346. key: v.id,
  347. label: this.vpcLabelFormat(v),
  348. rightLabel: v.manager ? this.$t('network.manager', [v.manager]) : '',
  349. disabled: v.network_count === 0,
  350. ...v,
  351. }
  352. },
  353. handleVpcChange (data) {
  354. this.vpcObj = data[0] ? data[0] : {}
  355. },
  356. validateIp () {
  357. const remainIps = this.networkObj.ports - this.networkObj.ports_used
  358. if (remainIps && remainIps < 8) {
  359. this.$message.warning(i18n.t('network.text_276'))
  360. return false
  361. }
  362. return true
  363. },
  364. onValuesChange (props, values) {
  365. R.forEachObjIndexed((value, key) => {
  366. this.$set(this.form.fd, key, value)
  367. }, values)
  368. },
  369. },
  370. }