]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN.pm
65c8d5270fcb63ce8f9a2440aadb6ada385e435a
[pve-network.git] / PVE / API2 / Network / SDN.pm
1 package PVE::API2::Network::SDN;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools;
8 use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
9 use PVE::RESTHandler;
10 use PVE::RPCEnvironment;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::Exception qw(raise_param_exc);
13 use PVE::API2::Network::SDN::Vnets;
14 use PVE::API2::Network::SDN::Zones;
15 use PVE::API2::Network::SDN::Controllers;
16
17 use base qw(PVE::RESTHandler);
18
19 __PACKAGE__->register_method ({
20 subclass => "PVE::API2::Network::SDN::Vnets",
21 path => 'vnets',
22 });
23
24 __PACKAGE__->register_method ({
25 subclass => "PVE::API2::Network::SDN::Zones",
26 path => 'zones',
27 });
28
29 __PACKAGE__->register_method ({
30 subclass => "PVE::API2::Network::SDN::Controllers",
31 path => 'controllers',
32 });
33
34 __PACKAGE__->register_method({
35 name => 'index',
36 path => '',
37 method => 'GET',
38 description => "Directory index.",
39 permissions => {
40 check => ['perm', '/', [ 'SDN.Audit' ]],
41 },
42 parameters => {
43 additionalProperties => 0,
44 properties => {},
45 },
46 returns => {
47 type => 'array',
48 items => {
49 type => "object",
50 properties => {
51 id => { type => 'string' },
52 },
53 },
54 links => [ { rel => 'child', href => "{id}" } ],
55 },
56 code => sub {
57 my ($param) = @_;
58
59 my $res = [
60 { id => 'vnets' },
61 { id => 'zones' },
62 { id => 'controllers' },
63 ];
64
65 return $res;
66 }});
67
68 my $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', '/sdn', ['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
133
134 1;