]>
Commit | Line | Data |
---|---|---|
4140be9e AD |
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::FrrEvpnPlugin; | |
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 '/cluster/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, "/cluster/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', '/cluster/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', '/cluster/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 $cfg = PVE::Network::SDN::Controllers::config(); | |
139 | ||
140 | my $scfg = undef; | |
141 | if ($scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id, 1)) { | |
142 | die "sdn controller object ID '$id' already defined\n"; | |
143 | } | |
144 | ||
145 | $cfg->{ids}->{$id} = $opts; | |
146 | $plugin->on_update_hook($id, $cfg); | |
147 | ||
148 | PVE::Network::SDN::Controllers::write_config($cfg); | |
149 | ||
150 | }, "create sdn controller object failed"); | |
151 | ||
152 | return undef; | |
153 | }}); | |
154 | ||
155 | __PACKAGE__->register_method ({ | |
156 | name => 'apply_configuration', | |
157 | protected => 1, | |
158 | path => '', | |
159 | method => 'PUT', | |
160 | description => "Apply sdn controller changes.", | |
161 | # permissions => { | |
162 | # check => ['perm', '/cluster/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 controller changes to apply" if !-e "/etc/pve/sdn/controllers.cfg.new"; | |
172 | rename("/etc/pve/sdn/controllers.cfg.new", "/etc/pve/sdn/controllers.cfg") | |
173 | || die "applying sdn/controllers.cfg changes failed - $!\n"; | |
174 | ||
175 | ||
176 | return undef; | |
177 | }}); | |
178 | ||
179 | __PACKAGE__->register_method ({ | |
180 | name => 'revert_configuration', | |
181 | protected => 1, | |
182 | path => '', | |
183 | method => 'DELETE', | |
184 | description => "Revert sdn controller changes.", | |
185 | # permissions => { | |
186 | # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']], | |
187 | # }, | |
188 | parameters => { | |
189 | additionalProperties => 0, | |
190 | }, | |
191 | returns => { type => 'null' }, | |
192 | code => sub { | |
193 | my ($param) = @_; | |
194 | ||
195 | die "no sdn controllers changes to revert" if !-e "/etc/pve/sdn/controllers.cfg.new"; | |
196 | unlink "/etc/pve/sdn/controllers.cfg.new"; | |
197 | ||
198 | return undef; | |
199 | }}); | |
200 | ||
201 | __PACKAGE__->register_method ({ | |
202 | name => 'update', | |
203 | protected => 1, | |
204 | path => '{controller}', | |
205 | method => 'PUT', | |
206 | description => "Update sdn controller object configuration.", | |
207 | # permissions => { | |
208 | # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']], | |
209 | # }, | |
210 | parameters => PVE::Network::SDN::Controllers::Plugin->updateSchema(), | |
211 | returns => { type => 'null' }, | |
212 | code => sub { | |
213 | my ($param) = @_; | |
214 | ||
215 | my $id = extract_param($param, 'controller'); | |
216 | my $digest = extract_param($param, 'digest'); | |
217 | ||
218 | PVE::Network::SDN::Controllers::lock_sdn_controllers_config( | |
219 | sub { | |
220 | ||
221 | my $cfg = PVE::Network::SDN::Controllers::config(); | |
222 | ||
223 | PVE::SectionConfig::assert_if_modified($cfg, $digest); | |
224 | ||
225 | my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id); | |
226 | ||
227 | my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type}); | |
228 | my $opts = $plugin->check_config($id, $param, 0, 1); | |
229 | ||
230 | foreach my $k (%$opts) { | |
231 | $scfg->{$k} = $opts->{$k}; | |
232 | } | |
233 | ||
234 | $plugin->on_update_hook($id, $cfg); | |
235 | ||
236 | PVE::Network::SDN::Controllers::write_config($cfg); | |
237 | ||
238 | }, "update sdn controller object failed"); | |
239 | ||
240 | return undef; | |
241 | }}); | |
242 | ||
243 | __PACKAGE__->register_method ({ | |
244 | name => 'delete', | |
245 | protected => 1, | |
246 | path => '{controller}', | |
247 | method => 'DELETE', | |
248 | description => "Delete sdn controller object configuration.", | |
249 | # permissions => { | |
250 | # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']], | |
251 | # }, | |
252 | parameters => { | |
253 | additionalProperties => 0, | |
254 | properties => { | |
255 | controller => get_standard_option('pve-sdn-controller-id', { | |
256 | completion => \&PVE::Network::SDN::Controllers::complete_sdn_controllers, | |
257 | }), | |
258 | }, | |
259 | }, | |
260 | returns => { type => 'null' }, | |
261 | code => sub { | |
262 | my ($param) = @_; | |
263 | ||
264 | my $id = extract_param($param, 'controller'); | |
265 | ||
266 | PVE::Network::SDN::Controllers::lock_sdn_controllers_config( | |
267 | sub { | |
268 | ||
269 | my $cfg = PVE::Network::SDN::Controllers::config(); | |
270 | ||
271 | my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id); | |
272 | ||
273 | my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type}); | |
274 | ||
275 | my $transport_cfg = PVE::Network::SDN::Zones::config(); | |
276 | ||
277 | $plugin->on_delete_hook($id, $transport_cfg); | |
278 | ||
279 | delete $cfg->{ids}->{$id}; | |
280 | PVE::Network::SDN::Controllers::write_config($cfg); | |
281 | ||
282 | }, "delete sdn controller object failed"); | |
283 | ||
284 | ||
285 | return undef; | |
286 | }}); | |
287 | ||
288 | 1; |