Create.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div>
  3. <page-header :title="$t('compute.text_189')" :tabs="cloudEnvOptions" :current-tab.sync="cloudEnv" />
  4. <page-body need-margin-bottom>
  5. <a-form class="mt-3" :form="form.fc" @submit.prevent="handleSubmit" v-bind="formItemLayout" hideRequiredMark>
  6. <a-form-item :label="$t('network.text_205', [$t('dictionary.project')])" class="mt-3" v-bind="formItemLayout">
  7. <domain-project :fc="form.fc" :decorators="{ project: decorators.project, domain: decorators.domain }" @update:domain="handleDomainChange" />
  8. </a-form-item>
  9. <a-form-item :label="$t('network.text_21')" v-bind="formItemLayout">
  10. <a-input v-decorator="decorators.name" :placeholder="$t('validator.resourceName')" />
  11. </a-form-item>
  12. <a-form-item :label="$t('common.description')" v-bind="formItemLayout">
  13. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  14. </a-form-item>
  15. <area-selects
  16. class="mb-0"
  17. ref="areaSelects"
  18. :wrapperCol="formItemLayout.wrapperCol"
  19. :labelCol="formItemLayout.labelCol"
  20. :names="areaselectsName"
  21. :providerParams="providerParams"
  22. :cloudregionParams="cloudregionParams"
  23. :isRequired="true"
  24. filterBrandResource="security_group"
  25. @change="handleRegionChange" />
  26. <a-form-item label="VPC" v-bind="formItemLayout" v-if="regionId !== 'default'">
  27. <base-select
  28. v-decorator="decorators.vpc"
  29. resource="vpcs"
  30. :params="vpcParams"
  31. :isDefaultSelect="true"
  32. :needParams="true"
  33. @change="vpcChange"
  34. :item.sync="curVpc"
  35. :labelFormat="vpcLabelFormat"
  36. :select-props="{ placeholder: $t('common_226') }" />
  37. </a-form-item>
  38. <a-form-item :label="$t('common.text00012')" class="mb-0">
  39. <tag
  40. v-decorator="decorators.__meta__" />
  41. </a-form-item>
  42. <page-footer>
  43. <template v-slot:right>
  44. <a-button type="primary" html-type="submit" class="ml-3" :loading="submiting">{{$t('network.text_30')}}</a-button>
  45. <a-button class="ml-3" @click="() => $router.back()">{{$t('common.cancel')}}</a-button>
  46. </template>
  47. </page-footer>
  48. </a-form>
  49. </page-body>
  50. </div>
  51. </template>
  52. <script>
  53. import { mapGetters } from 'vuex'
  54. import validateForm, { isRequired } from '@/utils/validate'
  55. import { Manager } from '@/utils/manager'
  56. import { getCloudEnvOptions } from '@/utils/common/hypervisor'
  57. import DomainProject from '@/sections/DomainProject'
  58. import AreaSelects from '@/sections/AreaSelects'
  59. import i18n from '@/locales'
  60. import { HYPERVISORS_MAP } from '@/constants'
  61. import Tag from '@/sections/Tag'
  62. export default {
  63. name: 'SecgroupCreate',
  64. components: {
  65. DomainProject,
  66. AreaSelects,
  67. Tag,
  68. },
  69. data () {
  70. const cloudEnvOptions = getCloudEnvOptions('security_group_brands', true)
  71. const queryType = this.$route.query.type
  72. let cloudEnv = queryType === 'idc' ? 'onpremise' : this.$route.query.type
  73. let routerQuery = this.$route.query.type
  74. if (!cloudEnvOptions.find(val => val.key === cloudEnv)) {
  75. cloudEnv = cloudEnvOptions[0].key
  76. routerQuery = cloudEnv === 'onpremise' ? 'idc' : cloudEnv
  77. }
  78. return {
  79. submiting: false,
  80. cloudEnvOptions,
  81. cloudEnv,
  82. routerQuery,
  83. form: {
  84. fc: this.$form.createForm(this, { onValuesChange: this.handleValuesChange }),
  85. fd: {},
  86. },
  87. guestIpPrefixValidateStatus: '',
  88. guestIpPrefixHelp: '',
  89. formItemLayout: {
  90. wrapperCol: {
  91. md: { span: 18 },
  92. xl: { span: 20 },
  93. xxl: { span: 22 },
  94. },
  95. labelCol: {
  96. md: { span: 6 },
  97. xl: { span: 4 },
  98. xxl: { span: 2 },
  99. },
  100. },
  101. decorators: {
  102. domain: [
  103. 'domain',
  104. {
  105. rules: [
  106. { validator: isRequired(), message: i18n.t('rules.domain'), trigger: 'change' },
  107. ],
  108. },
  109. ],
  110. project: [
  111. 'project',
  112. {
  113. rules: [
  114. { validator: isRequired(), message: i18n.t('rules.project'), trigger: 'change' },
  115. ],
  116. },
  117. ],
  118. name: [
  119. 'name',
  120. {
  121. initialValue: '',
  122. validateFirst: true,
  123. rules: [
  124. { required: true, message: this.$t('network.text_116') },
  125. { validator: this.$validate('resourceName') },
  126. ],
  127. },
  128. ],
  129. description: ['description'],
  130. vpc: [
  131. 'vpc',
  132. {
  133. rules: [
  134. { required: true, message: this.$t('network.text_274') },
  135. ],
  136. },
  137. ],
  138. __meta__: [
  139. '__meta__',
  140. {
  141. rules: [
  142. { validator: validateForm('tagName') },
  143. ],
  144. },
  145. ],
  146. },
  147. show: true,
  148. regionProvider: '',
  149. regionId: '',
  150. project_domain: '',
  151. vpcId: '',
  152. curVpc: null,
  153. }
  154. },
  155. computed: {
  156. ...mapGetters(['isAdminMode', 'isDomainMode', 'scope', 'userInfo']),
  157. vpcParams () {
  158. const params = {
  159. limit: 0,
  160. usable_vpc: true,
  161. scope: this.scope,
  162. cloudregion_id: this.regionId,
  163. }
  164. if (this.cloudEnv === 'private') {
  165. params.show_emulated = true
  166. }
  167. if (this.isAdminMode) {
  168. params.project_domain = this.project_domain
  169. delete params.scope
  170. }
  171. if (!this.regionId) return {}
  172. return params
  173. },
  174. cloudregionParams () {
  175. const params = {
  176. scope: this.scope,
  177. limit: 0,
  178. show_emulated: true,
  179. }
  180. if (this.cloudEnv === 'private') {
  181. params.is_private = true
  182. params.usable_vpc = true
  183. } else if (this.cloudEnv === 'public') {
  184. params.is_public = true
  185. params.usable_vpc = true
  186. } else {
  187. params.is_on_premise = true
  188. }
  189. if (this.isAdminMode) {
  190. params.project_domain = this.project_domain
  191. delete params.scope
  192. }
  193. return params
  194. },
  195. areaselectsName () {
  196. if (this.cloudEnv === 'private' || this.cloudEnv === 'onpremise') {
  197. return ['cloudregion']
  198. }
  199. return ['provider', 'cloudregion']
  200. },
  201. providerParams () {
  202. return {
  203. cloud_env: 'public',
  204. usable: false,
  205. usable_vpc: true,
  206. project_domain: this.form.fd.domain?.key,
  207. }
  208. },
  209. },
  210. provide () {
  211. return {
  212. form: this.form,
  213. }
  214. },
  215. watch: {
  216. cloudEnv (newValue) {
  217. this.$refs.areaSelects.fetchs(this.areaselectsName)
  218. if (newValue === 'private') {
  219. this.show = false
  220. this.isGroupGuestIpPrefix = false
  221. } else if (newValue === 'public') {
  222. this.show = false
  223. this.isGroupGuestIpPrefix = false
  224. } else {
  225. this.show = true
  226. this.isGroupGuestIpPrefix = true
  227. }
  228. },
  229. },
  230. created () {
  231. },
  232. methods: {
  233. handleValuesChange (props, values) {
  234. this.form.fd = {
  235. ...this.form.fd,
  236. ...values,
  237. }
  238. },
  239. handleDomainChange (val) {
  240. this.project_domain = val.key
  241. },
  242. handleRegionChange (data) {
  243. const { provider, cloudregion } = data
  244. if (data.hasOwnProperty('provider')) {
  245. if (provider) {
  246. this.regionProvider = provider.id || provider
  247. } else {
  248. this.regionProvider = ''
  249. this.regionId = ''
  250. }
  251. }
  252. if (data.hasOwnProperty('cloudregion')) {
  253. if (cloudregion) {
  254. this.regionId = cloudregion.id || cloudregion
  255. }
  256. }
  257. },
  258. vpcChange (vpcId) {
  259. this.vpcId = vpcId
  260. },
  261. vpcLabelFormat (item) {
  262. if (this.cloudEnv === 'public' || this.regionProvider === HYPERVISORS_MAP.hcso.provider || this.regionProvider === HYPERVISORS_MAP.hcs.provider) {
  263. if (item.manager) {
  264. if (item.cidr_block) {
  265. return (<div>{ item.name }<span v-if="item.cidr_block">({ item.cidr_block })</span><span class="ml-2 text-color-secondary">{ this.$t('common.cloudprovider_1var', [item.manager]) }</span></div>)
  266. }
  267. return (<div>{ item.name }<span class="ml-2 text-color-secondary">{ this.$t('common.cloudprovider_1var', [item.manager]) }</span></div>)
  268. }
  269. } else if (this.cloudEnv === 'onpremise') {
  270. if (item.cidr_block) {
  271. return (<div>{ item.name }<span v-if="item.cidr_block">({ item.cidr_block })</span></div>)
  272. }
  273. if (item.id === 'default') return (<div>{ item.name }<span v-if="item.cidr_block">({this.$t('common.text00047')})</span></div>)
  274. }
  275. return (<div>{ item.name }</div>)
  276. },
  277. genData (values) {
  278. const ret = {
  279. name: values.name,
  280. description: values.description,
  281. vpc: values.vpc,
  282. __meta__: values.__meta__,
  283. }
  284. if (!this.isAdminMode && !this.isDomainMode) {
  285. ret.project_id = this.userInfo.projectId
  286. } else {
  287. ret.project_id = values.project?.key
  288. }
  289. return ret
  290. },
  291. clearGuestIpPrefixError () {
  292. this.guestIpPrefixValidateStatus = ''
  293. this.guestIpPrefixHelp = ''
  294. },
  295. async handleSubmit () {
  296. const ListPath = this.$router.resolve(this.$route.path)
  297. try {
  298. const values = await this.form.fc.validateFields()
  299. this.submiting = true
  300. const data = this.genData(values)
  301. const manager = new Manager('secgroups')
  302. await manager.create({ data })
  303. this.$router.push({ path: ListPath.resolved.matched[0].path })
  304. } catch (error) {
  305. throw error
  306. } finally {
  307. this.submiting = false
  308. }
  309. },
  310. filterOption (input, option) {
  311. const lastIdx = option.componentOptions.children.length - 1
  312. return (
  313. option.componentOptions.children[lastIdx].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  314. )
  315. },
  316. },
  317. }
  318. </script>