[pmg-devel] [PATCH v3 api 7/8] add node-config api entry points

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Mar 16 11:24:14 CET 2021


adds /nodes/{nodename}/config to access node config

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
No changes since v2

 src/Makefile               |  1 +
 src/PMG/API2/NodeConfig.pm | 90 ++++++++++++++++++++++++++++++++++++++
 src/PMG/API2/Nodes.pm      |  7 +++
 3 files changed, 98 insertions(+)
 create mode 100644 src/PMG/API2/NodeConfig.pm

diff --git a/src/Makefile b/src/Makefile
index e0629b2..eac682b 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -158,6 +158,7 @@ LIBSOURCES =				\
 	PMG/API2/Certificates.pm	\
 	PMG/API2/ACME.pm		\
 	PMG/API2/ACMEPlugin.pm		\
+	PMG/API2/NodeConfig.pm		\
 	PMG/API2.pm			\
 
 SOURCES = ${LIBSOURCES} ${CLI_BINARIES} ${TEMPLATES_FILES} ${CONF_MANS} ${CLI_MANS} ${SERVICE_MANS} ${SERVICE_UNITS} ${TIMER_UNITS} pmg-sources.list pmg-apt.conf pmg-initramfs.conf
diff --git a/src/PMG/API2/NodeConfig.pm b/src/PMG/API2/NodeConfig.pm
new file mode 100644
index 0000000..284f663
--- /dev/null
+++ b/src/PMG/API2/NodeConfig.pm
@@ -0,0 +1,90 @@
+package PMG::API2::NodeConfig;
+
+use strict;
+use warnings;
+
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::Tools qw(extract_param);
+
+use PMG::NodeConfig;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    name => 'get_config',
+    path => '',
+    method => 'GET',
+    description => "Get node configuration options.",
+    protected => 1,
+    proxyto => 'node',
+    permissions => { check => [ 'admin', 'audit' ] },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns =>  PMG::NodeConfig::acme_config_schema({
+	digest => {
+	    type => 'string',
+	    description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
+	    maxLength => 40,
+	    optional => 1,
+	},
+    }),
+    code => sub {
+	my ($param) = @_;
+
+	return PMG::NodeConfig::load_config();
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'set_config',
+    path => '',
+    method => 'PUT',
+    description => "Set node configuration options.",
+    protected => 1,
+    proxyto => 'node',
+    permissions => { check => [ 'admin', 'audit' ] },
+    parameters => PMG::NodeConfig::acme_config_schema({
+	delete => {
+	    type => 'string', format => 'pve-configid-list',
+	    description => "A list of settings you want to delete.",
+	    optional => 1,
+	},
+	digest => {
+	    type => 'string',
+	    description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
+	    maxLength => 40,
+	    optional => 1,
+	},
+	node => get_standard_option('pve-node'),
+    }),
+    returns => { type => "null" },
+    code => sub {
+	my ($param) = @_;
+
+	my $node = extract_param($param, 'node');
+	my $delete = extract_param($param, 'delete');
+	my $digest = extract_param($param, 'digest');
+
+	PMG::NodeConfig::lock_config(sub {
+	    my $conf = PMG::NodeConfig::load_config();
+
+	    PVE::Tools::assert_if_modified($digest, delete $conf->{digest});
+
+	    foreach my $opt (PVE::Tools::split_list($delete)) {
+		delete $conf->{$opt};
+	    };
+
+	    foreach my $opt (keys %$param) {
+		$conf->{$opt} = $param->{$opt};
+	    }
+
+	    PMG::NodeConfig::write_config($conf);
+	});
+
+	return undef;
+    }});
+
+1;
diff --git a/src/PMG/API2/Nodes.pm b/src/PMG/API2/Nodes.pm
index b6f0cd5..8cdf935 100644
--- a/src/PMG/API2/Nodes.pm
+++ b/src/PMG/API2/Nodes.pm
@@ -28,6 +28,7 @@ use PMG::API2::MailTracker;
 use PMG::API2::Backup;
 use PMG::API2::PBS::Job;
 use PMG::API2::Certificates;
+use PMG::API2::NodeConfig;
 
 use base qw(PVE::RESTHandler);
 
@@ -91,6 +92,11 @@ __PACKAGE__->register_method ({
     path => 'certificates',
 });
 
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::NodeConfig",
+    path => 'config',
+});
+
 __PACKAGE__->register_method ({
     name => 'index',
     path => '',
@@ -133,6 +139,7 @@ __PACKAGE__->register_method ({
 	    { name => 'termproxy' },
 	    { name => 'rrddata' },
 	    { name => 'certificates' },
+	    { name => 'config' },
 	];
 
 	return $result;
-- 
2.20.1





More information about the pmg-devel mailing list