BatchCreateCache.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.image_cache.perform_batch_precache')}}</div>
  4. <div slot="body">
  5. <a-form :form="form.fc">
  6. <a-form-item :label="$t('compute.text_111')" v-bind="formItemLayout">
  7. <a-select v-decorator="decorators.hypervisor" @change="handleHypervisorChange" :placeholder="$t('compute.text_219')" :allow-clear="true">
  8. <a-select-option v-for="item in hostOptions" :key="item.key" :value="item.value">
  9. {{ item.value }}
  10. </a-select-option>
  11. </a-select>
  12. </a-form-item>
  13. <a-form-item :label="$t('compute.host_tag')" class="mb-0" v-bind="formItemLayout">
  14. <tag
  15. v-decorator="decorators.host_tags" :params="{ resources: 'host', with_cloud_meta: false }" extra="" multiple />
  16. </a-form-item>
  17. <a-form-item :label="$t('compute.text_99')" v-bind="formItemLayout">
  18. <a-select v-decorator="decorators.storage" :placeholder="$t('compute.text_219')" :allow-clear="true">
  19. <a-select-option v-for="item in storageOptions" :key="item.key" :value="item.value">
  20. {{ item.value }}
  21. </a-select-option>
  22. </a-select>
  23. </a-form-item>
  24. <a-form-item :label="$t('compute.storage_tag')" class="mb-0" v-bind="formItemLayout">
  25. <tag
  26. v-decorator="decorators.storage_tags" :params="{ resources: 'storage', with_cloud_meta: false }" multiple extra="" />
  27. </a-form-item>
  28. </a-form>
  29. </div>
  30. <div slot="footer">
  31. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  32. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  33. </div>
  34. </base-dialog>
  35. </template>
  36. <script>
  37. import * as R from 'ramda'
  38. import DialogMixin from '@/mixins/dialog'
  39. import WindowsMixin from '@/mixins/windows'
  40. import validateForm from '@/utils/validate'
  41. import Tag from '@/sections/Tag'
  42. export default {
  43. name: 'ImageBatchCreateCache',
  44. components: {
  45. Tag,
  46. },
  47. mixins: [DialogMixin, WindowsMixin],
  48. data () {
  49. return {
  50. loading: false,
  51. form: {
  52. fc: this.$form.createForm(this, {
  53. onValuesChange: (props, values) => {
  54. Object.keys(values).forEach((key) => {
  55. this.$set(this.form.fd, key, values[key])
  56. })
  57. },
  58. }),
  59. fd: {},
  60. },
  61. decorators: {
  62. hypervisor: [
  63. 'hypervisor',
  64. ],
  65. storage: [
  66. 'storage',
  67. ],
  68. host_tags: [
  69. 'host_tags',
  70. {
  71. rules: [
  72. { validator: validateForm('tagName') },
  73. ],
  74. },
  75. ],
  76. storage_tags: [
  77. 'storage_tags',
  78. {
  79. rules: [
  80. { validator: validateForm('tagName') },
  81. ],
  82. },
  83. ],
  84. },
  85. formItemLayout: {
  86. wrapperCol: {
  87. span: 20,
  88. },
  89. labelCol: {
  90. span: 4,
  91. },
  92. },
  93. hypervisors: [],
  94. hypervisor: null,
  95. storageTypes: {},
  96. }
  97. },
  98. computed: {
  99. hostOptions () {
  100. const ret = []
  101. for (let i = 0; i < this.hypervisors.length; i++) {
  102. ret.push({
  103. key: this.hypervisors[i],
  104. value: this.hypervisors[i],
  105. })
  106. }
  107. return ret
  108. },
  109. storageOptions () {
  110. const ret = []
  111. if (!this.hypervisor || !this.storageTypes[this.hypervisor]) {
  112. return ret
  113. }
  114. for (let i = 0; i < this.storageTypes[this.hypervisor].length; i++) {
  115. ret.push({
  116. key: this.storageTypes[this.hypervisor][i],
  117. value: this.storageTypes[this.hypervisor][i],
  118. })
  119. }
  120. return ret
  121. },
  122. },
  123. created () {
  124. this.fetchPlatform()
  125. },
  126. methods: {
  127. async fetchPlatform () {
  128. const res = await new this.$Manager('capabilities').list({
  129. params: {
  130. scope: this.$store.getters.scope,
  131. },
  132. })
  133. this.hypervisors = res.data.data[0].hypervisors
  134. if (this.hypervisors.length > 0) {
  135. this.hypervisor = this.hypervisors[0]
  136. }
  137. this.storageTypes = res.data.data[0].storage_types2
  138. },
  139. handleHypervisorChange (v) {
  140. this.hypervisor = v
  141. this.$nextTick(() => {
  142. this.form.fc.setFieldsValue({ storage: null })
  143. })
  144. },
  145. formatTags (tags) {
  146. const filters = [{}]
  147. Object.keys(tags).forEach(key => {
  148. let value = tags[key]
  149. if (R.is(String, value) && value) {
  150. value = [value]
  151. } else if (R.is(String, value) && !value) {
  152. value = []
  153. } else {
  154. value = value.map(item => item)
  155. }
  156. filters[0][key] = value
  157. })
  158. return { filters }
  159. },
  160. async handleConfirm () {
  161. this.loading = true
  162. try {
  163. const data = {
  164. image_id: this.params.imageId,
  165. auto_cache: true,
  166. }
  167. const { hypervisor, storage, host_tags, storage_tags } = await this.form.fc.validateFields()
  168. if (hypervisor) {
  169. const hostType = []
  170. if (hypervisor === 'kvm') {
  171. hostType.push('hypervisor')
  172. } else if (hypervisor === 'pod') {
  173. hostType.push('container')
  174. }
  175. data.host_type = hostType
  176. }
  177. if (storage) {
  178. const storageType = []
  179. const parts = storage.split('/')
  180. if (parts && parts.length > 0) {
  181. storageType.push(parts[0])
  182. }
  183. data.storage_type = storageType
  184. }
  185. if (host_tags && Object.keys(host_tags).length > 0) {
  186. data.host_tags = this.formatTags(host_tags)
  187. }
  188. if (storage_tags && Object.keys(storage_tags).length > 0) {
  189. data.storage_tags = this.formatTags(storage_tags)
  190. }
  191. const manager = new this.$Manager('cachedimages')
  192. manager.performClassAction({
  193. action: 'cache-image',
  194. data: data,
  195. })
  196. this.loading = false
  197. this.params.refresh()
  198. this.cancelDialog()
  199. } catch (error) {
  200. this.loading = false
  201. throw error
  202. }
  203. },
  204. },
  205. }
  206. </script>