]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN.pm
d/control: reword package description slightly
[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 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.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 run_command(['pvesh', 'set', "/nodes/$nodename/network"], outfunc => sub {
90 my $line = shift;
91 if ($line =~ /^["']?(UPID:[^\s"']+)["']?$/) {
92 $upid = $1;
93 }
94 });
95 #my $upid = PVE::API2::Network->reload_network_config(node => $nodename});
96 my $res = PVE::Tools::upid_decode($upid);
97
98 return $res->{pid};
99 };
100
101 __PACKAGE__->register_method ({
102 name => 'reload',
103 protected => 1,
104 path => '',
105 method => 'PUT',
106 description => "Apply sdn controller changes && reload.",
107 permissions => {
108 check => ['perm', '/sdn', ['SDN.Allocate']],
109 },
110 parameters => {
111 additionalProperties => 0,
112 },
113 returns => {
114 type => 'string',
115 },
116 code => sub {
117 my ($param) = @_;
118
119 my $rpcenv = PVE::RPCEnvironment::get();
120 my $authuser = $rpcenv->get_user();
121
122 PVE::Network::SDN::commit_config();
123
124 my $code = sub {
125 $rpcenv->{type} = 'priv'; # to start tasks in background
126 PVE::Cluster::check_cfs_quorum();
127 my $nodelist = PVE::Cluster::get_nodelist();
128 for my $node (@$nodelist) {
129 my $pid = eval { $create_reload_network_worker->($node) };
130 warn $@ if $@;
131 }
132
133 # FIXME: use libpve-apiclient (like in cluster join) to create
134 # tasks and moitor the tasks.
135
136 return;
137 };
138
139 return $rpcenv->fork_worker('reloadnetworkall', undef, $authuser, $code);
140
141 }});
142
143
144 1;