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