]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Controllers.pm
sdn: rename config to running_config
[pve-network.git] / PVE / API2 / Network / SDN / Controllers.pm
CommitLineData
4140be9e
AD
1package PVE::API2::Network::SDN::Controllers;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Tools qw(extract_param);
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
f9bc9640 9use PVE::Network::SDN;
4140be9e
AD
10use PVE::Network::SDN::Zones;
11use PVE::Network::SDN::Controllers;
12use PVE::Network::SDN::Controllers::Plugin;
fa253735 13use PVE::Network::SDN::Controllers::EvpnPlugin;
f23633dc 14use PVE::Network::SDN::Controllers::BgpPlugin;
4140be9e
AD
15use PVE::Network::SDN::Controllers::FaucetPlugin;
16
17use Storable qw(dclone);
18use PVE::JSONSchema qw(get_standard_option);
19use PVE::RPCEnvironment;
20
21use PVE::RESTHandler;
22
23use base qw(PVE::RESTHandler);
24
25my $sdn_controllers_type_enum = PVE::Network::SDN::Controllers::Plugin->lookup_types();
26
27my $api_sdn_controllers_config = sub {
28 my ($cfg, $id) = @_;
29
30 my $scfg = dclone(PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id));
31 $scfg->{controller} = $id;
32 $scfg->{digest} = $cfg->{digest};
33
34 return $scfg;
35};
36
37__PACKAGE__->register_method ({
38 name => 'index',
39 path => '',
40 method => 'GET',
41 description => "SDN controllers index.",
42 permissions => {
3551b612 43 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/controllers/<controller>'",
4140be9e
AD
44 user => 'all',
45 },
46 parameters => {
47 additionalProperties => 0,
48 properties => {
49 type => {
50 description => "Only list sdn controllers of specific type",
51 type => 'string',
52 enum => $sdn_controllers_type_enum,
53 optional => 1,
54 },
6f5f42e4
AD
55 running => {
56 type => 'boolean',
57 optional => 1,
58 description => "Display running config.",
59 },
60 pending => {
61 type => 'boolean',
62 optional => 1,
63 description => "Display pending config.",
64 },
4140be9e
AD
65 },
66 },
67 returns => {
68 type => 'array',
69 items => {
70 type => "object",
6f5f42e4
AD
71 properties => { controller => { type => 'string' },
72 type => { type => 'string' },
73 state => { type => 'string', optional => 1 },
74 pending => { optional => 1},
75 },
4140be9e
AD
76 },
77 links => [ { rel => 'child', href => "{controller}" } ],
78 },
79 code => sub {
80 my ($param) = @_;
81
82 my $rpcenv = PVE::RPCEnvironment::get();
83 my $authuser = $rpcenv->get_user();
84
6f5f42e4
AD
85 my $cfg = {};
86 if($param->{pending}) {
d73c7c36 87 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
88 my $config = PVE::Network::SDN::Controllers::config();
89 $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'controllers');
90 } elsif ($param->{running}) {
d73c7c36 91 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
92 $cfg = $running_cfg->{controllers};
93 } else {
94 $cfg = PVE::Network::SDN::Controllers::config();
95 }
4140be9e
AD
96
97 my @sids = PVE::Network::SDN::Controllers::sdn_controllers_ids($cfg);
98 my $res = [];
99 foreach my $id (@sids) {
3551b612
AD
100 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
101 next if !$rpcenv->check_any($authuser, "/sdn/controllers/$id", $privs, 1);
4140be9e
AD
102
103 my $scfg = &$api_sdn_controllers_config($cfg, $id);
104 next if $param->{type} && $param->{type} ne $scfg->{type};
105
106 my $plugin_config = $cfg->{ids}->{$id};
107 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
108 push @$res, $scfg;
109 }
110
111 return $res;
112 }});
113
114__PACKAGE__->register_method ({
115 name => 'read',
116 path => '{controller}',
117 method => 'GET',
118 description => "Read sdn controller configuration.",
3551b612
AD
119 permissions => {
120 check => ['perm', '/sdn/controllers/{controller}', ['SDN.Allocate']],
121 },
4140be9e
AD
122
123 parameters => {
124 additionalProperties => 0,
125 properties => {
126 controller => get_standard_option('pve-sdn-controller-id'),
6f5f42e4
AD
127 running => {
128 type => 'boolean',
129 optional => 1,
130 description => "Display running config.",
131 },
132 pending => {
133 type => 'boolean',
134 optional => 1,
135 description => "Display pending config.",
136 },
4140be9e
AD
137 },
138 },
139 returns => { type => 'object' },
140 code => sub {
141 my ($param) = @_;
142
6f5f42e4
AD
143 my $cfg = {};
144 if($param->{pending}) {
d73c7c36 145 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
146 my $config = PVE::Network::SDN::Controllers::config();
147 $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'controllers');
148 } elsif ($param->{running}) {
d73c7c36 149 my $running_cfg = PVE::Network::SDN::running_config();
6f5f42e4
AD
150 $cfg = $running_cfg->{controllers};
151 } else {
152 $cfg = PVE::Network::SDN::Controllers::config();
153 }
4140be9e
AD
154
155 return &$api_sdn_controllers_config($cfg, $param->{controller});
156 }});
157
158__PACKAGE__->register_method ({
159 name => 'create',
160 protected => 1,
161 path => '',
162 method => 'POST',
163 description => "Create a new sdn controller object.",
3551b612
AD
164 permissions => {
165 check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
166 },
4140be9e
AD
167 parameters => PVE::Network::SDN::Controllers::Plugin->createSchema(),
168 returns => { type => 'null' },
169 code => sub {
170 my ($param) = @_;
171
172 my $type = extract_param($param, 'type');
173 my $id = extract_param($param, 'controller');
174
175 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($type);
176 my $opts = $plugin->check_config($id, $param, 1, 1);
177
45c3f15c
AD
178 # create /etc/pve/sdn directory
179 PVE::Cluster::check_cfs_quorum();
180 mkdir("/etc/pve/sdn");
181
f9bc9640 182 PVE::Network::SDN::lock_sdn_config(
4140be9e
AD
183 sub {
184
56cdcac9 185 my $controller_cfg = PVE::Network::SDN::Controllers::config();
4140be9e
AD
186
187 my $scfg = undef;
56cdcac9 188 if ($scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id, 1)) {
4140be9e
AD
189 die "sdn controller object ID '$id' already defined\n";
190 }
191
56cdcac9
AD
192 $controller_cfg->{ids}->{$id} = $opts;
193 $plugin->on_update_hook($id, $controller_cfg);
4140be9e 194
56cdcac9 195 PVE::Network::SDN::Controllers::write_config($controller_cfg);
4140be9e
AD
196
197 }, "create sdn controller object failed");
198
199 return undef;
200 }});
201
4140be9e
AD
202__PACKAGE__->register_method ({
203 name => 'update',
204 protected => 1,
205 path => '{controller}',
206 method => 'PUT',
207 description => "Update sdn controller object configuration.",
3551b612
AD
208 permissions => {
209 check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
210 },
4140be9e
AD
211 parameters => PVE::Network::SDN::Controllers::Plugin->updateSchema(),
212 returns => { type => 'null' },
213 code => sub {
214 my ($param) = @_;
215
216 my $id = extract_param($param, 'controller');
217 my $digest = extract_param($param, 'digest');
218
f9bc9640 219 PVE::Network::SDN::lock_sdn_config(
4140be9e
AD
220 sub {
221
56cdcac9 222 my $controller_cfg = PVE::Network::SDN::Controllers::config();
4140be9e 223
56cdcac9 224 PVE::SectionConfig::assert_if_modified($controller_cfg, $digest);
4140be9e 225
56cdcac9 226 my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id);
4140be9e
AD
227
228 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type});
229 my $opts = $plugin->check_config($id, $param, 0, 1);
230
231 foreach my $k (%$opts) {
232 $scfg->{$k} = $opts->{$k};
233 }
234
56cdcac9 235 $plugin->on_update_hook($id, $controller_cfg);
4140be9e 236
56cdcac9 237 PVE::Network::SDN::Controllers::write_config($controller_cfg);
4140be9e 238
f9bc9640 239
4140be9e
AD
240 }, "update sdn controller object failed");
241
242 return undef;
243 }});
244
245__PACKAGE__->register_method ({
246 name => 'delete',
247 protected => 1,
248 path => '{controller}',
249 method => 'DELETE',
250 description => "Delete sdn controller object configuration.",
3551b612
AD
251 permissions => {
252 check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
253 },
4140be9e
AD
254 parameters => {
255 additionalProperties => 0,
256 properties => {
257 controller => get_standard_option('pve-sdn-controller-id', {
258 completion => \&PVE::Network::SDN::Controllers::complete_sdn_controllers,
259 }),
260 },
261 },
262 returns => { type => 'null' },
263 code => sub {
264 my ($param) = @_;
265
266 my $id = extract_param($param, 'controller');
267
f9bc9640 268 PVE::Network::SDN::lock_sdn_config(
4140be9e
AD
269 sub {
270
271 my $cfg = PVE::Network::SDN::Controllers::config();
272
273 my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id);
274
275 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type});
276
56cdcac9 277 my $zone_cfg = PVE::Network::SDN::Zones::config();
4140be9e 278
56cdcac9 279 $plugin->on_delete_hook($id, $zone_cfg);
4140be9e
AD
280
281 delete $cfg->{ids}->{$id};
282 PVE::Network::SDN::Controllers::write_config($cfg);
283
284 }, "delete sdn controller object failed");
285
286
287 return undef;
288 }});
289
2901;