f-88: api: nom obligatoire des interfaces de VM, migration nic-<i> #88

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-29 21:12:42 +01:00
commit ee7ecbf69f
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
15 changed files with 244 additions and 25 deletions

View file

@ -48,7 +48,7 @@ vm_create () {
api_state "vms/${name}" | grep -qx running && return 0
[ -n "${key}" ] && meta="{\"sshkey\":\"${key}\"}"
vm_volume "${name}" || return 1
api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"mac\":\"$(mac_for_ip "${ip}")\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \
api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"name\":\"nic-0\",\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"mac\":\"$(mac_for_ip "${ip}")\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \
&& wait_state "vms/${name}" running
}
@ -109,7 +109,7 @@ ping_rate () {
vm_body () {
local name="${1}" subnet="${2}" ip="${3}" disk="${4:-${VOLUMES}/${1}.qcow2}" meta="${5:-{\}}" dev="${6:-vda}"
printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \
printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"name":"nic-0","subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \
"${name}" "${meta}" "${subnet}" "${ip}" "$(mac_for_ip "${ip}")" "${disk}" "${dev}"
}
@ -121,7 +121,7 @@ vm_json () {
--nic)
sn=${2%%=*}; rest=${2#*=}; ip=${rest%%=*}; mac=$(mac_for_ip "${ip}")
[ "${rest}" != "${ip}" ] && mac=${rest#*=}
nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;;
nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{name: ("nic-" + (length | tostring)), subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;;
--dhcp-server) nics=$(jq -c '.[-1].dhcp_server = true' <<<"${nics}"); shift ;;
--disk) disks=$(jq -c --arg d "${2%%=*}" --arg p "${2#*=}" '. + [{path: $p, dev: $d}]' <<<"${disks}"); shift 2 ;;
--sshkey) meta=$(jq -c --arg v "${2}" '. + {sshkey: $v}' <<<"${meta}"); shift 2 ;;

View file

@ -447,7 +447,7 @@ test_api_field_reads_one_field_or_nothing () {
test_vm_body_builds_the_create_request () {
setup "vm_body : requête de création, disque par défaut, métadonnées et dev"
[[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \
[[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"name":"nic-0","subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \
|| fail "défaut : $(node 'vm_body t11-a sn-t11a 10.11.1.10')"
[[ $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","xvdb"]' ]] \
|| fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb')"
@ -458,10 +458,10 @@ test_vm_json_builds_nics_disks_metadata_and_uefi () {
setup "vm_json : NIC (la première primaire), disques, métadonnées, uefi"
local out
out=$(node 'vm_json t25-m --disk vda=/v/a.qcow2 --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --disk xvda=/v/b.qcow2 --sshkey "ssh-ed25519 AAAA x" --password "\$6\$s\$h" --user-data "I2Nsb3Vk" --uefi')
[[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \
[[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"name":"nic-0","subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"name":"nic-1","subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \
|| fail "complet : ${out}"
out=$(node 'vm_json t21-a --nic sn-a=10.0.1.10')
[[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
[[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"name":"nic-0","subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
out=$(node 'vm_json t21-m --nic sn-a=10.0.1.12=02:de:ad:be:ef:01 | jq -c ".interfaces[0] | [.ip, .mac]"')
[[ "${out}" == '["10.0.1.12","02:de:ad:be:ef:01"]' ]] || fail "mac forcée : ${out}"
out=$(node 'vm_json t76-p --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --dhcp-server | jq -c "[.interfaces[] | .dhcp_server]"')

View file

@ -42,6 +42,8 @@ done
check "VM : subnet inconnu, 422" expect_code 422 api_post_code vms "$(vm_body t11-x sn-t11z 10.11.1.12 "${NONE}")"
check "VM : aucune interface primaire, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | sed 's/"primary":true/"primary":false/')"
check "VM : interface sans mac, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].mac)')"
check "VM : interface sans nom, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].name)')"
check "VM : deux interfaces de même nom, 400" expect_code 400 api_post_code vms "$(vm_json t11-x --disk "vda=${NONE}" --nic sn-t11a=10.11.1.12 --nic sn-t11a=10.11.1.13 | jq -c '.interfaces[1].name = "nic-0"')"
for dev in sdb vdb xvdaa; do
check "VM : disque additionnel ${dev}, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c --arg d "${dev}" '.storage += [{"path":"/x.qcow2","dev":$d}]')"
done