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