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