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