]> git.proxmox.com Git - qemu-server.git/commitdiff
api: add endpoint for parsing .ovf files
authorFabian Ebner <f.ebner@proxmox.com>
Thu, 17 Mar 2022 11:31:03 +0000 (12:31 +0100)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Mon, 4 Apr 2022 14:40:55 +0000 (16:40 +0200)
Co-developed-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Dominic Jäger <d.jaeger@proxmox.com>
[split into its own patch + minor improvements/style fixes]
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
[renamed API handler, since it's not an index]
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
PVE/API2/Qemu/Makefile
PVE/API2/Qemu/OVF.pm [new file with mode: 0644]
PVE/QemuServer.pm

index 5d4abda6c6ab9688c1f206c26021e7ad646dfdb3..bdd4762b28a71cb172dd8a537fef754ba006f982 100644 (file)
@@ -1,4 +1,4 @@
-SOURCES=Agent.pm CPU.pm Machine.pm
+SOURCES=Agent.pm CPU.pm Machine.pm OVF.pm
 
 .PHONY: install
 install:
diff --git a/PVE/API2/Qemu/OVF.pm b/PVE/API2/Qemu/OVF.pm
new file mode 100644 (file)
index 0000000..cc0ef2d
--- /dev/null
@@ -0,0 +1,53 @@
+package PVE::API2::Qemu::OVF;
+
+use strict;
+use warnings;
+
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::QemuServer::OVF;
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    name => 'readovf',
+    path => '',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Read an .ovf manifest.",
+    protected => 1,
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           manifest => {
+               description => "Path to .ovf manifest.",
+               type => 'string',
+           },
+       },
+    },
+    returns => {
+       type => 'object',
+       additionalProperties => 1,
+       properties => PVE::QemuServer::json_ovf_properties(),
+       description => "VM config according to .ovf manifest.",
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $manifest = $param->{manifest};
+       die "check for file $manifest failed - $!\n" if !-f $manifest;
+
+       my $parsed = PVE::QemuServer::OVF::parse_ovf($manifest);
+       my $result;
+       $result->{cores} = $parsed->{qm}->{cores};
+       $result->{name} =  $parsed->{qm}->{name};
+       $result->{memory} = $parsed->{qm}->{memory};
+       my $disks = $parsed->{disks};
+       for my $disk (@$disks) {
+           $result->{$disk->{disk_address}} = $disk->{backing_file};
+       }
+       return $result;
+    }});
+
+1;
index bdc18f05bed802563e140adf1506ca48dac1d0b5..02e579657c71093aa91b787381c33d4d458ea758 100644 (file)
@@ -2203,6 +2203,38 @@ sub json_config_properties {
     return $prop;
 }
 
+# Properties that we can read from an OVF file
+sub json_ovf_properties {
+    my $prop = {};
+
+    for my $device (PVE::QemuServer::Drive::valid_drive_names()) {
+       $prop->{$device} = {
+           type => 'string',
+           format => 'pve-volume-id-or-absolute-path',
+           description => "Disk image that gets imported to $device",
+           optional => 1,
+       };
+    }
+
+    $prop->{cores} = {
+       type => 'integer',
+       description => "The number of CPU cores.",
+       optional => 1,
+    };
+    $prop->{memory} = {
+       type => 'integer',
+       description => "Amount of RAM for the VM in MB.",
+       optional => 1,
+    };
+    $prop->{name} = {
+       type => 'string',
+       description => "Name of the VM.",
+       optional => 1,
+    };
+
+    return $prop;
+}
+
 # return copy of $confdesc_cloudinit to generate documentation
 sub cloudinit_config_properties {