]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN/Zones.pm
d1492902425226fd2f166e2e754c4cbd3ab3d956
[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 if ($scfg->{nodes}) {
36 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
37 }
38
39 return $scfg;
40 };
41
42 __PACKAGE__->register_method ({
43 name => 'index',
44 path => '',
45 method => 'GET',
46 description => "SDN zones index.",
47 permissions => {
48 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/zones/<zone>'",
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) {
84 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
85 next if !$rpcenv->check_any($authuser, "/sdn/zones/$id", $privs, 1);
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.",
103 permissions => {
104 check => ['perm', '/sdn/zones/{zone}', ['SDN.Allocate']],
105 },
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.",
128 permissions => {
129 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
130 },
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
145 my $zone_cfg = PVE::Network::SDN::Zones::config();
146 my $controller_cfg = PVE::Network::SDN::Controllers::config();
147
148 my $scfg = undef;
149 if ($scfg = PVE::Network::SDN::Zones::sdn_zones_config($zone_cfg, $id, 1)) {
150 die "sdn zone object ID '$id' already defined\n";
151 }
152
153 $zone_cfg->{ids}->{$id} = $opts;
154 $plugin->on_update_hook($id, $zone_cfg, $controller_cfg);
155
156 PVE::Network::SDN::Zones::write_config($zone_cfg);
157
158 }, "create sdn zone object failed");
159
160 return undef;
161 }});
162
163 __PACKAGE__->register_method ({
164 name => 'revert_configuration',
165 protected => 1,
166 path => '',
167 method => 'DELETE',
168 description => "Revert sdn zone changes.",
169 permissions => {
170 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
171 },
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.",
191 permissions => {
192 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
193 },
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
205 my $zone_cfg = PVE::Network::SDN::Zones::config();
206 my $controller_cfg = PVE::Network::SDN::Controllers::config();
207
208 PVE::SectionConfig::assert_if_modified($zone_cfg, $digest);
209
210 my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($zone_cfg, $id);
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
219 $plugin->on_update_hook($id, $zone_cfg, $controller_cfg);
220
221 PVE::Network::SDN::Zones::write_config($zone_cfg);
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.",
234 permissions => {
235 check => ['perm', '/sdn/zones', ['SDN.Allocate']],
236 },
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
273 1;