]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN.pm
api: generate 'running-config' state instead of version increase on apply
[pve-network.git] / PVE / API2 / Network / SDN.pm
CommitLineData
86d22462 1package PVE::API2::Network::SDN;
92b6f291
AD
2
3use strict;
4use warnings;
5
4140be9e 6use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
14e55c65
TL
7use PVE::Exception qw(raise_param_exc);
8use PVE::JSONSchema qw(get_standard_option);
92b6f291 9use PVE::RESTHandler;
4140be9e 10use PVE::RPCEnvironment;
14e55c65
TL
11use PVE::SafeSyslog;
12use PVE::Tools qw(run_command);
5d3e0248 13use PVE::Network::SDN;
14e55c65
TL
14
15use PVE::API2::Network::SDN::Controllers;
4140be9e
AD
16use PVE::API2::Network::SDN::Vnets;
17use PVE::API2::Network::SDN::Zones;
70b03506 18use PVE::API2::Network::SDN::Ipams;
ee4f339e 19use PVE::API2::Network::SDN::Dns;
92b6f291
AD
20
21use base qw(PVE::RESTHandler);
22
4140be9e 23__PACKAGE__->register_method ({
35854815 24 subclass => "PVE::API2::Network::SDN::Vnets",
4140be9e 25 path => 'vnets',
35854815 26});
92b6f291 27
4140be9e 28__PACKAGE__->register_method ({
35854815 29 subclass => "PVE::API2::Network::SDN::Zones",
4140be9e 30 path => 'zones',
35854815 31});
92b6f291
AD
32
33__PACKAGE__->register_method ({
35854815 34 subclass => "PVE::API2::Network::SDN::Controllers",
4140be9e
AD
35 path => 'controllers',
36});
37
70b03506
AD
38__PACKAGE__->register_method ({
39 subclass => "PVE::API2::Network::SDN::Ipams",
40 path => 'ipams',
41});
42
ee4f339e
AD
43__PACKAGE__->register_method ({
44 subclass => "PVE::API2::Network::SDN::Dns",
45 path => 'dns',
46});
47
4140be9e 48__PACKAGE__->register_method({
35854815
TL
49 name => 'index',
50 path => '',
92b6f291 51 method => 'GET',
4140be9e 52 description => "Directory index.",
7d35eaf5 53 permissions => {
3551b612 54 check => ['perm', '/', [ 'SDN.Audit' ]],
92b6f291
AD
55 },
56 parameters => {
57 additionalProperties => 0,
4140be9e 58 properties => {},
92b6f291
AD
59 },
60 returns => {
61 type => 'array',
62 items => {
63 type => "object",
4140be9e
AD
64 properties => {
65 id => { type => 'string' },
66 },
92b6f291 67 },
4140be9e 68 links => [ { rel => 'child', href => "{id}" } ],
92b6f291
AD
69 },
70 code => sub {
71 my ($param) = @_;
72
35854815 73 my $res = [
4140be9e
AD
74 { id => 'vnets' },
75 { id => 'zones' },
76 { id => 'controllers' },
70b03506 77 { id => 'ipams' },
ee4f339e 78 { id => 'dns' },
4140be9e 79 ];
92b6f291
AD
80
81 return $res;
82 }});
83
5d50e70b
AD
84my $create_reload_network_worker = sub {
85 my ($nodename) = @_;
86
14e55c65
TL
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 });
5d50e70b
AD
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.",
3551b612
AD
107 permissions => {
108 check => ['perm', '/sdn', ['SDN.Allocate']],
109 },
5d50e70b
AD
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
5d3e0248
AD
122 PVE::Network::SDN::commit_config();
123
5d50e70b
AD
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();
14e55c65
TL
128 for my $node (@$nodelist) {
129 my $pid = eval { $create_reload_network_worker->($node) };
5d50e70b 130 warn $@ if $@;
5d50e70b 131 }
14e55c65
TL
132
133 # FIXME: use libpve-apiclient (like in cluster join) to create
134 # tasks and moitor the tasks.
135
5d50e70b
AD
136 return;
137 };
138
139 return $rpcenv->fork_worker('reloadnetworkall', undef, $authuser, $code);
140
141 }});
142
92b6f291
AD
143
1441;