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