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