index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <div class="vm-sched-policy">
  3. <a-form-item>
  4. <a-radio-group v-decorator="decorators.schedPolicyType" @change="change">
  5. <a-radio-button v-for="(item, key) of schedPolicyOptionsMap" :value="key" :key="key">{{ item.t ? $t(item.t) : item.label }}</a-radio-button>
  6. </a-radio-group>
  7. </a-form-item>
  8. <a-form-item v-if="schedPolicyComponent === 'host'" class="host-form-item">
  9. <template v-if="serverType === 'baremetal'">
  10. <base-select
  11. class="w-50"
  12. :options="hostData"
  13. :disabled-items="disabledHost"
  14. v-decorator="decorators.schedPolicyHost"
  15. :params="policyHostParams"
  16. :label-format="labelFormat"
  17. :need-params="true"
  18. :filterable="true"
  19. :showSync="true"
  20. @change="hostChange"
  21. :select-props="{ placeholder: lodash.get(schedPolicyOptionsMap, 'host.label') || '' }" />
  22. </template>
  23. <template v-else>
  24. <!-- <base-select
  25. class="w-50"
  26. resource="cloudproviders"
  27. v-if="showCloudproviderSelect"
  28. v-decorator="decorators.schedPolicyHost"
  29. :params="policycloudproviderParams"
  30. :disabledItems="disabledCloudproviders"
  31. :label-format="cloudproviderLabel"
  32. :resList.sync="allCloudproviders"
  33. :need-params="true"
  34. :filterable="true"
  35. :showSync="true"
  36. :select-props="{ placeholder: lodash.get(schedPolicyOptionsMap, 'host.label') || '' }" /> -->
  37. <base-select
  38. v-if="!showCloudproviderSelect"
  39. class="w-50"
  40. resource="hosts"
  41. :disabled-items="disabledHost"
  42. v-decorator="decorators.schedPolicyHost"
  43. :params="policyHostParams"
  44. :label-format="labelFormat"
  45. :need-params="true"
  46. :filterable="true"
  47. :showSync="true"
  48. :select-props="{ placeholder: lodash.get(schedPolicyOptionsMap, 'host.label') || '' }" />
  49. </template>
  50. </a-form-item>
  51. <a-form-item v-if="schedPolicyComponent === 'schedtag'">
  52. <policy-schedtag
  53. ref="policySchedtagRef"
  54. :form="form"
  55. :decorators="decorators.policySchedtag"
  56. :schedtag-params="policySchedtagParams" />
  57. </a-form-item>
  58. <a-form-item v-if="schedPolicyComponent === 'cloudprovider'">
  59. <base-select
  60. class="w-50"
  61. v-decorator="decorators.cloudprovider"
  62. resource="cloudproviders"
  63. :params="cloudproviderParams"
  64. :isDefaultSelect="true"
  65. :showSync="true"
  66. :select-props="{ placeholder: $t('compute.text_149') }" />
  67. </a-form-item>
  68. </div>
  69. </template>
  70. <script>
  71. import * as R from 'ramda'
  72. import lodash from 'lodash'
  73. import { SERVER_TYPE, SCHED_POLICY_OPTIONS_MAP } from '@Compute/constants'
  74. import { arrayToObj, uuid } from '@/utils/utils'
  75. import { HYPERVISORS_MAP } from '@/constants'
  76. import PolicySchedtag from './PolicySchedtag'
  77. export default {
  78. name: 'SchedPolicy',
  79. components: {
  80. PolicySchedtag,
  81. },
  82. props: {
  83. decorators: {
  84. type: Object,
  85. required: true,
  86. validator: val => val.schedPolicyType && val.schedPolicyHost && val.policySchedtag,
  87. },
  88. serverType: {
  89. type: String,
  90. required: true,
  91. validator: val => SERVER_TYPE[val],
  92. },
  93. policySchedtagParams: {
  94. type: Object,
  95. default: () => ({}),
  96. },
  97. policyHostParams: {
  98. type: Object,
  99. default: () => ({}),
  100. },
  101. disabledHost: {
  102. type: Array,
  103. default: () => [],
  104. },
  105. hostData: {
  106. type: Array,
  107. default: () => [],
  108. },
  109. form: {
  110. type: Object,
  111. validator: val => !val || val.fc, // 不传 或者 传就有fc
  112. },
  113. hideCloudaccountSched: { // 隐藏 指定云订阅(hosts接口)
  114. type: Boolean,
  115. default: false,
  116. },
  117. showSchedCloudprovider: { // 指定显示云账号(cloudprovider接口)
  118. type: Boolean,
  119. default: false,
  120. },
  121. cloudproviderParamsExtra: {
  122. type: Object,
  123. default: () => ({}),
  124. },
  125. provider: {
  126. type: String,
  127. },
  128. policycloudproviderParams: {
  129. type: Object,
  130. default: () => ({}),
  131. },
  132. },
  133. data () {
  134. return {
  135. schedPolicyComponent: '',
  136. lodash,
  137. usableCloudproviderMaps: {},
  138. allCloudproviders: [],
  139. }
  140. },
  141. computed: {
  142. schedPolicyOptionsMap () {
  143. const { default: _default, host, ...rest } = SCHED_POLICY_OPTIONS_MAP
  144. let ret = {}
  145. ret.default = { ..._default }
  146. ret.host = {
  147. ...host,
  148. label: host.label[this.serverType],
  149. }
  150. if (this.serverType !== SERVER_TYPE.public) {
  151. ret = {
  152. ...ret,
  153. ...rest,
  154. }
  155. }
  156. // 限制非管理后台模式下不能指定宿主机(私有云)、云账号(公有云)
  157. if (!this.$store.getters.isAdminMode && !this.$store.getters.isDomainMode) {
  158. delete ret.host
  159. }
  160. if (this.hideCloudaccountSched) {
  161. delete ret.host
  162. }
  163. if (this.serverType === SERVER_TYPE.public) {
  164. delete ret.host
  165. delete ret.cloudprovider
  166. }
  167. return ret
  168. },
  169. cloudproviderParams () {
  170. const params = {
  171. limit: 0,
  172. enabled: true,
  173. 'filter.0': 'status.equals("connected")',
  174. 'filter.1': 'health_status.equals("normal")',
  175. ...this.cloudproviderParamsExtra,
  176. }
  177. if (!params.scope && !params.project_domain) {
  178. params.scope = this.$store.getters.scope
  179. }
  180. return params
  181. },
  182. showCloudproviderSelect () { // 在公有云的情况下
  183. if (this.form && this.serverType === SERVER_TYPE.public) {
  184. const schedPolicyType = this.form.fc.getFieldsValue([this.decorators.schedPolicyType[0]])[this.decorators.schedPolicyType[0]]
  185. if (schedPolicyType === 'host') { // 公有云 此时 host 表示 指定云订阅
  186. return true
  187. }
  188. }
  189. return this.provider === HYPERVISORS_MAP.hcso.provider || this.provider === HYPERVISORS_MAP.hcs.provider
  190. },
  191. disabledCloudproviders () {
  192. return this.allCloudproviders.filter(val => !this.usableCloudproviderMaps[val.id]).map(val => val.id)
  193. },
  194. },
  195. watch: {
  196. schedPolicyOptionsMap (val) {
  197. this.$nextTick(() => {
  198. const keys = Object.keys(val)
  199. if (keys.length) {
  200. if (this.form && this.form.fc) {
  201. const schedPolicyType = this.schedPolicyOptionsMap[keys[0]].key
  202. this.form.fc.setFieldsValue({
  203. [this.decorators.schedPolicyType[0]]: schedPolicyType,
  204. })
  205. this.change(schedPolicyType)
  206. }
  207. }
  208. })
  209. },
  210. policycloudproviderParams (val, oldV) {
  211. if (!R.equals(val, oldV)) {
  212. this.fetchUsagebleCloudprovider()
  213. }
  214. },
  215. },
  216. created () {
  217. this.cloudproviderM = new this.$Manager('cloudproviders')
  218. this.fetchUsagebleCloudprovider()
  219. },
  220. methods: {
  221. cloudproviderLabel (item) {
  222. let label = item.name
  223. if (!this.usableCloudproviderMaps[item.id]) {
  224. if (item.status !== 'connected') {
  225. label += this.$t('compute.text_184')
  226. } else if (item.health_status !== 'normal') {
  227. label += this.$t('compute.text_185')
  228. } else if (item.enabled === false) {
  229. label += this.$t('compute.text_186')
  230. } else {
  231. label += this.$t('compute.text_187')
  232. }
  233. }
  234. return label
  235. },
  236. async fetchUsagebleCloudprovider () {
  237. try {
  238. const usageParmas = {
  239. enabled: true,
  240. 'filter.0': 'status.equals("connected")',
  241. 'filter.1': 'health_status.equals("normal")',
  242. usable: true,
  243. $t: uuid(),
  244. }
  245. const { data: { data = [] } } = await this.cloudproviderM.list({ params: { ...this.policycloudproviderParams, ...usageParmas } })
  246. this.usableCloudproviderMaps = arrayToObj(data)
  247. } catch (error) {
  248. throw error
  249. }
  250. },
  251. change (e) {
  252. const schedPolicyType = lodash.isString(e) ? e : e.target.value
  253. switch (schedPolicyType) {
  254. case lodash.get(this.schedPolicyOptionsMap, 'default.key'):
  255. this.schedPolicyComponent = ''
  256. break
  257. case lodash.get(this.schedPolicyOptionsMap, 'host.key'):
  258. this.schedPolicyComponent = 'host'
  259. break
  260. case lodash.get(this.schedPolicyOptionsMap, 'schedtag.key'):
  261. this.schedPolicyComponent = 'schedtag'
  262. break
  263. case lodash.get(this.schedPolicyOptionsMap, 'cloudprovider.key'):
  264. this.schedPolicyComponent = 'cloudprovider'
  265. break
  266. }
  267. },
  268. labelFormat (item) {
  269. if (this.serverType === SERVER_TYPE.public) {
  270. return `${item.account} / ${item.manager} / ${item.zone}`
  271. }
  272. return item.name
  273. },
  274. hostChange (e) {
  275. this.$emit('change', e)
  276. },
  277. },
  278. }
  279. </script>
  280. <style lang="less" scoped>
  281. .vm-sched-policy {
  282. .host-form-item ::v-deep .ant-form-item-control {
  283. width: 100%;
  284. }
  285. }
  286. </style>