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