public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 4/4] api2: add cloudinit config api
Date: Fri, 19 Mar 2021 13:06:56 +0100	[thread overview]
Message-ID: <20210319120656.86740-5-aderumier@odiso.com> (raw)
In-Reply-To: <20210319120656.86740-1-aderumier@odiso.com>

---
 PVE/API2/Qemu.pm | 132 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index ea74c69..9f3a55e 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -1039,6 +1039,138 @@ __PACKAGE__->register_method({
 	return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash);
    }});
 
+__PACKAGE__->register_method({
+    name => 'vm_cloudinit',
+    path => '{vmid}/cloudinit',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Get the cloudinit configuration with both current and pending values.",
+    permissions => {
+	check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+	},
+    },
+    returns => {
+	type => "array",
+	items => {
+	    type => "object",
+	    properties => {
+		key => {
+		    description => "Configuration option name.",
+		    type => 'string',
+		},
+		value => {
+		    description => "Current value.",
+		    type => 'string',
+		    optional => 1,
+		},
+		pending => {
+		    description => "Pending value.",
+		    type => 'string',
+		    optional => 1,
+		},
+		delete => {
+		    description => "Indicates a pending delete request if present and not 0. " .
+		                   "The value 2 indicates a force-delete request.",
+		    type => 'integer',
+		    minimum => 0,
+		    maximum => 2,
+		    optional => 1,
+		},
+	    },
+	},
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $vmid = $param->{vmid};
+	my $conf = PVE::QemuConfig->load_config($vmid);
+
+	if( defined($conf->{cipassword}) && 
+	    defined($conf->{cloudinit}->{cipassword}) && 
+	    $conf->{cipassword} ne $conf->{cloudinit}->{cipassword}) {
+	    $conf->{cipassword} = '********** ';
+	} elsif (defined($conf->{cipassword})) {
+	    $conf->{cipassword} = '**********';
+	}
+
+	$conf->{cloudinit}->{cipassword} = '**********' if defined($conf->{cloudinit}->{cipassword});
+	
+
+	my $cloudinit_current = $conf->{cloudinit};
+	my $res = [];
+
+	my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()};
+	push @cloudinit_opts, 'name';
+
+
+	#add cloud-init drive 
+	my $drives = {};
+	PVE::QemuConfig->foreach_volume($conf, sub {
+                my ($ds, $drive) = @_;
+		$drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive);
+        });
+
+	PVE::QemuConfig->foreach_volume($cloudinit_current, sub {
+                my ($ds, $drive) = @_;
+		$drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive);
+        });
+	foreach my $ds (keys %{$drives}) {
+	    push @cloudinit_opts, $ds;
+	}
+
+
+	$conf->{name} = "VM$vmid" if !$conf->{name};
+	
+	my $print_net_addr = sub {
+	    my ($conf, $opt, $netid) = @_;
+
+	    if (defined($conf->{$netid})) {
+	        
+		my $net = PVE::QemuServer::parse_net($conf->{$netid});
+		if (defined($conf->{$opt})) {
+		    $conf->{$opt} .= ",macaddr=".$net->{macaddr} if $net->{macaddr};
+		} else {
+		    $conf->{$opt} = "";
+		}
+	    }
+	};
+
+	foreach my $opt (@cloudinit_opts) {
+
+	    #add macaddr to ipconfig
+	    if ($opt =~ m/^ipconfig(\d+)/) {
+		my $netid = "net$1";
+		next if !defined($conf->{$netid}) && !defined($cloudinit_current->{$netid}) && !defined($conf->{$opt}) && !defined($cloudinit_current->{$opt} );
+
+		&$print_net_addr($conf, $opt, $netid);
+		&$print_net_addr($cloudinit_current, $opt, $netid);
+	    }
+
+	    my $item = {
+		key => $opt,
+	    };
+	    if ($cloudinit_current->{$opt}) {
+		$item->{value} = $cloudinit_current->{$opt};
+		if ($conf->{$opt}) {
+		    $item->{pending} = $conf->{$opt} if $conf->{$opt} ne $cloudinit_current->{$opt};
+		} else {
+		    $item->{delete} = 1;
+		}
+	    } else {
+		$item->{pending} = $conf->{$opt} if $conf->{$opt}
+	    }
+    
+	    push @$res, $item;
+	}
+	return $res;
+   }});
+
 # POST/PUT {vmid}/config implementation
 #
 # The original API used PUT (idempotent) an we assumed that all operations
-- 
2.20.1




      parent reply	other threads:[~2021-03-19 12:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 12:06 [pve-devel] [PATCH qemu-server 0/4] RFC: cloudinit pending behaviour change Alexandre Derumier
2021-03-19 12:06 ` [pve-devel] [PATCH qemu-server 1/4] cloudinit: add cloudinit section for current generated config Alexandre Derumier
2021-03-24  5:58   ` Thomas Lamprecht
2021-03-25 16:29     ` aderumier
2021-03-19 12:06 ` [pve-devel] [PATCH qemu-server 2/4] generate cloudinit when vm is offline too Alexandre Derumier
2021-03-19 12:06 ` [pve-devel] [PATCH qemu-server 3/4] cloudinit: make cloudnit options fastplug Alexandre Derumier
2021-03-19 12:06 ` Alexandre Derumier [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210319120656.86740-5-aderumier@odiso.com \
    --to=aderumier@odiso.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal