@vmngr/libvirt
Version:
Libvirt bindings for Node.js®
170 lines (169 loc) • 5.65 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var xml2js_1 = __importDefault(require("xml2js"));
function serializeDomainOs(osDesc) {
// tslint:disable-next-line:no-any
var os = { $: {} };
if (osDesc.type) {
os.type = { $: {} };
if (osDesc.type.arch)
os.type.$.arch = osDesc.type.arch;
if (osDesc.type.machine)
os.type.$.machine = osDesc.type.machine;
if (osDesc.type.value)
os.type._ = osDesc.type.value;
}
if (osDesc.boot) {
os.boot = { $: {} };
if (osDesc.boot.dev)
os.boot.$.dev = osDesc.boot.dev;
}
return os;
}
function serializeDomainDisk(diskDesc) {
// tslint:disable-next-line:no-any
var disk = { $: {} };
if (diskDesc.type)
disk.$.type = diskDesc.type;
if (diskDesc.device)
disk.$.device = diskDesc.device;
if (diskDesc.driver) {
disk.driver = { $: {} };
if (diskDesc.driver.name)
disk.driver.$.name = diskDesc.driver.name;
if (diskDesc.driver.type)
disk.driver.$.type = diskDesc.driver.type;
}
if (diskDesc.source) {
disk.source = { $: {} };
if (diskDesc.source.file)
disk.source.$.file = diskDesc.source.file;
}
if (diskDesc.target) {
disk.target = { $: {} };
if (diskDesc.target.dev)
disk.target.$.dev = diskDesc.target.dev;
if (diskDesc.target.bus)
disk.target.$.bus = diskDesc.target.bus;
}
return disk;
}
function serializeDomainInterface(interfaceDesc) {
// tslint:disable-next-line:no-any
var iface = { $: {} };
if (interfaceDesc.type)
iface.$.type = interfaceDesc.type;
if (interfaceDesc.source) {
iface.source = { $: {} };
if (interfaceDesc.source.network) {
iface.source.$.network = interfaceDesc.source.network;
}
}
if (interfaceDesc.mac) {
iface.mac = { $: {} };
if (interfaceDesc.mac.address) {
iface.mac.$.address = interfaceDesc.mac.address;
}
}
if (interfaceDesc.model) {
iface.model = { $: {} };
if (interfaceDesc.model.type) {
iface.model.$.type = interfaceDesc.model.type;
}
}
return iface;
}
function serializeDomainGraphics(graphicsDesc) {
// tslint:disable-next-line:no-any
var graphics = { $: {} };
if (graphicsDesc.type)
graphics.$.type = graphicsDesc.type;
if (graphicsDesc.port)
graphics.$.port = graphicsDesc.port;
if (graphicsDesc.listen)
graphics.$.listen = graphicsDesc.listen;
if (graphicsDesc.passwd)
graphics.$.passwd = graphicsDesc.passwd;
return graphics;
}
function domainDescToXml(desc) {
// tslint:disable-next-line:no-any
var domain = { $: {} };
if (desc.type)
domain.$.type = desc.type;
if (desc.id)
domain.$.id = desc.id;
if (desc.name)
domain.name = desc.name;
if (desc.uuid)
domain.uuid = desc.uuid;
if (desc.memory) {
domain.memory = { $: {} };
if (desc.memory.unit)
domain.memory.$.unit = desc.memory.unit;
if (desc.memory.value)
domain.memory._ = desc.memory.value;
}
if (desc.currentMemory) {
domain.currentMemory = { $: {} };
if (desc.currentMemory.unit) {
domain.currentMemory.$.unit = desc.currentMemory.unit;
}
if (desc.currentMemory.value) {
domain.currentMemory._ = desc.currentMemory.value;
}
}
if (desc.vcpu) {
domain.vcpu = { $: {} };
if (desc.vcpu.placement)
domain.vcpu.$.placement = desc.vcpu.placement;
if (desc.vcpu.value)
domain.vcpu._ = desc.vcpu.value;
}
if (desc.os)
domain.os = serializeDomainOs(desc.os);
if (desc.devices) {
domain.devices = {
emulator: [],
disk: [],
interface: [],
console: [],
graphics: [],
};
for (var _i = 0, _a = desc.devices; _i < _a.length; _i++) {
var deviceDesc = _a[_i];
// tslint:disable-next-line:no-any
var device = { $: {} };
switch (deviceDesc.type) {
case "emulator":
var emulatorDesc = deviceDesc.emulator;
if (emulatorDesc.value)
device._ = emulatorDesc.value;
domain.devices.emulator.push(device);
break;
case "disk":
domain.devices.disk.push(serializeDomainDisk(deviceDesc.disk));
break;
case "interface":
domain.devices.interface.push(serializeDomainInterface(deviceDesc.interface));
break;
case "console":
var consoleDesc = deviceDesc.console;
if (consoleDesc.type)
device.$.type = consoleDesc.type;
domain.devices.console.push(device);
break;
case "graphics":
domain.devices.graphics.push(serializeDomainGraphics(deviceDesc.graphics));
break;
default:
}
}
}
var builder = new xml2js_1.default.Builder();
return builder.buildObject({ domain: domain });
}
exports.domainDescToXml = domainDescToXml;