|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <a-spin :spinning="loading"> |
| 20 | + <a-form |
| 21 | + class="form" |
| 22 | + layout="vertical" |
| 23 | + :ref="formRef" |
| 24 | + :model="form" |
| 25 | + > |
| 26 | + <a-alert type="warning"> |
| 27 | + <template #message> |
| 28 | + <span v-html="$t('message.warning.host.devices')" /> |
| 29 | + </template> |
| 30 | + </a-alert> |
| 31 | + <br> |
| 32 | + <a-form-item :label="$t('label.virtualmachine')" name="virtualmachineid" ref="virtualmachineid"> |
| 33 | + <a-select |
| 34 | + v-focus="true" |
| 35 | + v-model:value="form.virtualmachineid" |
| 36 | + :placeholder="$t('label.select.vm')" |
| 37 | + showSearch |
| 38 | + optionFilterProp="label" |
| 39 | + :filterOption="filterOption" |
| 40 | + > |
| 41 | + <a-select-option v-for="vm in virtualmachines" :key="vm.id" :label="vm.name || vm.displayname"> |
| 42 | + {{ vm.name || vm.displayname }} |
| 43 | + </a-select-option> |
| 44 | + </a-select> |
| 45 | + <div class="actions"> |
| 46 | + <a-button @click="closeAction">{{ $t('label.cancel') }}</a-button> |
| 47 | + <a-button type="primary" ref="submit" @click="handleSubmit">{{ $t('label.ok') }}</a-button> |
| 48 | + </div> |
| 49 | + </a-form-item> |
| 50 | + </a-form> |
| 51 | + </a-spin> |
| 52 | +</template> |
| 53 | + |
| 54 | +<script> |
| 55 | +import { reactive } from 'vue' |
| 56 | +import { api } from '@/api' |
| 57 | +
|
| 58 | +export default { |
| 59 | + name: 'HostDevicesTransfer', |
| 60 | + props: { |
| 61 | + resource: { |
| 62 | + type: Object, |
| 63 | + required: true |
| 64 | + } |
| 65 | + }, |
| 66 | + data () { |
| 67 | + return { |
| 68 | + virtualmachines: [], |
| 69 | + loading: true, |
| 70 | + form: reactive({ virtualmachineid: null }), |
| 71 | + resourceType: 'UserVm' |
| 72 | + } |
| 73 | + }, |
| 74 | + created () { |
| 75 | + this.fetchVMs() |
| 76 | + }, |
| 77 | + methods: { |
| 78 | + fetchVMs () { |
| 79 | + this.loading = true |
| 80 | + const params = { hostid: this.resource.id, details: 'min' } |
| 81 | + const vmStates = ['Running'] |
| 82 | +
|
| 83 | + vmStates.forEach((state) => { |
| 84 | + params.state = state |
| 85 | + api('listVirtualMachines', params).then(response => { |
| 86 | + this.virtualmachines = this.virtualmachines.concat(response.listvirtualmachinesresponse.virtualmachine || []) |
| 87 | + }).catch(error => { |
| 88 | + this.$notifyError(error.message || 'Failed to fetch VMs') |
| 89 | + }).finally(() => { |
| 90 | + this.loading = false |
| 91 | + }) |
| 92 | + }) |
| 93 | + }, |
| 94 | + handleSubmit () { |
| 95 | + if (!this.form.virtualmachineid) { |
| 96 | + this.$notifyError(this.$t('message.error.select.vm')) |
| 97 | + return |
| 98 | + } |
| 99 | +
|
| 100 | + this.loading = true |
| 101 | + const vmId = this.form.virtualmachineid |
| 102 | + const hostDevicesName = this.resource.hostDevicesName |
| 103 | +
|
| 104 | + api('listVirtualMachines', { |
| 105 | + id: vmId, |
| 106 | + details: 'min' |
| 107 | + }).then(response => { |
| 108 | + const vm = response.listvirtualmachinesresponse.virtualmachine[0] |
| 109 | + const details = vm.details || {} |
| 110 | +
|
| 111 | + const xmlConfig = this.generateXmlConfig(hostDevicesName) |
| 112 | +
|
| 113 | + const params = { id: vmId } |
| 114 | +
|
| 115 | + // 기존 details 값을 유지하면서 새로운 값 추가 |
| 116 | + Object.keys(details).forEach(key => { |
| 117 | + params[`details[0].${key}`] = details[key] |
| 118 | + }) |
| 119 | +
|
| 120 | + // 필요한 추가 값 동적으로 설정 |
| 121 | + params[`details[0].extraconfig-1`] = xmlConfig |
| 122 | +
|
| 123 | + return api('updateVirtualMachine', params) |
| 124 | + }).then(() => { |
| 125 | + this.$message.success(this.$t('message.success.update.vm')) |
| 126 | + this.closeAction() |
| 127 | + }).catch(error => { |
| 128 | + this.$notifyError(error.message || 'Failed to update VM') |
| 129 | + this.formRef.value.scrollToField('virtualmachineid') |
| 130 | + }).finally(() => { |
| 131 | + this.loading = false |
| 132 | + }) |
| 133 | + }, |
| 134 | +
|
| 135 | + generateXmlConfig (hostDevicesName) { |
| 136 | + const [pciAddress] = hostDevicesName.split(' ') |
| 137 | + const [bus, slotFunction] = pciAddress.split(':') |
| 138 | + const [slot, func] = slotFunction.split('.') |
| 139 | +
|
| 140 | + return ` |
| 141 | + <devices> |
| 142 | + <hostdev mode='subsystem' type='pci' managed='yes'> |
| 143 | + <source> |
| 144 | + <address domain='0x0000' bus='${parseInt(bus, 16)}' slot='0x${parseInt(slot, 16)}' function='0x${parseInt(func, 16)}'/> |
| 145 | + </source> |
| 146 | + </hostdev> |
| 147 | + </devices> |
| 148 | + `.trim() |
| 149 | + }, |
| 150 | +
|
| 151 | + closeAction () { |
| 152 | + this.$emit('close-action') |
| 153 | + }, |
| 154 | +
|
| 155 | + filterOption (input, option) { |
| 156 | + return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0 |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | +</script> |
| 161 | + |
| 162 | +<style lang="scss" scoped> |
| 163 | +.form { |
| 164 | + width: 80vw; |
| 165 | +
|
| 166 | + @media (min-width: 500px) { |
| 167 | + width: 475px; |
| 168 | + } |
| 169 | +} |
| 170 | +.actions { |
| 171 | + display: flex; |
| 172 | + justify-content: flex-end; |
| 173 | + margin-top: 20px; |
| 174 | + button { |
| 175 | + &:not(:last-child) { |
| 176 | + margin-right: 10px; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | +</style> |
0 commit comments