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