ClouduserCreate.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ $t('common.create') }} {{ $t('dictionary.clouduser') }}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc"
  7. v-bind="formItemLayout">
  8. <a-form-item :label="$t('cloudenv.clouduser_list_t4')">
  9. <span>{{ params.user.name }}</span>
  10. </a-form-item>
  11. <a-form-item :label="$t('dictionary.project')">
  12. <base-select
  13. v-decorator="decorators.project_id"
  14. filterable
  15. :item.sync="form.fi.project"
  16. :options="projects">
  17. <template #optionTemplate="{ options }">
  18. <a-select-option v-for="item in options" :key="item.id" :value="item.id">
  19. {{ item.name }}<span class="text-color-secondary ml-2">{{ $t('common_624', [$t('dictionary.domain')]) }}: </span>{{ item.project_domain }}
  20. </a-select-option>
  21. </template>
  22. </base-select>
  23. </a-form-item>
  24. <a-form-item :label="$t('dictionary.cloudaccount')">
  25. <a-row :gutter="8">
  26. <a-col :span="12">
  27. <a-select v-model="form.fi.provider">
  28. <template v-for="item of providerOptions">
  29. <a-select-option :key="item[0]" :value="item[1].provider">
  30. <span class="text-color-secondary">{{ $t('common.brand') }}: </span>{{ item[1].label }}
  31. </a-select-option>
  32. </template>
  33. </a-select>
  34. </a-col>
  35. <a-col :span="12">
  36. <base-select
  37. v-decorator="decorators.cloudaccount_id"
  38. resource="cloudaccounts"
  39. filterable
  40. :item.sync="form.fi.cloudaccount"
  41. :params="cloudaccountParams"
  42. :mapper="cloudaccountMapper">
  43. <template #optionTemplate="{ options }">
  44. <a-select-option v-for="item in options" :key="item.id" :value="item.id">
  45. <span class="text-color-secondary">{{ $t('dictionary.cloudaccount') }}: </span>{{ item.name }}
  46. </a-select-option>
  47. </template>
  48. </base-select>
  49. </a-col>
  50. </a-row>
  51. </a-form-item>
  52. <template v-if="isGoogle">
  53. <a-form-item :label="$t('dictionary.cloudprovider')">
  54. <base-select
  55. v-decorator="decorators.cloudprovider_id"
  56. resource="cloudproviders"
  57. filterable
  58. :params="cloudproviderParams" />
  59. </a-form-item>
  60. </template>
  61. <a-form-item :label="$t('system.text_126')" :extra="nameExtra">
  62. <a-input v-decorator="decorators.generate_name" :placeholder="namePlaceholder" @change="e => { form.fi.generate_name = e.target.value }" />
  63. <template #extra>
  64. <name-repeated
  65. res="cloudusers"
  66. version="v1"
  67. :name="form.fi.generate_name" />
  68. </template>
  69. </a-form-item>
  70. <a-form-item :label="$t('dictionary.cloudgroup')" :extra="$t('cloudenv.clouduser_text2')">
  71. <list-select
  72. v-decorator="decorators.cloudgroup_ids"
  73. :listProps="cloudgroupListSelectProps"
  74. :formatter="formatterLabel"
  75. :dialog-params="{ mask: false }" />
  76. </a-form-item>
  77. <a-form-item :label="$t('system.text_146')" class="mb-0" :extra="isEnableMail ? '' : $t('system.is_config_mail_tips')">
  78. <a-input v-decorator="decorators.email" :disabled="!isEnableMail" :placeholder="$t('system.email_placeholder')" />
  79. </a-form-item>
  80. <a-form-item :wrapperCol="{ offset: 3 }">
  81. <a-checkbox v-decorator="decorators.notify" :disabled="!isCanSendNotify">{{ $t('system.email_checkbox_text') }}</a-checkbox>
  82. </a-form-item>
  83. </a-form>
  84. </div>
  85. <div slot="footer">
  86. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  87. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  88. </div>
  89. </base-dialog>
  90. </template>
  91. <script>
  92. import get from 'lodash/get'
  93. import * as R from 'ramda'
  94. import DialogMixin from '@/mixins/dialog'
  95. import WindowsMixin from '@/mixins/windows'
  96. import NameRepeated from '@/sections/NameRepeated'
  97. import ListSelect from '@/sections/ListSelect'
  98. import { getNameFilter } from '@/utils/common/tableFilter'
  99. import { HYPERVISORS_MAP } from '@/constants'
  100. export default {
  101. name: 'ClouduserCreateForUserDialog',
  102. components: {
  103. NameRepeated,
  104. ListSelect,
  105. },
  106. mixins: [DialogMixin, WindowsMixin],
  107. data () {
  108. const providerOptions = Object.entries(HYPERVISORS_MAP).filter(item => {
  109. return (this.$store.getters.capability.cloud_id_brands || []).includes(item[1].provider) && this.$store.getters.capability.brands.includes(item[1].provider)
  110. })
  111. const firstProvider = get(providerOptions, '[0][1].provider')
  112. return {
  113. loading: false,
  114. form: {
  115. fc: this.$form.createForm(this, {
  116. onValuesChange: (props, values) => {
  117. if (values.email) {
  118. this.$nextTick(() => {
  119. const isError = this.form.fc.getFieldError('email')
  120. if (R.isNil(isError) || R.isEmpty(isError)) {
  121. this.isCanSendNotify = true
  122. } else {
  123. this.isCanSendNotify = false
  124. }
  125. })
  126. }
  127. },
  128. }),
  129. fi: {
  130. generate_name: '',
  131. provider: firstProvider,
  132. cloudaccount: {},
  133. project: {},
  134. },
  135. },
  136. projects: [],
  137. decorators: {
  138. generate_name: [
  139. 'generate_name',
  140. {
  141. validateFirst: true,
  142. rules: [],
  143. },
  144. ],
  145. project_id: [
  146. 'project_id',
  147. {
  148. rules: [
  149. { required: true, message: this.$t('common.select') },
  150. ],
  151. },
  152. ],
  153. owner_id: [
  154. 'owner_id',
  155. {
  156. rules: [
  157. { required: true, message: this.$t('common.select') },
  158. ],
  159. },
  160. ],
  161. cloudaccount_id: [
  162. 'cloudaccount_id',
  163. {
  164. rules: [
  165. { required: true, message: this.$t('common.select') },
  166. ],
  167. },
  168. ],
  169. cloudprovider_id: [
  170. 'cloudprovider_id',
  171. {
  172. rules: [
  173. { required: true, message: this.$t('common.select') },
  174. ],
  175. },
  176. ],
  177. cloudgroup_ids: [
  178. 'cloudgroup_ids',
  179. {
  180. rules: [
  181. { required: firstProvider === 'Google', message: this.$t('common.select') },
  182. ],
  183. },
  184. ],
  185. email: [
  186. 'email',
  187. {
  188. rules: [
  189. { validator: this.emailValidator },
  190. ],
  191. },
  192. ],
  193. notify: [
  194. 'notify',
  195. ],
  196. },
  197. providerOptions,
  198. cloudgroupListSelectProps: {
  199. list: this.$list.createList(this, {
  200. id: this.id,
  201. resource: 'cloudgroups',
  202. apiVersion: 'v1',
  203. getParams: () => {
  204. return {
  205. scope: this.$store.getters.scope,
  206. provider: this.form.fi.provider,
  207. }
  208. },
  209. filterOptions: {
  210. name: getNameFilter(),
  211. },
  212. }),
  213. columns: [
  214. {
  215. field: 'name',
  216. title: this.$t('table.title.name'),
  217. showOverflow: 'title',
  218. },
  219. {
  220. field: 'description',
  221. title: this.$t('system.text_193'),
  222. showOverflow: 'title',
  223. },
  224. {
  225. field: 'cloudpolicies',
  226. title: this.$t('cloudenv.text_329'),
  227. type: 'expand',
  228. slots: {
  229. default: ({ row }) => {
  230. return [this.$t('cloudenv.text_245', [(row.cloudpolicies && row.cloudpolicies.length) || 0])]
  231. },
  232. content: ({ row }) => {
  233. if (R.isNil(row.cloudpolicies) || R.isEmpty(row.cloudpolicies)) return this.$t('cloudenv.text_330')
  234. return [
  235. <vxe-grid
  236. showOverflow='title'
  237. data={ row.cloudpolicies }
  238. columns={[
  239. {
  240. field: 'name',
  241. title: this.$t('common.name'),
  242. },
  243. {
  244. field: 'description',
  245. title: this.$t('table.title.desc'),
  246. formatter: ({ cellValue }) => cellValue || '-',
  247. },
  248. ]} />,
  249. ]
  250. },
  251. },
  252. },
  253. ],
  254. },
  255. formItemLayout: {
  256. wrapperCol: {
  257. span: 20,
  258. },
  259. labelCol: {
  260. span: 4,
  261. },
  262. },
  263. isCanSendNotify: false,
  264. isEnableMail: false,
  265. }
  266. },
  267. computed: {
  268. cloudaccountParams () {
  269. const params = {
  270. scope: this.$store.getters.scope,
  271. provider: this.form.fi.provider,
  272. enabled: true,
  273. health_status: 'normal',
  274. status: 'connected',
  275. tenant_id: this.form.fi.project.id,
  276. }
  277. if (this.$store.getters.isAdminMode) {
  278. params.project_domain = this.form.fi.project.domain_id
  279. }
  280. if (this.$store.getters.isDomainMode) {
  281. params.project_domains = [this.form.fi.project.domain_id]
  282. }
  283. return params
  284. },
  285. cloudproviderParams () {
  286. return {
  287. scope: this.$store.getters.scope,
  288. cloudaccount_id: this.form.fi.cloudaccount.id || '',
  289. tenant_id: this.form.fi.project.id,
  290. }
  291. },
  292. isGoogle () {
  293. return this.form.fi.provider === 'Google'
  294. },
  295. namePlaceholder () {
  296. return this.isGoogle ? this.$t('system.text_496') : this.params.user.name
  297. },
  298. nameExtra () {
  299. return this.isGoogle ? null : this.$t('cloudenv.clouduser_text1')
  300. },
  301. },
  302. watch: {
  303. isGoogle (val) {
  304. this.decorators.generate_name[1].rules = val ? [
  305. { required: true, message: this.$t('common.text00042') },
  306. { validator: this.$validate('email'), message: this.$t('system.text_497') },
  307. ] : []
  308. if (val) {
  309. this.decorators.cloudgroup_ids[1].rules[0].required = true
  310. } else {
  311. this.decorators.cloudgroup_ids[1].rules[0].required = false
  312. }
  313. this.form.fc.resetFields(['generate_name', 'cloudgroup_ids'])
  314. },
  315. 'form.fi.cloudaccount.provider' (val) {
  316. this.form.fc.resetFields(['cloudgroup_ids', 'cloudprovider_id'])
  317. },
  318. 'form.fi.project.id' (val) {
  319. this.form.fc.resetFields(['cloudgroup_ids', 'cloudprovider_id'])
  320. },
  321. },
  322. destroyed () {
  323. this.um = null
  324. },
  325. created () {
  326. this.um = new this.$Manager('users', 'v1')
  327. this.fetchProjects()
  328. this.fetchReceivers()
  329. },
  330. methods: {
  331. emailValidator (rule, value, callback) {
  332. if (R.isEmpty(value) || R.isNil(value)) {
  333. return callback()
  334. }
  335. return this.$validate('email')(rule, value, callback)
  336. },
  337. async fetchProjects () {
  338. try {
  339. const response = await this.um.getSpecific({
  340. id: this.params.user.id,
  341. spec: 'projects',
  342. params: {
  343. scope: this.$store.getters.scope,
  344. },
  345. })
  346. this.projects = response.data.data || []
  347. this.form.fc.setFieldsValue({
  348. project_id: this.projects[0].id,
  349. })
  350. } catch (error) {
  351. throw error
  352. }
  353. },
  354. async handleConfirm () {
  355. this.loading = true
  356. try {
  357. const { ...values } = await this.form.fc.validateFields()
  358. values.owner_id = this.params.user.id
  359. await this.params.onManager('create', {
  360. managerArgs: {
  361. data: values,
  362. },
  363. })
  364. this.cancelDialog()
  365. } finally {
  366. this.loading = false
  367. }
  368. },
  369. formatterLabel (row) {
  370. return row.description ? `${row.name} / ${row.description}` : row.name
  371. },
  372. cloudaccountMapper (data) {
  373. let _data = [...data]
  374. _data = _data.filter(item => {
  375. if (this.form.fi.provider !== 'Google' && item.share_mode === 'provider_domain' && item.project_domain !== this.form.fi.project.project_domain) {
  376. return false
  377. }
  378. return true
  379. })
  380. return _data
  381. },
  382. async fetchReceivers () {
  383. this.isEnableMail = false
  384. try {
  385. const params = { scope: this.$store.getters.scope, id: this.params.user.id }
  386. const response = await new this.$Manager('receivers', 'v1').list({ params })
  387. const receivers = response.data.data || []
  388. if (receivers.length > 0) {
  389. const receiver = receivers[0]
  390. if (receiver.enabled_contact_types.includes('email')) {
  391. this.isEnableMail = true
  392. this.form.fc.setFieldsValue({
  393. email: receiver.email,
  394. })
  395. }
  396. }
  397. } catch (error) {
  398. throw error
  399. }
  400. },
  401. },
  402. }
  403. </script>