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