1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

fix(instances): the XML dialog keeps the edit and defines only this VM

Found by review (Codex and agy) of the XML editor's dialog:

- A busy or moved VM made the page reload and the edit was lost. The
  dialog's requests now get these answers as JSON (X-Requested-With, as the
  create page's flavor forms) and show them; any other failure says the
  edit is still in the editor.
- libvirt finds the VM to define by the XML's UUID: another VM's UUID
  overwrote that VM past its lock, a new or missing one added a VM. The
  XML must keep this VM's UUID; a malformed one is refused with the
  parser's error, as on the create page.
- Save sends the XML whose diff the dialog shows, also when two previews
  answer out of order; a failed save keeps the diff and Save.
- The edited XML is defined with the line breaks libvirt gave, not the
  form's CRLF, and the VM's state is read before the definition, so a
  failed read cannot turn a saved change into an error.
This commit is contained in:
catborise 2026-10-24 19:57:34 +03:00
parent ec679d4d7a
commit 3730a95c02
4 changed files with 85 additions and 35 deletions

View file

@ -17,7 +17,8 @@
var save = document.getElementById("xml-review-save");
var reviewed = ""; // Save defines the XML the dialog shows, not later edits
// null after a redirect (the VM is busy or moved): the page says why
// The answer: diff, saved, or error (detail when the VM is busy or moved).
// Any other failure, such as a redirect, keeps the edit and says so.
function post(xml, confirm) {
var data = new FormData(form);
data.set("inst_xml", xml);
@ -25,12 +26,12 @@
if (confirm) {
data.set("confirm", "1");
}
return fetch(form.action, {method: "POST", body: data, redirect: "manual"}).then(function (response) {
if (response.type === "opaqueredirect") {
location.reload();
return null;
}
return fetch(form.action, {
method: "POST", body: data, redirect: "manual", headers: {"X-Requested-With": "XMLHttpRequest"},
}).then(function (response) {
return response.json();
}).then(function (answer) {
return {diff: answer.diff, saved: answer.saved, error: answer.error || answer.detail};
}).catch(function () {
return {error: dialog.dataset.failed};
});
@ -56,23 +57,26 @@
form.addEventListener("submit", function (event) {
event.preventDefault();
reviewed = editor.getValue();
post(reviewed, false).then(function (answer) {
if (answer) {
show(answer);
modal.show();
}
var xml = editor.getValue();
// the last answer shown is the one Save sends, whatever order answers come in
post(xml, false).then(function (answer) {
reviewed = xml;
show(answer);
modal.show();
});
});
save.addEventListener("click", function () {
save.disabled = true;
post(reviewed, true).then(function (answer) {
if (answer && answer.saved) {
if (answer.saved) {
location.hash = "xmledit";
location.reload();
} else if (answer) {
show({error: answer.error, diff: []});
} else {
// the diff stays, and Save can be tried again
error.textContent = answer.error;
error.classList.remove("d-none");
save.disabled = false;
}
});
});