Create.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.text_189')}}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc">
  7. <a-form-item :label="$t('compute.text_297', [$t('dictionary.project')])" v-bind="formItemLayout" class="mb-0">
  8. <domain-project :fc="form.fc" :form-layout="formItemLayout" :decorators="{ project: decorators.project, domain: decorators.domain }" />
  9. </a-form-item>
  10. <a-form-item :label="$t('compute.text_1016')" v-bind="formItemLayout">
  11. <a-select
  12. v-decorator="decorators.template"
  13. @change="handleSelectChange">
  14. <a-select-option v-for="(v, k) in templateOps" :key="k" :value="k">
  15. {{v}}
  16. </a-select-option>
  17. </a-select>
  18. </a-form-item>
  19. <a-form-item :label="$t('compute.text_228')" v-bind="formItemLayout">
  20. <a-input v-decorator="decorators.name" :placeholder="$t('compute.text_210')" />
  21. </a-form-item>
  22. <a-form-item :label="$t('common.description')" v-bind="formItemLayout">
  23. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  24. </a-form-item>
  25. <a-form-item :label="$t('compute.text_1154')" class="mb-0" v-bind="formItemLayout">
  26. <tag
  27. v-decorator="decorators.tag" :allowNoValue="false" />
  28. </a-form-item>
  29. </a-form>
  30. <a-tabs defaultActiveKey="in" @change="tabCallback">
  31. <a-tab-pane :tab="$t('compute.text_993')" key="in">
  32. <a-table
  33. :columns="columns"
  34. :dataSource="ruleData[templateType]['in']"
  35. :pagination="false" />
  36. </a-tab-pane>
  37. <a-tab-pane :tab="$t('compute.text_994')" key="out" forceRender>
  38. <a-table
  39. :columns="columns"
  40. :dataSource="ruleData[templateType]['out']"
  41. :pagination="false" />
  42. </a-tab-pane>
  43. </a-tabs>
  44. </div>
  45. <div slot="footer">
  46. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  47. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  48. </div>
  49. </base-dialog>
  50. </template>
  51. <script>
  52. import DialogMixin from '@/mixins/dialog'
  53. import WindowsMixin from '@/mixins/windows'
  54. import DomainProject from '@/sections/DomainProject'
  55. import validateForm, { isRequired } from '@/utils/validate'
  56. import Tag from '@/sections/Tag'
  57. export default {
  58. name: 'CreateSecgroupDialog',
  59. components: {
  60. DomainProject,
  61. Tag,
  62. },
  63. mixins: [DialogMixin, WindowsMixin],
  64. data () {
  65. const tenant = this.params.extParams && this.params.extParams.tenant
  66. const domain = this.params.extParams && this.params.extParams.domain
  67. return {
  68. loading: false,
  69. form: {
  70. fc: this.$form.createForm(this),
  71. },
  72. decorators: {
  73. domain: [
  74. 'domain',
  75. {
  76. initialValue: domain || this.$store.getters.userInfo.projectDomainId,
  77. rules: [
  78. { validator: isRequired(), message: this.$t('rules.domain'), trigger: 'change' },
  79. ],
  80. },
  81. ],
  82. project: [
  83. 'project',
  84. {
  85. initialValue: tenant || this.$store.getters.userInfo.projectId,
  86. rules: [
  87. { validator: isRequired(), message: this.$t('rules.project'), trigger: 'change' },
  88. ],
  89. },
  90. ],
  91. template: [
  92. 'template',
  93. {
  94. initialValue: '1',
  95. },
  96. ],
  97. name: [
  98. 'name',
  99. {
  100. validateFirst: true,
  101. rules: [
  102. { required: true, message: this.$t('compute.text_333') },
  103. // { validator: this.$validate('templateName') },
  104. ],
  105. },
  106. ],
  107. description: ['description'],
  108. tag: [
  109. 'tag',
  110. {
  111. initialValue: {},
  112. rules: [
  113. { required: false, message: this.$t('cloudenv.text_451') },
  114. { validator: validateForm('tagName') },
  115. ],
  116. },
  117. ],
  118. },
  119. formItemLayout: {
  120. wrapperCol: {
  121. span: 20,
  122. },
  123. labelCol: {
  124. span: 4,
  125. },
  126. },
  127. templateOps: {
  128. 1: this.$t('compute.text_1010'),
  129. 2: this.$t('compute.text_1011'),
  130. 3: this.$t('compute.text_144'),
  131. },
  132. columns: [
  133. {
  134. dataIndex: 'cidr',
  135. title: this.$t('compute.text_979'),
  136. width: 100,
  137. },
  138. {
  139. dataIndex: 'protocol',
  140. title: this.$t('compute.text_1017'),
  141. width: 150,
  142. },
  143. {
  144. dataIndex: 'ports',
  145. title: this.$t('compute.text_349'),
  146. width: 100,
  147. },
  148. {
  149. dataIndex: 'action',
  150. title: this.$t('compute.text_694'),
  151. width: 100,
  152. },
  153. {
  154. dataIndex: 'description',
  155. title: this.$t('compute.text_312'),
  156. },
  157. ],
  158. ruleData: {
  159. 1: {
  160. in: [
  161. {
  162. cidr: '0.0.0.0/0',
  163. protocol: 'TCP',
  164. ports: '80',
  165. action: this.$t('compute.text_976'),
  166. description: this.$t('compute.text_1006'),
  167. },
  168. {
  169. cidr: '0.0.0.0/0',
  170. protocol: 'TCP',
  171. ports: '443',
  172. action: this.$t('compute.text_976'),
  173. description: this.$t('compute.text_1007'),
  174. },
  175. {
  176. cidr: '0.0.0.0/0',
  177. protocol: 'TCP',
  178. ports: '3389',
  179. action: this.$t('compute.text_976'),
  180. description: this.$t('compute.text_1004'),
  181. },
  182. {
  183. cidr: '0.0.0.0/0',
  184. protocol: 'TCP',
  185. ports: '22',
  186. action: this.$t('compute.text_976'),
  187. description: this.$t('compute.text_1005'),
  188. },
  189. {
  190. cidr: '0.0.0.0/0',
  191. protocol: 'ICMP',
  192. ports: 'ALL',
  193. action: this.$t('compute.text_976'),
  194. description: this.$t('compute.text_1008'),
  195. },
  196. ],
  197. out: [
  198. {
  199. cidr: '0.0.0.0/0',
  200. protocol: 'ALL',
  201. ports: 'ALL',
  202. action: this.$t('compute.text_976'),
  203. description: '',
  204. },
  205. ],
  206. },
  207. 2: {
  208. in: [
  209. {
  210. cidr: '0.0.0.0/0',
  211. protocol: 'ALL',
  212. ports: 'ALL',
  213. action: this.$t('compute.text_976'),
  214. description: this.$t('compute.text_1018'),
  215. },
  216. ],
  217. out: [
  218. {
  219. cidr: '0.0.0.0/0',
  220. protocol: 'ALL',
  221. ports: 'ALL',
  222. action: this.$t('compute.text_976'),
  223. description: '',
  224. },
  225. ],
  226. },
  227. 3: {
  228. in: [
  229. {
  230. action: '',
  231. cidr: '',
  232. ports: '',
  233. protocol: '',
  234. description: this.$t('compute.text_1019'),
  235. },
  236. ],
  237. out: [],
  238. },
  239. },
  240. templateType: 1,
  241. checkedTab: 'in',
  242. }
  243. },
  244. methods: {
  245. handleSelectChange (e) {
  246. this.templateType = e
  247. },
  248. tabCallback (e) {
  249. this.checkedTab = e
  250. },
  251. validateForm () {
  252. return new Promise((resolve, reject) => {
  253. this.form.fc.validateFields((err, values) => {
  254. if (!err) {
  255. resolve(values)
  256. } else {
  257. reject(err)
  258. }
  259. })
  260. })
  261. },
  262. doCreate (data) {
  263. return new this.$Manager('secgroups').create({ data: data })
  264. },
  265. async handleConfirm () {
  266. this.loading = true
  267. try {
  268. const values = await this.validateForm()
  269. this.loading = true
  270. const rules = []
  271. const key = this.templateType || '1'
  272. if (key && key !== '3' && this.ruleData[key]) {
  273. Object.keys(this.ruleData[key]).forEach(k => {
  274. const items = this.ruleData[key][k]
  275. for (let i = 0; i < items.length; i++) {
  276. const obj = { ...items[i] }
  277. if (obj.protocol === 'ALL') {
  278. obj.protocol = 'any'
  279. }
  280. if (obj.protocol === 'ALL') {
  281. obj.protocol = 'any'
  282. }
  283. obj.priority = items.length - i
  284. obj.action = 'allow'
  285. obj.direction = k
  286. obj.protocol = obj.protocol.toLowerCase()
  287. if (obj.ports === 'ALL') {
  288. delete obj.ports
  289. }
  290. rules.push(obj)
  291. }
  292. })
  293. }
  294. const newValues = {
  295. name: values.name,
  296. description: values.description,
  297. rules,
  298. tenant: values.project && values.project.key,
  299. __meta__: values.tag,
  300. }
  301. await this.doCreate(newValues)
  302. this.loading = false
  303. this.cancelDialog()
  304. this.params.refresh && this.params.refresh()
  305. this.params.success && this.params.success()
  306. } catch (error) {
  307. this.loading = false
  308. throw error
  309. }
  310. },
  311. },
  312. }
  313. </script>