]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN.pm
fix zones on_update_hook
[pve-network.git] / PVE / API2 / Network / SDN.pm
CommitLineData
86d22462 1package PVE::API2::Network::SDN;
92b6f291
AD
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
4140be9e
AD
7use PVE::Tools;
8use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
92b6f291 9use PVE::RESTHandler;
4140be9e
AD
10use PVE::RPCEnvironment;
11use PVE::JSONSchema qw(get_standard_option);
12use PVE::Exception qw(raise_param_exc);
13use PVE::API2::Network::SDN::Vnets;
14use PVE::API2::Network::SDN::Zones;
15use PVE::API2::Network::SDN::Controllers;
92b6f291
AD
16
17use base qw(PVE::RESTHandler);
18
4140be9e
AD
19__PACKAGE__->register_method ({
20 subclass => "PVE::API2::Network::SDN::Vnets",
21 path => 'vnets',
22 });
92b6f291 23
4140be9e
AD
24__PACKAGE__->register_method ({
25 subclass => "PVE::API2::Network::SDN::Zones",
26 path => 'zones',
27 });
92b6f291
AD
28
29__PACKAGE__->register_method ({
4140be9e
AD
30 subclass => "PVE::API2::Network::SDN::Controllers",
31 path => 'controllers',
32});
33
34__PACKAGE__->register_method({
35 name => 'index',
36 path => '',
92b6f291 37 method => 'GET',
4140be9e 38 description => "Directory index.",
7d35eaf5 39 permissions => {
4140be9e 40 check => ['perm', '/', [ 'Sys.Audit' ]],
92b6f291
AD
41 },
42 parameters => {
43 additionalProperties => 0,
4140be9e 44 properties => {},
92b6f291
AD
45 },
46 returns => {
47 type => 'array',
48 items => {
49 type => "object",
4140be9e
AD
50 properties => {
51 id => { type => 'string' },
52 },
92b6f291 53 },
4140be9e 54 links => [ { rel => 'child', href => "{id}" } ],
92b6f291
AD
55 },
56 code => sub {
57 my ($param) = @_;
58
4140be9e
AD
59 my $res = [
60 { id => 'vnets' },
61 { id => 'zones' },
62 { id => 'controllers' },
63 ];
92b6f291
AD
64
65 return $res;
66 }});
67
5d50e70b
AD
68my $create_reload_network_worker = sub {
69 my ($nodename) = @_;
70
71 #fixme: how to proxy to final node ?
72 my $upid = PVE::Tools::run_command(['pvesh', 'set', "/nodes/$nodename/network"]);
73 #my $upid = PVE::API2::Network->reload_network_config(node => $nodename});
74 my $res = PVE::Tools::upid_decode($upid);
75
76 return $res->{pid};
77};
78
79__PACKAGE__->register_method ({
80 name => 'reload',
81 protected => 1,
82 path => '',
83 method => 'PUT',
84 description => "Apply sdn controller changes && reload.",
85# permissions => {
86# check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']],
87# },
88 parameters => {
89 additionalProperties => 0,
90 },
91 returns => {
92 type => 'string',
93 },
94 code => sub {
95 my ($param) = @_;
96
97 my $rpcenv = PVE::RPCEnvironment::get();
98 my $authuser = $rpcenv->get_user();
99
100 if (-e "/etc/pve/sdn/controllers.cfg.new") {
101 rename("/etc/pve/sdn/controllers.cfg.new", "/etc/pve/sdn/controllers.cfg")
102 || die "applying sdn/controllers.cfg changes failed - $!\n";
103 }
104
105 if (-e "/etc/pve/sdn/zones.cfg.new") {
106 rename("/etc/pve/sdn/zones.cfg.new", "/etc/pve/sdn/zones.cfg")
107 || die "applying sdn/zones.cfg changes failed - $!\n";
108 }
109
110 if (-e "/etc/pve/sdn/vnets.cfg.new") {
111 rename("/etc/pve/sdn/vnets.cfg.new", "/etc/pve/sdn/vnets.cfg")
112 || die "applying sdn/vnets.cfg changes failed - $!\n";
113 }
114
115 my $code = sub {
116 $rpcenv->{type} = 'priv'; # to start tasks in background
117 PVE::Cluster::check_cfs_quorum();
118 my $nodelist = PVE::Cluster::get_nodelist();
119 foreach my $node (@$nodelist) {
120
121 my $pid;
122 eval { $pid = &$create_reload_network_worker($node); };
123 warn $@ if $@;
124 next if !$pid;
125 }
126 return;
127 };
128
129 return $rpcenv->fork_worker('reloadnetworkall', undef, $authuser, $code);
130
131 }});
132
92b6f291
AD
133
1341;