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