]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN/Controllers.pm
create /etc/pve/sdn directory
[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 # create /etc/pve/sdn directory
136 PVE::Cluster::check_cfs_quorum();
137 mkdir("/etc/pve/sdn");
138
139 PVE::Network::SDN::Controllers::lock_sdn_controllers_config(
140 sub {
141
142 my $controller_cfg = PVE::Network::SDN::Controllers::config();
143
144 my $scfg = undef;
145 if ($scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id, 1)) {
146 die "sdn controller object ID '$id' already defined\n";
147 }
148
149 $controller_cfg->{ids}->{$id} = $opts;
150 $plugin->on_update_hook($id, $controller_cfg);
151
152 PVE::Network::SDN::Controllers::write_config($controller_cfg);
153
154 }, "create sdn controller object failed");
155
156 return undef;
157 }});
158
159 __PACKAGE__->register_method ({
160 name => 'revert_configuration',
161 protected => 1,
162 path => '',
163 method => 'DELETE',
164 description => "Revert sdn controller changes.",
165 permissions => {
166 check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
167 },
168 parameters => {
169 additionalProperties => 0,
170 },
171 returns => { type => 'null' },
172 code => sub {
173 my ($param) = @_;
174
175 die "no sdn controllers changes to revert" if !-e "/etc/pve/sdn/controllers.cfg.new";
176 unlink "/etc/pve/sdn/controllers.cfg.new";
177
178 return undef;
179 }});
180
181 __PACKAGE__->register_method ({
182 name => 'update',
183 protected => 1,
184 path => '{controller}',
185 method => 'PUT',
186 description => "Update sdn controller object configuration.",
187 permissions => {
188 check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
189 },
190 parameters => PVE::Network::SDN::Controllers::Plugin->updateSchema(),
191 returns => { type => 'null' },
192 code => sub {
193 my ($param) = @_;
194
195 my $id = extract_param($param, 'controller');
196 my $digest = extract_param($param, 'digest');
197
198 PVE::Network::SDN::Controllers::lock_sdn_controllers_config(
199 sub {
200
201 my $controller_cfg = PVE::Network::SDN::Controllers::config();
202
203 PVE::SectionConfig::assert_if_modified($controller_cfg, $digest);
204
205 my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id);
206
207 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type});
208 my $opts = $plugin->check_config($id, $param, 0, 1);
209
210 foreach my $k (%$opts) {
211 $scfg->{$k} = $opts->{$k};
212 }
213
214 $plugin->on_update_hook($id, $controller_cfg);
215
216 PVE::Network::SDN::Controllers::write_config($controller_cfg);
217
218 }, "update sdn controller object failed");
219
220 return undef;
221 }});
222
223 __PACKAGE__->register_method ({
224 name => 'delete',
225 protected => 1,
226 path => '{controller}',
227 method => 'DELETE',
228 description => "Delete sdn controller object configuration.",
229 permissions => {
230 check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
231 },
232 parameters => {
233 additionalProperties => 0,
234 properties => {
235 controller => get_standard_option('pve-sdn-controller-id', {
236 completion => \&PVE::Network::SDN::Controllers::complete_sdn_controllers,
237 }),
238 },
239 },
240 returns => { type => 'null' },
241 code => sub {
242 my ($param) = @_;
243
244 my $id = extract_param($param, 'controller');
245
246 PVE::Network::SDN::Controllers::lock_sdn_controllers_config(
247 sub {
248
249 my $cfg = PVE::Network::SDN::Controllers::config();
250
251 my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id);
252
253 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type});
254
255 my $zone_cfg = PVE::Network::SDN::Zones::config();
256
257 $plugin->on_delete_hook($id, $zone_cfg);
258
259 delete $cfg->{ids}->{$id};
260 PVE::Network::SDN::Controllers::write_config($cfg);
261
262 }, "delete sdn controller object failed");
263
264
265 return undef;
266 }});
267
268 1;