Create.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <base-dialog :width="1000" @cancel="cancelDialog">
  3. <div slot="header">{{$t('cloudenv.text_583')}}</div>
  4. <div slot="body" style="max-height:calc(100vh - 320px);overflow-y:auto">
  5. <a-alert class="mb-3" :message="$t('cloudenv.project_mapping_rule_priority')" />
  6. <a-form-model ref="ruleForm" :model="formData" :rules="rules" v-bind="layout" :validateOnRuleChange="true">
  7. <!-- 域 -->
  8. <a-form-model-item :label="$t('dictionary.domain')" prop="project_domain_id">
  9. <domain-select v-model="formData.project_domain_id" v-if="isAdminMode && l3PermissionEnable" :params="domainParams" />
  10. <template v-else> {{userInfo.projectDomain}} </template>
  11. </a-form-model-item>
  12. <!-- 名称 -->
  13. <a-form-model-item :label="$t('cloudenv.text_95')" prop="name">
  14. <a-input v-model="formData.name" :placeholder="$t('validator.resourceName')" />
  15. </a-form-model-item>
  16. <a-form-model-item :label="$t('common.description')">
  17. <a-textarea v-model="formData.description" :rows="1" :auto-size="{ minRows: 1, maxRows: 3 }" :placeholder="$t('common_367')" />
  18. </a-form-model-item>
  19. <!-- 规则 -->
  20. <a-form-model-item :label="$t('cloudenv.text_582')" :rules="rules.rules" prop="rules">
  21. <div v-for="(item,index) in formData.rules" :key="index" class="d-flex align-items-center">
  22. <a-card class="mb-3" style="flex: 1 1 auto">
  23. <!-- 条件 -->
  24. <a-form-model-item :label="$t('cloudenv.text_22')" v-bind="layout" :rules="rules.condition" :prop="`rules.${index}.condition`">
  25. <a-select v-model="item.condition">
  26. <a-select-option v-for="item in resourceAndTagOptions" :value="item.value" :key="item.value" :disabled="item.value === 'and_copy' && isSecAndcopy(index)">
  27. {{item.name}}
  28. </a-select-option>
  29. </a-select>
  30. </a-form-model-item>
  31. <!-- 标签key -->
  32. <template v-if="item.condition === 'and_copy'">
  33. <a-form-model-item :label="$t('cloudenv.tag_key')" v-bind="layout" :rules="rules.tag_key" :prop="`rules.${index}.tag_key`">
  34. <a-input v-model="item.tag_key" />
  35. </a-form-model-item>
  36. </template>
  37. <template v-else>
  38. <!-- 标签 -->
  39. <a-form-model-item :label="$t('cloudenv.text_16')" v-bind="layout" :rules="rules.tags" :prop="`rules.${index}.tags`">
  40. <tag v-if="tagShow" :defaultChecked="item.tags" @change="(val) => handleTagChange(val, index)" />
  41. </a-form-model-item>
  42. <a-form-model-item :label="$t('cloudenv.belong_type')" v-bind="layout" :rules="rules.belong_type" :prop="`rules.${index}.belong_type`">
  43. <a-form-model-item class="mb-0" :extra="item.belong_type === 'project_id' ? $t('cloudenv.text_592') : $t('cloudenv.belong_project_name_tip')">
  44. <a-radio-group v-model="item.belong_type" @change="validateBt(index)">
  45. <a-radio-button value="project_id">{{ $t('cloudenv.target_project') }}</a-radio-button>
  46. <a-radio-button value="project">{{ $t('cloudenv.target_name') }}</a-radio-button>
  47. </a-radio-group>
  48. <base-select
  49. v-if="item.belong_type === 'project_id'"
  50. resource="projects"
  51. remote
  52. :params="{...projectParams, $t: index}"
  53. v-model="item.project_id"
  54. :select-props="{placeholder: $t('common.tips.select', [$t('dictionary.project')])}"
  55. @change="validateBt(index)" />
  56. <a-input v-else type="text" v-model="item.project" :placeholder="$t('common.tips.input', [$t('dictionary.project')])" @change="validateBt(index)" />
  57. </a-form-model-item>
  58. </a-form-model-item>
  59. </template>
  60. </a-card>
  61. <div>
  62. <div v-if="formData.rules.length > 1 && index !== 0"><a-button icon="vertical-align-top" style="flex: 0 0 24px;margin-left: 16px;border:none" @click="changeRuleIndex('up', item, index)" /></div>
  63. <div><a-button style="flex: 0 0 24px;margin-left: 20px" shape="circle" icon="minus" size="small" @click="deleteRule(item,index)" /></div>
  64. <div v-if="formData.rules.length > 1 && index !== formData.rules.length - 1"><a-button icon="vertical-align-bottom" style="flex: 0 0 24px;margin-left: 16px;border:none" @click="changeRuleIndex('down', item, index)" /></div>
  65. </div>
  66. </div>
  67. <div class="d-flex align-items-center">
  68. <a-button type="primary" shape="circle" icon="plus" size="small" @click="addRule" />
  69. <a-button type="link" @click="addRule">{{$t('cloudenv.text_586')}}</a-button>
  70. </div>
  71. </a-form-model-item>
  72. <a-form-model-item :label="$t('cloudenv.text_282')" prop="public_scope">
  73. <a-radio-group v-model="formData.public_scope">
  74. <a-radio-button value="none">{{$t('cloudenv.text_285')}}</a-radio-button>
  75. <a-radio-button v-if="isAdminMode" value="system">{{$t('cloudenv.global_share')}}</a-radio-button>
  76. </a-radio-group>
  77. </a-form-model-item>
  78. </a-form-model>
  79. </div>
  80. <div slot="footer">
  81. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  82. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  83. </div>
  84. </base-dialog>
  85. </template>
  86. <script>
  87. import * as R from 'ramda'
  88. import { mapGetters } from 'vuex'
  89. import DomainSelect from '@/sections/DomainSelect'
  90. import DialogMixin from '@/mixins/dialog'
  91. import WindowsMixin from '@/mixins/windows'
  92. import Tag from '../components/Tag'
  93. export default {
  94. name: 'ProjectMappingCreateDialog',
  95. components: {
  96. DomainSelect,
  97. Tag,
  98. },
  99. mixins: [DialogMixin, WindowsMixin],
  100. data () {
  101. const projectDomainInitialValue = (this.params.domain && this.params.domain.key) || this.$store.getters.userInfo.projectDomainId
  102. return {
  103. loading: false,
  104. formData: {
  105. project_domain_id: projectDomainInitialValue,
  106. name: '',
  107. description: '',
  108. rules: [],
  109. public_scope: 'none',
  110. },
  111. rules: {
  112. name: [
  113. { required: true, message: this.$t('cloudenv.text_190') },
  114. { validator: this.$validate('resourceName') },
  115. ],
  116. project_domain_id: [
  117. { required: true, message: this.$t('cloudenv.text_284', [this.$t('dictionary.domain')]) },
  118. ],
  119. rules: [
  120. { required: true, validator: this.validateRules },
  121. ],
  122. condition: [
  123. { required: true, message: this.$t('common.tips.select', [this.$t('cloudenv.text_22')]) },
  124. ],
  125. tags: [
  126. { required: true, validator: this.validateTags },
  127. ],
  128. tag_key: [
  129. { required: true, message: this.$t('common.tips.input', [this.$t('cloudenv.tag_key')]) },
  130. ],
  131. belong_type: [
  132. { required: true, validator: this.validateBelongType, trigger: 'change' },
  133. ],
  134. public_scope: [
  135. { required: true },
  136. ],
  137. },
  138. layout: {
  139. wrapperCol: {
  140. span: 18,
  141. },
  142. labelCol: {
  143. span: 6,
  144. },
  145. },
  146. offsetLayout: {
  147. wrapperCol: {
  148. span: 18,
  149. offset: 6,
  150. },
  151. },
  152. domainParams: {
  153. limit: 20,
  154. filter: 'enabled.equals(1)',
  155. },
  156. resourceAndTagOptions: [
  157. {
  158. id: 1,
  159. name: this.$t('cloudenv.text_588'),
  160. value: 'and',
  161. },
  162. {
  163. id: 2,
  164. name: this.$t('cloudenv.text_587'),
  165. value: 'or',
  166. },
  167. {
  168. id: 3,
  169. name: this.$t('cloudenv.match_by_tag_key'),
  170. value: 'and_copy',
  171. },
  172. ],
  173. projectOptions: [],
  174. projectDomainId: projectDomainInitialValue,
  175. tagShow: true,
  176. }
  177. },
  178. computed: {
  179. ...mapGetters(['isAdminMode', 'isDomainMode', 'userInfo', 'l3PermissionEnable']),
  180. projectParams () {
  181. return {
  182. limit: 20,
  183. scope: this.$store.getters.scope,
  184. // project_domain_id: this.projectDomainId,
  185. }
  186. },
  187. },
  188. mounted () {
  189. },
  190. methods: {
  191. validateBt (index) {
  192. this.$refs.ruleForm.validateField('rules.' + index + '.belong_type')
  193. },
  194. validateBelongType (rule, value, callback) {
  195. const index = parseInt(rule.field.replace('rules.', '').replace('.belong_type', ''))
  196. const target = this.formData.rules[index]
  197. if (value === 'project_id' && !target[value]) {
  198. callback(new Error(this.$t('common.tips.select', [this.$t('dictionary.project')])))
  199. }
  200. if (value === 'project' && !(target[value].trim())) {
  201. callback(new Error(this.$t('common.tips.input', [this.$t('dictionary.project')])))
  202. }
  203. callback()
  204. },
  205. doCreate (data) {
  206. return new this.$Manager('project_mappings').create({ data: data })
  207. },
  208. doBindByAccount ({ accountIds, project_mapping_id }) {
  209. const manager = new this.$Manager('cloudaccounts')
  210. return manager.batchPerformAction({
  211. ids: accountIds,
  212. action: 'project-mapping',
  213. data: { project_mapping_id },
  214. })
  215. },
  216. doBindByCloudProvider ({ cloudproviderIds, project_mapping_id }) {
  217. const manager = new this.$Manager('cloudproviders')
  218. return manager.batchPerformAction({
  219. ids: cloudproviderIds,
  220. action: 'project-mapping',
  221. data: { project_mapping_id },
  222. })
  223. },
  224. async handleConfirm () {
  225. this.loading = true
  226. try {
  227. const validate = await this.$refs.ruleForm.validate()
  228. if (!validate) return
  229. // 获取参数
  230. const params = this.getCreateParams()
  231. await this.doCreate(params)
  232. this.cancelDialog()
  233. this.params.success && this.params.success()
  234. this.$message.success(this.$t('cloudenv.text_381'))
  235. } catch (error) {
  236. throw error
  237. } finally {
  238. this.loading = false
  239. }
  240. },
  241. getCreateParams () {
  242. const { project_domain_id, name, description, public_scope, rules = [] } = this.formData
  243. const result = {
  244. project_domain_id,
  245. name,
  246. description,
  247. rules: [],
  248. public_scope,
  249. }
  250. result.rules = rules.map((item, index) => {
  251. if (item.condition === 'and_copy') {
  252. return {
  253. condition: 'and',
  254. tags: [{ key: item.tag_key }],
  255. auto_create_project: true,
  256. }
  257. } else {
  258. const ret = {
  259. condition: item.condition,
  260. tags: this.getTagValue(item.tags),
  261. }
  262. ret[item.belong_type] = (item[item.belong_type] || '').trim()
  263. return ret
  264. }
  265. }).filter(item => {
  266. return !!item
  267. })
  268. return result
  269. },
  270. getTagValue (tag) {
  271. const result = []
  272. const keys = Object.keys(tag)
  273. keys.map(key => {
  274. result.push({
  275. key: R.replace(/(ext:|user:)/, '', key),
  276. value: tag[key],
  277. })
  278. })
  279. return result
  280. },
  281. handleTagChange (val, index) {
  282. this.formData.rules[index].tags = val
  283. },
  284. addRule () {
  285. this.formData.rules.push({
  286. condition: 'and',
  287. tags: {},
  288. tag_key: '',
  289. project_id: undefined,
  290. project: '',
  291. belong_type: 'project_id',
  292. })
  293. },
  294. deleteRule (item, idx) {
  295. this.formData.rules = this.formData.rules.filter((item, index) => index !== idx)
  296. },
  297. changeRuleIndex (type, item, idx) {
  298. const rules = R.clone(this.formData.rules)
  299. if (type === 'up') {
  300. [rules[idx - 1], [rules[idx]]] = [rules[idx], [rules[idx - 1]]]
  301. } else {
  302. [rules[idx], [rules[idx + 1]]] = [rules[idx + 1], [rules[idx]]]
  303. }
  304. this.tagShow = false
  305. this.formData.rules = rules
  306. this.$nextTick(() => {
  307. this.tagShow = true
  308. })
  309. },
  310. validateRules (rule, value, callback) {
  311. if (!this.formData.rules.length) {
  312. callback(new Error(this.$t('cloudenv.add_rule_tip')))
  313. }
  314. callback()
  315. },
  316. validateTags (rule, value, callback) {
  317. if (value) {
  318. const keys = Object.keys(value)
  319. if (!keys.length) {
  320. callback(new Error(this.$t('common.tips.select', [this.$t('cloudenv.text_16')])))
  321. } else if (keys.length > 20) {
  322. callback(new Error(this.$t('cloudenv.text_602')))
  323. } else {
  324. callback()
  325. }
  326. } else {
  327. callback(new Error(this.$t('common.tips.select', [this.$t('cloudenv.text_16')])))
  328. }
  329. },
  330. isSecAndcopy (index) {
  331. let has = false
  332. this.formData.rules.map((item, idx) => {
  333. if (item.condition === 'and_copy' && index !== idx) {
  334. has = true
  335. }
  336. })
  337. return has
  338. },
  339. filterOption (input, option) {
  340. return (
  341. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  342. )
  343. },
  344. },
  345. }
  346. </script>