Public.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <template>
  2. <div>
  3. <a-form
  4. class="mt-3"
  5. v-bind="formItemLayout"
  6. :form="form.fc"
  7. hideRequiredMark>
  8. <a-form-item :label="$t('storage.text_55', [$t('dictionary.project')])" v-bind="formItemLayout">
  9. <domain-project :fc="form.fc" :decorators="{ project: decorators.project, domain: decorators.domain }" @update:domain="handleDomainChange" />
  10. </a-form-item>
  11. <a-form-item :label="$t('storage.text_40')">
  12. <a-input v-decorator="decorators.name" />
  13. <template v-slot:extra>
  14. <name-repeated res="file_systems" :name="form.fd.name" :default-text="$t('compute.text_893')" />
  15. </template>
  16. </a-form-item>
  17. <a-form-item :label="$t('common.description')">
  18. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  19. </a-form-item>
  20. <!-- 计费方式 -->
  21. <clearing-radios v-bind="formItemLayout" :auto_renew="false" />
  22. <a-form-item :label="$t('network.expired_release')" v-if="form.fd.billing_type !== 'prepaid'">
  23. <duration :decorators="decorators.duration" :form="form" />
  24. </a-form-item>
  25. <area-selects
  26. class="mb-0"
  27. ref="areaSelects"
  28. :wrapperCol="formItemLayout.wrapperCol"
  29. :labelCol="formItemLayout.labelCol"
  30. :names="areaselectsName"
  31. :providerParams="providerParams"
  32. :cloudregionParams="regionParams"
  33. :zoneParams="zoneParams"
  34. :defaultActiveFirstOption="['provider', 'cloudregion']"
  35. filterBrandResource="compute_engine"
  36. @change="handleRegionChange" />
  37. <a-form-item :label="$t('compute.text_15')" required v-bind="formItemLayout">
  38. <base-select
  39. class="w-50"
  40. v-decorator="decorators.cloudprovider"
  41. resource="cloudproviders"
  42. :params="cloudproviderParams"
  43. :mapper="providerMapper"
  44. :isDefaultSelect="true"
  45. :showSync="true"
  46. :select-props="{ placeholder: $t('common.tips.select', [$t('compute.text_653')]) }" />
  47. </a-form-item>
  48. <!-- 套餐 -->
  49. <file-system-sku
  50. @update:options="skuChanged"
  51. ref="REF_SKU" />
  52. <a-form-item :label="$t('storage.capacity')" v-bind="formItemLayout" v-if="isShowCapacity">
  53. <a-col :span="12">
  54. <a-tooltip>
  55. <template slot="title" v-if="capacityTooltip">
  56. {{ capacityTooltip }}
  57. </template>
  58. <a-input-number v-model="capacity" v-bind="skuOptions" /> GB
  59. </a-tooltip>
  60. </a-col>
  61. </a-form-item>
  62. <network-selects
  63. ref="REF_NETWORK"
  64. :label="$t('network.text_16')"
  65. :isDefaultFetch="false"
  66. :vpcFormat="vpcFormat"
  67. :vpcParams="vpcParams"
  68. :networkParams="netParams"
  69. v-bind="formItemLayout"
  70. class="mb-0" />
  71. <a-form-item :label="$t('compute.text_1154')" class="mb-0">
  72. <tag
  73. v-decorator="decorators.tag" :allowNoValue="false" />
  74. </a-form-item>
  75. </a-form>
  76. <bottom-bar
  77. :values="form.fc.getFieldsValue()"
  78. :cloudAccountId="cloudAccountId"
  79. @submit="handleConfirm"
  80. @cancel="handleCancel" />
  81. </div>
  82. </template>
  83. <script>
  84. import * as R from 'ramda'
  85. import DomainProject from '@/sections/DomainProject'
  86. import Duration from '@Compute/sections/Duration'
  87. import NetworkSelects from '@/sections/NetworkSelects'
  88. import validateForm, { isRequired } from '@/utils/validate'
  89. import Tag from '@/sections/Tag'
  90. import AreaSelects from '@/sections/AreaSelects'
  91. import { getCloudEnvOptions } from '@/utils/common/hypervisor'
  92. import i18n from '@/locales'
  93. import BottomBar from '../components/BottomBar'
  94. import FileSystemSku from '../components/SKU'
  95. import fsCreateMixin from './mixin'
  96. function validateTag (rule, value, callback) {
  97. if (R.is(Object, value) && Object.keys(value).length > 20) {
  98. return callback(new Error(i18n.t('compute.text_209')))
  99. }
  100. callback()
  101. }
  102. export default {
  103. name: 'FileSystemPublicCreate',
  104. components: {
  105. DomainProject,
  106. Duration,
  107. AreaSelects,
  108. FileSystemSku,
  109. NetworkSelects,
  110. BottomBar,
  111. Tag,
  112. },
  113. mixins: [fsCreateMixin],
  114. data () {
  115. const cloudEnvOptions = getCloudEnvOptions('object_storage_brands', true)
  116. const decorators = {
  117. domain: [
  118. 'domain',
  119. {
  120. rules: [
  121. { validator: isRequired(), message: this.$t('rules.domain'), trigger: 'change' },
  122. ],
  123. },
  124. ],
  125. project: [
  126. 'project',
  127. {
  128. rules: [
  129. { validator: isRequired(), message: this.$t('rules.project'), trigger: 'change' },
  130. ],
  131. },
  132. ],
  133. name: [
  134. 'name',
  135. {
  136. validateFirst: true,
  137. rules: [
  138. { required: true, message: this.$t('network.text_218') },
  139. { validator: validateForm('serverName') },
  140. ],
  141. },
  142. ],
  143. description: ['description'],
  144. cloudprovider: [
  145. 'cloudprovider',
  146. {
  147. rules: [
  148. { required: true, message: this.$t('common.tips.select', [this.$t('compute.text_653')]) },
  149. ],
  150. },
  151. ],
  152. zone_id: [
  153. 'zone_id',
  154. {
  155. rules: [
  156. { required: false },
  157. ],
  158. },
  159. ],
  160. duration: {
  161. durationStandard: [
  162. 'durationStandard',
  163. {
  164. initialValue: 'none',
  165. },
  166. ],
  167. duration: [
  168. 'duration',
  169. {
  170. initialValue: '1h',
  171. },
  172. ],
  173. },
  174. billing_type: [
  175. 'billing_type',
  176. {
  177. initialValue: 'postpaid',
  178. },
  179. ],
  180. tag: [
  181. 'tag',
  182. {
  183. rules: [
  184. { validator: validateTag },
  185. ],
  186. },
  187. ],
  188. }
  189. return {
  190. loading: false,
  191. decorators,
  192. skuOptions: {},
  193. capacity: 0,
  194. zones: [],
  195. tailFormItemLayout: {
  196. wrapperCol: {
  197. lg: { span: 18, offset: 6 },
  198. xl: { span: 20, offset: 4 },
  199. xxl: { span: 21, offset: 3 },
  200. },
  201. },
  202. vpcList: [],
  203. cloudEnvOptions,
  204. routerQuery: this.$route.query.type,
  205. cloudEnv: this.$route.query.type ? this.$route.query.type : cloudEnvOptions[0].key,
  206. }
  207. },
  208. computed: {
  209. areaselectsName () {
  210. return ['provider', 'cloudregion', 'zone']
  211. },
  212. cloudAccountId () {
  213. const values = this.form.fc.getFieldsValue()
  214. const currentVpc = this.vpcList.filter(item => item.id === values.vpc)
  215. if (currentVpc[0]) {
  216. return currentVpc[0].account_id
  217. }
  218. return ''
  219. },
  220. cloudproviderParams () {
  221. const params = {
  222. limit: 0,
  223. enabled: 1,
  224. details: true,
  225. scope: this.scope,
  226. read_only: false,
  227. cloudregion: this.form.fd.cloudregion_id,
  228. }
  229. if (this.isAdminMode) {
  230. params.admin = true
  231. params.project_domain = this.form.fd.domain?.key
  232. delete params.scope
  233. delete params.domain_id
  234. }
  235. return params
  236. },
  237. providerParams () {
  238. const params = {
  239. limit: 0,
  240. enabled: 1,
  241. cloud_env: 'public',
  242. scope: this.scope,
  243. provider: this.capability.nas_brands,
  244. }
  245. if (!this.capability.nas_brands || this.capability.nas_brands.length === 0) {
  246. params.provider = 'Other'
  247. }
  248. if (this.isAdminMode) {
  249. params.admin = true
  250. params.project_domain = this.form.fd.domain?.key
  251. delete params.scope
  252. }
  253. return params
  254. },
  255. regionParams () {
  256. const params = {
  257. usable: true,
  258. show_emulated: true,
  259. }
  260. if (this.isAdminMode) {
  261. params.admin = true
  262. params.project_domain = this.form.fd.domain?.key
  263. delete params.scope
  264. }
  265. return params
  266. },
  267. zoneParams () {
  268. return {
  269. usable: true,
  270. show_emulated: true,
  271. order_by: 'created_at',
  272. order: 'asc',
  273. project_domain: this.form.fd.domain?.key,
  274. }
  275. },
  276. vpcParams () {
  277. const params = {
  278. cloudregion_id: this.form.fd.cloudregion_id,
  279. scope: this.scope,
  280. manager_id: this.form.fd.cloudprovider,
  281. }
  282. if (this.isAdminMode) {
  283. params.project_domain = this.form.fd.domain?.key
  284. }
  285. return params
  286. },
  287. netParams () {
  288. const params = {
  289. zone_id: this.form.fd.zone_id,
  290. scope: this.scope,
  291. }
  292. return params
  293. },
  294. isShowCapacity () {
  295. return this.form.fd.sku?.min_disk_size_gb !== -1
  296. },
  297. capacityConf () {
  298. const capacityConf = {}
  299. const { min_disk_size_gb, max_disk_size_gb } = this.form.fd.sku || {}
  300. if (this.isShowCapacity) {
  301. capacityConf.min = min_disk_size_gb
  302. capacityConf.max = max_disk_size_gb
  303. }
  304. return capacityConf
  305. },
  306. capacityTooltip () {
  307. const { min, max } = this.capacityConf
  308. if (min) {
  309. return this.$t('compute.text_137', [min, max])
  310. }
  311. return null
  312. },
  313. },
  314. provide () {
  315. return {
  316. form: this.form,
  317. scopeParams: this.scopeParams,
  318. formItemLayout: this.formItemLayout,
  319. tailFormItemLayout: this.tailFormItemLayout,
  320. }
  321. },
  322. watch: {
  323. capacity: {
  324. handler (val) {
  325. this.form.fc.setFieldsValue({
  326. capacity: val,
  327. })
  328. },
  329. },
  330. 'form.fd.zone': {
  331. handler (val) {
  332. if (val) {
  333. this.$refs.REF_SKU.fetchSkus(['cloudregion_id', 'zone_id'])
  334. } else {
  335. this.$refs.REF_SKU.fetchSkus(['cloudregion_id'])
  336. }
  337. },
  338. },
  339. },
  340. created () {
  341. this.form.fc.getFieldDecorator('cloudregion_id', { preserve: true })
  342. this.form.fc.getFieldDecorator('capacity', { preserve: true })
  343. },
  344. methods: {
  345. skuChanged (options) {
  346. this.skuOptions = options
  347. this.form.fc.setFieldsValue({
  348. capacity: this.skuOptions.min,
  349. })
  350. this.capacity = this.skuOptions.min
  351. // this.fetchZones()
  352. this.fetchVpc()
  353. },
  354. zoneChanged (e) {
  355. this.form.fc.setFieldsValue({
  356. zone_id: e.target.value,
  357. })
  358. this.fetchNetwork()
  359. },
  360. billing_type_change () {
  361. this.$refs.REF_SKU.fetchSkus()
  362. },
  363. vpcFormat (vpc) {
  364. const { name, manager } = vpc
  365. return (
  366. <div class='d-flex'>
  367. <span class='text-truncate flex-fill mr-2' title={name}>{name}</span>
  368. <span style="color: #8492a6; font-size: 13px">{this.$t('network.manager', [manager])}</span>
  369. </div>
  370. )
  371. },
  372. async fetchZones () {
  373. const manager = new this.$Manager('zones', 'v2')
  374. const params = {
  375. cloudregion_id: this.form.fc.getFieldValue('cloudregion'),
  376. scope: this.scope,
  377. }
  378. if (this.skuOptions.zone_ids && this.skuOptions.zone_ids.length > 0) {
  379. params.filter = `id.in(${this.skuOptions.zone_ids.map(item => `'${item}'`).join(',')})`
  380. }
  381. const { data = [] } = await manager.list({ params })
  382. this.zones = data.data || []
  383. if (this.zones.length > 0) {
  384. this.$nextTick(() => {
  385. this.form.fc.setFieldsValue({
  386. zone_id: this.zones[0].id,
  387. })
  388. })
  389. }
  390. },
  391. fetchVpc () {
  392. this.$refs.REF_NETWORK.fetchVpc(this.vpcListChange)
  393. },
  394. vpcListChange ({ vpcList }) {
  395. this.vpcList = vpcList
  396. },
  397. fetchNetwork () {
  398. this.$refs.REF_NETWORK.fetchNetwork()
  399. },
  400. handleDomainChange (val) {
  401. this.$refs.areaSelects.fetchs(this.areaselectsName)
  402. },
  403. handleRegionChange (val) {
  404. if (val.cloudregion) {
  405. this.form.fc.setFieldsValue({
  406. cloudregion_id: val.cloudregion.id,
  407. })
  408. this.$refs.REF_SKU.fetchSkus()
  409. this.fetchVpc()
  410. }
  411. if (val.zone) {
  412. this.form.fc.setFieldsValue({
  413. zone_id: val.zone.id,
  414. })
  415. }
  416. },
  417. async handleValuesChange (fc, changedFields) {
  418. this.form.fd = {
  419. ...this.form.fd,
  420. ...changedFields,
  421. }
  422. await this.$nextTick()
  423. const changedFieldsKey = Object.keys(changedFields)
  424. changedFieldsKey.forEach(field => {
  425. // if (changedFields[field] === undefined) return false
  426. const handleChange = this[`${field}_change`]
  427. if (this[`${field}_change`]) {
  428. return handleChange()
  429. }
  430. })
  431. },
  432. doCreate (data) {
  433. return new this.$Manager('file_systems').create({ data })
  434. },
  435. async handleConfirm () {
  436. this.loading = true
  437. try {
  438. const values = await this.form.fc.validateFields()
  439. const params = {
  440. billing_type: values.billing_type,
  441. generate_name: values.name,
  442. description: values.description,
  443. network_id: values.network,
  444. zone_id: values.zone_id,
  445. project_domain: (values.domain && values.domain.key) || this.userInfo.projectDomainId,
  446. project_id: (values.project && values.project.key) || this.userInfo.projectId,
  447. }
  448. if (values.tag) {
  449. params.__meta__ = values.tag
  450. }
  451. if (values.sku) {
  452. params.file_system_type = values.sku.file_system_type
  453. params.protocol = values.sku.protocol
  454. params.storage_type = values.sku.storage_type
  455. }
  456. if (values.capacity > 0) {
  457. params.capacity = values.capacity
  458. }
  459. if (values.billing_type === 'postpaid') {
  460. if (values.durationStandard !== 'none') {
  461. params.duration = values.durationStandard
  462. if (values.durationStandard === 'custom') {
  463. params.duration = values.duration
  464. }
  465. }
  466. if (values.auto_renew) {
  467. params.auto_renew = values.auto_renew
  468. }
  469. } else {
  470. params.duration = values.duration
  471. }
  472. await this.doCreate(params)
  473. this.$message.success(this.$t('network.nat.create.success'))
  474. this.$router.push('/nas')
  475. } catch (error) {
  476. throw error
  477. } finally {
  478. this.loading = false
  479. }
  480. },
  481. },
  482. }
  483. </script>