BastionHost.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <div>
  3. <a-form-item v-if="!inDialog" :label="$t('compute.bastionHost.bastion_host')">
  4. <a-tooltip
  5. :title="switchDisabled ? $t('compute.bastionHost.bastion_host.switch_disabled') : null">
  6. <a-switch
  7. v-decorator="decorator.bastion_host_enable"
  8. :checkedChildren="$t('compute.text_115')"
  9. :unCheckedChildren="$t('compute.text_116')"
  10. :disabled="switchDisabled"
  11. @change="changeHandle" />
  12. </a-tooltip>
  13. </a-form-item>
  14. <template v-if="inDialog || bastionHostEnable">
  15. <a-form-item class="mt-2" :label="inDialog ? $t('compute.bastionHost.bastion_host') : $t('compute.bastionHost.add_bastion_host')">
  16. <base-select
  17. v-decorator="decorator.bastion_host_id"
  18. :options="bastionHosts"
  19. :filterable="true"
  20. :select-props="{
  21. placeholder: $t('compute.bastionHost.bastion_host.placeholder'),
  22. loading: bastionHostLoading
  23. }"
  24. @change="bastionHostChangeHandle" />
  25. </a-form-item>
  26. <a-form-item class="mt-2" :label="$t('compute.bastion_host_org')">
  27. <base-select
  28. v-decorator="decorator.bastion_org_id"
  29. :options="orgs"
  30. :filterable="true"
  31. :select-props="{
  32. placeholder: $t('compute.bastionHost.bastion_host.placeholder'),
  33. loading: orgLoading
  34. }"
  35. @change="bastionOrgChangeHandle" />
  36. </a-form-item>
  37. <a-form-item class="mt-2" :label="$t('compute.bastionHost.node')">
  38. <base-select
  39. v-decorator="decorator.nodes"
  40. :options="nodes"
  41. :filterable="true"
  42. :select-props="{
  43. placeholder: $t('compute.bastionHost.node.placeholder'),
  44. allowClear: true,
  45. mode: 'multiple',
  46. loading: nodeLoading
  47. }" />
  48. </a-form-item>
  49. <a-form-item class="mt-2" :label="$t('compute.bastionHost.port')">
  50. <a-input-number
  51. v-decorator="decorator.port"
  52. :placeholder="$t('compute.bastionHost.port.placeholder')" />
  53. </a-form-item>
  54. <a-form-item class="mt-2" :label="$t('compute.bastionHost.privileged_account')">
  55. <base-select
  56. v-decorator="decorator.privileged_accounts"
  57. :options="privilegedAccounts"
  58. :filterable="true"
  59. :select-props="{
  60. placeholder: $t('compute.bastionHost.privileged_account.placeholder'),
  61. allowClear: true,
  62. loading: accountLoading
  63. }" />
  64. </a-form-item>
  65. <a-form-item class="mt-2" :label="$t('compute.bastionHost.account')">
  66. <base-select
  67. v-decorator="decorator.accounts"
  68. :options="accounts"
  69. :filterable="true"
  70. :select-props="{
  71. placeholder: $t('compute.bastionHost.account.placeholder'),
  72. allowClear: true,
  73. mode: 'multiple',
  74. loading: accountLoading
  75. }" />
  76. </a-form-item>
  77. <a-form-item class="mt-2" :label="$t('compute.bastionHost.domain')">
  78. <base-select
  79. v-decorator="decorator.bastion_domain_id"
  80. :options="domains"
  81. :filterable="true"
  82. :select-props="{
  83. placeholder: $t('common.tips.select', [$t('compute.bastionHost.domain')]),
  84. allowClear: true,
  85. loading: domainLoading
  86. }" />
  87. </a-form-item>
  88. </template>
  89. </div>
  90. </template>
  91. <script>
  92. export default {
  93. name: 'BastionHost',
  94. props: {
  95. form: {
  96. type: Object,
  97. required: true,
  98. validator: val => val.fd && val.fc,
  99. },
  100. decorator: {
  101. type: Object,
  102. required: true,
  103. },
  104. inDialog: {
  105. type: Boolean,
  106. default: false,
  107. },
  108. },
  109. data () {
  110. return {
  111. bastionHostEnable: false,
  112. bastionHostLoading: false,
  113. nodeLoading: false,
  114. accountLoading: false,
  115. domainLoading: false,
  116. orgs: [],
  117. bastionHosts: [],
  118. nodes: [],
  119. privilegedAccounts: [],
  120. accounts: [],
  121. domains: [],
  122. currentBastionHostId: '',
  123. }
  124. },
  125. computed: {
  126. switchDisabled () {
  127. return !this.bastionHosts.length
  128. },
  129. },
  130. watch: {
  131. bastionHostEnable (v) {
  132. if (v) {
  133. this.fetchBastionHosts()
  134. }
  135. },
  136. },
  137. created () {
  138. if (this.$appConfig.isPrivate && !this.$store.getters.isSysCE) {
  139. this.fetchBastionHosts()
  140. }
  141. },
  142. methods: {
  143. initData (data) {
  144. this.bastionHostEnable = true
  145. this.currentBastionHostId = data.bastion_host_id
  146. this.fetchOrgs(data.bastion_host_id, data.bastion_org_id)
  147. this.fetchNodes(data.bastion_org_id, data.nodes)
  148. this.fetchAllAccounts(data.bastion_org_id, data.accounts)
  149. this.fetchDomains(data.bastion_org_id, data.bastion_domain_id)
  150. },
  151. changeHandle (v) {
  152. this.bastionHostEnable = v
  153. },
  154. bastionHostChangeHandle (v) {
  155. this.currentBastionHostId = v
  156. this.fetchOrgs(v)
  157. },
  158. bastionOrgChangeHandle (v) {
  159. if (this.currentBastionHostId) {
  160. this.fetchNodes(v)
  161. this.fetchAllAccounts(v)
  162. this.fetchDomains(v)
  163. } else {
  164. this.nodes = []
  165. this.accounts = []
  166. this.domains = []
  167. }
  168. },
  169. async fetchOrgs (bastionHostId, defaultOrgId) {
  170. try {
  171. this.orgLoading = true
  172. this.orgs = []
  173. const { data: { orgs = [] } } = await new this.$Manager('bastion_hosts')
  174. .getSpecific({ id: bastionHostId, spec: 'bastion-orgs' })
  175. this.orgs = orgs.map(o => {
  176. return {
  177. key: o.id,
  178. label: o.name,
  179. }
  180. })
  181. this.form.fc.setFieldsValue({
  182. bastion_org_id: defaultOrgId,
  183. })
  184. } catch (error) {
  185. throw error
  186. } finally {
  187. this.orgLoading = false
  188. }
  189. },
  190. async fetchBastionHosts () {
  191. try {
  192. this.bastionHostLoading = true
  193. this.bastionHosts = []
  194. const { data: { data = [] } } = await new this.$Manager('bastion_hosts').list({})
  195. this.bastionHosts = data
  196. } catch (error) {
  197. throw error
  198. } finally {
  199. this.bastionHostLoading = false
  200. }
  201. },
  202. async fetchNodes (bastionOrgId, defaultNodes) {
  203. try {
  204. this.nodeLoading = true
  205. this.nodes = []
  206. const { data: { nodes = [] } } = await new this.$Manager('bastion_hosts')
  207. .getSpecific({ id: this.currentBastionHostId, spec: 'nodes', params: { bastion_org_id: bastionOrgId } })
  208. this.nodes = nodes.map(o => {
  209. return {
  210. key: o.id,
  211. label: o.name,
  212. }
  213. })
  214. this.form.fc.setFieldsValue({
  215. nodes: defaultNodes,
  216. })
  217. } catch (error) {
  218. throw error
  219. } finally {
  220. this.nodeLoading = false
  221. }
  222. },
  223. async fetchAllAccounts (bastionOrgId, defaultAccounts = []) {
  224. try {
  225. this.accountLoading = true
  226. this.accounts = []
  227. this.privilegedAccounts = []
  228. const { data: { account_templates = [] } } = await new this.$Manager('bastion_hosts')
  229. .getSpecific({ id: this.currentBastionHostId, spec: 'account-templates', params: { bastion_org_id: bastionOrgId } })
  230. this.accounts = account_templates.filter(o => !o.privileged).map(o => {
  231. return {
  232. key: o.id,
  233. label: o.name,
  234. }
  235. })
  236. this.privilegedAccounts = account_templates.filter(o => o.privileged).map(o => {
  237. return {
  238. key: o.id,
  239. label: o.name,
  240. }
  241. })
  242. const privilegedAccounts = defaultAccounts.length ? defaultAccounts[0] : []
  243. const accounts = defaultAccounts.filter((_, index) => index !== 0)
  244. this.form.fc.setFieldsValue({
  245. accounts,
  246. privileged_accounts: privilegedAccounts,
  247. })
  248. } catch (error) {
  249. throw error
  250. } finally {
  251. this.accountLoading = false
  252. }
  253. },
  254. async fetchDomains (bastionOrgId, defaultDomainId) {
  255. try {
  256. this.domainLoading = true
  257. this.domains = []
  258. const { data: { domains = [] } } = await new this.$Manager('bastion_hosts')
  259. .getSpecific({ id: this.currentBastionHostId, spec: 'bastion-domains', params: { bastion_org_id: bastionOrgId } })
  260. this.domains = domains.map(o => {
  261. return {
  262. key: o.id,
  263. label: o.name,
  264. }
  265. })
  266. this.form.fc.setFieldsValue({
  267. bastion_domain_id: defaultDomainId,
  268. })
  269. } catch (error) {
  270. throw error
  271. } finally {
  272. this.domainLoading = false
  273. }
  274. },
  275. },
  276. }
  277. </script>
  278. <style></style>